]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(compiler-sfc): generate source map for template block
authorEvan You <yyx990803@gmail.com>
Fri, 13 Dec 2019 18:22:30 +0000 (13:22 -0500)
committerEvan You <yyx990803@gmail.com>
Fri, 13 Dec 2019 18:22:30 +0000 (13:22 -0500)
packages/compiler-sfc/src/parse.ts

index 16e67aa2705d2ba5540506c044e814d78a8bc017..066dcdb6600c3e20256fe3fe6e72785a462a4bf1 100644 (file)
@@ -112,28 +112,20 @@ export function parse(
   })
 
   if (sourceMap) {
-    if (sfc.script && !sfc.script.src) {
-      sfc.script.map = generateSourceMap(
-        filename,
-        source,
-        sfc.script.content,
-        sourceRoot,
-        pad
-      )
-    }
-    if (sfc.styles) {
-      sfc.styles.forEach(style => {
-        if (!style.src) {
-          style.map = generateSourceMap(
-            filename,
-            source,
-            style.content,
-            sourceRoot,
-            pad
-          )
-        }
-      })
+    const genMap = (block: SFCBlock | null) => {
+      if (block && !block.src) {
+        block.map = generateSourceMap(
+          filename,
+          source,
+          block.content,
+          sourceRoot,
+          pad ? 0 : block.loc.start.line - 1
+        )
+      }
     }
+    genMap(sfc.template)
+    genMap(sfc.script)
+    sfc.styles.forEach(genMap)
   }
   sourceToSFC.set(sourceKey, sfc)
 
@@ -205,27 +197,19 @@ function generateSourceMap(
   source: string,
   generated: string,
   sourceRoot: string,
-  pad?: SFCParseOptions['pad']
+  lineOffset: number
 ): RawSourceMap {
   const map = new SourceMapGenerator({
     file: filename.replace(/\\/g, '/'),
     sourceRoot: sourceRoot.replace(/\\/g, '/')
   })
-  let offset = 0
-  if (!pad) {
-    offset =
-      source
-        .split(generated)
-        .shift()!
-        .split(splitRE).length - 1
-  }
   map.setSourceContent(filename, source)
   generated.split(splitRE).forEach((line, index) => {
     if (!emptyRE.test(line)) {
       map.addMapping({
         source: filename,
         original: {
-          line: index + 1 + offset,
+          line: index + 1 + lineOffset,
           column: 0
         },
         generated: {