From f264ceb89230f89a25de39d160341bb57364dea7 Mon Sep 17 00:00:00 2001 From: zhiyuanzmj <260480378@qq.com> Date: Tue, 25 Mar 2025 10:40:01 +0800 Subject: [PATCH] feat(runtime-vapor): support HMR for setup --- packages/runtime-vapor/src/component.ts | 55 ++++++++++++++++--------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/packages/runtime-vapor/src/component.ts b/packages/runtime-vapor/src/component.ts index 548babebf..9d1beb7fb 100644 --- a/packages/runtime-vapor/src/component.ts +++ b/packages/runtime-vapor/src/component.ts @@ -182,6 +182,14 @@ export function createComponent( appContext, ) + // HMR + if (__DEV__ && component.__hmrId) { + registerHMR(instance) + instance.isSingleRoot = isSingleRoot + instance.hmrRerender = hmrRerender.bind(null, instance) + instance.hmrReload = hmrReload.bind(null, instance) + } + if (__DEV__) { pushWarningContext(instance) startMeasure(instance, `init`) @@ -221,14 +229,6 @@ export function createComponent( // TODO make the proxy warn non-existent property access during dev instance.setupState = proxyRefs(setupResult) devRender(instance) - - // HMR - if (component.__hmrId) { - registerHMR(instance) - instance.isSingleRoot = isSingleRoot - instance.hmrRerender = hmrRerender.bind(null, instance) - instance.hmrReload = hmrReload.bind(null, instance) - } } } else { // component has a render function but no setup function @@ -283,18 +283,33 @@ export let isApplyingFallthroughProps = false */ export function devRender(instance: VaporComponentInstance): void { instance.block = - callWithErrorHandling( - instance.type.render!, - instance, - ErrorCodes.RENDER_FUNCTION, - [ - instance.setupState, - instance.props, - instance.emit, - instance.attrs, - instance.slots, - ], - ) || [] + (instance.type.render + ? callWithErrorHandling( + instance.type.render, + instance, + ErrorCodes.RENDER_FUNCTION, + [ + instance.setupState, + instance.props, + instance.emit, + instance.attrs, + instance.slots, + ], + ) + : callWithErrorHandling( + isFunction(instance.type) ? instance.type : instance.type.setup!, + instance, + ErrorCodes.SETUP_FUNCTION, + [ + instance.props, + { + slots: instance.slots, + attrs: instance.attrs, + emit: instance.emit, + expose: instance.expose, + }, + ], + )) || [] } const emptyContext: GenericAppContext = {