@@ -100,8 +100,15 @@ export class PatchContext {
100
100
}
101
101
102
102
/**
103
- * 合并子元素节点
104
- * 注意:基于抽象语法树的推导优化依赖于稳定的模板结构,如果开发者使用了自定义 render,则无法对其进行语法树级别的推导优化
103
+ * English:
104
+ * Perform ast optimization on element type VNode.
105
+ *
106
+ * AST-based optimization relies on a stable template structure, which cannot be
107
+ * optimized if the developer uses a custom render
108
+ *
109
+ * 中文:
110
+ * 对元素类型节点执行 ast 优化
111
+ * 基于 ast 的优化依赖于稳定的模板结构,如果开发者使用了自定义 render,则无法做出优化
105
112
*/
106
113
astShaking ( patchState : PatchState ) {
107
114
const { endTag, staticChildren, ast } = patchState
@@ -121,10 +128,23 @@ export class PatchContext {
121
128
return preV
122
129
} , [ ] )
123
130
124
- /** 子节点是否全部都是静态节点 */
131
+ /**
132
+ * English:
133
+ * Detect whether all child nodes are static
134
+ *
135
+ * 中文:
136
+ * 检测子节点是否全部都是静态节点
137
+ */
125
138
const childStatic = elements . length === 0 || ( elements . length === 1 && typeof elements [ 0 ] === 'string' )
126
139
127
- /** 如果当前语法树并未匹配到组件 render 模板 则仅判断当前节点及其子节点是否全是静态 */
140
+ /**
141
+ * English:
142
+ * If the current ast does not match the component's rendering template, detect
143
+ * whether the current node and its children are all static.
144
+ *
145
+ * 中文:
146
+ * 如果当前 ast 并未匹配到组件 render 模板 则仅判断当前节点及其子节点是否全是静态
147
+ */
128
148
if ( ast . unMatchedAst ) {
129
149
if ( ast . ssrString !== undefined && childStatic ) {
130
150
const str = ast . ssrString + elements [ 0 ] + endTag
@@ -134,11 +154,15 @@ export class PatchContext {
134
154
}
135
155
136
156
/**
157
+ * English:
158
+ * Resetting the current ast subtree.
159
+ * If children exist and the child element is not static, the ast for children is reconstructed.
160
+ *
161
+ * 中文:
137
162
* 重新设置当前节点抽象语法树的子树
138
- * ast 不一定有 children 子元素,但是其子元素有可能全部都是静态的,例如常量for循环
163
+ * 如果 children 存在,并且子元素非静态,则重新为 children 构建抽象语法树
139
164
*/
140
165
const children = getVNodeAstChildren ( ast )
141
- /** 如果存在 AST 子元素,并且子元素非静态,则构建抽象语法树 */
142
166
if ( children && ! childStatic ) {
143
167
children . elements = elements . map ( v => {
144
168
if ( typeof v === 'string' ) {
@@ -151,7 +175,13 @@ export class PatchContext {
151
175
} )
152
176
}
153
177
154
- /** 如果当前节点和子节点都是静态节点,则将节点合并 */
178
+ /**
179
+ * English:
180
+ * If both the current node and child nodes are static, the nodes are combined.
181
+ *
182
+ * 中文:
183
+ * 如果当前节点和子节点都是静态节点,则将节点合并。
184
+ * */
155
185
if ( ast . ssrString !== undefined && childStatic ) {
156
186
const str = ast . ssrString + elements [ 0 ] + endTag
157
187
Object . assign ( ast , callExpression (
@@ -161,22 +191,36 @@ export class PatchContext {
161
191
}
162
192
163
193
/**
194
+ * English:
195
+ * Optimizing the child tree of the current node abstract syntax tree
196
+ *
197
+ * 中文:
164
198
* 优化当前节点抽象语法树的子树
165
199
*/
166
200
this . reduceAstChildren ( patchState )
167
201
}
168
202
169
203
/**
204
+ * English:
205
+ * Merge component-level VNodes.
206
+ * If the entire component is static, the result is passed up.
207
+ * If the component is dynamic, retain the rendering function.
208
+ *
209
+ * 中文:
170
210
* 合并组件级节点
171
- * 如果整个组件都是静态的,则将结果向上传递,如果组件是动态的,则保留渲染函数
172
- * * todo astComponentShaking 里面需要考虑到 unMatchedAst 静态情况
211
+ * 如果整个组件都是静态的,则将结果向上传递
212
+ * 如果组件是动态的,则保留渲染函数
173
213
*/
174
214
astComponentShaking ( patchState : PatchState ) {
175
215
const prevAst = patchState . prevAst
176
216
const ssrRenderAst = prevAst . ssrRenderAst
177
217
178
218
/**
179
- * 当前组件无优化
219
+ * English:
220
+ * Current component nodes are not optimized.
221
+ *
222
+ * 中文:
223
+ * 当前组件节点无优化
180
224
*/
181
225
if ( ssrRenderAst . unMatchedAst && ! ssrRenderAst . ssrStatic ) {
182
226
return this . setSSRRenderTree ( prevAst )
@@ -185,6 +229,10 @@ export class PatchContext {
185
229
let renderAst = null
186
230
187
231
/**
232
+ * English:
233
+ * The current component does not match ast, but is a static component
234
+ *
235
+ * 中文:
188
236
* 当前组件虽未匹配 ast,但是为静态组件
189
237
*/
190
238
if ( ssrRenderAst . unMatchedAst && ssrRenderAst . ssrStatic ) {
@@ -194,8 +242,12 @@ export class PatchContext {
194
242
}
195
243
196
244
/**
197
- * 如果当前组件已被优化成静态组件
198
- * 则将当前组件的值与父组件关联
245
+ * English:
246
+ * If the current component has been optimized to be static, associate the
247
+ * value of the current component with the parent ast.
248
+ *
249
+ * 中文:
250
+ * 如果当前组件已被优化成静态组件,则将当前组件的值与父 ast 节点关联
199
251
*/
200
252
const value = getStatisAstComponentValue ( renderAst )
201
253
if ( value ) {
@@ -206,6 +258,48 @@ export class PatchContext {
206
258
this . setSSRRenderTree ( prevAst )
207
259
}
208
260
261
+ /**
262
+ * English:
263
+ * Generate a new rendering chain based on Ast's optimization information.
264
+ *
265
+ * The information for the rendering chain is stored in ssrRenderTree, which
266
+ * has the following structure.
267
+ * {
268
+ * render() {},
269
+ * children: [
270
+ * {
271
+ * render() {},
272
+ * children: []
273
+ * },
274
+ * {
275
+ * render() {},
276
+ * children: []
277
+ * }
278
+ * ]
279
+ * }
280
+ * Deep priority traverses ssrRenderTree and executes the render function to
281
+ * render the application by optimized path.
282
+ *
283
+ * 中文:
284
+ * 根据 Ast 的优化信息生成全新的渲染链
285
+ * 渲染链的信息保存在 ssrRenderTree 中,ssrRenderTree 的结构如下所示:
286
+ * {
287
+ * render() {},
288
+ * children: [
289
+ * {
290
+ * render() {},
291
+ * children: []
292
+ * },
293
+ * {
294
+ * render() {},
295
+ * children: []
296
+ * }
297
+ * ]
298
+ * }
299
+ * 深度优先遍历 ssrRenderTree,并执行 render 函数,即可按优化路径渲染应用
300
+ *
301
+ * @param {Object } ast
302
+ */
209
303
setSSRRenderTree ( ast : Object ) {
210
304
let renderStr = 'function render() {}'
211
305
if ( ast . ssrStatic && ast . ssrString !== undefined ) {
@@ -277,19 +371,28 @@ export class PatchContext {
277
371
}
278
372
279
373
/**
374
+ * English:
375
+ * Optimizing the child tree of the current ast
376
+ *
377
+ * 中文:
280
378
* 优化当前节点抽象语法树的子树
281
- * @param {* } patchState
379
+ * @param {PatchState } patchState
282
380
*/
283
381
reduceAstChildren ( patchState : PatchState ) {
284
382
const ast = patchState . ast
285
383
const children = getVNodeAstChildren ( ast )
286
384
/**
385
+ * English:
386
+ * If the current element node is an ssrNode node, and there is only one ssrNode child,
387
+ * and the child has no grandchildren, elevate the child node.
388
+ *
389
+ * 中文:
287
390
* 如果当前元素节点为 ssrNode 节点,且只有一个 ssrNode 子节点,并且子节点没有孙节点,则把子节点提升
288
391
*/
289
392
if (
290
- children && children . elements . length === 1 && //只有一个子节点
291
- isSSRNodeAst ( ast ) && isSSRNodeAst ( children . elements [ 0 ] ) && //当前元素节点为 ssrNode 节点, 子节点为 ssrNode 节点
292
- children . elements [ 0 ] . arguments . length === 1 // 子节点没有孙节点
393
+ children && children . elements . length === 1 && // Only one child node.
394
+ isSSRNodeAst ( ast ) && isSSRNodeAst ( children . elements [ 0 ] ) && // The current element node is an ssrNode node and the child node is an ssrNode node
395
+ children . elements [ 0 ] . arguments . length === 1 // Child node has no grand node.
293
396
) {
294
397
const node = children . elements [ 0 ] . arguments [ 0 ]
295
398
const args = ast . arguments
0 commit comments