Skip to content

Commit a3601e9

Browse files
authored
perf(transform-vif): don't need to createBlock for a component (#853)
1 parent 342d46d commit a3601e9

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

packages/compiler-core/__tests__/transforms/vIf.spec.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
SimpleExpressionNode,
1313
ConditionalExpression,
1414
IfConditionalExpression,
15-
VNodeCall
15+
VNodeCall,
16+
ElementTypes
1617
} from '../../src/ast'
1718
import { ErrorCodes } from '../../src/errors'
1819
import { CompilerOptions, generate } from '../../src'
@@ -77,6 +78,22 @@ describe('compiler: v-if', () => {
7778
expect(node.branches[0].children[2].type).toBe(NodeTypes.ELEMENT)
7879
expect((node.branches[0].children[2] as ElementNode).tag).toBe(`p`)
7980
})
81+
82+
test('component v-if', () => {
83+
const { node } = parseWithIfTransform(`<Component v-if="ok"></Component>`)
84+
expect(node.type).toBe(NodeTypes.IF)
85+
expect(node.branches.length).toBe(1)
86+
expect((node.branches[0].children[0] as ElementNode).tag).toBe(
87+
`Component`
88+
)
89+
expect((node.branches[0].children[0] as ElementNode).tagType).toBe(
90+
ElementTypes.COMPONENT
91+
)
92+
expect(
93+
((node.branches[0].children[0] as ElementNode)!
94+
.codegenNode as VNodeCall)!.isBlock
95+
).toBe(false)
96+
})
8097

8198
test('v-if + v-else', () => {
8299
const { node } = parseWithIfTransform(`<div v-if="ok"/><p v-else/>`)

packages/compiler-core/src/transforms/vIf.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,12 @@ function createChildrenCodegenNode(
212212
const vnodeCall = (firstChild as ElementNode)
213213
.codegenNode as BlockCodegenNode
214214
// Change createVNode to createBlock.
215-
if (vnodeCall.type === NodeTypes.VNODE_CALL) {
215+
if (
216+
vnodeCall.type === NodeTypes.VNODE_CALL &&
217+
// component vnodes are always tracked and its children are
218+
// compiled into slots so no need to make it a block
219+
(firstChild as ElementNode).tagType !== ElementTypes.COMPONENT
220+
) {
216221
vnodeCall.isBlock = true
217222
helper(OPEN_BLOCK)
218223
helper(CREATE_BLOCK)

0 commit comments

Comments
 (0)