Skip to content

Commit f6362b6

Browse files
authored
fix(sourcemap): preserve original sourcesContent (#13662)
1 parent 6f1c55e commit f6362b6

File tree

7 files changed

+37
-40
lines changed

7 files changed

+37
-40
lines changed

packages/vite/src/node/plugins/css.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
439439
const getContentWithSourcemap = async (content: string) => {
440440
if (config.css?.devSourcemap) {
441441
const sourcemap = this.getCombinedSourcemap()
442-
if (sourcemap.mappings && !sourcemap.sourcesContent) {
442+
if (sourcemap.mappings) {
443443
await injectSourcesContent(sourcemap, cleanUrl(id), config.logger)
444444
}
445445
return getCodeWithSourcemap('css', content, sourcemap)

packages/vite/src/node/plugins/importAnalysisBuild.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,10 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
652652
source: chunk.fileName,
653653
hires: true,
654654
})
655-
const map = combineSourcemaps(
656-
chunk.fileName,
657-
[nextMap as RawSourceMap, chunk.map as RawSourceMap],
658-
false,
659-
) as SourceMap
655+
const map = combineSourcemaps(chunk.fileName, [
656+
nextMap as RawSourceMap,
657+
chunk.map as RawSourceMap,
658+
]) as SourceMap
660659
map.toUrl = () => genSourceMapUrl(map)
661660
chunk.map = map
662661

packages/vite/src/node/server/middlewares/indexHtml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
283283
let content = ''
284284
if (result) {
285285
if (result.map) {
286-
if (result.map.mappings && !result.map.sourcesContent) {
286+
if (result.map.mappings) {
287287
await injectSourcesContent(
288288
result.map,
289289
proxyModulePath,

packages/vite/src/node/server/sourcemap.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,29 @@ export async function injectSourcesContent(
3333
} catch {}
3434

3535
const missingSources: string[] = []
36-
map.sourcesContent = await Promise.all(
37-
map.sources.map((sourcePath) => {
36+
const sourcesContent = map.sourcesContent || []
37+
await Promise.all(
38+
map.sources.map(async (sourcePath, index) => {
39+
let content = null
3840
if (sourcePath && !virtualSourceRE.test(sourcePath)) {
3941
sourcePath = decodeURI(sourcePath)
4042
if (sourceRoot) {
4143
sourcePath = path.resolve(sourceRoot, sourcePath)
4244
}
43-
return fsp.readFile(sourcePath, 'utf-8').catch(() => {
44-
missingSources.push(sourcePath)
45-
return null
46-
})
45+
// inject content from source file when sourcesContent is null
46+
content =
47+
sourcesContent[index] ??
48+
(await fsp.readFile(sourcePath, 'utf-8').catch(() => {
49+
missingSources.push(sourcePath)
50+
return null
51+
}))
4752
}
48-
return null
53+
sourcesContent[index] = content
4954
}),
5055
)
5156

57+
map.sourcesContent = sourcesContent
58+
5259
// Use this command…
5360
// DEBUG="vite:sourcemap" vite build
5461
// …to log the missing sources.

packages/vite/src/node/server/transformRequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ async function loadAndTransform(
285285

286286
if (map && mod.file) {
287287
map = (typeof map === 'string' ? JSON.parse(map) : map) as SourceMap
288-
if (map.mappings && !map.sourcesContent) {
288+
if (map.mappings) {
289289
await injectSourcesContent(map, mod.file, logger)
290290
}
291291

packages/vite/src/node/ssr/ssrTransform.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -276,18 +276,14 @@ async function ssrTransformScript(
276276

277277
let map = s.generateMap({ hires: true })
278278
if (inMap && inMap.mappings && inMap.sources.length > 0) {
279-
map = combineSourcemaps(
280-
url,
281-
[
282-
{
283-
...map,
284-
sources: inMap.sources,
285-
sourcesContent: inMap.sourcesContent,
286-
} as RawSourceMap,
287-
inMap as RawSourceMap,
288-
],
289-
false,
290-
) as SourceMap
279+
map = combineSourcemaps(url, [
280+
{
281+
...map,
282+
sources: inMap.sources,
283+
sourcesContent: inMap.sourcesContent,
284+
} as RawSourceMap,
285+
inMap as RawSourceMap,
286+
]) as SourceMap
291287
} else {
292288
map.sources = [path.basename(url)]
293289
// needs to use originalCode instead of code

packages/vite/src/node/utils.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,6 @@ const nullSourceMap: RawSourceMap = {
748748
export function combineSourcemaps(
749749
filename: string,
750750
sourcemapList: Array<DecodedSourceMap | RawSourceMap>,
751-
excludeContent = true,
752751
): RawSourceMap {
753752
if (
754753
sourcemapList.length === 0 ||
@@ -778,19 +777,15 @@ export function combineSourcemaps(
778777
const useArrayInterface =
779778
sourcemapList.slice(0, -1).find((m) => m.sources.length !== 1) === undefined
780779
if (useArrayInterface) {
781-
map = remapping(sourcemapList, () => null, excludeContent)
780+
map = remapping(sourcemapList, () => null)
782781
} else {
783-
map = remapping(
784-
sourcemapList[0],
785-
function loader(sourcefile) {
786-
if (sourcefile === escapedFilename && sourcemapList[mapIndex]) {
787-
return sourcemapList[mapIndex++]
788-
} else {
789-
return null
790-
}
791-
},
792-
excludeContent,
793-
)
782+
map = remapping(sourcemapList[0], function loader(sourcefile) {
783+
if (sourcefile === escapedFilename && sourcemapList[mapIndex]) {
784+
return sourcemapList[mapIndex++]
785+
} else {
786+
return null
787+
}
788+
})
794789
}
795790
if (!map.file) {
796791
delete map.file

0 commit comments

Comments
 (0)