ホームページ >ウェブフロントエンド >jsチュートリアル >Vue ソース コード エントリ ファイルのサンプル分析
この記事では、主に Vue のソース コード エントリ ファイルの分析を紹介します (推奨)。これが非常に優れていると思うので、参考として共有します。編集者をフォローして見てみましょう。皆さんのお役に立てれば幸いです。
私はしばらく Vue プロジェクトを開発していて、以前は angularjs を使用していましたが、その時はソース コードを見て自分の考えを記録する時間がありませんでした。もう苦労して考え続けたいです! !
私自身、ソースコードを読むたびにとても幸せな気分になります。あなたも私と同じでしょうか。
vue のソース コードは、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 プロトタイプ チェーン (プロトタイプ) メソッドの集中ファイルでもあります
// _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 inpidual 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
Vue が行うことの 1 つは、異なる実行環境に応じて異なる $mount を書き換えることです
const mount = Vue.prototype.$mount
Vue.prototype.$mount = function (
el?: string | Element,
hydrating?: boolean
): Component {
...
return mount.call(this, el, hydrating)
}概要:
この時点で、実行を開始するファイルが見つかりました。各ファイルが何に使用されるか、具体的にはどのように実行するか、そして何が行われたかについては次回書きます。しかし、最初は細かいことを気にしすぎるべきではなく、コードのすべての行を理解する必要もありません。そうであれば、それは非常に疲れるでしょうし、続ける勇気がないかもしれません。
関連する推奨事項:
webpack マルチエントリファイルページのパッケージ化の詳細な説明
以上がVue ソース コード エントリ ファイルのサンプル分析の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。