$mount

首先看一下mount的代码

  1. /*把原本不带编译的$mount方法保存下来,在最后会调用。*/
  2. const mount = Vue.prototype.$mount
  3. /*挂载组件,带模板编译*/
  4. Vue.prototype.$mount = function (
  5. el?: string | Element,
  6. hydrating?: boolean
  7. ): Component {
  8. el = el && query(el)
  9. /* istanbul ignore if */
  10. if (el === document.body || el === document.documentElement) {
  11. process.env.NODE_ENV !== 'production' && warn(
  12. `Do not mount Vue to <html> or <body> - mount to normal elements instead.`
  13. )
  14. return this
  15. }
  16. const options = this.$options
  17. // resolve template/el and convert to render function
  18. /*处理模板templete,编译成render函数,render不存在的时候才会编译template,否则优先使用render*/
  19. if (!options.render) {
  20. let template = options.template
  21. /*template存在的时候取template,不存在的时候取el的outerHTML*/
  22. if (template) {
  23. /*当template是字符串的时候*/
  24. if (typeof template === 'string') {
  25. if (template.charAt(0) === '#') {
  26. template = idToTemplate(template)
  27. /* istanbul ignore if */
  28. if (process.env.NODE_ENV !== 'production' && !template) {
  29. warn(
  30. `Template element not found or is empty: ${options.template}`,
  31. this
  32. )
  33. }
  34. }
  35. } else if (template.nodeType) {
  36. /*当template为DOM节点的时候*/
  37. template = template.innerHTML
  38. } else {
  39. /*报错*/
  40. if (process.env.NODE_ENV !== 'production') {
  41. warn('invalid template option:' + template, this)
  42. }
  43. return this
  44. }
  45. } else if (el) {
  46. /*获取element的outerHTML*/
  47. template = getOuterHTML(el)
  48. }
  49. if (template) {
  50. /* istanbul ignore if */
  51. if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
  52. mark('compile')
  53. }
  54. /*将template编译成render函数,这里会有render以及staticRenderFns两个返回,这是vue的编译时优化,static静态不需要在VNode更新时进行patch,优化性能*/
  55. const { render, staticRenderFns } = compileToFunctions(template, {
  56. shouldDecodeNewlines,
  57. delimiters: options.delimiters
  58. }, this)
  59. options.render = render
  60. options.staticRenderFns = staticRenderFns
  61. /* istanbul ignore if */
  62. if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
  63. mark('compile end')
  64. measure(`${this._name} compile`, 'compile', 'compile end')
  65. }
  66. }
  67. }
  68. /*Github:https://github.com/answershuto*/
  69. /*调用const mount = Vue.prototype.$mount保存下来的不带编译的mount*/
  70. return mount.call(this, el, hydrating)
  71. }

通过mount代码我们可以看到,在mount的过程中,如果render函数不存在(render函数存在会优先使用render)会将template进行compileToFunctions得到render以及staticRenderFns。譬如说手写组件时加入了template的情况都会在运行时进行编译。而render function在运行后会返回VNode节点,供页面的渲染以及在update的时候patch。接下来我们来看一下template是如何编译的。

一些基础

首先,template会被编译成AST,那么AST是什么?

在计算机科学中,抽象语法树(abstract syntax tree或者缩写为AST),或者语法树(syntax tree),是源代码的抽象语法结构的树状表现形式,这里特指编程语言的源代码。具体可以查看抽象语法树

