vue2.0源码解析(一)

1、先下载vue源码(当前版本为:2.6.11)

    地址:

git clone https://github.com/vuejs/vue.git

2.切换到package.json

dev脚本中 -c scripts/config.js 指明配置⽂件所在,参数为:TARGET:web-full-dev

3、根据命令变量在scripts/config.js中找到如下代码

'web-full-dev': {
    entry: resolve('web/entry-runtime-with-compiler.js'),
    dest: resolve('dist/vue.js'),
    format: 'umd',
    env: 'development',
    alias: { he: './entity-decoder' },
    banner
  },

根据resolve处的代码结合:找到入口

const aliases = require('./alias')
const resolve = p => {
  const base = p.split('/')[0]
  if (aliases[base]) {
    return path.resolve(aliases[base], p.slice(base.length + 1))
  } else {
    return path.resolve(__dirname, '../', p)
  }
}
const path = require('path')

const resolve = p => path.resolve(__dirname, '../', p)

module.exports = {
  vue: resolve('src/platforms/web/entry-runtime-with-compiler'),
  compiler: resolve('src/compiler'),
  core: resolve('src/core'),
  shared: resolve('src/shared'),
  web: resolve('src/platforms/web'),
  weex: resolve('src/platforms/weex'),
  server: resolve('src/server'),
  sfc: resolve('src/sfc')
}

最终确定入口为:platforms/web/entry-runtime-with-compiler.js

platforms/web/entry-runtime-with-compiler.js该文件在Vue原型上面挂载了mount以及判断了el,如下所示:

// 对mount方法进行保存
const mount = Vue.prototype.$mount
// 原型上面挂载$mount方法
Vue.prototype.$mount = function (
  el?: string | Element,
  hydrating?: boolean
): Component {
  el = el && query(el)

  // 对el进行判断处理,不能是body
  /* istanbul ignore if */
  if (el === document.body || el === document.documentElement) {
    process.env.NODE_ENV !== 'production' && warn(
      `Do not mount Vue to  or  - mount to normal elements instead.`
    )
    return this
  }

  // 对传递进来的options进行保存
  const options = this.$options
  // resolve template/el and convert to render function
  if (!options.render) {
    let template = options.template
    if (template) {
      if (typeof template === 'string') {
        if (template.charAt(0) === '#') {
          template = idToTemplate(template)
          /* istanbul ignore if */
          if (process.env.NODE_ENV !== 'production' && !template) {
            warn(
              `Template element not found or is empty: ${options.template}`,
              this
            )
          }
        }
      } else if (template.nodeType) {
        template = template.innerHTML
      } else {
        if (process.env.NODE_ENV !== 'production') {
          warn('invalid template option:' + template, this)
        }
        return this
      }
    } else if (el) {
      template = getOuterHTML(el)
    }
    if (template) {
      /* istanbul ignore if */
      if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
        mark('compile')
      }

      const { render, staticRenderFns } = compileToFunctions(template, {
        outputSourceRange: process.env.NODE_ENV !== 'production',
        shouldDecodeNewlines,
        shouldDecodeNewlinesForHref,
        delimiters: options.delimiters,
        comments: options.comments
      }, this)
      options.render = render
      options.staticRenderFns = staticRenderFns

      /* istanbul ignore if */
      if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
        mark('compile end')
        measure(`vue ${this._name} compile`, 'compile', 'compile end')
      }
    }
  }
  return mount.call(this, el, hydrating)
}

/**
 * Get outerHTML of elements, taking care
 * of SVG elements in IE as well.
 */
function getOuterHTML (el: Element): string {
  if (el.outerHTML) {
    return el.outerHTML
  } else {
    const container = document.createElement('div')
    container.appendChild(el.cloneNode(true))
    return container.innerHTML
  }
}

4、根据import Vue from './runtime/index',再次深入找Vue实例对象,src\platforms\web\runtime\index.js,这个文件里面并没有Vue实例对象,这这个文件处理_patch_以及$mount两个方法,如下所示:

Vue.prototype.__patch__ = inBrowser ? patch : noop

// public mount method
Vue.prototype.$mount = function (
  el?: string | Element,
  hydrating?: boolean
): Component {
  el = el && inBrowser ? query(el) : undefined
  return mountComponent(this, el, hydrating)
}

5、再找到:src\core\index.js,这个文件里面处理响应:Object.defineProperty

6、再找到:src\core\instance\index.js,这个文件里面有Vue实例对象,以及在实例对象上面挂载了不同的方法,如下所示:

import { initMixin } from './init'
import { stateMixin } from './state'
import { renderMixin } from './render'
import { eventsMixin } from './events'
import { lifecycleMixin } from './lifecycle'
import { warn } from '../util/index'

// Vue实例对象
function Vue (options) {
  if (process.env.NODE_ENV !== 'production' &&
    !(this instanceof Vue)
  ) {
    warn('Vue is a constructor and should be called with the `new` keyword')
  }
  this._init(options)
}

/**
 * initMixin:
    * 实现_init() core/instance/init.js
      initLifecycle
      initEvents
      initRender
      beforeCreate
      initInjections
      initState
      initProvide
      created
 */
initMixin(Vue)
/**
 * stateMixin: 
 *   $data
    * $props
    * $set
    * $delete
    * $watch
 */
stateMixin(Vue)
/**
 * eventsMixin:
 *   $on,$emit,$once,$off
 */
eventsMixin(Vue)
/**
 * lifecycleMixin:
 *    _update,$forceUpdate,$destroy
 */
lifecycleMixin(Vue)
/**
 * renderMixin:
 *   $nextTick,_render
 */
renderMixin(Vue)

export default Vue

主线流程梳理完成:

   vue2.0源码解析(一)_第1张图片

 

 

你可能感兴趣的:(vue,vue.js)