實例講解vue源碼架構

在本篇文章中小編給大家分享了關於vue源碼架構的相關知識點內容,有需要的朋友們學習下。

下載

去github上下載Vue https://github.com/vuejs/vue

npm install 
npm run dev

運行起來

rollup + flow

vue使用使用rollup打包,flow規範數據類型

rollup可以先用webpack套用,讀起來差不多,時間有限,畢竟只有5分鐘,這個就不用去看rollup文檔了

入口

打開package.json

我們看scripts配置

"dev": "rollup -w -c scripts/config.js --environment TARGET:web-full-dev",
 "dev:cjs": "rollup -w -c scripts/config.js --environment TARGET:web-runtime-cjs-dev",

找到scripts/config.js

打開

根據配置TARGET的不同會選擇不同的config

同時在這裏配置了process.env.NODE_ENV 環境

TARGET有CommonJS,ES Modules,UMD關於js引入類型的

還有weex,ssr

'web-runtime-cjs-dev': {
  entry: resolve('web/entry-runtime.js'),
  dest: resolve('dist/vue.runtime.common.dev.js'),
  format: 'cjs',
  env: 'development',
  banner
 }

在alias.js下設置了別名路徑

我們先介紹src/platforms

裏面有web和weex 分別的web和weex入口

在web文件下是CommonJS,ES Modules,UMD關於js引入類型,server的打包入口

打開web/entry-runtime.js

引入

import Vue from './runtime/index'
export default Vue

打開./runtime/index

import Vue from 'core/index'
 
Vue.prototype.$mount = function (
 el?: string | Element,
 hydrating?: boolean
): Component {
 el = el && inBrowser ? query(el) : undefined
 return mountComponent(this, el, hydrating)
}
export default Vue

在vue原型上添加了mount方法

處理了devtools,沒有安裝提醒安裝devtools

給了這句提示dev環境提示

You are running Vue in development mode.
Make sure to turn on production mode when deploying for production.
See more tips at https://vuejs.org/guide/deployment.html

platforms目錄夾講解完畢

core目錄

打開core/instance/index

映入眼前的是

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(Vue)
stateMixin(Vue)
eventsMixin(Vue)
lifecycleMixin(Vue)
renderMixin(Vue)
 
export default Vue

先執行的是initMixin(Vue)

打開init

export function initMixin (Vue) {
 Vue.prototype._init = function (options?: Object) {
  const vm = this
  // a uid 
  vm._uid = uid++
   
  let startTag, endTag
  /* istanbul ignore if */
  if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
   startTag = `vue-perf-start:${vm._uid}`
   endTag = `vue-perf-end:${vm._uid}`
   mark(startTag)
  }
 
  // a flag to avoid this being observed
  vm._isVue = true
  // 處理傳入的options
  // merge options
  if (options && options._isComponent) {
   // optimize internal component instantiation
   // since dynamic options merging is pretty slow, and none of the
   // internal component options needs special treatment.
   initInternalComponent(vm, options)
  } else {
    // 傳入的options,默認的options一起合併掛載到vm.$options上
   vm.$options = mergeOptions(
    resolveConstructorOptions(vm.constructor),
    options || {},
    vm
   )
  }
  /* istanbul ignore else */
  if (process.env.NODE_ENV !== 'production') {
   // 代理
   initProxy(vm)
  } else {
   vm._renderProxy = vm
  }
  // 生命週期
  initLifecycle(vm)
   // emit on 事件
  initEvents(vm)
  // 處理render vdom
  initRender(vm)
  callHook(vm, 'beforeCreate')
  // 處理Injections
  initInjections(vm) // resolve injections before data/props
  // 雙向數據綁定,監聽訂閱
  initState(vm)
  initProvide(vm) // resolve provide after data/props
  callHook(vm, 'created')
   
  /* istanbul ignore if */
  if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
   vm._name = formatComponentName(vm, false)
   mark(endTag)
   measure(`vue ${vm._name} init`, startTag, endTag)
  }
  // 渲染到dom
  if (vm.$options.el) {
   vm.$mount(vm.$options.el)
  }
 }
}

lifecycle

打開 lifecycle

export function callHook (vm: Component, hook: string) {
 // disable dep collection when invoking lifecycle hooks
 pushTarget()
 //執行對象的周期函數,周期函數最後被處理成數組
 const handlers = vm.$options[hook]
 const info = `${hook} hook`
 if (handlers) {
  for (let i = 0, j = handlers.length; i < j; i++) {
   invokeWithErrorHandling(handlers[i], vm, null, vm, info)
  }
 }
 if (vm._hasHookEvent) {
  vm.$emit('hook:' + hook)
 }
 popTarget()

callHook 的時候,是執行相應週期,開發者在周期函數裏所寫的

Events

initEvents實現了 emit on 等方法,請參考監聽者訂閱者模式,這裏不詳解

render
renderMixin函數
添加了 $nextTick _render 原型對象

$nextTick會在dom跟新後立即調用

nextTick(fn, this)是一個自執行函數

_render返回的是node的js數據,還不是dom

做了Vdom

initRender函數
給vm添加了_c和 $createElement用來渲染的方法

state

if (!(key in vm)) {
   proxy(vm, `_props`, key)
  }

給vue屬性做代理,訪問this.a可以得到this.data.a 的值

export function initState (vm: Component) {
 vm._watchers = []
 const opts = vm.$options
 if (opts.props) initProps(vm, opts.props)
 if (opts.methods) initMethods(vm, opts.methods)
 if (opts.data) {
  initData(vm)
 } else {
  observe(vm._data = {}, true /* asRootData */)
 }
 if (opts.computed) initComputed(vm, opts.computed)
 if (opts.watch && opts.watch !== nativeWatch) {
  initWatch(vm, opts.watch)
 }
}

給數據做監聽

stateMixin函數

添加原型對象

Vue.prototype.$set = set
Vue.prototype.$delete = del

其他

src/compiler 做了編譯處理

core/componetd 做了keep-alive

core/util 封裝了通用方法

core/vdom vdom算法

以上整體架構分析完畢

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章