Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sourcemap): preserve original sourcesContent #13662

Merged
merged 2 commits into from Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/css.ts
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
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
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
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
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
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
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