Skip to content

Commit 619efd9

Browse files
authored
fix(compiler-sfc): custom blocks sourcemap (#1812)
1 parent 6f8bac5 commit 619efd9

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

packages/compiler-sfc/__tests__/parse.spec.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ describe('compiler:sfc', () => {
3333
expect(mapping.originalLine - mapping.generatedLine).toBe(padding)
3434
})
3535
})
36+
37+
test('custom block', () => {
38+
const padding = Math.round(Math.random() * 10)
39+
const custom = parse(
40+
`${'\n'.repeat(padding)}<i18n>\n{\n "greeting": "hello"\n}\n</i18n>\n`
41+
).descriptor.customBlocks[0]
42+
43+
expect(custom!.map).not.toBeUndefined()
44+
45+
const consumer = new SourceMapConsumer(custom!.map!)
46+
consumer.eachMapping(mapping => {
47+
expect(mapping.originalLine - mapping.generatedLine).toBe(padding)
48+
})
49+
})
3650
})
3751

3852
test('pad content', () => {
@@ -45,11 +59,16 @@ export default {}
4559
</script>
4660
<style>
4761
h1 { color: red }
48-
</style>`
62+
</style>
63+
<i18n>
64+
{ "greeting": "hello" }
65+
</i18n>
66+
`
4967
const padFalse = parse(content.trim(), { pad: false }).descriptor
5068
expect(padFalse.template!.content).toBe('\n<div></div>\n')
5169
expect(padFalse.script!.content).toBe('\nexport default {}\n')
5270
expect(padFalse.styles[0].content).toBe('\nh1 { color: red }\n')
71+
expect(padFalse.customBlocks[0].content).toBe('\n{ "greeting": "hello" }\n')
5372

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

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

7095
const padSpace = parse(content.trim(), { pad: 'space' }).descriptor
7196
expect(padSpace.script!.content).toBe(
@@ -78,6 +103,12 @@ h1 { color: red }
78103
' '
79104
) + '\nh1 { color: red }\n'
80105
)
106+
expect(padSpace.customBlocks[0].content).toBe(
107+
`<template>\n<div></div>\n</template>\n<script>\nexport default {}\n</script>\n<style>\nh1 { color: red }\n</style>\n<i18n>`.replace(
108+
/./g,
109+
' '
110+
) + '\n{ "greeting": "hello" }\n'
111+
)
81112
})
82113

83114
test('should ignore nodes with no content', () => {

packages/compiler-sfc/src/parse.ts

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ export function parse(
204204
genMap(descriptor.template)
205205
genMap(descriptor.script)
206206
descriptor.styles.forEach(genMap)
207+
descriptor.customBlocks.forEach(genMap)
207208
}
208209

209210
const result = {

0 commit comments

Comments
 (0)