Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: __vite__mapDeps code injection #15732

Merged
merged 7 commits into from Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
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)
Copy link

@lisonge lisonge Mar 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if fileDeps.length == 0, maybe we do not need exec s.prependLeft or prepend

because sometimes __vite__mapDeps is not used

image

I can't find how to remove the configuration of __vite__mapDeps

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use [] instead of __vite__mapDeps([${renderedDeps.join(',')}]) if renderedDeps.length is 0. Then, we can skip injecting __vite__mapDeps. Contributions are welcome 👍

}

// 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
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
@@ -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
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
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'
}
Comment on lines +22 to +25
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used banner to inject hashbang and test this case since having #! ... inside after-preload-dynamic-hashbang.js seems to cause a parse error somewhere in the pipeline. The error looks like this:

$ pnpm -C playground/js-sourcemap build

> @vitejs/test-js-sourcemap@0.0.0 build /home/hiroshi/code/others/vite/playground/js-sourcemap
> vite build

vite v5.1.3 building for production...
✓ 7 modules transformed.
x Build failed in 56ms
error during build:
RollupError: Expected ident
    at error (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/rollup@4.2.0/node_modules/rollup/dist/es/shared/parseAst.js:337:30)
    at nodeConverters (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/rollup@4.2.0/node_modules/rollup/dist/es/shared/parseAst.js:2072:9)
    at convertNode (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/rollup@4.2.0/node_modules/rollup/dist/es/shared/parseAst.js:957:12)
    at convertProgram (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/rollup@4.2.0/node_modules/rollup/dist/es/shared/parseAst.js:948:48)
    at parseAstAsync (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/rollup@4.2.0/node_modules/rollup/dist/es/shared/parseAst.js:2138:20)
    at Module.tryParseAsync (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/rollup@4.2.0/node_modules/rollup/dist/es/shared/node-entry.js:13357:21)
    at Module.setSource (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/rollup@4.2.0/node_modules/rollup/dist/es/shared/node-entry.js:12953:35)
    at ModuleLoader.addModuleSource (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/rollup@4.2.0/node_modules/rollup/dist/es/shared/node-entry.js:17580:13)
 ELIFECYCLE  Command failed with exit code 1.

},
},
},
Expand Down