Skip to content

Commit

Permalink
fix: __vite__mapDeps code injection (#15732)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Feb 21, 2024
1 parent edb9c4c commit aff54e1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
10 changes: 4 additions & 6 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand All @@ -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$/,
)
})
})
5 changes: 5 additions & 0 deletions playground/js-sourcemap/after-preload-dynamic-hashbang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hashbang is injected via rollupOptions.output.banner

import('./dynamic/dynamic-foo')

console.log('after preload dynamic hashbang')
1 change: 1 addition & 0 deletions playground/js-sourcemap/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ <h1>JS Sourcemap</h1>
<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>
10 changes: 9 additions & 1 deletion playground/js-sourcemap/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
},
},
},
Expand Down

0 comments on commit aff54e1

Please sign in to comment.