fix(compiler): do not hoist element with dynamic key (#187)

This commit is contained in:
Illya Klymov
2019-10-10 18:19:17 +03:00
committed by Evan You
parent f11dadc1d2
commit 80f5cb2700
3 changed files with 106 additions and 18 deletions

View File

@@ -6,12 +6,18 @@ import {
ElementCodegenNode,
PlainElementNode,
ComponentNode,
TemplateNode
TemplateNode,
ElementNode
} from '../ast'
import { TransformContext } from '../transform'
import { APPLY_DIRECTIVES } from '../runtimeHelpers'
import { PatchFlags } from '@vue/shared'
import { isSlotOutlet } from '../utils'
import { isSlotOutlet, findProp } from '../utils'
function hasDynamicKey(node: ElementNode) {
const keyProp = findProp(node, 'key')
return keyProp && keyProp.type === NodeTypes.DIRECTIVE
}
export function hoistStatic(root: RootNode, context: TransformContext) {
walk(
@@ -47,7 +53,11 @@ function walk(
child.type === NodeTypes.ELEMENT &&
child.tagType === ElementTypes.ELEMENT
) {
if (!doNotHoistNode && isStaticNode(child, resultCache)) {
if (
!doNotHoistNode &&
isStaticNode(child, resultCache) &&
!hasDynamicKey(child)
) {
// whole tree is static
child.codegenNode = context.hoist(child.codegenNode!)
continue
@@ -56,9 +66,10 @@ function walk(
// hoisting.
const flag = getPatchFlag(child)
if (
!flag ||
flag === PatchFlags.NEED_PATCH ||
flag === PatchFlags.TEXT
(!flag ||
flag === PatchFlags.NEED_PATCH ||
flag === PatchFlags.TEXT) &&
!hasDynamicKey(child)
) {
let codegenNode = child.codegenNode as ElementCodegenNode
if (codegenNode.callee === APPLY_DIRECTIVES) {