feat(compiler-core): more hoisting optimizations (#276)

This commit is contained in:
HcySunYang
2019-10-15 23:41:24 +08:00
committed by Evan You
parent 9a37c4b2c3
commit 68a3879b88
11 changed files with 373 additions and 15 deletions

View File

@@ -2,6 +2,7 @@ import {
RootNode,
NodeTypes,
TemplateChildNode,
SimpleExpressionNode,
ElementTypes,
ElementCodegenNode,
PlainElementNode,
@@ -11,7 +12,7 @@ import {
} from '../ast'
import { TransformContext } from '../transform'
import { APPLY_DIRECTIVES } from '../runtimeHelpers'
import { PatchFlags } from '@vue/shared'
import { PatchFlags, isString, isSymbol } from '@vue/shared'
import { isSlotOutlet, findProp } from '../utils'
function hasDynamicKey(node: ElementNode) {
@@ -107,7 +108,7 @@ function getPatchFlag(node: PlainElementNode): number | undefined {
}
function isStaticNode(
node: TemplateChildNode,
node: TemplateChildNode | SimpleExpressionNode,
resultCache: Map<TemplateChildNode, boolean>
): boolean {
switch (node.type) {
@@ -119,7 +120,7 @@ function isStaticNode(
return resultCache.get(node) as boolean
}
const flag = getPatchFlag(node)
if (!flag) {
if (!flag || flag === PatchFlags.TEXT) {
// element self is static. check its children.
for (let i = 0; i < node.children.length; i++) {
if (!isStaticNode(node.children[i], resultCache)) {
@@ -137,9 +138,17 @@ function isStaticNode(
return true
case NodeTypes.IF:
case NodeTypes.FOR:
case NodeTypes.INTERPOLATION:
case NodeTypes.COMPOUND_EXPRESSION:
return false
case NodeTypes.INTERPOLATION:
return isStaticNode(node.content, resultCache)
case NodeTypes.SIMPLE_EXPRESSION:
return node.isConstant
case NodeTypes.COMPOUND_EXPRESSION:
return node.children.every(child => {
return (
isString(child) || isSymbol(child) || isStaticNode(child, resultCache)
)
})
default:
if (__DEV__) {
const exhaustiveCheck: never = node