wip(ssr): v-for

This commit is contained in:
Evan You 2020-02-03 18:30:56 -05:00
parent 2ad0eed5cd
commit 93c37b94f2
5 changed files with 39 additions and 14 deletions

View File

@ -229,6 +229,7 @@ export interface ForNode extends Node {
valueAlias: ExpressionNode | undefined valueAlias: ExpressionNode | undefined
keyAlias: ExpressionNode | undefined keyAlias: ExpressionNode | undefined
objectIndexAlias: ExpressionNode | undefined objectIndexAlias: ExpressionNode | undefined
parseResult: ForParseResult
children: TemplateChildNode[] children: TemplateChildNode[]
codegenNode?: ForCodegenNode codegenNode?: ForCodegenNode
} }
@ -619,7 +620,7 @@ export function createCallExpression<T extends CallExpression['callee']>(
export function createFunctionExpression( export function createFunctionExpression(
params: FunctionExpression['params'], params: FunctionExpression['params'],
returns: FunctionExpression['returns'], returns: FunctionExpression['returns'] = undefined,
newline: boolean = false, newline: boolean = false,
isSlot: boolean = false, isSlot: boolean = false,
loc: SourceLocation = locStub loc: SourceLocation = locStub

View File

@ -439,8 +439,12 @@ function genNodeList(
genNode(node, context) genNode(node, context)
} }
if (i < nodes.length - 1) { if (i < nodes.length - 1) {
if (multilines) {
comma && push(',') comma && push(',')
multilines && newline() newline()
} else {
comma && push(', ')
}
} }
} }
} }

View File

@ -33,7 +33,7 @@ export { transformOn } from './transforms/vOn'
// exported for compiler-ssr // exported for compiler-ssr
export { processIfBranches } from './transforms/vIf' export { processIfBranches } from './transforms/vIf'
export { processForNode } from './transforms/vFor' export { processForNode, createForLoopParams } from './transforms/vFor'
export { export {
transformExpression, transformExpression,
processExpression processExpression

View File

@ -45,7 +45,7 @@ export const transformFor = createStructuralDirectiveTransform(
'for', 'for',
(node, dir, context) => { (node, dir, context) => {
const { helper } = context const { helper } = context
return processForNode(node, dir, context, (forNode, parseResult) => { return processForNode(node, dir, context, forNode => {
// create the loop render function expression now, and add the // create the loop render function expression now, and add the
// iterator on exit after all children have been traversed // iterator on exit after all children have been traversed
const renderExp = createCallExpression(helper(RENDER_LIST), [ const renderExp = createCallExpression(helper(RENDER_LIST), [
@ -122,7 +122,7 @@ export const transformFor = createStructuralDirectiveTransform(
renderExp.arguments.push( renderExp.arguments.push(
createFunctionExpression( createFunctionExpression(
createForLoopParams(parseResult), createForLoopParams(forNode.parseResult),
childBlock, childBlock,
true /* force newline */ true /* force newline */
) )
@ -137,10 +137,7 @@ export function processForNode(
node: ElementNode, node: ElementNode,
dir: DirectiveNode, dir: DirectiveNode,
context: TransformContext, context: TransformContext,
processCodegen?: ( processCodegen?: (forNode: ForNode) => (() => void) | undefined
forNode: ForNode,
parseResult: ForParseResult
) => (() => void) | undefined
) { ) {
if (!dir.exp) { if (!dir.exp) {
context.onError( context.onError(
@ -173,6 +170,7 @@ export function processForNode(
valueAlias: value, valueAlias: value,
keyAlias: key, keyAlias: key,
objectIndexAlias: index, objectIndexAlias: index,
parseResult,
children: node.tagType === ElementTypes.TEMPLATE ? node.children : [node] children: node.tagType === ElementTypes.TEMPLATE ? node.children : [node]
} }
@ -188,7 +186,7 @@ export function processForNode(
index && addIdentifiers(index) index && addIdentifiers(index)
} }
const onExit = processCodegen && processCodegen(forNode, parseResult) const onExit = processCodegen && processCodegen(forNode)
return () => { return () => {
scopes.vFor-- scopes.vFor--

View File

@ -1,9 +1,18 @@
import { import {
createStructuralDirectiveTransform, createStructuralDirectiveTransform,
ForNode, ForNode,
processForNode processForNode,
createCallExpression,
createFunctionExpression,
createForLoopParams,
createBlockStatement
} from '@vue/compiler-dom' } from '@vue/compiler-dom'
import { SSRTransformContext } from '../ssrCodegenTransform' import {
SSRTransformContext,
createChildContext,
processChildren
} from '../ssrCodegenTransform'
import { SSR_RENDER_LIST } from '../runtimeHelpers'
// Plugin for the first transform pass, which simply constructs the AST node // Plugin for the first transform pass, which simply constructs the AST node
export const ssrTransformFor = createStructuralDirectiveTransform( export const ssrTransformFor = createStructuralDirectiveTransform(
@ -13,4 +22,17 @@ export const ssrTransformFor = createStructuralDirectiveTransform(
// This is called during the 2nd transform pass to construct the SSR-sepcific // This is called during the 2nd transform pass to construct the SSR-sepcific
// codegen nodes. // codegen nodes.
export function processFor(node: ForNode, context: SSRTransformContext) {} export function processFor(node: ForNode, context: SSRTransformContext) {
const renderLoop = createFunctionExpression(
createForLoopParams(node.parseResult)
)
const childContext = createChildContext(context)
processChildren(node.children, childContext)
renderLoop.body = createBlockStatement(childContext.body)
context.pushStatement(
createCallExpression(context.helper(SSR_RENDER_LIST), [
node.source,
renderLoop
])
)
}