fix: __vite__mapDeps code injection#15732
Conversation
|
|
__vite__mapDeps injected into a wrong position__vite__mapDeps before the correct source map comment
__vite__mapDeps before the correct source map comment__vite__mapDeps before the source map comment at the end
__vite__mapDeps before the source map comment at the end__vite__mapDeps before the source map comment at the last line
There was a problem hiding this comment.
Related to the fact that mapFileCommentRegex matches anywhere in the file, I noticed that sourcemap: "inline" currently removes such string in the code as well:
vite/packages/vite/src/node/plugins/importAnalysisBuild.ts
Lines 556 to 561 in 013be31
For example:
// input
console.log(`
this is string literal
//# sourceMappingURL=1.css.map
`)
// output
console.log(`
this is string literal
`)I suppose we can handle this issue separately?
(Probably the fix is as simple as hi-ogawa#1, but I didn't find a relevant test, so I'll try to come up with that separately)
|
|
||
| const dynamicImportPrefixRE = /import\s*\(/ | ||
|
|
||
| // modified convert-source-map's `mapFileCommentRegex` to match only at the last line |
There was a problem hiding this comment.
mapFileCommentRegexto match only at the last line
This is correct for codes generated by Rollup itself. However, a plugin may inject a source map comment between some codes or that doesn't match this regex (e.g. /* sourceMappingURL=validURL *//* sourceMappingMeta={"foo":"bar"} */).
This is not allowed by the spec (although what "at the end of the source" means is not strictly defined). That said, it's supported by many runtimes (tc39/ecma426#64) and I guess it's better to avoid making this assumption.
In this case, how about prepending mapDepsCode instead of appending it? We need to deal with the Hashbang Grammar but that's not so hard.
There was a problem hiding this comment.
In this case, how about prepending
mapDepsCodeinstead of appending it? We need to deal with the Hashbang Grammar but that's not so hard.
Thanks for the review!
Right, I was also wondering about prepending it. I think I was worrying about hashbang as well, but probably prepending would be more straight forward overall. Let me try.
There was a problem hiding this comment.
I updated a PR to prepend the code 7e714e1
The test case looks rather artificial but I (manually) confirmed that it covers a new code path and also sourcemap works for this case.
__vite__mapDeps before the source map comment at the last line__vite__mapDeps code injection for sourcemap
__vite__mapDeps code injection for sourcemap__vite__mapDeps code injection
| banner(chunk) { | ||
| if (chunk.name.endsWith('after-preload-dynamic-hashbang')) { | ||
| return '#!/usr/bin/env node' | ||
| } |
There was a problem hiding this comment.
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/[email protected] 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/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:337:30)
at nodeConverters (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:2072:9)
at convertNode (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:957:12)
at convertProgram (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:948:48)
at parseAstAsync (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:2138:20)
at Module.tryParseAsync (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:13357:21)
at Module.setSource (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:12953:35)
at ModuleLoader.addModuleSource (file:///home/hiroshi/code/others/vite/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:17580:13)
ELIFECYCLE Command failed with exit code 1.|
|
||
| const dynamicImportPrefixRE = /import\s*\(/ | ||
|
|
||
| // modified convert-source-map's `mapFileCommentRegex` to match only at the last line |
There was a problem hiding this comment.
I updated a PR to prepend the code 7e714e1
The test case looks rather artificial but I (manually) confirmed that it covers a new code path and also sourcemap works for this case.
|
/ecosystem-ci run |
|
📝 Ran ecosystem CI on
|
__vite__mapDeps code injection__vite__mapDeps code injection
bluwy
left a comment
There was a problem hiding this comment.
LGTM! I also prefer the prepend approach
| s.prependLeft(code.indexOf('\n') + 1, mapDepsCode) | ||
| } else { | ||
| s.append(mapDepsCode) | ||
| s.prepend(mapDepsCode) |
There was a problem hiding this comment.
We can use [] instead of __vite__mapDeps([${renderedDeps.join(',')}]) if renderedDeps.length is 0. Then, we can skip injecting __vite__mapDeps. Contributions are welcome 👍

Description
__vite__mapDepsis not defined #15702I copied and adjusted(EDIT: now prependingmapFileCommentRegexconstant from https://github.com/thlorenz/convert-source-map/blob/1afbeee2f2a42a3747c31dfcfc355387afdf42e2/index.js#L14 so that it will only matches the last line of the source code.__vite__mapDepsat the top)I manually verified that sourcemap is working via
pnpm -C playground/js-sourcemap build + preview:Show screenshot
2024-02-21.13-28-46.webm
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).