From 344cef3d66c76d17403ea3200777b85005216cf6 Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Wed, 23 Nov 2022 15:13:05 +0000 Subject: [PATCH] add `define` export for custom element registration --- package.json | 12 +++++++ src/index.ts | 52 +++-------------------------- src/local-time-element-define.ts | 26 +++++++++++++++ src/relative-time-element-define.ts | 26 +++++++++++++++ src/time-ago-element-define.ts | 26 +++++++++++++++ src/time-until-element-define.ts | 26 +++++++++++++++ 6 files changed, 120 insertions(+), 48 deletions(-) create mode 100644 src/local-time-element-define.ts create mode 100644 src/relative-time-element-define.ts create mode 100644 src/time-ago-element-define.ts create mode 100644 src/time-until-element-define.ts diff --git a/package.json b/package.json index aac8833..03dddf2 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,18 @@ "dist", "custom-elements.json" ], + "exports": { + ".": "./dist/index.js", + "./define": "./dist/index.js", + "./relative-time": "./dist/relative-time-element.js", + "./relative-time/define": "./dist/relative-time-element-define.js", + "./local-time": "./dist/local-time-element.js", + "./local-time/define": "./dist/local-time-element-define.js", + "./time-ago": "./dist/time-ago-element.js", + "./time-ago/define": "./dist/time-ago-element-define.js", + "./time-until": "./dist/time-until-element.js", + "./time-until/define": "./dist/time-until-element-define.js" + }, "scripts": { "clean": "rm -rf dist", "lint": "eslint . --ext .js,.ts && tsc --noEmit", diff --git a/src/index.ts b/src/index.ts index b1de0f7..d43c2c6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,50 +1,6 @@ -import LocalTimeElement from './local-time-element.js' -import RelativeTimeElement from './relative-time-element.js' -import TimeAgoElement from './time-ago-element.js' -import TimeUntilElement from './time-until-element.js' - -const root = (typeof globalThis !== 'undefined' ? globalThis : window) as typeof window -try { - customElements.define('relative-time', RelativeTimeElement) - root.RelativeTimeElement = RelativeTimeElement -} catch (e: unknown) { - if (!(e instanceof DOMException && e.name === 'NotSupportedError') && !(e instanceof ReferenceError)) throw e -} - -try { - customElements.define('local-time', LocalTimeElement) - root.LocalTimeElement = LocalTimeElement -} catch (e: unknown) { - if (!(e instanceof DOMException && e.name === 'NotSupportedError') && !(e instanceof ReferenceError)) throw e -} - -try { - customElements.define('time-ago', TimeAgoElement) - root.TimeAgoElement = TimeAgoElement -} catch (e: unknown) { - if (!(e instanceof DOMException && e.name === 'NotSupportedError') && !(e instanceof ReferenceError)) throw e -} - -try { - customElements.define('time-until', TimeUntilElement) - root.TimeUntilElement = TimeUntilElement -} catch (e: unknown) { - if (!(e instanceof DOMException && e.name === 'NotSupportedError') && !(e instanceof ReferenceError)) throw e -} - -declare global { - interface Window { - RelativeTimeElement: typeof RelativeTimeElement - LocalTimeElement: typeof LocalTimeElement - TimeAgoElement: typeof TimeAgoElement - TimeUntilElement: typeof TimeUntilElement - } - interface HTMLElementTagNameMap { - 'relative-time': RelativeTimeElement - 'local-time': LocalTimeElement - 'time-ago': TimeAgoElement - 'time-until': TimeUntilElement - } -} +import LocalTimeElement from './local-time-element-define.js' +import RelativeTimeElement from './relative-time-element-define.js' +import TimeAgoElement from './time-ago-element-define.js' +import TimeUntilElement from './time-until-element-define.js' export {LocalTimeElement, RelativeTimeElement, TimeAgoElement, TimeUntilElement} diff --git a/src/local-time-element-define.ts b/src/local-time-element-define.ts new file mode 100644 index 0000000..0ad5aaa --- /dev/null +++ b/src/local-time-element-define.ts @@ -0,0 +1,26 @@ +import LocalTimeElement from './local-time-element.js' + +const root = (typeof globalThis !== 'undefined' ? globalThis : window) as typeof window +try { + customElements.define('local-time', LocalTimeElement) + root.LocalTimeElement = LocalTimeElement +} catch (e: unknown) { + if ( + !(root.DOMException && e instanceof DOMException && e.name === 'NotSupportedError') && + !(e instanceof ReferenceError) + ) { + throw e + } +} + +declare global { + interface Window { + LocalTimeElement: typeof LocalTimeElement + } + interface HTMLElementTagNameMap { + 'local-time': LocalTimeElement + } +} + +export default LocalTimeElement +export * from './local-time-element.js' diff --git a/src/relative-time-element-define.ts b/src/relative-time-element-define.ts new file mode 100644 index 0000000..33137da --- /dev/null +++ b/src/relative-time-element-define.ts @@ -0,0 +1,26 @@ +import RelativeTimeElement from './relative-time-element.js' + +const root = (typeof globalThis !== 'undefined' ? globalThis : window) as typeof window +try { + customElements.define('relative-time', RelativeTimeElement) + root.RelativeTimeElement = RelativeTimeElement +} catch (e: unknown) { + if ( + !(root.DOMException && e instanceof DOMException && e.name === 'NotSupportedError') && + !(e instanceof ReferenceError) + ) { + throw e + } +} + +declare global { + interface Window { + RelativeTimeElement: typeof RelativeTimeElement + } + interface HTMLElementTagNameMap { + 'relative-time': RelativeTimeElement + } +} + +export default RelativeTimeElement +export * from './relative-time-element.js' diff --git a/src/time-ago-element-define.ts b/src/time-ago-element-define.ts new file mode 100644 index 0000000..b6993bc --- /dev/null +++ b/src/time-ago-element-define.ts @@ -0,0 +1,26 @@ +import TimeAgoElement from './time-ago-element.js' + +const root = (typeof globalThis !== 'undefined' ? globalThis : window) as typeof window +try { + customElements.define('time-ago', TimeAgoElement) + root.TimeAgoElement = TimeAgoElement +} catch (e: unknown) { + if ( + !(root.DOMException && e instanceof DOMException && e.name === 'NotSupportedError') && + !(e instanceof ReferenceError) + ) { + throw e + } +} + +declare global { + interface Window { + TimeAgoElement: typeof TimeAgoElement + } + interface HTMLElementTagNameMap { + 'time-ago': TimeAgoElement + } +} + +export default TimeAgoElement +export * from './time-ago-element.js' diff --git a/src/time-until-element-define.ts b/src/time-until-element-define.ts new file mode 100644 index 0000000..8222c17 --- /dev/null +++ b/src/time-until-element-define.ts @@ -0,0 +1,26 @@ +import TimeUntilElement from './time-until-element.js' + +const root = (typeof globalThis !== 'undefined' ? globalThis : window) as typeof window +try { + customElements.define('time-until', TimeUntilElement) + root.TimeUntilElement = TimeUntilElement +} catch (e: unknown) { + if ( + !(root.DOMException && e instanceof DOMException && e.name === 'NotSupportedError') && + !(e instanceof ReferenceError) + ) { + throw e + } +} + +declare global { + interface Window { + TimeUntilElement: typeof TimeUntilElement + } + interface HTMLElementTagNameMap { + 'time-until': TimeUntilElement + } +} + +export default TimeUntilElement +export * from './time-until-element.js'