render.js
7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* @flow */
import { escape } from 'he'
import { compileToFunctions } from 'web/compiler/index'
import { createComponentInstanceForVnode } from 'core/vdom/create-component'
import { noop } from 'shared/util'
let warned = Object.create(null)
const warnOnce = msg => {
if (!warned[msg]) {
warned[msg] = true
console.warn(`\n\u001b[31m${msg}\u001b[39m\n`)
}
}
const normalizeAsync = (cache, method) => {
const fn = cache[method]
if (!fn) {
return
} else if (fn.length > 1) {
return (key, cb) => fn.call(cache, key, cb)
} else {
return (key, cb) => cb(fn.call(cache, key))
}
}
const compilationCache = Object.create(null)
const normalizeRender = vm => {
const { render, template } = vm.$options
if (!render) {
if (template) {
const renderFns = (
compilationCache[template] ||
(compilationCache[template] = compileToFunctions(template))
)
Object.assign(vm.$options, renderFns)
} else {
throw new Error(
`render function or template not defined in component: ${
vm.$options.name || vm.$options._componentTag || 'anonymous'
}`
)
}
}
}
function renderNode (node, isRoot, context) {
const { write, next } = context
if (node.componentOptions) {
// check cache hit
const Ctor = node.componentOptions.Ctor
const getKey = Ctor.options.serverCacheKey
const name = Ctor.options.name
const cache = context.cache
if (getKey && cache && name) {
const key = name + '::' + getKey(node.componentOptions.propsData)
const { has, get } = context
if (has) {
has(key, hit => {
if (hit && get) {
get(key, res => write(res, next))
} else {
renderComponentWithCache(node, isRoot, key, context)
}
})
} else if (get) {
get(key, res => {
if (res) {
write(res, next)
} else {
renderComponentWithCache(node, isRoot, key, context)
}
})
}
} else {
if (getKey && !cache) {
warnOnce(
`[vue-server-renderer] Component ${
Ctor.options.name || '(anonymous)'
} implemented serverCacheKey, ` +
'but no cache was provided to the renderer.'
)
}
if (getKey && !name) {
warnOnce(
`[vue-server-renderer] Components that implement "serverCacheKey" ` +
`must also define a unique "name" option.`
)
}
renderComponent(node, isRoot, context)
}
} else {
if (node.tag) {
renderElement(node, isRoot, context)
} else if (node.isComment) {
write(`<!--${node.text}-->`, next)
} else {
write(node.raw ? node.text : escape(String(node.text)), next)
}
}
}
function renderComponent (node, isRoot, context) {
const prevActive = context.activeInstance
const child = context.activeInstance = createComponentInstanceForVnode(node, context.activeInstance)
normalizeRender(child)
const childNode = child._render()
childNode.parent = node
context.renderStates.push({
type: 'Component',
prevActive
})
renderNode(childNode, isRoot, context)
}
function renderComponentWithCache (node, isRoot, key, context) {
const write = context.write
write.caching = true
const buffer = write.cacheBuffer
const bufferIndex = buffer.push('') - 1
context.renderStates.push({
type: 'ComponentWithCache',
buffer, bufferIndex, key
})
renderComponent(node, isRoot, context)
}
function renderElement (el, isRoot, context) {
if (isRoot) {
if (!el.data) el.data = {}
if (!el.data.attrs) el.data.attrs = {}
el.data.attrs['server-rendered'] = 'true'
}
const startTag = renderStartingTag(el, context)
const endTag = `</${el.tag}>`
const { write, next } = context
if (context.isUnaryTag(el.tag)) {
write(startTag, next)
} else if (!el.children || !el.children.length) {
write(startTag + endTag, next)
} else {
const children: Array<VNode> = el.children
context.renderStates.push({
type: 'Element',
rendered: 0,
total: children.length,
endTag, children
})
write(startTag, next)
}
}
function hasAncestorData (node: VNode) {
const parentNode = node.parent
return parentNode && (parentNode.data || hasAncestorData(parentNode))
}
function renderStartingTag (node: VNode, context) {
let markup = `<${node.tag}`
const { directives, modules } = context
// construct synthetic data for module processing
// because modules like style also produce code by parent VNode data
if (!node.data && hasAncestorData(node)) {
node.data = {}
}
if (node.data) {
// check directives
const dirs = node.data.directives
if (dirs) {
for (let i = 0; i < dirs.length; i++) {
const dirRenderer = directives[dirs[i].name]
if (dirRenderer) {
// directives mutate the node's data
// which then gets rendered by modules
dirRenderer(node, dirs[i])
}
}
}
// apply other modules
for (let i = 0; i < modules.length; i++) {
const res = modules[i](node)
if (res) {
markup += res
}
}
}
// attach scoped CSS ID
let scopeId
const activeInstance = context.activeInstance
if (activeInstance &&
activeInstance !== node.context &&
(scopeId = activeInstance.$options._scopeId)) {
markup += ` ${scopeId}`
}
while (node) {
if ((scopeId = node.context.$options._scopeId)) {
markup += ` ${scopeId}`
}
node = node.parent
}
return markup + '>'
}
const nextFactory = context => function next () {
const lastState = context.renderStates.pop()
if (!lastState) {
context.done()
// cleanup context, avoid leakage
context = (null: any)
return
}
switch (lastState.type) {
case 'Component':
context.activeInstance = lastState.prevActive
next()
break
case 'Element':
const { children, total } = lastState
const rendered = lastState.rendered++
if (rendered < total) {
context.renderStates.push(lastState)
renderNode(children[rendered], false, context)
} else {
context.write(lastState.endTag, next)
}
break
case 'ComponentWithCache':
const { buffer, bufferIndex, key } = lastState
const result = buffer[bufferIndex]
context.cache.set(key, result)
if (bufferIndex === 0) {
// this is a top-level cached component,
// exit caching mode.
context.write.caching = false
} else {
// parent component is also being cached,
// merge self into parent's result
buffer[bufferIndex - 1] += result
}
buffer.length = bufferIndex
next()
break
}
}
export function createRenderFunction (
modules: Array<Function>,
directives: Object,
isUnaryTag: Function,
cache: any
) {
if (cache && (!cache.get || !cache.set)) {
throw new Error('renderer cache must implement at least get & set.')
}
const get = cache && normalizeAsync(cache, 'get')
const has = cache && normalizeAsync(cache, 'has')
return function render (
component: Component,
write: (text: string, next: Function) => void,
done: Function
) {
warned = Object.create(null)
const context = {
activeInstance: component,
renderStates: [],
next: noop, // for flow
write, done,
isUnaryTag, modules, directives,
cache, get, has
}
context.next = nextFactory(context)
normalizeRender(component)
renderNode(component._render(), true, context)
}
}