Skip to content

Commit 93c37b9

Browse files
committed
wip(ssr): v-for
1 parent 2ad0eed commit 93c37b9

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

packages/compiler-core/src/ast.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ export interface ForNode extends Node {
229229
valueAlias: ExpressionNode | undefined
230230
keyAlias: ExpressionNode | undefined
231231
objectIndexAlias: ExpressionNode | undefined
232+
parseResult: ForParseResult
232233
children: TemplateChildNode[]
233234
codegenNode?: ForCodegenNode
234235
}
@@ -619,7 +620,7 @@ export function createCallExpression<T extends CallExpression['callee']>(
619620

620621
export function createFunctionExpression(
621622
params: FunctionExpression['params'],
622-
returns: FunctionExpression['returns'],
623+
returns: FunctionExpression['returns'] = undefined,
623624
newline: boolean = false,
624625
isSlot: boolean = false,
625626
loc: SourceLocation = locStub

packages/compiler-core/src/codegen.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,12 @@ function genNodeList(
439439
genNode(node, context)
440440
}
441441
if (i < nodes.length - 1) {
442-
comma && push(',')
443-
multilines && newline()
442+
if (multilines) {
443+
comma && push(',')
444+
newline()
445+
} else {
446+
comma && push(', ')
447+
}
444448
}
445449
}
446450
}

packages/compiler-core/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export { transformOn } from './transforms/vOn'
3333

3434
// exported for compiler-ssr
3535
export { processIfBranches } from './transforms/vIf'
36-
export { processForNode } from './transforms/vFor'
36+
export { processForNode, createForLoopParams } from './transforms/vFor'
3737
export {
3838
transformExpression,
3939
processExpression

packages/compiler-core/src/transforms/vFor.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const transformFor = createStructuralDirectiveTransform(
4545
'for',
4646
(node, dir, context) => {
4747
const { helper } = context
48-
return processForNode(node, dir, context, (forNode, parseResult) => {
48+
return processForNode(node, dir, context, forNode => {
4949
// create the loop render function expression now, and add the
5050
// iterator on exit after all children have been traversed
5151
const renderExp = createCallExpression(helper(RENDER_LIST), [
@@ -122,7 +122,7 @@ export const transformFor = createStructuralDirectiveTransform(
122122

123123
renderExp.arguments.push(
124124
createFunctionExpression(
125-
createForLoopParams(parseResult),
125+
createForLoopParams(forNode.parseResult),
126126
childBlock,
127127
true /* force newline */
128128
)
@@ -137,10 +137,7 @@ export function processForNode(
137137
node: ElementNode,
138138
dir: DirectiveNode,
139139
context: TransformContext,
140-
processCodegen?: (
141-
forNode: ForNode,
142-
parseResult: ForParseResult
143-
) => (() => void) | undefined
140+
processCodegen?: (forNode: ForNode) => (() => void) | undefined
144141
) {
145142
if (!dir.exp) {
146143
context.onError(
@@ -173,6 +170,7 @@ export function processForNode(
173170
valueAlias: value,
174171
keyAlias: key,
175172
objectIndexAlias: index,
173+
parseResult,
176174
children: node.tagType === ElementTypes.TEMPLATE ? node.children : [node]
177175
}
178176

@@ -188,7 +186,7 @@ export function processForNode(
188186
index && addIdentifiers(index)
189187
}
190188

191-
const onExit = processCodegen && processCodegen(forNode, parseResult)
189+
const onExit = processCodegen && processCodegen(forNode)
192190

193191
return () => {
194192
scopes.vFor--
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import {
22
createStructuralDirectiveTransform,
33
ForNode,
4-
processForNode
4+
processForNode,
5+
createCallExpression,
6+
createFunctionExpression,
7+
createForLoopParams,
8+
createBlockStatement
59
} from '@vue/compiler-dom'
6-
import { SSRTransformContext } from '../ssrCodegenTransform'
10+
import {
11+
SSRTransformContext,
12+
createChildContext,
13+
processChildren
14+
} from '../ssrCodegenTransform'
15+
import { SSR_RENDER_LIST } from '../runtimeHelpers'
716

817
// Plugin for the first transform pass, which simply constructs the AST node
918
export const ssrTransformFor = createStructuralDirectiveTransform(
@@ -13,4 +22,17 @@ export const ssrTransformFor = createStructuralDirectiveTransform(
1322

1423
// This is called during the 2nd transform pass to construct the SSR-sepcific
1524
// codegen nodes.
16-
export function processFor(node: ForNode, context: SSRTransformContext) {}
25+
export function processFor(node: ForNode, context: SSRTransformContext) {
26+
const renderLoop = createFunctionExpression(
27+
createForLoopParams(node.parseResult)
28+
)
29+
const childContext = createChildContext(context)
30+
processChildren(node.children, childContext)
31+
renderLoop.body = createBlockStatement(childContext.body)
32+
context.pushStatement(
33+
createCallExpression(context.helper(SSR_RENDER_LIST), [
34+
node.source,
35+
renderLoop
36+
])
37+
)
38+
}

0 commit comments

Comments
 (0)