Vue-源码知识-Vue3-编译器-generate

2023/7/2 Vue

# 简介

生成(generate)render 函数

# 函数生成方案

  • 函数本质上就是一段字符串
  • 字符串的拼接方式
  • 字符串拼接的格式处理
    • 换行使用 '\n'
    • 缩进使用空格

# 过程

  • 解析 JS AST 生成函数字符串
  • 最后通过 new Function 转化为 render 函数

# 例子

<div>hello world</div>
1
const _Vue = Vue

return function render(_ctx, _cache) {
    const { createElementVNode: _createElementVNode } = _Vue
    return _createElementVNode('div', [], ['hello world'])
}
1
2
3
4
5
6
Last Updated: 2024/6/11 14:20:38
    飘向北方
    那吾克热-NW,尤长靖