fix(compiler-sfc): custom blocks sourcemap (#1812)

This commit is contained in:
kazuya kawaguchi 2020-08-15 06:47:28 +09:00 committed by GitHub
parent 6f8bac5fca
commit 619efd9ac5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View File

@ -33,6 +33,20 @@ describe('compiler:sfc', () => {
expect(mapping.originalLine - mapping.generatedLine).toBe(padding) 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', () => { test('pad content', () => {
@ -45,11 +59,16 @@ export default {}
</script> </script>
<style> <style>
h1 { color: red } h1 { color: red }
</style>` </style>
<i18n>
{ "greeting": "hello" }
</i18n>
`
const padFalse = parse(content.trim(), { pad: false }).descriptor const padFalse = parse(content.trim(), { pad: false }).descriptor
expect(padFalse.template!.content).toBe('\n<div></div>\n') expect(padFalse.template!.content).toBe('\n<div></div>\n')
expect(padFalse.script!.content).toBe('\nexport default {}\n') expect(padFalse.script!.content).toBe('\nexport default {}\n')
expect(padFalse.styles[0].content).toBe('\nh1 { color: red }\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 const padTrue = parse(content.trim(), { pad: true }).descriptor
expect(padTrue.script!.content).toBe( expect(padTrue.script!.content).toBe(
@ -58,6 +77,9 @@ h1 { color: red }
expect(padTrue.styles[0].content).toBe( expect(padTrue.styles[0].content).toBe(
Array(6 + 1).join('\n') + '\nh1 { color: red }\n' 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 const padLine = parse(content.trim(), { pad: 'line' }).descriptor
expect(padLine.script!.content).toBe( expect(padLine.script!.content).toBe(
@ -66,6 +88,9 @@ h1 { color: red }
expect(padLine.styles[0].content).toBe( expect(padLine.styles[0].content).toBe(
Array(6 + 1).join('\n') + '\nh1 { color: red }\n' 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 const padSpace = parse(content.trim(), { pad: 'space' }).descriptor
expect(padSpace.script!.content).toBe( expect(padSpace.script!.content).toBe(
@ -78,6 +103,12 @@ h1 { color: red }
' ' ' '
) + '\nh1 { color: red }\n' ) + '\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', () => { test('should ignore nodes with no content', () => {

View File

@ -204,6 +204,7 @@ export function parse(
genMap(descriptor.template) genMap(descriptor.template)
genMap(descriptor.script) genMap(descriptor.script)
descriptor.styles.forEach(genMap) descriptor.styles.forEach(genMap)
descriptor.customBlocks.forEach(genMap)
} }
const result = { const result = {