Skip to content

Commit 54c29ab

Browse files
committed
wip: vapor hmr reload
1 parent 366dcb7 commit 54c29ab

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

packages/runtime-core/src/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ export interface GenericComponentInstance {
491491
/**
492492
* @internal vapor only
493493
*/
494-
hmrReload?: () => void
494+
hmrReload?: (newComp: any) => void
495495
}
496496

497497
/**

packages/runtime-core/src/componentCurrentInstance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export const unsetCurrentInstance = (): void => {
9696
*/
9797
export const simpleSetCurrentInstance = (
9898
i: GenericComponentInstance | null,
99-
unset?: GenericComponentInstance,
99+
unset?: GenericComponentInstance | null,
100100
): void => {
101101
currentInstance = i
102102
if (unset) {

packages/runtime-core/src/hmr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function reload(id: string, newComp: HMRComponent): void {
119119

120120
if (newComp.vapor) {
121121
for (const instance of instances) {
122-
instance.hmrReload!()
122+
instance.hmrReload!(newComp)
123123
}
124124
} else {
125125
for (const instance of instances as ComponentInternalInstance[]) {

packages/runtime-vapor/src/component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export function createComponent(
150150
// HMR
151151
if (component.__hmrId) {
152152
registerHMR(instance)
153+
instance.isSingleRoot = isSingleRoot
153154
instance.hmrRerender = hmrRerender.bind(null, instance)
154155
instance.hmrReload = hmrReload.bind(null, instance)
155156
}
@@ -260,9 +261,10 @@ export class VaporComponentInstance implements GenericComponentInstance {
260261
setupState?: Record<string, any>
261262
devtoolsRawSetupState?: any
262263
hmrRerender?: () => void
263-
hmrReload?: () => void
264+
hmrReload?: (newComp: VaporComponent) => void
264265
propsOptions?: NormalizedPropsOptions
265266
emitsOptions?: ObjectEmitsOptions | null
267+
isSingleRoot?: boolean
266268

267269
constructor(
268270
comp: VaporComponent,

packages/runtime-vapor/src/hmr.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import {
55
simpleSetCurrentInstance,
66
} from '@vue/runtime-core'
77
import { normalizeBlock } from './block'
8-
import { type VaporComponentInstance, devRender } from './component'
8+
import {
9+
type VaporComponent,
10+
type VaporComponentInstance,
11+
createComponent,
12+
devRender,
13+
mountComponent,
14+
unmountComponent,
15+
} from './component'
916
import { insert, remove } from './dom/node'
1017

1118
export function hmrRerender(instance: VaporComponentInstance): void {
@@ -22,7 +29,22 @@ export function hmrRerender(instance: VaporComponentInstance): void {
2229
insert(instance.block, parent, anchor)
2330
}
2431

25-
export function hmrReload(instance: VaporComponentInstance): void {
26-
// in parent block, find the corresponding block of this instance
27-
// create new instance, replace
32+
export function hmrReload(
33+
instance: VaporComponentInstance,
34+
newComp: VaporComponent,
35+
): void {
36+
const normalized = normalizeBlock(instance.block)
37+
const parent = normalized[0].parentNode!
38+
const anchor = normalized[normalized.length - 1].nextSibling
39+
unmountComponent(instance, parent)
40+
const prev = currentInstance
41+
simpleSetCurrentInstance(instance.parent)
42+
const newInstance = createComponent(
43+
newComp,
44+
instance.rawProps,
45+
instance.rawSlots,
46+
instance.isSingleRoot,
47+
)
48+
simpleSetCurrentInstance(prev, instance.parent)
49+
mountComponent(newInstance, parent, anchor)
2850
}

0 commit comments

Comments
 (0)