From b604b5c27ac97d895032a30f43248163603ebacc Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Wed, 23 Nov 2022 15:25:20 +0000 Subject: [PATCH] Ensure HTMLElement exists in all contexts When requiring this module in NodeJS, it will error as `HTMLElement` does not exist. Creating a variable defaulting to `null` allows the class to be created in the server without causing errors. This is useful for introspecting the class within NodeJS. --- src/relative-time-element.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/relative-time-element.ts b/src/relative-time-element.ts index 7d91bd2..0543181 100644 --- a/src/relative-time-element.ts +++ b/src/relative-time-element.ts @@ -3,6 +3,8 @@ import {DateTimeFormat as DateTimeFormatPonyFill} from './datetimeformat-ponyfil import {RelativeTimeFormat as RelativeTimeFormatPonyfill} from './relative-time-ponyfill.js' import {isDuration, withinDuration} from './duration.js' import {strftime} from './strftime.js' +const root = (typeof globalThis !== 'undefined' ? globalThis : window) as typeof window +const HTMLElement = root.HTMLElement || (null as unknown as typeof window['HTMLElement']) const supportsIntlDatetime = typeof Intl !== 'undefined' && 'DateTimeFormat' in Intl const DateTimeFormat = supportsIntlDatetime ? Intl.DateTimeFormat : DateTimeFormatPonyFill