wip: vapor hmr reload

This commit is contained in:
Evan You 2024-12-08 23:37:40 +08:00
parent 366dcb7c76
commit 54c29aba9a
No known key found for this signature in database
GPG Key ID: 00E9AB7A6704CE0A
5 changed files with 32 additions and 8 deletions

View File

@ -491,7 +491,7 @@ export interface GenericComponentInstance {
/** /**
* @internal vapor only * @internal vapor only
*/ */
hmrReload?: () => void hmrReload?: (newComp: any) => void
} }
/** /**

View File

@ -96,7 +96,7 @@ export const unsetCurrentInstance = (): void => {
*/ */
export const simpleSetCurrentInstance = ( export const simpleSetCurrentInstance = (
i: GenericComponentInstance | null, i: GenericComponentInstance | null,
unset?: GenericComponentInstance, unset?: GenericComponentInstance | null,
): void => { ): void => {
currentInstance = i currentInstance = i
if (unset) { if (unset) {

View File

@ -119,7 +119,7 @@ function reload(id: string, newComp: HMRComponent): void {
if (newComp.vapor) { if (newComp.vapor) {
for (const instance of instances) { for (const instance of instances) {
instance.hmrReload!() instance.hmrReload!(newComp)
} }
} else { } else {
for (const instance of instances as ComponentInternalInstance[]) { for (const instance of instances as ComponentInternalInstance[]) {

View File

@ -150,6 +150,7 @@ export function createComponent(
// HMR // HMR
if (component.__hmrId) { if (component.__hmrId) {
registerHMR(instance) registerHMR(instance)
instance.isSingleRoot = isSingleRoot
instance.hmrRerender = hmrRerender.bind(null, instance) instance.hmrRerender = hmrRerender.bind(null, instance)
instance.hmrReload = hmrReload.bind(null, instance) instance.hmrReload = hmrReload.bind(null, instance)
} }
@ -260,9 +261,10 @@ export class VaporComponentInstance implements GenericComponentInstance {
setupState?: Record<string, any> setupState?: Record<string, any>
devtoolsRawSetupState?: any devtoolsRawSetupState?: any
hmrRerender?: () => void hmrRerender?: () => void
hmrReload?: () => void hmrReload?: (newComp: VaporComponent) => void
propsOptions?: NormalizedPropsOptions propsOptions?: NormalizedPropsOptions
emitsOptions?: ObjectEmitsOptions | null emitsOptions?: ObjectEmitsOptions | null
isSingleRoot?: boolean
constructor( constructor(
comp: VaporComponent, comp: VaporComponent,

View File

@ -5,7 +5,14 @@ import {
simpleSetCurrentInstance, simpleSetCurrentInstance,
} from '@vue/runtime-core' } from '@vue/runtime-core'
import { normalizeBlock } from './block' 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' import { insert, remove } from './dom/node'
export function hmrRerender(instance: VaporComponentInstance): void { export function hmrRerender(instance: VaporComponentInstance): void {
@ -22,7 +29,22 @@ export function hmrRerender(instance: VaporComponentInstance): void {
insert(instance.block, parent, anchor) insert(instance.block, parent, anchor)
} }
export function hmrReload(instance: VaporComponentInstance): void { export function hmrReload(
// in parent block, find the corresponding block of this instance instance: VaporComponentInstance,
// create new instance, replace 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)
} }