溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue源碼入口文件分析(推薦)

發(fā)布時(shí)間:2020-08-20 22:16:08 來(lái)源:腳本之家 閱讀:231 作者:zhanglearning 欄目:web開(kāi)發(fā)

開(kāi)發(fā)vue項(xiàng)目有段時(shí)間了, 之前用angularjs 后來(lái)用 reactjs 但是那時(shí)候一直沒(méi)有時(shí)間把自己看源碼的思考記錄下來(lái),現(xiàn)在我不想再浪費(fèi)這 來(lái)之不易的思考, 我要堅(jiān)持??!

看源碼我個(gè)人感覺(jué)非常開(kāi)心,每每看上一段,自己就充實(shí)許多,不知道你是否和我一樣。

vue 源碼是眾多module(模塊)用 rollup 工具合并而成, 從package.json 中能夠看到?,F(xiàn)在讓我們從github上下載vue項(xiàng)目,開(kāi)始我們今天的“思考”。

我下載的源碼版本是:"version": "2.5.7",

源碼起始位置從這里可以看到

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

// 從build/config.js 中找到 TARGET: web-full-dev 這是運(yùn)行和編譯(支持現(xiàn)在的瀏覽器,由于里面大量應(yīng)用了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
 },

找到了開(kāi)始文件就是 "web/entry-runtime-with-compiler.js", 然后我們一路找 Vue 對(duì)象 終于在 “instance/index.js” 中找到了:

// 這是Vue 的開(kāi)始位置
function Vue (options) {
 // 判斷如果是不是生產(chǎn)環(huán)境,且不是通過(guò)new關(guān)鍵字來(lái)創(chuàng)建對(duì)象的話,就在控制臺(tái)打印一個(gè)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)
}

看似到這里都結(jié)束了,因?yàn)槲覀兡康木褪钦业介_(kāi)始位置,但是我有個(gè)疑問(wèn),為什么Vue需要這么多層 ?

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

當(dāng)我仔細(xì)看了源碼后恍然大悟,我們先看看他們這些文件都做了什么:

(1)instance/index.js

從Vue 模塊命名中能看出一些端倪, instance (實(shí)例) 。

這個(gè)文件是Vue 對(duì)象的開(kāi)始,同時(shí)也是Vue 原型鏈(prototype) 方法的集中文件

// _init
initMixin(Vue)
// $set、$delete、$watch
stateMixin(Vue)
// $on、$once、$off、$emit
eventsMixin(Vue)
// _update、$forceUpdate、$destroy
lifecycleMixin(Vue)
// $nextTick、_render、以及多個(gè)內(nèi)部調(diào)用的方法
renderMixin(Vue)

這些方法只有實(shí)例化了才能調(diào)用。

(2)core/index.js

這個(gè)文件在Instance/index.js 創(chuàng)建和初步加工后,再次加工。 那他主要做了什么呢? 我們不考慮運(yùn)行環(huán)境

initGlobalAPI(Vue)

對(duì),就調(diào)用了這個(gè)方法,很簡(jiǎn)單明了吧 --- "初始化全局接口",

讓我們走進(jìn)initGlobalAPI 方法

export function initGlobalAPI (Vue: GlobalAPI) {
 // config
 const configDef = {}
 configDef.get = () => config
 // 在 非生產(chǎn)環(huán)境,如何修改了配置文件config里面的內(nèi)容會(huì)提示警告
 if (process.env.NODE_ENV !== 'production') {
  configDef.set = () => {
   warn(
    'Do not replace the Vue.config object, set individual fields instead.'
   )
  }
 }
 // 定義config 屬性, 監(jiān)聽(tīng)變化
 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 創(chuàng)建 ASSET_TYPES 的 空對(duì)象
 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)
}

這里面基本都是 靜態(tài)方法,即:用 Vue. xxx 的形式調(diào)用。

(3)runtime/index.js

這里就加一些擴(kuò)展和 在 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根據(jù)不同運(yùn)行環(huán)境,重寫不同$mount

const mount = Vue.prototype.$mount
Vue.prototype.$mount = function (
 el?: string | Element,
 hydrating?: boolean
): Component {
 ...
 return mount.call(this, el, hydrating)
}

總結(jié):

到此我們找到了開(kāi)始執(zhí)行的文件,和每個(gè)文件有什么用,具體怎么做的,做了什么我會(huì)下次再寫。不過(guò)我們剛開(kāi)始不要太在乎每個(gè)細(xì)節(jié),不要非得弄懂每一行代碼,如果那樣,真的太累了,而且可能沒(méi)有勇氣堅(jiān)持下去。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI