From 1d96f2a95812196826013728972467b334dbc87d Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Tue, 26 Mar 2024 10:22:45 +0900 Subject: [PATCH 1/2] fix: remove unnecessary `__vite__mapDeps` --- .../src/node/plugins/importAnalysisBuild.ts | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 718ff6190cfeac..95b986ebfe5d8e 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -498,32 +498,36 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { s.update( markerStartPos, markerStartPos + preloadMarker.length + 2, - `__vite__mapDeps([${renderedDeps.join(',')}])`, + renderedDeps.length > 0 + ? `__vite__mapDeps([${renderedDeps.join(',')}])` + : `[]`, ) rewroteMarkerStartPos.add(markerStartPos) } } } - const fileDepsCode = `[${fileDeps - .map((fileDep) => - fileDep.runtime ? fileDep.url : JSON.stringify(fileDep.url), - ) - .join(',')}]` - - const mapDepsCode = `\ -function __vite__mapDeps(indexes) { - if (!__vite__mapDeps.viteFileDeps) { - __vite__mapDeps.viteFileDeps = ${fileDepsCode} - } - return indexes.map((i) => __vite__mapDeps.viteFileDeps[i]) -}\n` - - // inject extra code at the top or next line of hashbang - if (code.startsWith('#!')) { - s.prependLeft(code.indexOf('\n') + 1, mapDepsCode) - } else { - s.prepend(mapDepsCode) + if (fileDeps.length > 0) { + const fileDepsCode = `[${fileDeps + .map((fileDep) => + fileDep.runtime ? fileDep.url : JSON.stringify(fileDep.url), + ) + .join(',')}]` + + const mapDepsCode = `\ + function __vite__mapDeps(indexes) { + if (!__vite__mapDeps.viteFileDeps) { + __vite__mapDeps.viteFileDeps = ${fileDepsCode} + } + return indexes.map((i) => __vite__mapDeps.viteFileDeps[i]) + }\n` + + // inject extra code at the top or next line of hashbang + if (code.startsWith('#!')) { + s.prependLeft(code.indexOf('\n') + 1, mapDepsCode) + } else { + s.prepend(mapDepsCode) + } } // there may still be markers due to inlined dynamic imports, remove From 716c7667feed3c185811654048b2b5bf7d7fb205 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Tue, 26 Mar 2024 10:53:26 +0900 Subject: [PATCH 2/2] test: add test --- .../src/node/plugins/importAnalysisBuild.ts | 12 ++++++------ .../js-sourcemap/__tests__/js-sourcemap.spec.ts | 17 +++++++++++++++-- .../after-preload-dynamic-no-dep.js | 3 +++ .../js-sourcemap/dynamic/dynamic-no-dep.js | 1 + playground/js-sourcemap/index.html | 1 + playground/js-sourcemap/vite.config.js | 3 +++ 6 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 playground/js-sourcemap/after-preload-dynamic-no-dep.js create mode 100644 playground/js-sourcemap/dynamic/dynamic-no-dep.js diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 95b986ebfe5d8e..d7f6491ceb7306 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -515,12 +515,12 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { .join(',')}]` const mapDepsCode = `\ - function __vite__mapDeps(indexes) { - if (!__vite__mapDeps.viteFileDeps) { - __vite__mapDeps.viteFileDeps = ${fileDepsCode} - } - return indexes.map((i) => __vite__mapDeps.viteFileDeps[i]) - }\n` +function __vite__mapDeps(indexes) { + if (!__vite__mapDeps.viteFileDeps) { + __vite__mapDeps.viteFileDeps = ${fileDepsCode} + } + return indexes.map((i) => __vite__mapDeps.viteFileDeps[i]) +}\n` // inject extra code at the top or next line of hashbang if (code.startsWith('#!')) { diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index fd5d91a26af1b1..f21ace14adab60 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -152,9 +152,22 @@ describe.runIf(isBuild)('build tests', () => { } `) // verify sourcemap comment is preserved at the last line - const js = findAssetFile(/after-preload-dynamic.*\.js$/) + const js = findAssetFile(/after-preload-dynamic-[-\w]{8}\.js$/) expect(js).toMatch( - /\n\/\/# sourceMappingURL=after-preload-dynamic.*\.js\.map\n$/, + /\n\/\/# sourceMappingURL=after-preload-dynamic-[-\w]{8}\.js\.map\n$/, ) }) + + test('__vite__mapDeps injected after banner', async () => { + const js = findAssetFile(/after-preload-dynamic-hashbang-[-\w]{8}\.js$/) + expect(js.split('\n').slice(0, 2)).toEqual([ + '#!/usr/bin/env node', + 'function __vite__mapDeps(indexes) {', + ]) + }) + + test('no unused __vite__mapDeps', async () => { + const js = findAssetFile(/after-preload-dynamic-no-dep-[-\w]{8}\.js$/) + expect(js).not.toMatch(/__vite__mapDeps/) + }) }) diff --git a/playground/js-sourcemap/after-preload-dynamic-no-dep.js b/playground/js-sourcemap/after-preload-dynamic-no-dep.js new file mode 100644 index 00000000000000..55f150702d02dc --- /dev/null +++ b/playground/js-sourcemap/after-preload-dynamic-no-dep.js @@ -0,0 +1,3 @@ +import('./dynamic/dynamic-no-dep') + +console.log('after preload dynamic no dep') diff --git a/playground/js-sourcemap/dynamic/dynamic-no-dep.js b/playground/js-sourcemap/dynamic/dynamic-no-dep.js new file mode 100644 index 00000000000000..d2291ff747319c --- /dev/null +++ b/playground/js-sourcemap/dynamic/dynamic-no-dep.js @@ -0,0 +1 @@ +console.log('dynamic/dynamic-no-dep') diff --git a/playground/js-sourcemap/index.html b/playground/js-sourcemap/index.html index 37ebf9397507c7..10afcc6c2d0807 100644 --- a/playground/js-sourcemap/index.html +++ b/playground/js-sourcemap/index.html @@ -7,5 +7,6 @@

JS Sourcemap

+ diff --git a/playground/js-sourcemap/vite.config.js b/playground/js-sourcemap/vite.config.js index ba1c3510fc23b5..0c6d09b8d8906f 100644 --- a/playground/js-sourcemap/vite.config.js +++ b/playground/js-sourcemap/vite.config.js @@ -18,6 +18,9 @@ export default defineConfig({ if (name.endsWith('after-preload-dynamic-hashbang.js')) { return 'after-preload-dynamic-hashbang' } + if (name.endsWith('after-preload-dynamic-no-dep.js')) { + return 'after-preload-dynamic-no-dep' + } }, banner(chunk) { if (chunk.name.endsWith('after-preload-dynamic-hashbang')) {