]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
perf(codegen): optimize source map generation
authorEvan You <yyx990803@gmail.com>
Fri, 24 Nov 2023 12:48:12 +0000 (20:48 +0800)
committerEvan You <yyx990803@gmail.com>
Sat, 25 Nov 2023 08:18:29 +0000 (16:18 +0800)
packages/compiler-core/src/codegen.ts

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