mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 22:59:10 +00:00
fix: __vite__mapDeps
code injection (#15732)
This commit is contained in:
parent
edb9c4c798
commit
aff54e1d5e
@ -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
|
||||
|
@ -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$/,
|
||||
)
|
||||
})
|
||||
})
|
||||
|
@ -0,0 +1,5 @@
|
||||
// hashbang is injected via rollupOptions.output.banner
|
||||
|
||||
import('./dynamic/dynamic-foo')
|
||||
|
||||
console.log('after preload dynamic hashbang')
|
@ -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>
|
||||
|
@ -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'
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user