// HMR
if (component.__hmrId) {
registerHMR(instance)
+ instance.isSingleRoot = isSingleRoot
instance.hmrRerender = hmrRerender.bind(null, instance)
instance.hmrReload = hmrReload.bind(null, instance)
}
setupState?: Record<string, any>
devtoolsRawSetupState?: any
hmrRerender?: () => void
- hmrReload?: () => void
+ hmrReload?: (newComp: VaporComponent) => void
propsOptions?: NormalizedPropsOptions
emitsOptions?: ObjectEmitsOptions | null
+ isSingleRoot?: boolean
constructor(
comp: VaporComponent,
simpleSetCurrentInstance,
} from '@vue/runtime-core'
import { normalizeBlock } from './block'
-import { type VaporComponentInstance, devRender } from './component'
+import {
+ type VaporComponent,
+ type VaporComponentInstance,
+ createComponent,
+ devRender,
+ mountComponent,
+ unmountComponent,
+} from './component'
import { insert, remove } from './dom/node'
export function hmrRerender(instance: VaporComponentInstance): void {
insert(instance.block, parent, anchor)
}
-export function hmrReload(instance: VaporComponentInstance): void {
- // in parent block, find the corresponding block of this instance
- // create new instance, replace
+export function hmrReload(
+ instance: VaporComponentInstance,
+ newComp: VaporComponent,
+): void {
+ const normalized = normalizeBlock(instance.block)
+ const parent = normalized[0].parentNode!
+ const anchor = normalized[normalized.length - 1].nextSibling
+ unmountComponent(instance, parent)
+ const prev = currentInstance
+ simpleSetCurrentInstance(instance.parent)
+ const newInstance = createComponent(
+ newComp,
+ instance.rawProps,
+ instance.rawSlots,
+ instance.isSingleRoot,
+ )
+ simpleSetCurrentInstance(prev, instance.parent)
+ mountComponent(newInstance, parent, anchor)
}