@@ -12,17 +12,14 @@ import {
1212 mountComponent ,
1313 unmountComponent ,
1414} from './component'
15- import { handleTeleportRootComponentHmrReload } from './components/Teleport '
15+ import { isArray } from '@vue/shared '
1616
1717export function hmrRerender ( instance : VaporComponentInstance ) : void {
1818 const normalized = normalizeBlock ( instance . block )
1919 const parent = normalized [ 0 ] . parentNode !
2020 const anchor = normalized [ normalized . length - 1 ] . nextSibling
2121 remove ( instance . block , parent )
22- if ( instance . hmrRerenderEffects ) {
23- instance . hmrRerenderEffects . forEach ( e => e ( ) )
24- instance . hmrRerenderEffects . length = 0
25- }
22+ instance . scope . cleanup ( )
2623 const prev = setCurrentInstance ( instance )
2724 pushWarningContext ( instance )
2825 devRender ( instance )
@@ -39,7 +36,8 @@ export function hmrReload(
3936 const parent = normalized [ 0 ] . parentNode !
4037 const anchor = normalized [ normalized . length - 1 ] . nextSibling
4138 unmountComponent ( instance , parent )
42- const prev = setCurrentInstance ( instance . parent )
39+ const parentInstance = instance . parent as VaporComponentInstance | null
40+ const prev = setCurrentInstance ( parentInstance )
4341 const newInstance = createComponent (
4442 newComp ,
4543 instance . rawProps ,
@@ -48,5 +46,59 @@ export function hmrReload(
4846 )
4947 setCurrentInstance ( ...prev )
5048 mountComponent ( newInstance , parent , anchor )
51- handleTeleportRootComponentHmrReload ( instance , newInstance )
49+
50+ updateParentBlockOnHmrReload ( parentInstance , instance , newInstance )
51+ updateParentTeleportOnHmrReload ( instance , newInstance )
52+ }
53+
54+ /**
55+ * dev only
56+ * update parentInstance.block to ensure that the correct parent and
57+ * anchor are found during parentInstance HMR rerender/reload, as
58+ * `normalizeBlock` relies on the current instance.block
59+ */
60+ function updateParentBlockOnHmrReload (
61+ parentInstance : VaporComponentInstance | null ,
62+ instance : VaporComponentInstance ,
63+ newInstance : VaporComponentInstance ,
64+ ) : void {
65+ if ( parentInstance ) {
66+ if ( parentInstance . block === instance ) {
67+ parentInstance . block = newInstance
68+ } else if ( isArray ( parentInstance . block ) ) {
69+ for ( let i = 0 ; i < parentInstance . block . length ; i ++ ) {
70+ if ( parentInstance . block [ i ] === instance ) {
71+ parentInstance . block [ i ] = newInstance
72+ break
73+ }
74+ }
75+ }
76+ }
77+ }
78+
79+ /**
80+ * dev only
81+ * during root component HMR reload, since the old component will be unmounted
82+ * and a new one will be mounted, we need to update the teleport's nodes
83+ * to ensure that the correct parent and anchor are found during parentInstance
84+ * HMR rerender/reload, as `normalizeBlock` relies on the current instance.block
85+ */
86+ export function updateParentTeleportOnHmrReload (
87+ instance : VaporComponentInstance ,
88+ newInstance : VaporComponentInstance ,
89+ ) : void {
90+ const teleport = instance . parentTeleport
91+ if ( teleport ) {
92+ newInstance . parentTeleport = teleport
93+ if ( teleport . nodes === instance ) {
94+ teleport . nodes = newInstance
95+ } else if ( isArray ( teleport . nodes ) ) {
96+ for ( let i = 0 ; i < teleport . nodes . length ; i ++ ) {
97+ if ( teleport . nodes [ i ] === instance ) {
98+ teleport . nodes [ i ] = newInstance
99+ break
100+ }
101+ }
102+ }
103+ }
52104}
0 commit comments