From 484aec6152d8c42281ac3833f27487ce01a8420c Mon Sep 17 00:00:00 2001 From: patak Date: Fri, 1 Dec 2023 16:17:47 +0100 Subject: [PATCH 1/2] perf: skip computing sourceRoot in injectSourcesContent --- packages/vite/src/node/server/sourcemap.ts | 42 +++++++++++++--------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/packages/vite/src/node/server/sourcemap.ts b/packages/vite/src/node/server/sourcemap.ts index c8f2c8d88fdacd..06bb063dcd3df4 100644 --- a/packages/vite/src/node/server/sourcemap.ts +++ b/packages/vite/src/node/server/sourcemap.ts @@ -19,11 +19,7 @@ interface SourceMapLike { sourceRoot?: string } -export async function injectSourcesContent( - map: SourceMapLike, - file: string, - logger: Logger, -): Promise { +async function computeSourceRoute(map: SourceMapLike, file: string) { let sourceRoot: string | undefined try { // The source root is undefined for virtual modules and permission errors. @@ -31,27 +27,41 @@ export async function injectSourcesContent( path.resolve(path.dirname(file), map.sourceRoot || ''), ) } catch {} + return sourceRoot +} + +export async function injectSourcesContent( + map: SourceMapLike, + file: string, + logger: Logger, +): Promise { + let sourceRootPromise: Promise const missingSources: string[] = [] const sourcesContent = map.sourcesContent || [] await Promise.all( - map.sources.map(async (sourcePath, index) => { - let content = null - if (sourcePath && !virtualSourceRE.test(sourcePath)) { + map.sources + .filter( + (sourcePath, index) => + !sourcesContent[index] && + sourcePath && + !virtualSourceRE.test(sourcePath), + ) + .map(async (sourcePath, index) => { + // inject content from source file when sourcesContent is null + sourceRootPromise ??= computeSourceRoute(map, file) + const sourceRoot = await sourceRootPromise sourcePath = decodeURI(sourcePath) if (sourceRoot) { sourcePath = path.resolve(sourceRoot, sourcePath) } - // inject content from source file when sourcesContent is null - content = - sourcesContent[index] ?? - (await fsp.readFile(sourcePath, 'utf-8').catch(() => { + sourcesContent[index] = await fsp + .readFile(sourcePath, 'utf-8') + .catch(() => { missingSources.push(sourcePath) return null - })) - } - sourcesContent[index] = content - }), + }) + }), ) map.sourcesContent = sourcesContent From 1b2d1e0dbcdbbe29aaac898d001591a128894747 Mon Sep 17 00:00:00 2001 From: patak Date: Sat, 2 Dec 2023 15:41:29 +0100 Subject: [PATCH 2/2] fix: use flat for --- packages/vite/src/node/server/sourcemap.ts | 52 ++++++++++++---------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/packages/vite/src/node/server/sourcemap.ts b/packages/vite/src/node/server/sourcemap.ts index 06bb063dcd3df4..eafde8c25b67d1 100644 --- a/packages/vite/src/node/server/sourcemap.ts +++ b/packages/vite/src/node/server/sourcemap.ts @@ -39,30 +39,36 @@ export async function injectSourcesContent( const missingSources: string[] = [] const sourcesContent = map.sourcesContent || [] - await Promise.all( - map.sources - .filter( - (sourcePath, index) => - !sourcesContent[index] && - sourcePath && - !virtualSourceRE.test(sourcePath), + const sourcesContentPromises: Promise[] = [] + for (let index = 0; index < map.sources.length; index++) { + const sourcePath = map.sources[index] + if ( + !sourcesContent[index] && + sourcePath && + !virtualSourceRE.test(sourcePath) + ) { + sourcesContentPromises.push( + (async () => { + // inject content from source file when sourcesContent is null + sourceRootPromise ??= computeSourceRoute(map, file) + const sourceRoot = await sourceRootPromise + let resolvedSourcePath = decodeURI(sourcePath) + if (sourceRoot) { + resolvedSourcePath = path.resolve(sourceRoot, resolvedSourcePath) + } + + sourcesContent[index] = await fsp + .readFile(resolvedSourcePath, 'utf-8') + .catch(() => { + missingSources.push(resolvedSourcePath) + return null + }) + })(), ) - .map(async (sourcePath, index) => { - // inject content from source file when sourcesContent is null - sourceRootPromise ??= computeSourceRoute(map, file) - const sourceRoot = await sourceRootPromise - sourcePath = decodeURI(sourcePath) - if (sourceRoot) { - sourcePath = path.resolve(sourceRoot, sourcePath) - } - sourcesContent[index] = await fsp - .readFile(sourcePath, 'utf-8') - .catch(() => { - missingSources.push(sourcePath) - return null - }) - }), - ) + } + } + + await Promise.all(sourcesContentPromises) map.sourcesContent = sourcesContent