Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vue源码分析-编译 #96

Open
kekobin opened this issue Aug 5, 2020 · 0 comments
Open

vue源码分析-编译 #96

kekobin opened this issue Aug 5, 2020 · 0 comments

Comments

@kekobin
Copy link
Owner

kekobin commented Aug 5, 2020

前言

Vue.js 提供了 2 个版本,一个是 Runtime + Compiler 的,一个是 Runtime only 的,前者是包含编译代码的,可以把编译过程放在运行时做,后者是不包含编译代码的,需要借助 webpack 的 vue-loader 事先把模板编译成 render函数。
只要求理解整体的流程、输入和输出即可

编译入口

const { render, staticRenderFns } =  compileToFunctions(template, {
    shouldDecodeNewlines,
    shouldDecodeNewlinesForHref,
    delimiters: options.delimiters,
    comments: options.comments
  }, this)
options.render = render
options.staticRenderFns = staticRenderFns

编译的入口抽离出的真正逻辑如下:
1.解析模板字符串生成 AST

const ast = parse(template.trim(), options)

2.优化语法树

optimize(ast, options)

3.生成代码

const code = generate(ast, options)

parse

编译过程首先就是对模板做解析,生成 AST,它是一种抽象语法树,是对源代码的抽象语法结构的树状表现形式。在很多编译技术中,如 babel 编译 ES6 的代码都会先生成 AST。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant