From 3c40da46e269a77824e2ade35003591a4fb7b696 Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 28 Jun 2023 20:16:37 +0800 Subject: [PATCH 1/2] fix(sourcemap): inject content from source file when sourcesContent is null --- packages/vite/src/node/plugins/css.ts | 2 +- .../src/node/server/middlewares/indexHtml.ts | 2 +- packages/vite/src/node/server/sourcemap.ts | 21 ++++++++++++------- .../vite/src/node/server/transformRequest.ts | 2 +- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index b2465d4a1d45ba..60063e7947c497 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -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) diff --git a/packages/vite/src/node/server/middlewares/indexHtml.ts b/packages/vite/src/node/server/middlewares/indexHtml.ts index 3948f997a3bb91..838d5a1bf7c0e3 100644 --- a/packages/vite/src/node/server/middlewares/indexHtml.ts +++ b/packages/vite/src/node/server/middlewares/indexHtml.ts @@ -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, diff --git a/packages/vite/src/node/server/sourcemap.ts b/packages/vite/src/node/server/sourcemap.ts index a2e8c55542fc8e..c8f2c8d88fdacd 100644 --- a/packages/vite/src/node/server/sourcemap.ts +++ b/packages/vite/src/node/server/sourcemap.ts @@ -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. diff --git a/packages/vite/src/node/server/transformRequest.ts b/packages/vite/src/node/server/transformRequest.ts index 92f9abbc04a4fd..2c9c58c47d7ea3 100644 --- a/packages/vite/src/node/server/transformRequest.ts +++ b/packages/vite/src/node/server/transformRequest.ts @@ -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) } From 37a445e984024fb9d63f92ec4a0aaf61a4da2a9c Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 28 Jun 2023 20:19:43 +0800 Subject: [PATCH 2/2] fix(sourcemap): should not exclude source content --- .../src/node/plugins/importAnalysisBuild.ts | 9 ++++---- packages/vite/src/node/ssr/ssrTransform.ts | 20 +++++++----------- packages/vite/src/node/utils.ts | 21 +++++++------------ 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 8e8482776d9c03..d74987a6bca258 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -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 diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index ec15226ddb6f09..d582ee0ef25c26 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -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 diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index c1dbc05c204e46..1ac8617f6bc7ac 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -748,7 +748,6 @@ const nullSourceMap: RawSourceMap = { export function combineSourcemaps( filename: string, sourcemapList: Array, - excludeContent = true, ): RawSourceMap { if ( sourcemapList.length === 0 || @@ -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