diff options
Diffstat (limited to 'packages/integrations')
-rw-r--r-- | packages/integrations/lit/package.json | 9 | ||||
-rw-r--r-- | packages/integrations/lit/server-shim.js | 41 | ||||
-rw-r--r-- | packages/integrations/lit/server.js | 1 | ||||
-rw-r--r-- | packages/integrations/lit/test/server.test.js | 10 |
4 files changed, 41 insertions, 20 deletions
diff --git a/packages/integrations/lit/package.json b/packages/integrations/lit/package.json index 8ad1a5279..19892743d 100644 --- a/packages/integrations/lit/package.json +++ b/packages/integrations/lit/package.json @@ -34,7 +34,8 @@ "test": "mocha" }, "dependencies": { - "@lit-labs/ssr": "^2.2.0", + "@lit-labs/ssr": "^3.1.0", + "@lit-labs/ssr-dom-shim": "^1.1.0", "parse5": "^7.1.2" }, "devDependencies": { @@ -42,12 +43,12 @@ "astro-scripts": "workspace:*", "chai": "^4.3.6", "cheerio": "^1.0.0-rc.11", - "lit": "^2.2.5", + "lit": "^2.7.0", "mocha": "^9.2.2", "sass": "^1.52.2" }, "peerDependencies": { - "@webcomponents/template-shadowroot": "^0.1.0", - "lit": "^2.1.3" + "@webcomponents/template-shadowroot": "^0.2.1", + "lit": "^2.7.0" } } diff --git a/packages/integrations/lit/server-shim.js b/packages/integrations/lit/server-shim.js index 3f4a8df4f..ed371f89a 100644 --- a/packages/integrations/lit/server-shim.js +++ b/packages/integrations/lit/server-shim.js @@ -1,20 +1,35 @@ -import { installWindowOnGlobal } from '@lit-labs/ssr/lib/dom-shim.js'; +import { customElements as litCE, HTMLElement as litShimHTMLElement } from '@lit-labs/ssr-dom-shim'; -if (typeof fetch === 'function') { - const _fetch = fetch; - installWindowOnGlobal(); - globalThis.fetch = window.fetch = _fetch; -} else { - installWindowOnGlobal(); +// 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; } -window.global = window; -document.getElementsByTagName = () => []; -// See https://github.com/lit/lit/issues/2393 -document.currentScript = null; +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; -const ceDefine = customElements.define; +// 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. customElements.define = function (tagName, Ctr) { Ctr[Symbol.for('tagName')] = tagName; - return ceDefine.call(this, tagName, Ctr); + return litCeDefine.call(this, tagName, Ctr); }; diff --git a/packages/integrations/lit/server.js b/packages/integrations/lit/server.js index ae89bd610..762c77844 100644 --- a/packages/integrations/lit/server.js +++ b/packages/integrations/lit/server.js @@ -1,5 +1,4 @@ import './server-shim.js'; -import '@lit-labs/ssr/lib/render-lit-html.js'; import { LitElementRenderer } from '@lit-labs/ssr/lib/lit-element-renderer.js'; import * as parse5 from 'parse5'; diff --git a/packages/integrations/lit/test/server.test.js b/packages/integrations/lit/test/server.test.js index 315937401..51e083241 100644 --- a/packages/integrations/lit/test/server.test.js +++ b/packages/integrations/lit/test/server.test.js @@ -1,6 +1,8 @@ import { expect } from 'chai'; -import server from '../server.js'; import { LitElement, html } from 'lit'; +// Must come after lit import because @lit/reactive-element defines +// globalThis.customElements which the server shim expects to be defined. +import server from '../server.js'; import * as cheerio from 'cheerio'; const { check, renderToStaticMarkup } = server; @@ -12,6 +14,10 @@ describe('check', () => { it('should be false with a registered non-lit component', async () => { const tagName = 'non-lit-component'; + // Lit no longer shims HTMLElement globally, so we need to do it ourselves. + if (!globalThis.HTMLElement) { + globalThis.HTMLElement = class {}; + } customElements.define(tagName, class TestComponent extends HTMLElement {}); expect(await check(tagName)).to.equal(false); }); @@ -85,7 +91,7 @@ describe('renderToStaticMarkup', () => { }); it('should render DSD attributes based on shadowRootOptions', async () => { - const tagName = 'lit-component'; + const tagName = 'shadow-root-options-component'; customElements.define( tagName, class extends LitElement { |