Skip to content

Commit c11002f

Browse files
committedNov 25, 2023
perf(codegen): optimize source map generation
1 parent 3be53d9 commit c11002f

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed
 

‎packages/compiler-core/src/codegen.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -206,25 +206,28 @@ function createCodegenContext(
206206
context.push('\n' + ` `.repeat(n), NewlineType.Start)
207207
}
208208

209-
function addMapping(loc: Position, name?: string) {
210-
context.map!.addMapping({
211-
name,
212-
source: context.filename,
213-
original: {
214-
line: loc.line,
215-
column: loc.column - 1 // source-map column is 0 based
216-
},
217-
generated: {
218-
line: context.line,
219-
column: context.column - 1
220-
}
209+
function addMapping(loc: Position, name: string | null = null) {
210+
// @ts-ignore we use the private property to directly add the mapping
211+
// because the addMapping() implementation in source-map-js has a bunch of
212+
// unnecessary arg and validation checks that are pure overhead in our case.
213+
const { _names, _mappings } = context.map
214+
if (name !== null && !_names.has(name)) _names.add(name)
215+
_mappings.add({
216+
originalLine: loc.line,
217+
originalColumn: loc.column - 1, // source-map column is 0 based
218+
generatedLine: context.line,
219+
generatedColumn: context.column - 1,
220+
source: filename,
221+
name
221222
})
222223
}
223224

224225
if (!__BROWSER__ && sourceMap) {
225226
// lazy require source-map implementation, only in non-browser builds
226227
context.map = new SourceMapGenerator()
227-
context.map!.setSourceContent(filename, context.source)
228+
context.map.setSourceContent(filename, context.source)
229+
// @ts-ignore
230+
context.map._sources.add(filename)
228231
}
229232

230233
return context

0 commit comments

Comments
 (0)
Please sign in to comment.