AST会经过generate得到render函数,render的返回值是VNode,VNode是Vue的虚拟DOM节点,具体定义如下:

  1. export default class VNode {
  2. tag: string | void;
  3. data: VNodeData | void;
  4. children: ?Array<VNode>;
  5. text: string | void;
  6. elm: Node | void;
  7. ns: string | void;
  8. context: Component | void; // rendered in this component's scope
  9. functionalContext: Component | void; // only for functional component root nodes
  10. key: string | number | void;
  11. componentOptions: VNodeComponentOptions | void;
  12. componentInstance: Component | void; // component instance
  13. parent: VNode | void; // component placeholder node
  14. raw: boolean; // contains raw HTML? (server only)
  15. isStatic: boolean; // hoisted static node
  16. isRootInsert: boolean; // necessary for enter transition check
  17. isComment: boolean; // empty comment placeholder?
  18. isCloned: boolean; // is a cloned node?
  19. isOnce: boolean; // is a v-once node?
  20. /*Github:https://github.com/answershuto*/
  21. constructor (
  22. tag?: string,
  23. data?: VNodeData,
  24. children?: ?Array<VNode>,
  25. text?: string,
  26. elm?: Node,
  27. context?: Component,
  28. componentOptions?: VNodeComponentOptions
  29. ) {
  30. /*当前节点的标签名*/
  31. this.tag = tag
  32. /*当前节点对应的对象,包含了具体的一些数据信息,是一个VNodeData类型,可以参考VNodeData类型中的数据信息*/
  33. this.data = data
  34. /*当前节点的子节点,是一个数组*/
  35. this.children = children
  36. /*当前节点的文本*/
  37. this.text = text
  38. /*当前虚拟节点对应的真实dom节点*/
  39. this.elm = elm
  40. /*当前节点的名字空间*/
  41. this.ns = undefined
  42. /*编译作用域*/
  43. this.context = context
  44. /*函数化组件作用域*/
  45. this.functionalContext = undefined
  46. /*节点的key属性,被当作节点的标志,用以优化*/
  47. this.key = data && data.key
  48. /*组件的option选项*/
  49. this.componentOptions = componentOptions
  50. /*当前节点对应的组件的实例*/
  51. this.componentInstance = undefined
  52. /*当前节点的父节点*/
  53. this.parent = undefined
  54. /*简而言之就是是否为原生HTML或只是普通文本,innerHTML的时候为true,textContent的时候为false*/
  55. this.raw = false
  56. /*静态节点标志*/
  57. this.isStatic = false
  58. /*是否作为跟节点插入*/
  59. this.isRootInsert = true
  60. /*是否为注释节点*/
  61. this.isComment = false
  62. /*是否为克隆节点*/
  63. this.isCloned = false
  64. /*是否有v-once指令*/
  65. this.isOnce = false
  66. }
  67. // DEPRECATED: alias for componentInstance for backwards compat.
  68. /* istanbul ignore next */
  69. get child (): Component | void {
  70. return this.componentInstance
  71. }
  72. }

关于VNode的一些细节,请参考VNode节点

createCompiler

createCompiler用以创建编译器,返回值是compile以及compileToFunctions。compile是一个编译器,它会将传入的template转换成对应的AST、render函数以及staticRenderFns函数。而compileToFunctions则是带缓存的编译器,同时staticRenderFns以及render函数会被转换成Funtion对象。

因为不同平台有一些不同的options,所以createCompiler会根据平台区分传入一个baseOptions,会与compile本身传入的options合并得到最终的finalOptions。

compileToFunctions

首先还是贴一下compileToFunctions的代码。

  1. /*带缓存的编译器,同时staticRenderFns以及render函数会被转换成Funtion对象*/
  2. function compileToFunctions (
  3. template: string,
  4. options?: CompilerOptions,
  5. vm?: Component
  6. ): CompiledFunctionResult {
  7. options = options || {}
  8. /* istanbul ignore if */
  9. if (process.env.NODE_ENV !== 'production') {
  10. // detect possible CSP restriction
  11. try {
  12. new Function('return 1')
  13. } catch (e) {
  14. if (e.toString().match(/unsafe-eval|CSP/)) {
  15. warn(
  16. 'It seems you are using the standalone build of Vue.js in an ' +
  17. 'environment with Content Security Policy that prohibits unsafe-eval. ' +
  18. 'The template compiler cannot work in this environment. Consider ' +
  19. 'relaxing the policy to allow unsafe-eval or pre-compiling your ' +
  20. 'templates into render functions.'
  21. )
  22. }
  23. }
  24. }
  25. /*Github:https://github.com/answershuto*/
  26. // check cache
  27. /*有缓存的时候直接取出缓存中的结果即可*/
  28. const key = options.delimiters
  29. ? String(options.delimiters) + template
  30. : template
  31. if (functionCompileCache[key]) {
  32. return functionCompileCache[key]
  33. }
  34. // compile
  35. /*编译*/
  36. const compiled = compile(template, options)
  37. // check compilation errors/tips
  38. if (process.env.NODE_ENV !== 'production') {
  39. if (compiled.errors && compiled.errors.length) {
  40. warn(
  41. `Error compiling template:\n\n${template}\n\n` +
  42. compiled.errors.map(e => `- ${e}`).join('\n') + '\n',
  43. vm
  44. )
  45. }
  46. if (compiled.tips && compiled.tips.length) {
  47. compiled.tips.forEach(msg => tip(msg, vm))
  48. }
  49. }
  50. // turn code into functions
  51. const res = {}
  52. const fnGenErrors = []
  53. /*将render转换成Funtion对象*/
  54. res.render = makeFunction(compiled.render, fnGenErrors)
  55. /*将staticRenderFns全部转化成Funtion对象 */
  56. const l = compiled.staticRenderFns.length
  57. res.staticRenderFns = new Array(l)
  58. for (let i = 0; i < l; i++) {
  59. res.staticRenderFns[i] = makeFunction(compiled.staticRenderFns[i], fnGenErrors)
  60. }
  61. // check function generation errors.
  62. // this should only happen if there is a bug in the compiler itself.
  63. // mostly for codegen development use
  64. /* istanbul ignore if */
  65. if (process.env.NODE_ENV !== 'production') {
  66. if ((!compiled.errors || !compiled.errors.length) && fnGenErrors.length) {
  67. warn(
  68. `Failed to generate render function:\n\n` +
  69. fnGenErrors.map(({ err, code }) => `${err.toString()} in\n\n${code}\n`).join('\n'),
  70. vm
  71. )
  72. }
  73. }
  74. /*存放在缓存中,以免每次都重新编译*/
  75. return (functionCompileCache[key] = res)
  76. }

