@@ -130,6 +130,7 @@ export function defineCustomElement(
130130 const propKeys = rawKeys . map ( camelize )
131131
132132 class VueCustomElement extends VueElement {
133+ static def = Comp
133134 static get observedAttributes ( ) {
134135 return attrKeys
135136 }
@@ -192,13 +193,7 @@ export class VueElement extends BaseClass {
192193 )
193194 }
194195 this . attachShadow ( { mode : 'open' } )
195- if ( _def . styles ) {
196- _def . styles . forEach ( css => {
197- const s = document . createElement ( 'style' )
198- s . textContent = css
199- this . shadowRoot ! . appendChild ( s )
200- } )
201- }
196+ this . _applyStyles ( )
202197 }
203198 }
204199
@@ -268,6 +263,16 @@ export class VueElement extends BaseClass {
268263 vnode . ce = instance => {
269264 this . _instance = instance
270265 instance . isCE = true
266+ // HMR
267+ if ( __DEV__ ) {
268+ instance . appContext . reload = ( ) => {
269+ render ( this . _createVNode ( ) , this . shadowRoot ! )
270+ this . shadowRoot ! . querySelectorAll ( 'style' ) . forEach ( s => {
271+ this . shadowRoot ! . removeChild ( s )
272+ } )
273+ this . _applyStyles ( )
274+ }
275+ }
271276
272277 // intercept emit
273278 instance . emit = ( event : string , ...args : any [ ] ) => {
@@ -293,4 +298,14 @@ export class VueElement extends BaseClass {
293298 }
294299 return vnode
295300 }
301+
302+ private _applyStyles ( ) {
303+ if ( this . _def . styles ) {
304+ this . _def . styles . forEach ( css => {
305+ const s = document . createElement ( 'style' )
306+ s . textContent = css
307+ this . shadowRoot ! . appendChild ( s )
308+ } )
309+ }
310+ }
296311}
0 commit comments