Skip to content

Commit ac0e168

Browse files
committed
optimize siblings
1 parent 7a82b64 commit ac0e168

File tree

2 files changed

+61
-19
lines changed

2 files changed

+61
-19
lines changed

src/server/optimizing-compiler/codegen.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ function genSSRElement (el: ASTElement, state: CodegenState): string {
5454
} else if (el.if && !el.ifProcessed) {
5555
return genIf(el, state, genSSRElement)
5656
} else if (el.tag === 'template' && !el.slotTarget) {
57-
return genSSRChildren(el, state) || 'void 0'
57+
return el.ssrOptimizability === optimizability.FULL
58+
? genChildrenAsStringNode(el, state)
59+
: genSSRChildren(el, state) || 'void 0'
5860
}
5961

6062
switch (el.ssrOptimizability) {
@@ -79,7 +81,7 @@ function genSSRElement (el: ASTElement, state: CodegenState): string {
7981
function genNormalElement (el, state, stringifyChildren) {
8082
const data = el.plain ? undefined : genData(el, state)
8183
const children = stringifyChildren
82-
? genStringChildren(el, state)
84+
? `[${genChildrenAsStringNode(el, state)}]`
8385
: genSSRChildren(el, state, true)
8486
return `_c('${el.tag}'${
8587
data ? `,${data}` : ''
@@ -98,9 +100,9 @@ function genSSRNode (el, state) {
98100
: genText(el, state)
99101
}
100102

101-
function genStringChildren (el, state) {
103+
function genChildrenAsStringNode (el, state) {
102104
return el.children.length
103-
? `[_ssrNode(${flattenSegments(childrenToSegments(el, state))})]`
105+
? `_ssrNode(${flattenSegments(childrenToSegments(el, state))})`
104106
: ''
105107
}
106108

@@ -200,24 +202,27 @@ function childrenToSegments (el, state): Array<StringSegment> {
200202
if ((binding = el.attrsMap['v-text'])) {
201203
return [{ type: INTERPOLATION, value: binding }]
202204
}
205+
return el.children
206+
? nodesToSegments(el.children, state)
207+
: []
208+
}
203209

204-
const children = el.children
205-
if (children) {
206-
const segments = []
207-
for (let i = 0; i < children.length; i++) {
208-
const c = children[i]
209-
if (c.type === 1) {
210-
segments.push.apply(segments, elementToSegments(c, state))
211-
} else if (c.type === 2) {
212-
segments.push({ type: INTERPOLATION, value: c.expression })
213-
} else if (c.type === 3) {
214-
segments.push({ type: RAW, value: c.text })
215-
}
210+
function nodesToSegments (
211+
children: Array<ASTNode>,
212+
state: CodegenState
213+
): Array<StringSegment> {
214+
const segments = []
215+
for (let i = 0; i < children.length; i++) {
216+
const c = children[i]
217+
if (c.type === 1) {
218+
segments.push.apply(segments, elementToSegments(c, state))
219+
} else if (c.type === 2) {
220+
segments.push({ type: INTERPOLATION, value: c.expression })
221+
} else if (c.type === 3) {
222+
segments.push({ type: RAW, value: c.text })
216223
}
217-
return segments
218-
} else {
219-
return []
220224
}
225+
return segments
221226
}
222227

223228
function flattenSegments (segments: Array<StringSegment>): string {

src/server/optimizing-compiler/optimizer.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,49 @@ function walk (node: ASTNode, isRoot?: boolean) {
6464
node.attrsMap['v-text']
6565
) {
6666
node.ssrOptimizability = optimizability.FULL
67+
} else {
68+
node.children = optimizeSiblings(node)
6769
}
6870
} else {
6971
node.ssrOptimizability = optimizability.FULL
7072
}
7173
}
7274

75+
function optimizeSiblings (el) {
76+
const children = el.children
77+
const optimizedChildren = []
78+
79+
let currentOptimizableGroup = []
80+
const pushGroup = () => {
81+
if (currentOptimizableGroup.length) {
82+
optimizedChildren.push({
83+
type: 1,
84+
parent: el,
85+
tag: 'template',
86+
attrsList: [],
87+
attrsMap: {},
88+
children: currentOptimizableGroup,
89+
ssrOptimizability: optimizability.FULL
90+
})
91+
}
92+
currentOptimizableGroup = []
93+
}
94+
95+
for (let i = 0; i < children.length; i++) {
96+
const c = children[i]
97+
if (c.ssrOptimizability === optimizability.FULL) {
98+
currentOptimizableGroup.push(c)
99+
} else {
100+
// wrap fully-optimizable adjacent siblings inside a template tag
101+
// so that they can be optimized into a single ssrNode by codegen
102+
pushGroup()
103+
optimizedChildren.push(c)
104+
}
105+
}
106+
pushGroup()
107+
return optimizedChildren
108+
}
109+
73110
function isUnOptimizableTree (node: ASTNode): boolean {
74111
if (node.type === 2 || node.type === 3) { // text or expression
75112
return false

0 commit comments

Comments
 (0)