Skip to content

Commit

Permalink
fix: inject __vite__mapDeps code before sourcemap file comment (#15483
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hi-ogawa committed Jan 2, 2024
1 parent 2a540ee commit d2aa096
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
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
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$/,
)
})
})

0 comments on commit d2aa096

Please sign in to comment.