讓ChatGPT解讀Vue3源碼過(guò)程解析
目錄
- 前言
- 實(shí)戰(zhàn)
- setup
- 小結(jié)
- 總結(jié)
前言
ChatGPT 最近十分火爆,今天我也來(lái)讓 ChatGPT 幫我閱讀一下 Vue3 的源代碼。
都知道 Vue3 組件有一個(gè) setup
函數(shù)。那么它內(nèi)部做了什么呢,今天跟隨 ChatGPT 來(lái)一探究竟。
實(shí)戰(zhàn)
setup
setup
函數(shù)在什么位置呢,我們不知道他的實(shí)現(xiàn)函數(shù)名稱,于是問(wèn)一下 ChatGPT:
ChatGPT 告訴我,setup
函數(shù)在packages/runtime-core/src/component.ts
文件中。眾所周知,runtime-core
是 Vue3 的運(yùn)行時(shí)核心代碼。我們進(jìn)去看一眼。
按照它所說(shuō)的,我們找到了 setupComponent
和 createComponentInstance
函數(shù),并沒(méi)有找到 setupRenderEffect
函數(shù),ChatGPT 的只知道 2021 年以前的知識(shí),Vue3 代碼經(jīng)過(guò)了很多變動(dòng),不過(guò)沒(méi)關(guān)系,這不影響太多。
ChatGPT 告訴我,setupComponent
函數(shù)是在createComponentInstance
函數(shù)中執(zhí)行的,createComponentInstance
看名字是創(chuàng)建組件實(shí)例,看一下詳細(xì)代碼。
直接復(fù)制給 ChatGPT:
我們根據(jù) ChatGPT 的解釋來(lái)閱讀代碼,發(fā)現(xiàn)createComponentInstance
只是創(chuàng)建了組件的實(shí)例并返回。并沒(méi)有像它上面說(shuō)的在函數(shù)中執(zhí)行了 setupComponent
,笨笨的 ChatGPT。
那就自己找一下setupComponent
是在哪里被調(diào)用的。
可以packages/runtime-core/
搜一下函數(shù)名,很快就找到了。在packages/runtime-core/src/renderer.ts
文件中的mountComponent
函數(shù)中。
mountComponent
是掛載組件的方法,前面還有一堆自定義渲染器的邏輯,不在此篇展開(kāi)。
const mountComponent: MountComponentFn = (...args) => { const instance: ComponentInternalInstance = compatMountInstance || (initialVNode.component = createComponentInstance(initialVNode,parentComponent,parentSuspense )) // ... 省略代碼 // resolve props and slots for setup context if (!(__COMPAT__ && compatMountInstance)) {// ...這里調(diào)用了setupComponent,傳入了實(shí)例,還寫(xiě)了注釋,感人 setupComponent(instance) } // setupRenderEffect 居然也在這 setupRenderEffect( instance, initialVNode, container, anchor, parentSuspense, isSVG, optimized ) }
mountComponent
函數(shù)先調(diào)用了createComponentInstance
, 返回個(gè)組件實(shí)例,又把實(shí)例當(dāng)作參數(shù)傳給了 setupComponent
。順便我們還在這發(fā)現(xiàn)了 ChatGPT 搞丟的setupRenderEffect
函數(shù),它是用來(lái)處理一些渲染副作用的。
回到 setupComponent
函數(shù),Evan 的注釋告訴我們它是處理 props 和 slots 的。
export function setupComponent( instance: ComponentInternalInstance, isSSR = false) { isInSSRComponentSetup = isSSR const { props, children } = instance.vnode const isStateful = isStatefulComponent(instance) initProps(instance, props, isStateful, isSSR) initSlots(instance, children) const setupResult = isStateful ? setupStatefulComponent(instance, isSSR) : undefined isInSSRComponentSetup = false return setupResult}
把代碼喂給 ChatGPT:
setupComponent
函數(shù)中,處理完 props 和 slots 后,根據(jù)是否是有狀態(tài)組件調(diào)用了setupStatefulComponent
。
直接整個(gè) setupStatefulComponent
喂給 ChatGPT:
太長(zhǎng)了,大概意思:
- 創(chuàng)建了代理緩存accessCache,干嘛用的咱也不知道,可以問(wèn) ChatGPT
- 創(chuàng)建公共實(shí)例代理對(duì)象(proxy)
- 執(zhí)行組件的 setup()
后續(xù)操作是調(diào)用 handleSetupResult
和 finishComponentSetup
返回渲染函數(shù)。開(kāi)始走渲染邏輯了。
小結(jié)
小結(jié)一下setup
的始末:
- 從組件掛載開(kāi)始調(diào)用
createComponentInstance
創(chuàng)建組件實(shí)例 - 傳遞組件實(shí)例給
setupComponent
setupComponent
內(nèi)部初始化 props 和 slotssetupStatefulComponent
執(zhí)行組件的setup
- 完成 setup 流程
- 返回渲染函數(shù)
- ...
總結(jié)
ChatGPT 很強(qiáng)大,也很笨,畢竟它不聯(lián)網(wǎng),且只有 2021 年以前的數(shù)據(jù)。可用來(lái)幫助我們讀一下晦澀的源碼還是可以的,但也只能輔助作用,還需要自己的思考。
以上就是讓ChatGPT解讀Vue3源碼過(guò)程解析的詳細(xì)內(nèi)容,更多關(guān)于ChatGPT讀Vue3源碼的資料請(qǐng)關(guān)注其它相關(guān)文章!
