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 1 commit
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
13 changes: 9 additions & 4 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Expand Up @@ -41,6 +41,12 @@ const preloadMarkerWithQuote = new RegExp(`['"]${preloadMarker}['"]`, 'g')

const dynamicImportPrefixRE = /import\s*\(/

// adjusted `convertSourceMap.mapFileCommentRegex` to match only at the last line
// 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$/
hi-ogawa marked this conversation as resolved.
Show resolved Hide resolved

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,10 +513,9 @@ 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) {
// inject extra code before the last sourcemap comment
const mapFileCommentMatch = lastMapFileCommentRegex.exec(code)
if (config.build.sourcemap && mapFileCommentMatch) {
s.appendRight(mapFileCommentMatch.index, mapDepsCode)
} else {
s.append(mapDepsCode)
Expand Down
18 changes: 13 additions & 5 deletions playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Expand Up @@ -137,23 +137,31 @@ 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": "i3BAQA,OAAO,2BAAuB,EAAC,wBAE/B,QAAQ,IAAI,uBAAuB",
"sources": [
"../../after-preload-dynamic.js",
],
"sourcesContent": [
"import('./dynamic/dynamic-foo')
"// false-positive sourcemap comment examples
//# sourceMappingURL=1.css.map
/*# sourceMappingURL=2.css.map */
;\`
//# sourceMappingURL=3.css.map
/*# sourceMappingURL=4.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$/,
)
})
})
8 changes: 8 additions & 0 deletions playground/js-sourcemap/after-preload-dynamic.js
@@ -1,3 +1,11 @@
// false-positive sourcemap comment examples
//# sourceMappingURL=1.css.map
/*# sourceMappingURL=2.css.map */
;`
//# sourceMappingURL=3.css.map
/*# sourceMappingURL=4.css.map */
`

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

console.log('after preload dynamic')