Skip to content

Commit bff4ea7

Browse files
committedJul 22, 2021
fix(hmr): fix custom elements hmr edge cases
1 parent 5b76843 commit bff4ea7

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed
 

‎packages/runtime-core/src/component.ts

+4
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ export interface ComponentInternalInstance {
290290
* is custom element?
291291
*/
292292
isCE?: boolean
293+
/**
294+
* custom element specific HMR method
295+
*/
296+
ceReload?: () => void
293297

294298
// the rest are only for stateful components ---------------------------------
295299

‎packages/runtime-core/src/hmr.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ClassComponent,
88
isClassComponent
99
} from './component'
10-
import { queueJob, queuePostFlushCb } from './scheduler'
10+
import { nextTick, queueJob } from './scheduler'
1111
import { extend } from '@vue/shared'
1212
import { warn } from './warning'
1313

@@ -124,7 +124,7 @@ function reload(id: string, newComp: ComponentOptions | ClassComponent) {
124124
// on patch.
125125
hmrDirtyComponents.add(component)
126126
// 3. Make sure to unmark the component after the reload.
127-
queuePostFlushCb(() => {
127+
nextTick(() => {
128128
hmrDirtyComponents.delete(component)
129129
})
130130
}
@@ -133,7 +133,10 @@ function reload(id: string, newComp: ComponentOptions | ClassComponent) {
133133
// invalidate options resolution cache
134134
instance.appContext.optionsCache.delete(instance.type as any)
135135

136-
if (instance.parent) {
136+
if (instance.ceReload) {
137+
// custom element
138+
instance.ceReload()
139+
} else if (instance.parent) {
137140
// 4. Force the parent instance to re-render. This will cause all updated
138141
// components to be unmounted and re-mounted. Queue the update so that we
139142
// don't end up forcing the same parent to re-render multiple times.

‎packages/runtime-dom/src/apiCustomElement.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,9 @@ export class VueElement extends BaseClass {
167167
* @internal
168168
*/
169169
_instance: ComponentInternalInstance | null = null
170-
/**
171-
* @internal
172-
*/
173-
_connected = false
170+
171+
private _connected = false
172+
private _styles?: HTMLStyleElement[]
174173

175174
constructor(
176175
private _def: ComponentOptions & { styles?: string[] },
@@ -262,12 +261,16 @@ export class VueElement extends BaseClass {
262261
instance.isCE = true
263262
// HMR
264263
if (__DEV__) {
265-
instance.appContext.reload = () => {
266-
render(this._createVNode(), this.shadowRoot!)
267-
this.shadowRoot!.querySelectorAll('style').forEach(s => {
268-
this.shadowRoot!.removeChild(s)
269-
})
264+
instance.ceReload = () => {
265+
this._instance = null
266+
// reset styles
267+
if (this._styles) {
268+
this._styles.forEach(s => this.shadowRoot!.removeChild(s))
269+
this._styles.length = 0
270+
}
270271
this._applyStyles()
272+
// reload
273+
render(this._createVNode(), this.shadowRoot!)
271274
}
272275
}
273276

@@ -302,6 +305,10 @@ export class VueElement extends BaseClass {
302305
const s = document.createElement('style')
303306
s.textContent = css
304307
this.shadowRoot!.appendChild(s)
308+
// record for HMR
309+
if (__DEV__) {
310+
;(this._styles || (this._styles = [])).push(s)
311+
}
305312
})
306313
}
307314
}

0 commit comments

Comments
 (0)
Please sign in to comment.