Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update lit-ssr dependency #6681

Merged
merged 13 commits into from
Mar 29, 2023
5 changes: 5 additions & 0 deletions .changeset/gold-windows-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/lit': patch
e111077 marked this conversation as resolved.
Show resolved Hide resolved
---

Bump deps to lit-ssr 3.0 and polyfill supoprt for template[shadowrootmode] support. Also fix breakage caused by [email protected] hydration behavior requiring lit-ssr 3.0
e111077 marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions examples/framework-lit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
},
"dependencies": {
"astro": "^2.1.8",
"lit": "^2.2.5",
"lit": "^2.7.0",
"@astrojs/lit": "^1.3.0",
"@webcomponents/template-shadowroot": "^0.1.0"
"@webcomponents/template-shadowroot": "^0.2.1"
}
}
4 changes: 2 additions & 2 deletions packages/astro/e2e/fixtures/lit-component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"private": true,
"dependencies": {
"@astrojs/lit": "workspace:*",
"@webcomponents/template-shadowroot": "^0.1.0",
"@webcomponents/template-shadowroot": "^0.2.1",
"astro": "workspace:*",
"lit": "^2.2.5"
"lit": "^2.7.0"
}
}
4 changes: 2 additions & 2 deletions packages/astro/e2e/fixtures/multiple-frameworks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"astro": "workspace:*"
},
"dependencies": {
"@webcomponents/template-shadowroot": "^0.1.0",
"lit": "^2.2.5",
"@webcomponents/template-shadowroot": "^0.2.1",
"lit": "^2.7.0",
"preact": "^10.7.3",
"react": "^18.1.0",
"react-dom": "^18.1.0",
Expand Down
5 changes: 0 additions & 5 deletions packages/astro/e2e/lit-component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ const test = testFactory({
// TODO: configure playwright to handle web component APIs
// https://github.com/microsoft/playwright/issues/14241
test.describe('Lit components', () => {
test.beforeAll(() => {
delete globalThis.window;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer needs to shim the window?

Copy link
Contributor Author

@e111077 e111077 Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the shims needed for lit are baked into Lit's node build. I just pushed a commit though because we do need to actually re-shim only customElements.define and HTMLElement because our baked-in shims in ReactiveElement's node build is forgiving and tries to call anything anyone else has shimmed, but we have a small hack we are relying on so we re-shim that

});

test.describe('Development', () => {
let devServer;
const t = test.extend({});
Expand Down Expand Up @@ -158,7 +154,6 @@ test.describe('Lit components', () => {
const t = test.extend({});

t.beforeAll(async ({ astro }) => {
delete globalThis.window;
// Playwright's Node version doesn't have these functions, so stub them.
process.stdout.clearLine = () => {};
process.stdout.cursorTo = () => {};
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/test/fixtures/lit-element/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"private": true,
"dependencies": {
"@astrojs/lit": "workspace:*",
"@webcomponents/template-shadowroot": "^0.1.0",
"@webcomponents/template-shadowroot": "^0.2.1",
"astro": "workspace:*",
"lit": "^2.2.5"
"lit": "^2.7.0"
}
}
1 change: 0 additions & 1 deletion packages/astro/test/ssr-lit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ describe('Lit integration in SSR', () => {
}

it('Is able to load', async () => {
delete globalThis.window; // On Windows this results in `ReferenceError: window is not defined`
const html = await fetchHTML('/');
const $ = cheerioLoad(html);
expect($('#win').text()).to.equal('function');
Expand Down
9 changes: 5 additions & 4 deletions packages/integrations/lit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,21 @@
"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": {
"astro": "workspace:*",
"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"
}
}
34 changes: 21 additions & 13 deletions packages/integrations/lit/server-shim.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import { installWindowOnGlobal } from '@lit-labs/ssr/lib/dom-shim.js';
import {customElements as litCE} 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;
// 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 does something that triggers
e111077 marked this conversation as resolved.
Show resolved Hide resolved
// ReactiveElement.finalize() to be called. So this is the only thing we will
// re-shim since Lit will try to respect other global DOM shims.
globalThis.customElements = litCE;

const ceDefine = customElements.define;
const litCeDefine = 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);
};
1 change: 0 additions & 1 deletion packages/integrations/lit/server.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
10 changes: 8 additions & 2 deletions packages/integrations/lit/test/server.test.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
});
Expand Down Expand Up @@ -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 {
Expand Down
Loading