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

55 lines
1.4 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
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!,
processChildrenAsStatement(rootBranch.children, 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 = processChildrenAsStatement(
branch.children,
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`, ['`<!---->`'])
])
}
}