vue3-yuanma/packages/compiler-core/src/transforms/hoistStatic.ts
Evan You 9c9ef609d8 fix: import patchFlags from @vue/shared in compiler instead
Otherwise this imports unnecessary stuff from the runtime.
2019-10-04 22:47:09 -04:00

116 lines
3.4 KiB
TypeScript

import {
RootNode,
NodeTypes,
TemplateChildNode,
CallExpression,
ElementNode,
ElementTypes
} from '../ast'
import { TransformContext } from '../transform'
import { APPLY_DIRECTIVES } from '../runtimeConstants'
import { PropsExpression } from './transformElement'
import { PatchFlags } from '@vue/shared'
export function hoistStatic(root: RootNode, context: TransformContext) {
walk(root.children, context, new Map<TemplateChildNode, boolean>())
}
function walk(
children: TemplateChildNode[],
context: TransformContext,
resultCache: Map<TemplateChildNode, boolean>
) {
for (let i = 0; i < children.length; i++) {
const child = children[i]
// only plain elements are eligible for hoisting.
if (
child.type === NodeTypes.ELEMENT &&
child.tagType === ElementTypes.ELEMENT
) {
if (isStaticNode(child, resultCache)) {
// whole tree is static
child.codegenNode = context.hoist(child.codegenNode!)
continue
} else {
// node may contain dynamic children, but its props may be eligible for
// hoisting.
const flag = getPatchFlag(child)
if (!flag || flag === PatchFlags.NEED_PATCH) {
let codegenNode = child.codegenNode as CallExpression
if (codegenNode.callee.includes(APPLY_DIRECTIVES)) {
codegenNode = codegenNode.arguments[0] as CallExpression
}
const props = codegenNode.arguments[1] as
| PropsExpression
| `null`
| undefined
if (props && props !== `null`) {
;(child.codegenNode as CallExpression).arguments[1] = context.hoist(
props
)
}
}
}
}
if (child.type === NodeTypes.ELEMENT || child.type === NodeTypes.FOR) {
walk(child.children, context, resultCache)
} else if (child.type === NodeTypes.IF) {
for (let i = 0; i < child.branches.length; i++) {
walk(child.branches[i].children, context, resultCache)
}
}
}
}
function getPatchFlag(node: ElementNode): number | undefined {
let codegenNode = node.codegenNode as CallExpression
if (codegenNode.callee.includes(APPLY_DIRECTIVES)) {
codegenNode = codegenNode.arguments[0] as CallExpression
}
const flag = codegenNode.arguments[3]
return flag ? parseInt(flag as string, 10) : undefined
}
function isStaticNode(
node: TemplateChildNode,
resultCache: Map<TemplateChildNode, boolean>
): boolean {
switch (node.type) {
case NodeTypes.ELEMENT:
if (node.tagType !== ElementTypes.ELEMENT) {
return false
}
if (resultCache.has(node)) {
return resultCache.get(node) as boolean
}
const flag = getPatchFlag(node)
if (!flag) {
// element self is static. check its children.
for (let i = 0; i < node.children.length; i++) {
if (!isStaticNode(node.children[i], resultCache)) {
resultCache.set(node, false)
return false
}
}
resultCache.set(node, true)
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
}
}