diff --git a/core/pfe-core/package.json b/core/pfe-core/package.json index d44afee046..6a23731fd1 100644 --- a/core/pfe-core/package.json +++ b/core/pfe-core/package.json @@ -11,6 +11,11 @@ "exports": { ".": "./core.js", "./*": "./*", + "./ssr-shims.js": { + "node": "./ssr-shims.js", + "import": "./core.js", + "default": "./core.js" + }, "./controllers/*": "./controllers/*", "./decorators/*": "./decorators/*", "./functions/*": "./functions/*", diff --git a/core/pfe-core/ssr-shims.ts b/core/pfe-core/ssr-shims.ts new file mode 100644 index 0000000000..30a6c904c9 --- /dev/null +++ b/core/pfe-core/ssr-shims.ts @@ -0,0 +1,56 @@ +class ObserverShim { + observe(): void { + void 0; + } + + disconnect(): void { + void 0; + } +} + +class MiniHTMLElement { + innerHTML = ''; + constructor(public tagName: string) { } +} + +class MiniHTMLTemplateElement extends MiniHTMLElement { + content = { cloneNode: (): string => this.innerHTML }; +} + +class MiniDocument { + createElement(tagName: string): MiniHTMLElement { + switch (tagName) { + case 'template': + return new MiniHTMLTemplateElement(tagName); + default: + return new MiniHTMLElement(tagName); + } + } +} + +// @ts-expect-error: this runs in node +globalThis.window ??= globalThis; +// @ts-expect-error: this runs in node +globalThis.document ??= new MiniDocument(); +// @ts-expect-error: this runs in node +globalThis.navigator ??= { userAgent: '' }; +// @ts-expect-error: this runs in node +globalThis.ErrorEvent ??= Event; +// @ts-expect-error: this runs in node +globalThis.IntersectionObserver ??= ObserverShim; +// @ts-expect-error: this runs in node +globalThis.MutationObserver ??= ObserverShim; +// @ts-expect-error: this runs in node +globalThis.getComputedStyle ??= function() { + return { + getPropertyPriority() { + return ''; + }, + getPropertyValue() { + return ''; + }, + }; +} + +; +