Skip to content

Commit

Permalink
fix(sourcemap): preserve original sourcesContent (vitejs#13662)
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day authored and xinxinhe1810 committed Jul 4, 2023
1 parent 6dfb271 commit f3057ad
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
const getContentWithSourcemap = async (content: string) => {
if (config.css?.devSourcemap) {
const sourcemap = this.getCombinedSourcemap()
if (sourcemap.mappings && !sourcemap.sourcesContent) {
if (sourcemap.mappings) {
await injectSourcesContent(sourcemap, cleanUrl(id), config.logger)
}
return getCodeWithSourcemap('css', content, sourcemap)
Expand Down
9 changes: 4 additions & 5 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,10 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
source: chunk.fileName,
hires: true,
})
const map = combineSourcemaps(
chunk.fileName,
[nextMap as RawSourceMap, chunk.map as RawSourceMap],
false,
) as SourceMap
const map = combineSourcemaps(chunk.fileName, [
nextMap as RawSourceMap,
chunk.map as RawSourceMap,
]) as SourceMap
map.toUrl = () => genSourceMapUrl(map)
chunk.map = map

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
let content = ''
if (result) {
if (result.map) {
if (result.map.mappings && !result.map.sourcesContent) {
if (result.map.mappings) {
await injectSourcesContent(
result.map,
proxyModulePath,
Expand Down
21 changes: 14 additions & 7 deletions packages/vite/src/node/server/sourcemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,29 @@ export async function injectSourcesContent(
} catch {}

const missingSources: string[] = []
map.sourcesContent = await Promise.all(
map.sources.map((sourcePath) => {
const sourcesContent = map.sourcesContent || []
await Promise.all(
map.sources.map(async (sourcePath, index) => {
let content = null
if (sourcePath && !virtualSourceRE.test(sourcePath)) {
sourcePath = decodeURI(sourcePath)
if (sourceRoot) {
sourcePath = path.resolve(sourceRoot, sourcePath)
}
return fsp.readFile(sourcePath, 'utf-8').catch(() => {
missingSources.push(sourcePath)
return null
})
// inject content from source file when sourcesContent is null
content =
sourcesContent[index] ??
(await fsp.readFile(sourcePath, 'utf-8').catch(() => {
missingSources.push(sourcePath)
return null
}))
}
return null
sourcesContent[index] = content
}),
)

map.sourcesContent = sourcesContent

// Use this command…
// DEBUG="vite:sourcemap" vite build
// …to log the missing sources.
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ async function loadAndTransform(

if (map && mod.file) {
map = (typeof map === 'string' ? JSON.parse(map) : map) as SourceMap
if (map.mappings && !map.sourcesContent) {
if (map.mappings) {
await injectSourcesContent(map, mod.file, logger)
}

Expand Down
20 changes: 8 additions & 12 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,14 @@ async function ssrTransformScript(

let map = s.generateMap({ hires: true })
if (inMap && inMap.mappings && inMap.sources.length > 0) {
map = combineSourcemaps(
url,
[
{
...map,
sources: inMap.sources,
sourcesContent: inMap.sourcesContent,
} as RawSourceMap,
inMap as RawSourceMap,
],
false,
) as SourceMap
map = combineSourcemaps(url, [
{
...map,
sources: inMap.sources,
sourcesContent: inMap.sourcesContent,
} as RawSourceMap,
inMap as RawSourceMap,
]) as SourceMap
} else {
map.sources = [path.basename(url)]
// needs to use originalCode instead of code
Expand Down
21 changes: 8 additions & 13 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,6 @@ const nullSourceMap: RawSourceMap = {
export function combineSourcemaps(
filename: string,
sourcemapList: Array<DecodedSourceMap | RawSourceMap>,
excludeContent = true,
): RawSourceMap {
if (
sourcemapList.length === 0 ||
Expand Down Expand Up @@ -778,19 +777,15 @@ export function combineSourcemaps(
const useArrayInterface =
sourcemapList.slice(0, -1).find((m) => m.sources.length !== 1) === undefined
if (useArrayInterface) {
map = remapping(sourcemapList, () => null, excludeContent)
map = remapping(sourcemapList, () => null)
} else {
map = remapping(
sourcemapList[0],
function loader(sourcefile) {
if (sourcefile === escapedFilename && sourcemapList[mapIndex]) {
return sourcemapList[mapIndex++]
} else {
return null
}
},
excludeContent,
)
map = remapping(sourcemapList[0], function loader(sourcefile) {
if (sourcefile === escapedFilename && sourcemapList[mapIndex]) {
return sourcemapList[mapIndex++]
} else {
return null
}
})
}
if (!map.file) {
delete map.file
Expand Down

0 comments on commit f3057ad

Please sign in to comment.