test(compiler-ssr): v-for tests

This commit is contained in:
Evan You
2020-02-03 20:47:41 -05:00
parent 93c37b94f2
commit 8cf6b5731d
5 changed files with 172 additions and 12 deletions

View File

@@ -5,7 +5,8 @@ import {
createCallExpression,
createFunctionExpression,
createForLoopParams,
createBlockStatement
createBlockStatement,
NodeTypes
} from '@vue/compiler-dom'
import {
SSRTransformContext,
@@ -23,16 +24,28 @@ export const ssrTransformFor = createStructuralDirectiveTransform(
// This is called during the 2nd transform pass to construct the SSR-sepcific
// codegen nodes.
export function processFor(node: ForNode, context: SSRTransformContext) {
const childContext = createChildContext(context)
const needFragmentWrapper =
node.children.length !== 1 || node.children[0].type !== NodeTypes.ELEMENT
if (needFragmentWrapper) {
childContext.pushStringPart(`<!---->`)
}
processChildren(node.children, childContext)
if (needFragmentWrapper) {
childContext.pushStringPart(`<!---->`)
}
const renderLoop = createFunctionExpression(
createForLoopParams(node.parseResult)
)
const childContext = createChildContext(context)
processChildren(node.children, childContext)
renderLoop.body = createBlockStatement(childContext.body)
// v-for always renders a fragment
context.pushStringPart(`<!---->`)
context.pushStatement(
createCallExpression(context.helper(SSR_RENDER_LIST), [
node.source,
renderLoop
])
)
context.pushStringPart(`<!---->`)
}

View File

@@ -59,15 +59,15 @@ function processIfBranch(
context: SSRTransformContext
): BlockStatement {
const { children } = branch
const firstChild = children[0]
// TODO optimize away nested fragments when the only child is a ForNode
const needFragmentWrapper =
children.length !== 1 || firstChild.type !== NodeTypes.ELEMENT
(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)
const childContext = createChildContext(context)
if (needFragmentWrapper) {
childContext.pushStringPart(`<!---->`)
}
processChildren(branch.children, childContext)
processChildren(children, childContext)
if (needFragmentWrapper) {
childContext.pushStringPart(`<!---->`)
}