Skip to content

Commit

Permalink
Merge pull request #197 from github/allow-hmr-plugins
Browse files Browse the repository at this point in the history
Allow CustomElements to be redefined
  • Loading branch information
manuelpuyol authored Apr 30, 2022
2 parents ec24198 + 8b98632 commit 0775b42
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
{
"path": "lib/index.js",
"import": "{controller, attr, target, targets}",
"limit": "1.6kb"
"limit": "1.64kb"
}
]
}
13 changes: 10 additions & 3 deletions src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import {dasherize} from './dasherize.js'
*/
export function register(classObject: CustomElement): void {
const name = dasherize(classObject.name).replace(/-element$/, '')
if (!window.customElements.get(name)) {

try {
window.customElements.define(name, classObject)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window[classObject.name] = classObject
window.customElements.define(name, classObject)
window[classObject.name] = customElements.get(name)
} catch (e: unknown) {
// The only reason for window.customElements.define to throw a `NotSupportedError`
// is if the element has already been defined.
if (e instanceof DOMException && e.name === 'NotSupportedError') return

throw e
}
}

0 comments on commit 0775b42

Please sign in to comment.