Skip to content

Commit b771fdb

Browse files
authored
perf(compiler-sfc): ignore empty blocks (#3520)
1 parent 65f82ce commit b771fdb

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,13 @@ h1 { color: red }
135135

136136
test('should ignore other nodes with no content', () => {
137137
expect(parse(`<script/>`).descriptor.script).toBe(null)
138+
expect(parse(`<script> \n\t </script>`).descriptor.script).toBe(null)
138139
expect(parse(`<style/>`).descriptor.styles.length).toBe(0)
140+
expect(parse(`<style> \n\t </style>`).descriptor.styles.length).toBe(0)
139141
expect(parse(`<custom/>`).descriptor.customBlocks.length).toBe(0)
142+
expect(
143+
parse(`<custom> \n\t </custom>`).descriptor.customBlocks.length
144+
).toBe(0)
140145
})
141146

142147
test('handle empty nodes with src attribute', () => {

packages/compiler-sfc/src/parse.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ export function parse(
162162
if (node.type !== NodeTypes.ELEMENT) {
163163
return
164164
}
165-
if (!node.children.length && !hasSrc(node) && node.tag !== 'template') {
165+
// 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)) {
166167
return
167168
}
168169
switch (node.tag) {
@@ -415,3 +416,15 @@ function hasSrc(node: ElementNode) {
415416
return p.name === 'src'
416417
})
417418
}
419+
420+
/**
421+
* Returns true if the node has no children
422+
* once the empty text nodes (trimmed content) have been filtered out.
423+
*/
424+
function isEmpty(node: ElementNode) {
425+
return (
426+
node.children.filter(
427+
child => child.type !== NodeTypes.TEXT || child.content.trim() !== ''
428+
).length === 0
429+
)
430+
}

0 commit comments

Comments
 (0)