vue3-core/packages/compiler-ssr/src/transforms/ssrVIf.ts

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-02-04 04:51:41 +08:00
import {
createStructuralDirectiveTransform,
2020-02-06 10:04:40 +08:00
processIf,
2020-02-04 04:51:41 +08:00
IfNode,
createIfStatement,
createBlockStatement,
createCallExpression,
IfBranchNode,
BlockStatement,
NodeTypes
2020-02-04 04:51:41 +08:00
} from '@vue/compiler-dom'
import {
SSRTransformContext,
2020-02-07 06:45:34 +08:00
processChildrenAsStatement
2020-02-04 04:51:41 +08:00
} from '../ssrCodegenTransform'
2020-02-04 06:47:06 +08:00
// Plugin for the first transform pass, which simply constructs the AST node
2020-02-04 04:51:41 +08:00
export const ssrTransformIf = createStructuralDirectiveTransform(
/^(if|else|else-if)$/,
2020-02-06 10:04:40 +08:00
processIf
2020-02-04 04:51:41 +08:00
)
// This is called during the 2nd transform pass to construct the SSR-sepcific
// codegen nodes.
2020-02-06 10:04:40 +08:00
export function ssrProcessIf(node: IfNode, context: SSRTransformContext) {
2020-02-04 04:51:41 +08:00
const [rootBranch] = node.branches
const ifStatement = createIfStatement(
rootBranch.condition!,
processIfBranch(rootBranch, context)
2020-02-04 04:51:41 +08:00
)
context.pushStatement(ifStatement)
let currentIf = ifStatement
for (let i = 1; i < node.branches.length; i++) {
const branch = node.branches[i]
const branchBlockStatement = processIfBranch(branch, context)
2020-02-04 04:51:41 +08:00
if (branch.condition) {
// else-if
currentIf = currentIf.alternate = createIfStatement(
branch.condition,
branchBlockStatement
)
} else {
// else
currentIf.alternate = branchBlockStatement
}
}
if (!currentIf.alternate) {
currentIf.alternate = createBlockStatement([
createCallExpression(`_push`, ['`<!---->`'])
])
}
}
function processIfBranch(
branch: IfBranchNode,
context: SSRTransformContext
): BlockStatement {
const { children } = branch
const needFragmentWrapper =
(children.length !== 1 || children[0].type !== NodeTypes.ELEMENT) &&
// optimize away nested fragments when the only child is a ForNode
!(children.length === 1 && children[0].type === NodeTypes.FOR)
return processChildrenAsStatement(children, context, needFragmentWrapper)
}