Skip to content

Commit 08f843f

Browse files
committed
progress
1 parent 852ae10 commit 08f843f

File tree

2 files changed

+105
-44
lines changed

2 files changed

+105
-44
lines changed

src/server/optimizing-compiler/codegen.js

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
// a node is not optimizable it simply falls back to the default codegen.
66

77
// import * as directives from './directives'
8-
import { FULL, SELF, PARTIAL, CHILDREN } from './optimizer'
9-
8+
import { optimizability } from './optimizer'
109
import {
1110
genIf,
1211
genFor,
@@ -23,6 +22,16 @@ type SSRCompileResult = {
2322
stringRenderFns: Array<string>;
2423
};
2524

25+
// segment types
26+
const HTML = 0
27+
const TEXT = 1
28+
const EXP = 2
29+
30+
type StringSegment = {
31+
type: number;
32+
value: string;
33+
};
34+
2635
class SSRCodegenState extends CodegenState {
2736
stringRenderFns: Array<string>;
2837

@@ -53,16 +62,16 @@ function genSSRElement (el: ASTElement, state: SSRCodegenState): string {
5362
}
5463

5564
switch (el.ssrOptimizability) {
56-
case FULL:
65+
case optimizability.FULL:
5766
// stringify whole tree
5867
return genStringElement(el, state, true)
59-
case SELF:
68+
case optimizability.SELF:
6069
// stringify self and check children
6170
return genStringElement(el, state, false)
62-
case CHILDREN:
71+
case optimizability.CHILDREN:
6372
// generate self as VNode and stringify children
6473
return genNormalElement(el, state, true)
65-
case PARTIAL:
74+
case optimizability.PARTIAL:
6675
// generate self as VNode and check children
6776
return genNormalElement(el, state, false)
6877
default:
@@ -71,32 +80,81 @@ function genSSRElement (el: ASTElement, state: SSRCodegenState): string {
7180
}
7281
}
7382

74-
function genSSRNode (el, state) {
75-
return el.type === 1
76-
? genSSRElement(el, state)
77-
: genText(el, state)
78-
}
79-
80-
function genSSRChildren (el, state, checkSkip) {
81-
return genChildren(el, state, checkSkip, genSSRElement, genSSRNode)
82-
}
83-
8483
function genNormalElement (el, state, stringifyChildren) {
8584
const data = el.plain ? undefined : genData(el, state)
8685
const children = stringifyChildren
8786
? genStringChildren(el, state)
8887
: genSSRChildren(el, state, true)
8988
return `_c('${el.tag}'${
90-
data ? `,${data}` : '' // data
89+
data ? `,${data}` : ''
9190
}${
92-
children ? `,${children}` : '' // children
91+
children ? `,${children}` : ''
9392
})`
9493
}
9594

96-
function genStringElement (el, state, stringifyChildren) {
97-
return '"string element"'
95+
function genSSRChildren (el, state, checkSkip) {
96+
return genChildren(el, state, checkSkip, genSSRElement, genSSRNode)
97+
}
98+
99+
function genSSRNode (el, state) {
100+
return el.type === 1
101+
? genSSRElement(el, state)
102+
: genText(el, state)
98103
}
99104

100105
function genStringChildren (el, state) {
101-
return '"string children"'
106+
return `[_ss(${flattenSegments(childrenToSegments(el, state))})]`
107+
}
108+
109+
function genStringElement (el, state, stringifyChildren) {
110+
if (stringifyChildren) {
111+
return `_ss(${flattenSegments(elementToSegments(el, state))})`
112+
} else {
113+
const children = genSSRChildren(el, state, true)
114+
return `_ss(${
115+
flattenSegments(elementToOpenTagSegments(el, state))
116+
}","${el.tag}"${
117+
children ? `,${children}` : ''
118+
})`
119+
}
120+
}
121+
122+
function elementToSegments (el, state): Array<StringSegment> {
123+
const openSegments = elementToOpenTagSegments(el, state)
124+
const childrenSegments = childrenToSegments(el, state)
125+
const { isUnaryTag } = state.options
126+
const close = (isUnaryTag && isUnaryTag(el.tag))
127+
? []
128+
: [{ type: HTML, value: `</${el.tag}>` }]
129+
return openSegments.concat(childrenSegments, close)
130+
}
131+
132+
function elementToOpenTagSegments (el, state): Array<StringSegment> {
133+
// TODO: handle attrs/props/styles/classes/directives
134+
return [{ type: HTML, value: `<${el.tag}>` }]
135+
}
136+
137+
function childrenToSegments (el, state): Array<StringSegment> {
138+
const children = el.children
139+
if (children) {
140+
const segments = []
141+
for (let i = 0; i < children.length; i++) {
142+
const c = children[i]
143+
if (c.type === 1) {
144+
segments.push.apply(segments, elementToSegments(c, state))
145+
} else if (c.type === 2) {
146+
segments.push({ type: EXP, value: c.expression })
147+
} else if (c.type === 3) {
148+
segments.push({ type: TEXT, value: c.text })
149+
}
150+
}
151+
return segments
152+
} else {
153+
return []
154+
}
155+
}
156+
157+
function flattenSegments (segments: Array<StringSegment>): string {
158+
console.log(segments)
159+
return 'TODO'
102160
}

src/server/optimizing-compiler/optimizer.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
/* @flow */
22

3-
import { no, makeMap, isBuiltInTag } from 'shared/util'
4-
5-
// optimizability constants
6-
export const FALSE = 0 // whole sub tree un-optimizable
7-
export const FULL = 1 // whole sub tree optimizable
8-
export const SELF = 2 // self optimizable but has some un-optimizable children
9-
export const CHILDREN = 3 // self un-optimizable but have fully optimizable children
10-
export const PARTIAL = 4 // self un-optimizable with some un-optimizable children
11-
12-
let isPlatformReservedTag
13-
143
/**
154
* In SSR, the vdom tree is generated only once and never patched, so
165
* we can optimize most element / trees into plain string render functions.
@@ -20,6 +9,20 @@ let isPlatformReservedTag
209
* detection (which is designed for client re-render). In SSR we bail only for
2110
* components/slots/custom directives.
2211
*/
12+
13+
import { no, makeMap, isBuiltInTag } from 'shared/util'
14+
15+
// optimizability constants
16+
export const optimizability = {
17+
FALSE: 0, // whole sub tree un-optimizable
18+
FULL: 1, // whole sub tree optimizable
19+
SELF: 2, // self optimizable but has some un-optimizable children
20+
CHILDREN: 3, // self un-optimizable but have fully optimizable children
21+
PARTIAL: 4 // self un-optimizable with some un-optimizable children
22+
}
23+
24+
let isPlatformReservedTag
25+
2326
export function optimize (root: ?ASTElement, options: CompilerOptions) {
2427
if (!root) return
2528
isPlatformReservedTag = options.isReservedTag || no
@@ -28,39 +31,39 @@ export function optimize (root: ?ASTElement, options: CompilerOptions) {
2831

2932
function walk (node: ASTNode, isRoot?: boolean) {
3033
if (isUnOptimizableTree(node)) {
31-
node.ssrOptimizability = FALSE
34+
node.ssrOptimizability = optimizability.FALSE
3235
return
3336
}
3437
// root node or nodes with custom directives should always be a VNode
3538
if (isRoot || hasCustomDirective(node)) {
36-
node.ssrOptimizability = CHILDREN
39+
node.ssrOptimizability = optimizability.CHILDREN
3740
}
3841
if (node.type === 1) {
3942
for (let i = 0, l = node.children.length; i < l; i++) {
4043
const child = node.children[i]
4144
walk(child)
42-
if (child.ssrOptimizability !== FULL) {
43-
node.ssrOptimizability = node.ssrOptimizability === CHILDREN
44-
? PARTIAL
45-
: SELF
45+
if (child.ssrOptimizability !== optimizability.FULL) {
46+
node.ssrOptimizability = node.ssrOptimizability === optimizability.CHILDREN
47+
? optimizability.PARTIAL
48+
: optimizability.SELF
4649
}
4750
}
4851
if (node.ifConditions) {
4952
for (let i = 1, l = node.ifConditions.length; i < l; i++) {
5053
const block = node.ifConditions[i].block
5154
walk(block)
52-
if (block.ssrOptimizability !== FULL) {
53-
node.ssrOptimizability = node.ssrOptimizability === CHILDREN
54-
? PARTIAL
55-
: SELF
55+
if (block.ssrOptimizability !== optimizability.FULL) {
56+
node.ssrOptimizability = node.ssrOptimizability === optimizability.CHILDREN
57+
? optimizability.PARTIAL
58+
: optimizability.SELF
5659
}
5760
}
5861
}
5962
if (node.ssrOptimizability == null) {
60-
node.ssrOptimizability = FULL
63+
node.ssrOptimizability = optimizability.FULL
6164
}
6265
} else {
63-
node.ssrOptimizability = FULL
66+
node.ssrOptimizability = optimizability.FULL
6467
}
6568
}
6669

0 commit comments

Comments
 (0)