]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
perf: avoid sfc source map unnecessary serialization and parsing
authorEvan You <yyx990803@gmail.com>
Mon, 27 Nov 2023 04:18:01 +0000 (12:18 +0800)
committerEvan You <yyx990803@gmail.com>
Mon, 27 Nov 2023 04:26:51 +0000 (12:26 +0800)
packages/compiler-core/src/codegen.ts
packages/compiler-sfc/src/parse.ts
packages/compiler-sfc/src/style/preprocessors.ts
packages/global.d.ts

index c4957fc312d53074789e27c52c4d645b58e42209..ea53640f3ba32ed977cc5a7fcb139261b26d22d8 100644 (file)
@@ -207,10 +207,10 @@ function createCodegenContext(
   }
 
   function addMapping(loc: Position, name: string | null = null) {
-    // @ts-ignore we use the private property to directly add the mapping
+    // 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
+    const { _names, _mappings } = context.map!
     if (name !== null && !_names.has(name)) _names.add(name)
     _mappings.add({
       originalLine: loc.line,
@@ -354,8 +354,7 @@ export function generate(
     ast,
     code: context.code,
     preamble: isSetupInlined ? preambleContext.code : ``,
-    // SourceMapGenerator does have toJSON() method but it's not in the types
-    map: context.map ? (context.map as any).toJSON() : undefined
+    map: context.map ? context.map.toJSON() : undefined
   }
 }
 
index 03971d431e8f47c7cdc93e711b491afce2929fb6..ba8517658fae597b772b98d8bf2be896c4eea26a 100644 (file)
@@ -360,7 +360,7 @@ function generateSourceMap(
       }
     }
   })
-  return JSON.parse(map.toString())
+  return map.toJSON()
 }
 
 function padContent(
index 9b3610501c77981be12a1a35fdf7e6e17a59fda1..e4630745667d05b4f97b1d1462083cac77bbf69a 100644 (file)
@@ -38,7 +38,12 @@ const scss: StylePreprocessor = (source, map, options, load = require) => {
     if (map) {
       return {
         code: result.css.toString(),
-        map: merge(map, JSON.parse(result.map.toString())),
+        map: merge(
+          map,
+          result.map.toJSON
+            ? result.map.toJSON()
+            : JSON.parse(result.map.toString())
+        ),
         errors: [],
         dependencies
       }
index 97405129be946755a82cd1581d3f759577d33ddc..64699ae775965fc09e285400cfbd795ba9c576a8 100644 (file)
@@ -44,6 +44,24 @@ declare module 'estree-walker' {
   )
 }
 
+declare module 'source-map-js' {
+  export interface SourceMapGenerator {
+    // SourceMapGenerator has this method but the types do not include it
+    toJSON(): RawSourceMap
+    _names: Set<string>
+    _mappings: {
+      add(mapping: {
+        originalLine: number
+        originalColumn: number
+        generatedLine: number
+        generatedColumn: number
+        source: string
+        name: string | null
+      }): void
+    }
+  }
+}
+
 declare interface String {
   /**
    * @deprecated Please use String.prototype.slice instead of String.prototype.substring in the repository.