我们可以发现,在闭包中,会有一个functionCompileCache对象作为缓存器。

  1. /*作为缓存,防止每次都重新编译*/
  2. const functionCompileCache: {
  3. [key: string]: CompiledFunctionResult;
  4. } = Object.create(null)

在进入compileToFunctions以后,会先检查缓存中是否有已经编译好的结果,如果有结果则直接从缓存中读取。这样做防止每次同样的模板都要进行重复的编译工作。

  1. // check cache
  2. /*有缓存的时候直接取出缓存中的结果即可*/
  3. const key = options.delimiters
  4. ? String(options.delimiters) + template
  5. : template
  6. if (functionCompileCache[key]) {
  7. return functionCompileCache[key]
  8. }

在compileToFunctions的末尾会将编译结果进行缓存

  1. /*存放在缓存中,以免每次都重新编译*/
  2. return (functionCompileCache[key] = res)

compile

  1. /*编译,将模板template编译成AST、render函数以及staticRenderFns函数*/
  2. function compile (
  3. template: string,
  4. options?: CompilerOptions
  5. ): CompiledResult {
  6. const finalOptions = Object.create(baseOptions)
  7. const errors = []
  8. const tips = []
  9. finalOptions.warn = (msg, tip) => {
  10. (tip ? tips : errors).push(msg)
  11. }
  12. /*做下面这些merge的目的因为不同平台可以提供自己本身平台的一个baseOptions,内部封装了平台自己的实现,然后把共同的部分抽离开来放在这层compiler中,所以在这里需要merge一下*/
  13. if (options) {
  14. // merge custom modules
  15. /*合并modules*/
  16. if (options.modules) {
  17. finalOptions.modules = (baseOptions.modules || []).concat(options.modules)
  18. }
  19. // merge custom directives
  20. if (options.directives) {
  21. /*合并directives*/
  22. finalOptions.directives = extend(
  23. Object.create(baseOptions.directives),
  24. options.directives
  25. )
  26. }
  27. // copy other options
  28. for (const key in options) {
  29. /*合并其余的options,modules与directives已经在上面做了特殊处理了*/
  30. if (key !== 'modules' && key !== 'directives') {
  31. finalOptions[key] = options[key]
  32. }
  33. }
  34. }
  35. /*基础模板编译,得到编译结果*/
  36. const compiled = baseCompile(template, finalOptions)
  37. if (process.env.NODE_ENV !== 'production') {
  38. errors.push.apply(errors, detectErrors(compiled.ast))
  39. }
  40. compiled.errors = errors
  41. compiled.tips = tips
  42. return compiled
  43. }

compile主要做了两件事,一件是合并option(前面说的将平台自有的option与传入的option进行合并),另一件是baseCompile,进行模板template的编译。

来看一下baseCompile

baseCompile

  1. function baseCompile (
  2. template: string,
  3. options: CompilerOptions
  4. ): CompiledResult {
  5. /*parse解析得到AST*/
  6. const ast = parse(template.trim(), options)
  7. /*
  8. 将AST进行优化
  9. 优化的目标:生成模板AST,检测不需要进行DOM改变的静态子树。
  10. 一旦检测到这些静态树,我们就能做以下这些事情:
  11. 1.把它们变成常数,这样我们就再也不需要每次重新渲染时创建新的节点了。
  12. 2.在patch的过程中直接跳过。
  13. */
  14. optimize(ast, options)
  15. /*根据AST生成所需的code(内部包含render与staticRenderFns)*/
  16. const code = generate(ast, options)
  17. return {
  18. ast,
  19. render: code.render,
  20. staticRenderFns: code.staticRenderFns
  21. }
  22. }

