fix(compiler-core): avoid generating useless createVNode helper (#2938)
close #2739
This commit is contained in:
@@ -14,7 +14,7 @@ import {
|
||||
import { TransformContext } from '../transform'
|
||||
import { PatchFlags, isString, isSymbol } from '@vue/shared'
|
||||
import { isSlotOutlet } from '../utils'
|
||||
import { CREATE_VNODE } from '../runtimeHelpers'
|
||||
import { CREATE_BLOCK, CREATE_VNODE, OPEN_BLOCK } from '../runtimeHelpers'
|
||||
|
||||
export function hoistStatic(root: RootNode, context: TransformContext) {
|
||||
walk(
|
||||
@@ -212,6 +212,8 @@ export function getConstantType(
|
||||
// static then they don't need to be blocks since there will be no
|
||||
// nested updates.
|
||||
if (codegenNode.isBlock) {
|
||||
context.removeHelper(OPEN_BLOCK)
|
||||
context.removeHelper(CREATE_BLOCK)
|
||||
codegenNode.isBlock = false
|
||||
context.helper(CREATE_VNODE)
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ import { PatchFlags, PatchFlagNames } from '@vue/shared'
|
||||
export const transformFor = createStructuralDirectiveTransform(
|
||||
'for',
|
||||
(node, dir, context) => {
|
||||
const { helper } = context
|
||||
const { helper, removeHelper } = context
|
||||
return processFor(node, dir, context, forNode => {
|
||||
// create the loop render function expression now, and add the
|
||||
// iterator on exit after all children have been traversed
|
||||
@@ -166,6 +166,16 @@ export const transformFor = createStructuralDirectiveTransform(
|
||||
if (isTemplate && keyProperty) {
|
||||
injectProp(childBlock, keyProperty, context)
|
||||
}
|
||||
if (childBlock.isBlock !== !isStableFragment) {
|
||||
if (childBlock.isBlock) {
|
||||
// switch from block to vnode
|
||||
removeHelper(OPEN_BLOCK)
|
||||
removeHelper(CREATE_BLOCK)
|
||||
} else {
|
||||
// switch from vnode to block
|
||||
removeHelper(CREATE_VNODE)
|
||||
}
|
||||
}
|
||||
childBlock.isBlock = !isStableFragment
|
||||
if (childBlock.isBlock) {
|
||||
helper(OPEN_BLOCK)
|
||||
|
||||
@@ -31,7 +31,8 @@ import {
|
||||
CREATE_BLOCK,
|
||||
FRAGMENT,
|
||||
CREATE_COMMENT,
|
||||
OPEN_BLOCK
|
||||
OPEN_BLOCK,
|
||||
CREATE_VNODE
|
||||
} from '../runtimeHelpers'
|
||||
import { injectProp, findDir, findProp } from '../utils'
|
||||
import { PatchFlags, PatchFlagNames } from '@vue/shared'
|
||||
@@ -225,7 +226,7 @@ function createChildrenCodegenNode(
|
||||
keyIndex: number,
|
||||
context: TransformContext
|
||||
): BlockCodegenNode {
|
||||
const { helper } = context
|
||||
const { helper, removeHelper } = context
|
||||
const keyProperty = createObjectProperty(
|
||||
`key`,
|
||||
createSimpleExpression(
|
||||
@@ -275,7 +276,8 @@ function createChildrenCodegenNode(
|
||||
const vnodeCall = (firstChild as ElementNode)
|
||||
.codegenNode as BlockCodegenNode
|
||||
// Change createVNode to createBlock.
|
||||
if (vnodeCall.type === NodeTypes.VNODE_CALL) {
|
||||
if (vnodeCall.type === NodeTypes.VNODE_CALL && !vnodeCall.isBlock) {
|
||||
removeHelper(CREATE_VNODE)
|
||||
vnodeCall.isBlock = true
|
||||
helper(OPEN_BLOCK)
|
||||
helper(CREATE_BLOCK)
|
||||
|
||||
Reference in New Issue
Block a user