Skip to content

Commit

Permalink
feat(compiler-sfc): generate source map for template block
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 13, 2019
1 parent 9e757b5 commit 865c1ce
Showing 1 changed file with 15 additions and 31 deletions.
46 changes: 15 additions & 31 deletions packages/compiler-sfc/src/parse.ts
Expand Up @@ -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)

Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 865c1ce

Please sign in to comment.