当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue源码入口文件分析(推荐)

vue源码入口文件分析(推荐)

2018年02月09日  | 移动技术网IT编程  | 我要评论

宋仲基宋慧乔结婚几号,奥烨移民,许昌二手房网

开发vue项目有段时间了, 之前用angularjs 后来用 reactjs 但是那时候一直没有时间把自己看源码的思考记录下来,现在我不想再浪费这 来之不易的思考, 我要坚持!!

看源码我个人感觉非常开心,每每看上一段,自己就充实许多,不知道你是否和我一样。

vue 源码是众多module(模块)用 rollup 工具合并而成, 从package.json 中能够看到。现在让我们从github上下载vue项目,开始我们今天的“思考”。

我下载的源码版本是:"version": "2.5.7",

源码起始位置从这里可以看到

"dev": "rollup -w -c build/config.js --environment target:web-full-dev"

// 从build/config.js 中找到 target: web-full-dev 这是运行和编译(支持现在的浏览器,由于里面大量应用了es6-7)后的

 // runtime+compiler development build (browser)
 '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
 },

找到了开始文件就是 "web/entry-runtime-with-compiler.js", 然后我们一路找 vue 对象 终于在 “instance/index.js” 中找到了:

// 这是vue 的开始位置
function vue (options) {
 // 判断如果是不是生产环境,且不是通过new关键字来创建对象的话,就在控制台打印一个warning
 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)
}

看似到这里都结束了,因为我们目的就是找到开始位置,但是我有个疑问,为什么vue需要这么多层 ?

entry-runtime-with-compiler.js
->
runtime/index.js
->
core/index.js
->
instance/index.js

当我仔细看了源码后恍然大悟,我们先看看他们这些文件都做了什么:

(1)instance/index.js

从vue 模块命名中能看出一些端倪, instance (实例) 。

这个文件是vue 对象的开始,同时也是vue 原型链(prototype) 方法的集中文件

// _init
initmixin(vue)
// $set、$delete、$watch
statemixin(vue)
// $on、$once、$off、$emit
eventsmixin(vue)
// _update、$forceupdate、$destroy
lifecyclemixin(vue)
// $nexttick、_render、以及多个内部调用的方法
rendermixin(vue)

这些方法只有实例化了才能调用。

(2)core/index.js

这个文件在instance/index.js 创建和初步加工后,再次加工。 那他主要做了什么呢? 我们不考虑运行环境

initglobalapi(vue)

对,就调用了这个方法,很简单明了吧 --- "初始化全局接口",

让我们走进initglobalapi 方法

export function initglobalapi (vue: globalapi) {
 // config
 const configdef = {}
 configdef.get = () => config
 // 在 非生产环境,如何修改了配置文件config里面的内容会提示警告
 if (process.env.node_env !== 'production') {
  configdef.set = () => {
   warn(
    'do not replace the vue.config object, set individual fields instead.'
   )
  }
 }
 // 定义config 属性, 监听变化
 object.defineproperty(vue, 'config', configdef)

 // exposed util methods.
 // note: these are not considered part of the public api - avoid relying on
 // them unless you are aware of the risk.
 vue.util = {
  warn,
  extend,
  mergeoptions,
  definereactive
 }

 vue.set = set
 vue.delete = del
 vue.nexttick = nexttick

 vue.options = object.create(null)
 // 给vue 创建 asset_types 的 空对象
 asset_types.foreach(type => {
  vue.options[type + 's'] = object.create(null)
 })

 // this is used to identify the "base" constructor to extend all plain-object
 // components with in weex's multi-instance scenarios.
 vue.options._base = vue

 extend(vue.options.components, builtincomponents)
 // vue.use
 inituse(vue)
 // vue.mixin
 initmixin(vue)
 // vue.extend
 initextend(vue)
 // vue.component, vue.directive, vue.filter
 initassetregisters(vue)
}

这里面基本都是 静态方法,即:用 vue. xxx 的形式调用。

(3)runtime/index.js

这里就加一些扩展和 在 vue.prototype上添加了__patch__和$mount(挂载元素)。

// vue.options.directives(model和show)和 vue.options.components(transition和transitiongroup)
extend(vue.options.directives, platformdirectives)
extend(vue.options.components, platformcomponents)

// install platform patch function
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)
}

(4)entry-runtime-with-compiler.js

就干了一件事就是重写$mount, vue根据不同运行环境,重写不同$mount

const mount = vue.prototype.$mount
vue.prototype.$mount = function (
 el?: string | element,
 hydrating?: boolean
): component {
 ...
 return mount.call(this, el, hydrating)
}

总结:

到此我们找到了开始执行的文件,和每个文件有什么用,具体怎么做的,做了什么我会下次再写。不过我们刚开始不要太在乎每个细节,不要非得弄懂每一行代码,如果那样,真的太累了,而且可能没有勇气坚持下去。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网