fix: __vite__mapDeps code injection (#15732)

This commit is contained in:
Hiroshi Ogawa 2024-02-21 22:05:09 +09:00 committed by GitHub
parent edb9c4c798
commit aff54e1d5e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 23 additions and 11 deletions

View File

@ -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

View File

@ -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$/,
)
})
})

View File

@ -0,0 +1,5 @@
// hashbang is injected via rollupOptions.output.banner
import('./dynamic/dynamic-foo')
console.log('after preload dynamic hashbang')

View File

@ -6,5 +6,6 @@
<script type="module" src="./foo.js"></script>
<script type="module" src="./bar.ts"></script>
<script type="module" src="./after-preload-dynamic.js"></script>
<script type="module" src="./after-preload-dynamic-hashbang.js"></script>
<script type="module" src="./with-multiline-import.ts"></script>
<script type="module" src="./zoo.js"></script>

View File

@ -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'
}
},
},
},