-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Conversation
Run & review this pull request in StackBlitz Codeflow. |
__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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
if (config.build.sourcemap === 'inline') { | |
chunk.code = chunk.code.replace( | |
convertSourceMap.mapFileCommentRegex, | |
'', | |
) | |
chunk.code += `\n//# sourceMappingURL=${genSourceMapUrl(map)}` |
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)
@@ -41,6 +41,12 @@ const preloadMarkerWithQuote = new RegExp(`['"]${preloadMarker}['"]`, 'g') | |||
|
|||
const dynamicImportPrefixRE = /import\s*\(/ | |||
|
|||
// modified convert-source-map's `mapFileCommentRegex` to match only at the last line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mapFileCommentRegex
to 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/source-map#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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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.
@@ -41,6 +41,12 @@ const preloadMarkerWithQuote = new RegExp(`['"]${preloadMarker}['"]`, 'g') | |||
|
|||
const dynamicImportPrefixRE = /import\s*\(/ | |||
|
|||
// modified convert-source-map's `mapFileCommentRegex` to match only at the last line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
/ecosystem-ci run |
📝 Ran ecosystem CI on
|
__vite__mapDeps
code injection__vite__mapDeps
code injection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! I also prefer the prepend approach
} else { | ||
s.append(mapDepsCode) | ||
s.prepend(mapDepsCode) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 👍
Description
__vite__mapDeps
is not defined #15702I copied and adjusted(EDIT: now prependingmapFileCommentRegex
constant 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__mapDeps
at 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
).