baseCompile首先会将模板template进行parse得到一个AST,再通过optimize做一些优化,最后通过generate得到render以及staticRenderFns。

parse

parse的源码可以参见https://github.com/answershuto/learnVue/blob/master/vue-src/compiler/parser/index.js#L53

parse会用正则等方式解析template模板中的指令、class、style等数据,形成AST。

optimize

optimize的主要作用是标记static静态节点,这是Vue在编译过程中的一处优化,后面当update更新界面时,会有一个patch的过程,diff算法会直接跳过静态节点,从而减少了比较的过程,优化了patch的性能。

generate

generate是将AST转化成render funtion字符串的过程,得到结果是render的字符串以及staticRenderFns字符串。


至此,我们的template模板已经被转化成了我们所需的AST、render function字符串以及staticRenderFns字符串。

举个例子

来看一下这段代码的编译结果

  1. <div class="main" :class="bindClass">
  2. <div>{{text}}</div>
  3. <div>hello world</div>
  4. <div v-for="(item, index) in arr">
  5. <p>{{item.name}}</p>
  6. <p>{{item.value}}</p>
  7. <p>{{index}}</p>
  8. <p>---</p>
  9. </div>
  10. <div v-if="text">
  11. {{text}}
  12. </div>
  13. <div v-else></div>
  14. </div>

转化后得到AST,如下图:

聊聊Vue的template编译 - 图1

我们可以看到最外层的div是这颗AST的根节点,节点上有许多数据代表这个节点的形态,比如static表示是否是静态节点,staticClass表示静态class属性(非bind:class)。children代表该节点的子节点,可以看到children是一个长度为4的数组,里面包含的是该节点下的四个div子节点。children里面的节点与父节点的结构类似,层层往下形成一棵AST。

再来看看由AST得到的render函数

  1. with(this){
  2. return _c( 'div',
  3. {
  4. /*static class*/
  5. staticClass:"main",
  6. /*bind class*/
  7. class:bindClass
  8. },
  9. [
  10. _c( 'div', [_v(_s(text))]),
  11. _c('div',[_v("hello world")]),
  12. /*这是一个v-for循环*/
  13. _l(
  14. (arr),
  15. function(item,index){
  16. return _c( 'div',
  17. [_c('p',[_v(_s(item.name))]),
  18. _c('p',[_v(_s(item.value))]),
  19. _c('p',[_v(_s(index))]),
  20. _c('p',[_v("---")])]
  21. )
  22. }
  23. ),
  24. /*这是v-if*/
  25. (text)?_c('div',[_v(_s(text))]):_c('div',[_v("no text")])],
  26. 2
  27. )
  28. }

_c,_v,_s,_q

看了render function字符串,发现有大量的_c,_v,_s,_q,这些函数究竟是什么?

带着问题,我们来看一下core/instance/render

  1. /*处理v-once的渲染函数*/
  2. Vue.prototype._o = markOnce
  3. /*将字符串转化为数字,如果转换失败会返回原字符串*/
  4. Vue.prototype._n = toNumber
  5. /*将val转化成字符串*/
  6. Vue.prototype._s = toString
  7. /*处理v-for列表渲染*/
  8. Vue.prototype._l = renderList
  9. /*处理slot的渲染*/
  10. Vue.prototype._t = renderSlot
  11. /*检测两个变量是否相等*/
  12. Vue.prototype._q = looseEqual
  13. /*检测arr数组中是否包含与val变量相等的项*/
  14. Vue.prototype._i = looseIndexOf
  15. /*处理static树的渲染*/
  16. Vue.prototype._m = renderStatic
  17. /*处理filters*/
  18. Vue.prototype._f = resolveFilter
  19. /*从config配置中检查eventKeyCode是否存在*/
  20. Vue.prototype._k = checkKeyCodes
  21. /*合并v-bind指令到VNode中*/
  22. Vue.prototype._b = bindObjectProps
  23. /*创建一个文本节点*/
  24. Vue.prototype._v = createTextVNode
  25. /*创建一个空VNode节点*/
  26. Vue.prototype._e = createEmptyVNode
  27. /*处理ScopedSlots*/
  28. Vue.prototype._u = resolveScopedSlots
  29. /*创建VNode节点*/
  30. vm._c = (a, b, c, d) => createElement(vm, a, b, c, d, false)

通过这些函数,render函数最后会返回一个VNode节点,在_update的时候,经过patch与之前的VNode节点进行比较,得出差异后将这些差异渲染到真实的DOM上。