Skip to content

Commit

Permalink
feat(runtime-dom): hmr for custom elements
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 22, 2021
1 parent a7fa4ac commit 7a7e1d8
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export function defineCustomElement(
const propKeys = rawKeys.map(camelize)

class VueCustomElement extends VueElement {
static def = Comp
static get observedAttributes() {
return attrKeys
}
Expand Down Expand Up @@ -192,13 +193,7 @@ export class VueElement extends BaseClass {
)
}
this.attachShadow({ mode: 'open' })
if (_def.styles) {
_def.styles.forEach(css => {
const s = document.createElement('style')
s.textContent = css
this.shadowRoot!.appendChild(s)
})
}
this._applyStyles()
}
}

Expand Down Expand Up @@ -268,6 +263,16 @@ export class VueElement extends BaseClass {
vnode.ce = instance => {
this._instance = instance
instance.isCE = true
// HMR
if (__DEV__) {
instance.appContext.reload = () => {
render(this._createVNode(), this.shadowRoot!)
this.shadowRoot!.querySelectorAll('style').forEach(s => {
this.shadowRoot!.removeChild(s)
})
this._applyStyles()
}
}

// intercept emit
instance.emit = (event: string, ...args: any[]) => {
Expand All @@ -293,4 +298,14 @@ export class VueElement extends BaseClass {
}
return vnode
}

private _applyStyles() {
if (this._def.styles) {
this._def.styles.forEach(css => {
const s = document.createElement('style')
s.textContent = css
this.shadowRoot!.appendChild(s)
})
}
}
}

0 comments on commit 7a7e1d8

Please sign in to comment.