Skip to content

Commit aff54e1

Browse files
authored
fix: __vite__mapDeps code injection (#15732)
1 parent edb9c4c commit aff54e1

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

packages/vite/src/node/plugins/importAnalysisBuild.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -507,13 +507,11 @@ function __vite__mapDeps(indexes) {
507507
return indexes.map((i) => __vite__mapDeps.viteFileDeps[i])
508508
}\n`
509509

510-
// inject extra code before sourcemap comment
511-
const mapFileCommentMatch =
512-
convertSourceMap.mapFileCommentRegex.exec(code)
513-
if (mapFileCommentMatch) {
514-
s.appendRight(mapFileCommentMatch.index, mapDepsCode)
510+
// inject extra code at the top or next line of hashbang
511+
if (code.startsWith('#!')) {
512+
s.prependLeft(code.indexOf('\n') + 1, mapDepsCode)
515513
} else {
516-
s.append(mapDepsCode)
514+
s.prepend(mapDepsCode)
517515
}
518516

519517
// there may still be markers due to inlined dynamic imports, remove

playground/js-sourcemap/__tests__/js-sourcemap.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe.runIf(isBuild)('build tests', () => {
137137
const map = findAssetFile(/after-preload-dynamic.*\.js\.map/)
138138
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
139139
{
140-
"mappings": "i3BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
140+
"mappings": ";;;;;;i3BAAA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
141141
"sources": [
142142
"../../after-preload-dynamic.js",
143143
],
@@ -150,10 +150,10 @@ describe.runIf(isBuild)('build tests', () => {
150150
"version": 3,
151151
}
152152
`)
153-
//
153+
// verify sourcemap comment is preserved at the last line
154154
const js = findAssetFile(/after-preload-dynamic.*\.js$/)
155-
expect(js.trim().split('\n').at(-1)).toMatch(
156-
/^\/\/# sourceMappingURL=after-preload-dynamic.*\.js\.map$/,
155+
expect(js).toMatch(
156+
/\n\/\/# sourceMappingURL=after-preload-dynamic.*\.js\.map\n$/,
157157
)
158158
})
159159
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// hashbang is injected via rollupOptions.output.banner
2+
3+
import('./dynamic/dynamic-foo')
4+
5+
console.log('after preload dynamic hashbang')

playground/js-sourcemap/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ <h1>JS Sourcemap</h1>
66
<script type="module" src="./foo.js"></script>
77
<script type="module" src="./bar.ts"></script>
88
<script type="module" src="./after-preload-dynamic.js"></script>
9+
<script type="module" src="./after-preload-dynamic-hashbang.js"></script>
910
<script type="module" src="./with-multiline-import.ts"></script>
1011
<script type="module" src="./zoo.js"></script>

playground/js-sourcemap/vite.config.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@ export default defineConfig({
1212
rollupOptions: {
1313
output: {
1414
manualChunks(name) {
15-
if (name.includes('after-preload-dynamic')) {
15+
if (name.endsWith('after-preload-dynamic.js')) {
1616
return 'after-preload-dynamic'
1717
}
18+
if (name.endsWith('after-preload-dynamic-hashbang.js')) {
19+
return 'after-preload-dynamic-hashbang'
20+
}
21+
},
22+
banner(chunk) {
23+
if (chunk.name.endsWith('after-preload-dynamic-hashbang')) {
24+
return '#!/usr/bin/env node'
25+
}
1826
},
1927
},
2028
},

0 commit comments

Comments
 (0)