Skip to content

Commit

Permalink
fix(compiler-sfc): custom blocks sourcemap (#1812)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Aug 14, 2020
1 parent 6f8bac5 commit 619efd9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
33 changes: 32 additions & 1 deletion packages/compiler-sfc/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ describe('compiler:sfc', () => {
expect(mapping.originalLine - mapping.generatedLine).toBe(padding)
})
})

test('custom block', () => {
const padding = Math.round(Math.random() * 10)
const custom = parse(
`${'\n'.repeat(padding)}<i18n>\n{\n "greeting": "hello"\n}\n</i18n>\n`
).descriptor.customBlocks[0]

expect(custom!.map).not.toBeUndefined()

const consumer = new SourceMapConsumer(custom!.map!)
consumer.eachMapping(mapping => {
expect(mapping.originalLine - mapping.generatedLine).toBe(padding)
})
})
})

test('pad content', () => {
Expand All @@ -45,11 +59,16 @@ export default {}
</script>
<style>
h1 { color: red }
</style>`
</style>
<i18n>
{ "greeting": "hello" }
</i18n>
`
const padFalse = parse(content.trim(), { pad: false }).descriptor
expect(padFalse.template!.content).toBe('\n<div></div>\n')
expect(padFalse.script!.content).toBe('\nexport default {}\n')
expect(padFalse.styles[0].content).toBe('\nh1 { color: red }\n')
expect(padFalse.customBlocks[0].content).toBe('\n{ "greeting": "hello" }\n')

const padTrue = parse(content.trim(), { pad: true }).descriptor
expect(padTrue.script!.content).toBe(
Expand All @@ -58,6 +77,9 @@ h1 { color: red }
expect(padTrue.styles[0].content).toBe(
Array(6 + 1).join('\n') + '\nh1 { color: red }\n'
)
expect(padTrue.customBlocks[0].content).toBe(
Array(9 + 1).join('\n') + '\n{ "greeting": "hello" }\n'
)

const padLine = parse(content.trim(), { pad: 'line' }).descriptor
expect(padLine.script!.content).toBe(
Expand All @@ -66,6 +88,9 @@ h1 { color: red }
expect(padLine.styles[0].content).toBe(
Array(6 + 1).join('\n') + '\nh1 { color: red }\n'
)
expect(padLine.customBlocks[0].content).toBe(
Array(9 + 1).join('\n') + '\n{ "greeting": "hello" }\n'
)

const padSpace = parse(content.trim(), { pad: 'space' }).descriptor
expect(padSpace.script!.content).toBe(
Expand All @@ -78,6 +103,12 @@ h1 { color: red }
' '
) + '\nh1 { color: red }\n'
)
expect(padSpace.customBlocks[0].content).toBe(
`<template>\n<div></div>\n</template>\n<script>\nexport default {}\n</script>\n<style>\nh1 { color: red }\n</style>\n<i18n>`.replace(
/./g,
' '
) + '\n{ "greeting": "hello" }\n'
)
})

test('should ignore nodes with no content', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/compiler-sfc/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export function parse(
genMap(descriptor.template)
genMap(descriptor.script)
descriptor.styles.forEach(genMap)
descriptor.customBlocks.forEach(genMap)
}

const result = {
Expand Down

0 comments on commit 619efd9

Please sign in to comment.