File tree 2 files changed +42
-4
lines changed
2 files changed +42
-4
lines changed Original file line number Diff line number Diff line change @@ -1220,7 +1220,9 @@ transition: fade-out
1220
1220
1221
1221
---
1222
1222
1223
- 首先来看看完整的编译流程:
1223
+ ## 编译流程
1224
+
1225
+ 首先来看看完整的模板编译流程:
1224
1226
1225
1227
![ compile] ( /public/compile-process.excalidraw.png )
1226
1228
@@ -1238,21 +1240,57 @@ const render = new Function(code) // 渲染函数
1238
1240
1239
1241
![ template-to-render] ( /public/template-to-render.excalidraw.png )
1240
1242
1241
- 编译器的最终目的就是将 ** 模板转换成渲染函数 ** ,而渲染函数的执行会生成虚拟DOM,用于后续的挂载和更新 。
1243
+ 可以看到,模板编译器的最终目的就是将 ** 模板转换(源代码)成渲染函数(目标代码) ** 。
1242
1244
1243
1245
---
1244
1246
1245
1247
## 抽象语法树 AST (Abstract Syntax Tree )
1246
1248
1247
- 前面我们已经知道了,模板会被解析器解析成 AST,在 JS 中,AST 本质就是一个 JS 对象。
1249
+ 前面我们已经知道了,模板会被解析器解析成 AST,那么什么是 AST 呢?
1250
+
1251
+ 摘自[ 维基百科] ( https://zh.wikipedia.org/zh-cn/%E6%8A%BD%E8%B1%A1%E8%AA%9E%E6%B3%95%E6%A8%B9 ) :
1252
+
1253
+ > 在计算机科学中,抽象语法树(Abstract Syntax Tree,AST),是源代码语法结构的一种抽象表示。它以树状的形式表现编程语言的语法结构,树上的每个节点都表示源代码中的一种结构。
1254
+
1255
+ 由上定义可知:
1256
+
1257
+ + AST 是一个树状结构
1258
+ + AST 表示源代码的语法结构
1259
+
1260
+ 关于 AST,可以看看 [ AST Explorer] ( https://astexplorer.net/ )
1261
+
1262
+ 那 AST 有什么用呢?
1263
+
1264
+ ---
1265
+
1266
+ 在前端领域,AST 其实应用广泛,比如:
1267
+
1268
+ + babel: 典型的转译器,也是根据源代码的 AST 转换成其他代码的 AST,再生成目标代码,如 ES6 转 ES5
1269
+ + jsx: 大名鼎鼎的 jsx 语法其实也是需要编译的,并且最终编译完也是很多的 ` render ` 函数
1270
+ + ESlint: ESlint 也需要对源代码的 AST 进行解析处理,分析是否符合规则
1271
+ + TypeScript: 天天都在用的 ts 也是需要编译的,由 ts 编译成 js
1272
+ + V8: Chrome 的 V8 引擎能直接执行 js,想都不用想,肯定需要编译
1273
+ + 语法高亮: 每天都看着五颜六色的代码,也是通过编译实现的
1274
+ + 代码提示: 同上
1275
+ + 错误检验: 同上
1276
+ + ...
1277
+
1278
+ 更多参考:
1279
+
1280
+ - [ https://juejin.cn/post/7031908854388490248 ] ( https://juejin.cn/post/7031908854388490248 )
1281
+ - [ https://juejin.cn/post/6844904035271573511 ] ( https://juejin.cn/post/6844904035271573511 )
1282
+
1283
+ ---
1284
+
1285
+ 了解了什么是 AST 后,我们再来看看 Vue 模板对应的 AST 长什么样子。
1248
1286
1249
1287
假设有如下模板:
1250
1288
1251
1289
``` js
1252
1290
const template = ` <div class="test"><span>Hello</span></div>`
1253
1291
```
1254
1292
1255
- 经过解析之后,它对应的 AST 结构如下 :
1293
+ 经过解析之后,它对应的 AST 结构如下表示 :
1256
1294
1257
1295
``` js
1258
1296
const ast = {
You can’t perform that action at this time.
0 commit comments