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: inject __vite__mapDeps code before sourcemap file comment #15483

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
13 changes: 11 additions & 2 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,13 +634,22 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
)
.join(',')}]`

s.append(`\
const mapDepsCode = `\
function __vite__mapDeps(indexes) {
if (!__vite__mapDeps.viteFileDeps) {
__vite__mapDeps.viteFileDeps = ${fileDepsCode}
}
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)
} else {
s.append(mapDepsCode)
}

// there may still be markers due to inlined dynamic imports, remove
// all the markers regardless
Expand Down
5 changes: 5 additions & 0 deletions playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,10 @@ describe.runIf(isBuild)('build tests', () => {
"version": 3,
}
`)
//
const js = findAssetFile(/after-preload-dynamic.*\.js$/)
expect(js.trim().split('\n').at(-1)).toMatch(
/^\/\/# sourceMappingURL=after-preload-dynamic.*\.js\.map$/,
)
})
})