Skip to content

Commit 77151dc

Browse files
committed
support source map in sfc-parser
1 parent b0b9691 commit 77151dc

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/compiler/parser/sfc-parser.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { parseHTML } from './html-parser'
99
import { makeMap } from 'shared/util'
1010

1111
const splitRE = /\r?\n/g
12+
const emptyRE = /^(?:\/\/)?\s*$/
1213
const isSpecialTag = makeMap('script,style,template', true)
1314

1415
type Attribute = {
@@ -68,7 +69,7 @@ export function parseComponent (
6869
function end () {
6970
depth--
7071
if (currentBlock && options && options.map) {
71-
addSourceMap(currentBlock)
72+
addSourceMap(currentBlock, options.map)
7273
}
7374
currentBlock = null
7475
}
@@ -95,8 +96,29 @@ export function parseComponent (
9596
return Array(leadingContent.split(splitRE).length).join(padChar)
9697
}
9798

98-
function addSourceMap (block: SFCBlock) {
99-
99+
function addSourceMap (block: SFCBlock, options: Object) {
100+
const filename = options.filename
101+
if (!filename) {
102+
throw new Error('Should provide original filename in the map option.')
103+
}
104+
const map = new SourceMapGenerator()
105+
map.setSourceContent(filename, content)
106+
block.content.split(splitRE).forEach((line, index) => {
107+
if (!emptyRE.test(line)) {
108+
map.addMapping({
109+
source: filename,
110+
original: {
111+
line: index + 1,
112+
column: 0
113+
},
114+
generated: {
115+
line: index + 1,
116+
column: 0
117+
}
118+
})
119+
}
120+
})
121+
block.map = JSON.parse(map.toString())
100122
}
101123

102124
parseHTML(content, {

0 commit comments

Comments
 (0)