From 6e4b571a8fe5916261e2c7e4973a3a3c030524c7 Mon Sep 17 00:00:00 2001 From: Manuel Puyol Date: Mon, 4 Apr 2022 16:27:06 +0000 Subject: [PATCH 1/6] Use try/catch instead of checking if element is registered --- src/register.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/register.ts b/src/register.ts index d8e4fac6..0d9802d4 100644 --- a/src/register.ts +++ b/src/register.ts @@ -12,10 +12,13 @@ export function register(classObject: CustomElement): void { .replace(/([A-Z]($|[a-z]))/g, '-$1') .replace(/(^-|-Element$)/g, '') .toLowerCase() - if (!window.customElements.get(name)) { + + try { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore window[classObject.name] = classObject window.customElements.define(name, classObject) + } catch { + // silently ignore } } From 113eddaff42e248ca4328d9ddd5f89d1655a4398 Mon Sep 17 00:00:00 2001 From: Manuel Puyol Date: Mon, 4 Apr 2022 16:31:04 +0000 Subject: [PATCH 2/6] Make sure window is in sync with CustomElements registration --- src/register.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/register.ts b/src/register.ts index 0d9802d4..ad6845df 100644 --- a/src/register.ts +++ b/src/register.ts @@ -14,10 +14,10 @@ export function register(classObject: CustomElement): void { .toLowerCase() 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 { // silently ignore } From 4979d0989a7953e78bede72b6c3ce7f293427a90 Mon Sep 17 00:00:00 2001 From: Manuel Puyol Date: Mon, 25 Apr 2022 16:26:03 +0000 Subject: [PATCH 3/6] rethrow errors unless its specifically about redefining --- src/register.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/register.ts b/src/register.ts index ad6845df..19006de9 100644 --- a/src/register.ts +++ b/src/register.ts @@ -18,7 +18,11 @@ export function register(classObject: CustomElement): void { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore window[classObject.name] = customElements.get(name) - } catch { - // silently ignore + } catch (e: any) { + // 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 } } From bc02a16aec553b8c17cef8358998a0c79dd8ba97 Mon Sep 17 00:00:00 2001 From: Manuel Puyol Date: Mon, 25 Apr 2022 16:28:01 +0000 Subject: [PATCH 4/6] change any to unknown --- src/register.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/register.ts b/src/register.ts index 19006de9..ec916190 100644 --- a/src/register.ts +++ b/src/register.ts @@ -18,7 +18,7 @@ export function register(classObject: CustomElement): void { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore window[classObject.name] = customElements.get(name) - } catch (e: any) { + } 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 From 1b489195f933553103070b06a8d3077c5585a589 Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Sat, 30 Apr 2022 08:24:04 +0100 Subject: [PATCH 5/6] Bump size-limit to 1.6.3kb --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 539220bd..080f975c 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ { "path": "lib/index.js", "import": "{controller, attr, target, targets}", - "limit": "1.6kb" + "limit": "1.63kb" } ] } From 8b986327e062e20c1cbc2b0f8ca863640ac5dc00 Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Sat, 30 Apr 2022 08:34:07 +0100 Subject: [PATCH 6/6] Update size to 1.64kb --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 080f975c..db965cc6 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ { "path": "lib/index.js", "import": "{controller, attr, target, targets}", - "limit": "1.63kb" + "limit": "1.64kb" } ] }