2019-10-04 11:30:25 +08:00
|
|
|
import {
|
|
|
|
RootNode,
|
|
|
|
NodeTypes,
|
|
|
|
TemplateChildNode,
|
2019-10-06 10:47:20 +08:00
|
|
|
ElementTypes,
|
|
|
|
ElementCodegenNode,
|
2019-10-08 05:12:22 +08:00
|
|
|
PlainElementNode,
|
|
|
|
ComponentNode,
|
2019-10-10 23:19:17 +08:00
|
|
|
TemplateNode,
|
|
|
|
ElementNode
|
2019-10-04 11:30:25 +08:00
|
|
|
} from '../ast'
|
|
|
|
import { TransformContext } from '../transform'
|
2019-10-06 05:18:25 +08:00
|
|
|
import { APPLY_DIRECTIVES } from '../runtimeHelpers'
|
2019-10-05 10:47:09 +08:00
|
|
|
import { PatchFlags } from '@vue/shared'
|
2019-10-10 23:19:17 +08:00
|
|
|
import { isSlotOutlet, findProp } from '../utils'
|
|
|
|
|
|
|
|
function hasDynamicKey(node: ElementNode) {
|
|
|
|
const keyProp = findProp(node, 'key')
|
|
|
|
return keyProp && keyProp.type === NodeTypes.DIRECTIVE
|
|
|
|
}
|
2019-10-04 11:30:25 +08:00
|
|
|
|
2019-10-04 21:03:00 +08:00
|
|
|
export function hoistStatic(root: RootNode, context: TransformContext) {
|
2019-10-08 05:12:22 +08:00
|
|
|
walk(
|
|
|
|
root.children,
|
|
|
|
context,
|
|
|
|
new Map(),
|
|
|
|
isSingleElementRoot(root, root.children[0])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isSingleElementRoot(
|
|
|
|
root: RootNode,
|
|
|
|
child: TemplateChildNode
|
|
|
|
): child is PlainElementNode | ComponentNode | TemplateNode {
|
|
|
|
const { children } = root
|
|
|
|
return (
|
|
|
|
children.length === 1 &&
|
|
|
|
child.type === NodeTypes.ELEMENT &&
|
|
|
|
!isSlotOutlet(child)
|
|
|
|
)
|
2019-10-04 11:30:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function walk(
|
|
|
|
children: TemplateChildNode[],
|
|
|
|
context: TransformContext,
|
2019-10-08 05:12:22 +08:00
|
|
|
resultCache: Map<TemplateChildNode, boolean>,
|
|
|
|
doNotHoistNode: boolean = false
|
2019-10-04 11:30:25 +08:00
|
|
|
) {
|
|
|
|
for (let i = 0; i < children.length; i++) {
|
|
|
|
const child = children[i]
|
2019-10-05 02:34:26 +08:00
|
|
|
// only plain elements are eligible for hoisting.
|
|
|
|
if (
|
|
|
|
child.type === NodeTypes.ELEMENT &&
|
|
|
|
child.tagType === ElementTypes.ELEMENT
|
|
|
|
) {
|
2019-10-10 23:19:17 +08:00
|
|
|
if (
|
|
|
|
!doNotHoistNode &&
|
|
|
|
isStaticNode(child, resultCache) &&
|
|
|
|
!hasDynamicKey(child)
|
|
|
|
) {
|
2019-10-04 11:30:25 +08:00
|
|
|
// whole tree is static
|
2019-10-08 22:50:00 +08:00
|
|
|
child.codegenNode = context.hoist(child.codegenNode!)
|
2019-10-04 11:30:25 +08:00
|
|
|
continue
|
2019-10-05 02:34:26 +08:00
|
|
|
} else {
|
|
|
|
// node may contain dynamic children, but its props may be eligible for
|
|
|
|
// hoisting.
|
|
|
|
const flag = getPatchFlag(child)
|
2019-10-05 10:51:51 +08:00
|
|
|
if (
|
2019-10-10 23:19:17 +08:00
|
|
|
(!flag ||
|
|
|
|
flag === PatchFlags.NEED_PATCH ||
|
|
|
|
flag === PatchFlags.TEXT) &&
|
|
|
|
!hasDynamicKey(child)
|
2019-10-05 10:51:51 +08:00
|
|
|
) {
|
2019-10-08 22:50:00 +08:00
|
|
|
let codegenNode = child.codegenNode as ElementCodegenNode
|
2019-10-06 05:18:25 +08:00
|
|
|
if (codegenNode.callee === APPLY_DIRECTIVES) {
|
2019-10-06 10:47:20 +08:00
|
|
|
codegenNode = codegenNode.arguments[0]
|
2019-10-05 02:34:26 +08:00
|
|
|
}
|
2019-10-06 10:47:20 +08:00
|
|
|
const props = codegenNode.arguments[1]
|
2019-10-05 02:34:26 +08:00
|
|
|
if (props && props !== `null`) {
|
2019-10-06 10:47:20 +08:00
|
|
|
codegenNode.arguments[1] = context.hoist(props)
|
2019-10-05 02:34:26 +08:00
|
|
|
}
|
2019-10-04 11:30:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-08 05:12:22 +08:00
|
|
|
if (child.type === NodeTypes.ELEMENT) {
|
2019-10-05 02:34:26 +08:00
|
|
|
walk(child.children, context, resultCache)
|
2019-10-08 05:12:22 +08:00
|
|
|
} else if (child.type === NodeTypes.FOR) {
|
|
|
|
// Do not hoist v-for single child because it has to be a block
|
|
|
|
walk(child.children, context, resultCache, child.children.length === 1)
|
2019-10-04 11:30:25 +08:00
|
|
|
} else if (child.type === NodeTypes.IF) {
|
|
|
|
for (let i = 0; i < child.branches.length; i++) {
|
2019-10-08 05:12:22 +08:00
|
|
|
const branchChildren = child.branches[i].children
|
|
|
|
// Do not hoist v-if single child because it has to be a block
|
|
|
|
walk(branchChildren, context, resultCache, branchChildren.length === 1)
|
2019-10-04 11:30:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 22:50:00 +08:00
|
|
|
function getPatchFlag(node: PlainElementNode): number | undefined {
|
|
|
|
let codegenNode = node.codegenNode as ElementCodegenNode
|
2019-10-06 05:18:25 +08:00
|
|
|
if (codegenNode.callee === APPLY_DIRECTIVES) {
|
2019-10-06 10:47:20 +08:00
|
|
|
codegenNode = codegenNode.arguments[0]
|
2019-10-04 11:30:25 +08:00
|
|
|
}
|
2019-10-05 02:34:26 +08:00
|
|
|
const flag = codegenNode.arguments[3]
|
|
|
|
return flag ? parseInt(flag as string, 10) : undefined
|
2019-10-04 11:30:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function isStaticNode(
|
|
|
|
node: TemplateChildNode,
|
2019-10-05 02:34:26 +08:00
|
|
|
resultCache: Map<TemplateChildNode, boolean>
|
2019-10-04 11:30:25 +08:00
|
|
|
): boolean {
|
|
|
|
switch (node.type) {
|
|
|
|
case NodeTypes.ELEMENT:
|
2019-10-05 02:34:26 +08:00
|
|
|
if (node.tagType !== ElementTypes.ELEMENT) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (resultCache.has(node)) {
|
|
|
|
return resultCache.get(node) as boolean
|
2019-10-04 11:30:25 +08:00
|
|
|
}
|
|
|
|
const flag = getPatchFlag(node)
|
|
|
|
if (!flag) {
|
|
|
|
// element self is static. check its children.
|
|
|
|
for (let i = 0; i < node.children.length; i++) {
|
2019-10-05 02:34:26 +08:00
|
|
|
if (!isStaticNode(node.children[i], resultCache)) {
|
|
|
|
resultCache.set(node, false)
|
2019-10-04 11:30:25 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-10-05 02:34:26 +08:00
|
|
|
resultCache.set(node, true)
|
2019-10-04 11:30:25 +08:00
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case NodeTypes.TEXT:
|
|
|
|
case NodeTypes.COMMENT:
|
|
|
|
return true
|
|
|
|
case NodeTypes.IF:
|
|
|
|
case NodeTypes.FOR:
|
|
|
|
case NodeTypes.INTERPOLATION:
|
|
|
|
case NodeTypes.COMPOUND_EXPRESSION:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
if (__DEV__) {
|
|
|
|
const exhaustiveCheck: never = node
|
|
|
|
exhaustiveCheck
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|