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 4 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: 8 additions & 2 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Expand Up @@ -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
Copy link
Member

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.

Copy link
Collaborator Author

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.

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 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.

// https://github.com/thlorenz/convert-source-map/blob/1afbeee2f2a42a3747c31dfcfc355387afdf42e2/index.js#L14
const lastMapFileCommentRegex =
// eslint-disable-next-line regexp/no-super-linear-backtracking
/\/\/[@#][ \t]+sourceMappingURL=([^\s'"`]+)\n$|\/\*[@#][ \t]+sourceMappingURL=([^*]+?)[ \t]*\*\/\n$/

function toRelativePath(filename: string, importer: string) {
const relPath = path.posix.relative(path.posix.dirname(importer), filename)
return relPath[0] === '.' ? relPath : `./${relPath}`
Expand Down Expand Up @@ -507,9 +513,9 @@ function __vite__mapDeps(indexes) {
return indexes.map((i) => __vite__mapDeps.viteFileDeps[i])
}\n`

// inject extra code before sourcemap comment
// inject extra code before the last sourcemap comment
const mapFileCommentMatch =
convertSourceMap.mapFileCommentRegex.exec(code)
config.build.sourcemap && lastMapFileCommentRegex.exec(code)
if (mapFileCommentMatch) {
s.appendRight(mapFileCommentMatch.index, mapDepsCode)
} else {
Expand Down
16 changes: 11 additions & 5 deletions playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Expand Up @@ -137,23 +137,29 @@ 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": "22BAAA,QAAQ,IAAI;AAAA;AAAA;AAAA;AAAA,CAIX,QAED,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
"sources": [
"../../after-preload-dynamic.js",
],
"sourcesContent": [
"import('./dynamic/dynamic-foo')
"console.log(\`
false-positive sourcemap comments
//# sourceMappingURL=1.css.map
/*# sourceMappingURL=2.css.map */
\`)

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

console.log('after preload dynamic')
",
],
"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$/,
)
})
})
6 changes: 6 additions & 0 deletions playground/js-sourcemap/after-preload-dynamic.js
@@ -1,3 +1,9 @@
console.log(`
false-positive sourcemap comments
//# sourceMappingURL=1.css.map
/*# sourceMappingURL=2.css.map */
`)

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

console.log('after preload dynamic')