2024-04-17 03:38:53 -05:00
|
|
|
import { customElements as litCE, HTMLElement as litShimHTMLElement } from '@lit-labs/ssr-dom-shim';
|
2023-01-09 16:59:20 -05:00
|
|
|
|
2023-03-29 11:43:40 -05:00
|
|
|
// Something at build time injects document.currentScript = undefined instead of
|
|
|
|
// document.currentScript = null. This causes Sass build to fail because it
|
|
|
|
// seems to be expecting `=== null`. This set to `undefined` doesn't seem to be
|
|
|
|
// caused by Lit and only happens at build / test time, but not in dev or
|
|
|
|
// preview time.
|
|
|
|
if (globalThis.document) {
|
|
|
|
document.currentScript = null;
|
2023-01-09 16:59:20 -05:00
|
|
|
}
|
2022-03-18 17:35:45 -05:00
|
|
|
|
2023-03-29 11:43:40 -05:00
|
|
|
if (globalThis.HTMLElement) {
|
|
|
|
// Seems Astro's Element shim does nothing when `.setAttribute` is called
|
|
|
|
// and subsequently `.getAttribute` is called. Causes Lit to not SSR attrs
|
|
|
|
globalThis.HTMLElement = litShimHTMLElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Astro seems to have a DOM shim and the only real difference that we need out
|
|
|
|
// of the Lit DOM shim is that the Lit DOM shim reads
|
|
|
|
// `HTMLElement.observedAttributes` which is meant to trigger
|
|
|
|
// `ReactiveElement.finalize()`. So this is the only thing we will re-shim since
|
|
|
|
// Lit will try to respect other global DOM shims.
|
|
|
|
globalThis.customElements = litCE;
|
|
|
|
|
|
|
|
const litCeDefine = customElements.define;
|
2022-06-21 07:32:05 -05:00
|
|
|
|
2023-03-29 11:43:40 -05:00
|
|
|
// We need to patch customElements.define to keep track of the tagName on the
|
|
|
|
// class itself so that we can transform JSX custom element class definintion to
|
|
|
|
// a DSD string on the server, because there is no way to get the tagName from a
|
|
|
|
// CE class otherwise. Not an issue on client:only because the browser supports
|
|
|
|
// appending a class instance directly to the DOM.
|
2022-06-21 07:33:55 -05:00
|
|
|
customElements.define = function (tagName, Ctr) {
|
2022-06-21 07:32:05 -05:00
|
|
|
Ctr[Symbol.for('tagName')] = tagName;
|
2023-03-29 11:43:40 -05:00
|
|
|
return litCeDefine.call(this, tagName, Ctr);
|
2022-06-21 07:33:55 -05:00
|
|
|
};
|