Skip to content

Commit d2aa096

Browse files
authored
fix: inject __vite__mapDeps code before sourcemap file comment (#15483)
1 parent 2a540ee commit d2aa096

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/vite/src/node/plugins/importAnalysisBuild.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -634,13 +634,22 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
634634
)
635635
.join(',')}]`
636636

637-
s.append(`\
637+
const mapDepsCode = `\
638638
function __vite__mapDeps(indexes) {
639639
if (!__vite__mapDeps.viteFileDeps) {
640640
__vite__mapDeps.viteFileDeps = ${fileDepsCode}
641641
}
642642
return indexes.map((i) => __vite__mapDeps.viteFileDeps[i])
643-
}`)
643+
}\n`
644+
645+
// inject extra code before sourcemap comment
646+
const mapFileCommentMatch =
647+
convertSourceMap.mapFileCommentRegex.exec(code)
648+
if (mapFileCommentMatch) {
649+
s.appendRight(mapFileCommentMatch.index, mapDepsCode)
650+
} else {
651+
s.append(mapDepsCode)
652+
}
644653

645654
// there may still be markers due to inlined dynamic imports, remove
646655
// all the markers regardless

playground/js-sourcemap/__tests__/js-sourcemap.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,10 @@ describe.runIf(isBuild)('build tests', () => {
129129
"version": 3,
130130
}
131131
`)
132+
//
133+
const js = findAssetFile(/after-preload-dynamic.*\.js$/)
134+
expect(js.trim().split('\n').at(-1)).toMatch(
135+
/^\/\/# sourceMappingURL=after-preload-dynamic.*\.js\.map$/,
136+
)
132137
})
133138
})

0 commit comments

Comments
 (0)