From aff54e1d5e3129a442aeec8b6aef024024ba5b1b Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Wed, 21 Feb 2024 22:05:09 +0900 Subject: [PATCH] fix: `__vite__mapDeps` code injection (#15732) --- packages/vite/src/node/plugins/importAnalysisBuild.ts | 10 ++++------ playground/js-sourcemap/__tests__/js-sourcemap.spec.ts | 8 ++++---- .../js-sourcemap/after-preload-dynamic-hashbang.js | 5 +++++ playground/js-sourcemap/index.html | 1 + playground/js-sourcemap/vite.config.js | 10 +++++++++- 5 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 playground/js-sourcemap/after-preload-dynamic-hashbang.js diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index da43afbe6..2311f0859 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -507,13 +507,11 @@ function __vite__mapDeps(indexes) { return indexes.map((i) => __vite__mapDeps.viteFileDeps[i]) }\n` - // inject extra code before sourcemap comment - const mapFileCommentMatch = - convertSourceMap.mapFileCommentRegex.exec(code) - if (mapFileCommentMatch) { - s.appendRight(mapFileCommentMatch.index, mapDepsCode) + // inject extra code at the top or next line of hashbang + if (code.startsWith('#!')) { + s.prependLeft(code.indexOf('\n') + 1, mapDepsCode) } else { - s.append(mapDepsCode) + s.prepend(mapDepsCode) } // there may still be markers due to inlined dynamic imports, remove diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index 7c22274b2..be37165a3 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -137,7 +137,7 @@ describe.runIf(isBuild)('build tests', () => { const map = findAssetFile(/after-preload-dynamic.*\.js\.map/) expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(` { - "mappings": "i3BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB", + "mappings": ";;;;;;i3BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB", "sources": [ "../../after-preload-dynamic.js", ], @@ -150,10 +150,10 @@ describe.runIf(isBuild)('build tests', () => { "version": 3, } `) - // + // verify sourcemap comment is preserved at the last line const js = findAssetFile(/after-preload-dynamic.*\.js$/) - expect(js.trim().split('\n').at(-1)).toMatch( - /^\/\/# sourceMappingURL=after-preload-dynamic.*\.js\.map$/, + expect(js).toMatch( + /\n\/\/# sourceMappingURL=after-preload-dynamic.*\.js\.map\n$/, ) }) }) diff --git a/playground/js-sourcemap/after-preload-dynamic-hashbang.js b/playground/js-sourcemap/after-preload-dynamic-hashbang.js new file mode 100644 index 000000000..918cdeff7 --- /dev/null +++ b/playground/js-sourcemap/after-preload-dynamic-hashbang.js @@ -0,0 +1,5 @@ +// hashbang is injected via rollupOptions.output.banner + +import('./dynamic/dynamic-foo') + +console.log('after preload dynamic hashbang') diff --git a/playground/js-sourcemap/index.html b/playground/js-sourcemap/index.html index 80ee729d9..37ebf9397 100644 --- a/playground/js-sourcemap/index.html +++ b/playground/js-sourcemap/index.html @@ -6,5 +6,6 @@ + diff --git a/playground/js-sourcemap/vite.config.js b/playground/js-sourcemap/vite.config.js index 41484f2c9..ba1c3510f 100644 --- a/playground/js-sourcemap/vite.config.js +++ b/playground/js-sourcemap/vite.config.js @@ -12,9 +12,17 @@ export default defineConfig({ rollupOptions: { output: { manualChunks(name) { - if (name.includes('after-preload-dynamic')) { + if (name.endsWith('after-preload-dynamic.js')) { return 'after-preload-dynamic' } + if (name.endsWith('after-preload-dynamic-hashbang.js')) { + return 'after-preload-dynamic-hashbang' + } + }, + banner(chunk) { + if (chunk.name.endsWith('after-preload-dynamic-hashbang')) { + return '#!/usr/bin/env node' + } }, }, },