Skip to content

Commit 0775b42

Browse files
authored
Merge pull request #197 from github/allow-hmr-plugins
Allow CustomElements to be redefined
2 parents ec24198 + 8b98632 commit 0775b42

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
{
5454
"path": "lib/index.js",
5555
"import": "{controller, attr, target, targets}",
56-
"limit": "1.6kb"
56+
"limit": "1.64kb"
5757
}
5858
]
5959
}

src/register.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ import {dasherize} from './dasherize.js'
1010
*/
1111
export function register(classObject: CustomElement): void {
1212
const name = dasherize(classObject.name).replace(/-element$/, '')
13-
if (!window.customElements.get(name)) {
13+
14+
try {
15+
window.customElements.define(name, classObject)
1416
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
1517
// @ts-ignore
16-
window[classObject.name] = classObject
17-
window.customElements.define(name, classObject)
18+
window[classObject.name] = customElements.get(name)
19+
} catch (e: unknown) {
20+
// The only reason for window.customElements.define to throw a `NotSupportedError`
21+
// is if the element has already been defined.
22+
if (e instanceof DOMException && e.name === 'NotSupportedError') return
23+
24+
throw e
1825
}
1926
}

0 commit comments

Comments
 (0)