feat(compiler-sfc): add ignoreEmpty option for sfc parse method
This commit is contained in:
parent
ec6abe8d5e
commit
8dbecfcbb3
@ -167,6 +167,28 @@ h1 { color: red }
|
|||||||
expect(descriptor.script!.attrs['src']).toBe('com')
|
expect(descriptor.script!.attrs['src']).toBe('com')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('ignoreEmpty: false', () => {
|
||||||
|
const { descriptor } = parse(
|
||||||
|
`<script></script>\n<script setup>\n</script>`,
|
||||||
|
{
|
||||||
|
ignoreEmpty: false
|
||||||
|
}
|
||||||
|
)
|
||||||
|
expect(descriptor.script).toBeTruthy()
|
||||||
|
expect(descriptor.script!.loc).toMatchObject({
|
||||||
|
source: '',
|
||||||
|
start: { line: 1, column: 9, offset: 8 },
|
||||||
|
end: { line: 1, column: 9, offset: 8 }
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(descriptor.scriptSetup).toBeTruthy()
|
||||||
|
expect(descriptor.scriptSetup!.loc).toMatchObject({
|
||||||
|
source: '\n',
|
||||||
|
start: { line: 2, column: 15, offset: 32 },
|
||||||
|
end: { line: 3, column: 1, offset: 33 }
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
test('nested templates', () => {
|
test('nested templates', () => {
|
||||||
const content = `
|
const content = `
|
||||||
<template v-if="ok">ok</template>
|
<template v-if="ok">ok</template>
|
||||||
|
@ -18,6 +18,7 @@ export interface SFCParseOptions {
|
|||||||
sourceMap?: boolean
|
sourceMap?: boolean
|
||||||
sourceRoot?: string
|
sourceRoot?: string
|
||||||
pad?: boolean | 'line' | 'space'
|
pad?: boolean | 'line' | 'space'
|
||||||
|
ignoreEmpty?: boolean
|
||||||
compiler?: TemplateCompiler
|
compiler?: TemplateCompiler
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,6 +105,7 @@ export function parse(
|
|||||||
filename = 'anonymous.vue',
|
filename = 'anonymous.vue',
|
||||||
sourceRoot = '',
|
sourceRoot = '',
|
||||||
pad = false,
|
pad = false,
|
||||||
|
ignoreEmpty = true,
|
||||||
compiler = CompilerDOM
|
compiler = CompilerDOM
|
||||||
}: SFCParseOptions = {}
|
}: SFCParseOptions = {}
|
||||||
): SFCParseResult {
|
): SFCParseResult {
|
||||||
@ -163,7 +165,12 @@ export function parse(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// we only want to keep the nodes that are not empty (when the tag is not a template)
|
// we only want to keep the nodes that are not empty (when the tag is not a template)
|
||||||
if (node.tag !== 'template' && isEmpty(node) && !hasSrc(node)) {
|
if (
|
||||||
|
ignoreEmpty &&
|
||||||
|
node.tag !== 'template' &&
|
||||||
|
isEmpty(node) &&
|
||||||
|
!hasSrc(node)
|
||||||
|
) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch (node.tag) {
|
switch (node.tag) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user