perf(codegen): optimize source map generation

This commit is contained in:
Evan You 2023-11-24 20:48:12 +08:00
parent 3be53d9b97
commit c11002f16a
1 changed files with 16 additions and 13 deletions

View File

@ -206,25 +206,28 @@ function createCodegenContext(
context.push('\n' + ` `.repeat(n), NewlineType.Start) context.push('\n' + ` `.repeat(n), NewlineType.Start)
} }
function addMapping(loc: Position, name?: string) { function addMapping(loc: Position, name: string | null = null) {
context.map!.addMapping({ // @ts-ignore we use the private property to directly add the mapping
name, // because the addMapping() implementation in source-map-js has a bunch of
source: context.filename, // unnecessary arg and validation checks that are pure overhead in our case.
original: { const { _names, _mappings } = context.map
line: loc.line, if (name !== null && !_names.has(name)) _names.add(name)
column: loc.column - 1 // source-map column is 0 based _mappings.add({
}, originalLine: loc.line,
generated: { originalColumn: loc.column - 1, // source-map column is 0 based
line: context.line, generatedLine: context.line,
column: context.column - 1 generatedColumn: context.column - 1,
} source: filename,
name
}) })
} }
if (!__BROWSER__ && sourceMap) { if (!__BROWSER__ && sourceMap) {
// lazy require source-map implementation, only in non-browser builds // lazy require source-map implementation, only in non-browser builds
context.map = new SourceMapGenerator() context.map = new SourceMapGenerator()
context.map!.setSourceContent(filename, context.source) context.map.setSourceContent(filename, context.source)
// @ts-ignore
context.map._sources.add(filename)
} }
return context return context