Skip to content

Commit 8dbecfc

Browse files
committed
feat(compiler-sfc): add ignoreEmpty option for sfc parse method
1 parent ec6abe8 commit 8dbecfc

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,28 @@ h1 { color: red }
167167
expect(descriptor.script!.attrs['src']).toBe('com')
168168
})
169169

170+
test('ignoreEmpty: false', () => {
171+
const { descriptor } = parse(
172+
`<script></script>\n<script setup>\n</script>`,
173+
{
174+
ignoreEmpty: false
175+
}
176+
)
177+
expect(descriptor.script).toBeTruthy()
178+
expect(descriptor.script!.loc).toMatchObject({
179+
source: '',
180+
start: { line: 1, column: 9, offset: 8 },
181+
end: { line: 1, column: 9, offset: 8 }
182+
})
183+
184+
expect(descriptor.scriptSetup).toBeTruthy()
185+
expect(descriptor.scriptSetup!.loc).toMatchObject({
186+
source: '\n',
187+
start: { line: 2, column: 15, offset: 32 },
188+
end: { line: 3, column: 1, offset: 33 }
189+
})
190+
})
191+
170192
test('nested templates', () => {
171193
const content = `
172194
<template v-if="ok">ok</template>

packages/compiler-sfc/src/parse.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface SFCParseOptions {
1818
sourceMap?: boolean
1919
sourceRoot?: string
2020
pad?: boolean | 'line' | 'space'
21+
ignoreEmpty?: boolean
2122
compiler?: TemplateCompiler
2223
}
2324

@@ -104,6 +105,7 @@ export function parse(
104105
filename = 'anonymous.vue',
105106
sourceRoot = '',
106107
pad = false,
108+
ignoreEmpty = true,
107109
compiler = CompilerDOM
108110
}: SFCParseOptions = {}
109111
): SFCParseResult {
@@ -163,7 +165,12 @@ export function parse(
163165
return
164166
}
165167
// we only want to keep the nodes that are not empty (when the tag is not a template)
166-
if (node.tag !== 'template' && isEmpty(node) && !hasSrc(node)) {
168+
if (
169+
ignoreEmpty &&
170+
node.tag !== 'template' &&
171+
isEmpty(node) &&
172+
!hasSrc(node)
173+
) {
167174
return
168175
}
169176
switch (node.tag) {

0 commit comments

Comments
 (0)