@ecopages/lit 0.2.0-alpha.11 → 0.2.0-alpha.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -6,19 +6,14 @@ All notable changes to `@ecopages/lit` are documented here.
6
6
 
7
7
  ## [UNRELEASED] — TBD
8
8
 
9
- ### Features
10
-
11
- - Aligned Lit with the shared orchestration, lazy dependency, and global injector flow.
12
-
13
9
  ### Bug Fixes
14
10
 
15
- - Fixed mixed-template SSR composition, placeholder cleanup, lazy preload handling, and browser-router bootstrap reruns.
16
- - Routed Lit SSR through the static template pipeline so registered custom elements emit declarative shadow DOM.
11
+ - Fixed Lit document-shell composition, declarative shadow DOM SSR, nested child serialization, lazy preload handling, explicit render paths, and mixed-renderer boundary resolution.
17
12
 
18
- ### Refactoring
13
+ ### Documentation
19
14
 
20
- - Cleaned up ambient module declarations.
15
+ - Updated the README to document `.lit.tsx` route ownership, standalone setup, and mixed-renderer Lit boundary handling.
21
16
 
22
17
  ### Tests
23
18
 
24
- - Updated integration coverage for the orchestration pipeline and Node and esbuild compatibility.
19
+ - Updated integration coverage for explicit boundary composition and Node and esbuild compatibility.
package/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # @ecopages/lit
2
2
 
3
- Integration plugin for [Lit](https://lit.dev/) web components in Ecopages.
4
-
5
- This integration is optimized for use alongside `@ecopages/kitajs` (or another HTML JSX engine), allowing you to author your pages in Kitajs JSX templates while embedding interactive Lit components where needed.
3
+ Integration plugin for [Lit](https://lit.dev/) in Ecopages. Use it when Lit should own `.lit.tsx` routes or when another integration needs Lit to render nested custom-element boundaries.
6
4
 
7
5
  ## Installation
8
6
 
@@ -12,7 +10,21 @@ bunx jsr add @ecopages/lit
12
10
 
13
11
  ## Usage
14
12
 
15
- It is recommended to use `@ecopages/lit` in conjunction with `@ecopages/kitajs`. Configure your project to include both integrations in your `eco.config.ts`:
13
+ Register `litPlugin()` in your `eco.config.ts`.
14
+
15
+ ```ts
16
+ import { ConfigBuilder } from '@ecopages/core/config-builder';
17
+ import { litPlugin } from '@ecopages/lit';
18
+
19
+ const config = await new ConfigBuilder()
20
+ .setBaseUrl(import.meta.env.ECOPAGES_BASE_URL)
21
+ .setIntegrations([litPlugin()])
22
+ .build();
23
+
24
+ export default config;
25
+ ```
26
+
27
+ Lit also works well alongside an HTML-first renderer such as `@ecopages/kitajs` when you want Lit to own only the nested custom elements:
16
28
 
17
29
  ```ts
18
30
  import { ConfigBuilder } from '@ecopages/core/config-builder';
@@ -27,4 +39,14 @@ const config = await new ConfigBuilder()
27
39
  export default config;
28
40
  ```
29
41
 
30
- This setup enables server-rendered KitaJS pages that embed and hydrate Lit custom elements on the client.
42
+ This setup lets Kita own the page shell while Lit owns the nested Lit component boundaries.
43
+
44
+ ## What This Integration Owns
45
+
46
+ - `.lit.tsx` route files.
47
+ - Nested Lit component boundaries rendered inside pages owned by other integrations.
48
+ - The Lit hydration support script required for SSR custom elements and declarative shadow DOM.
49
+
50
+ ## Mixed Rendering
51
+
52
+ When a non-Lit render pass enters a Lit-owned component boundary, Ecopages hands that boundary to the Lit renderer. That keeps Lit SSR in charge of custom elements, declarative shadow DOM, and Lit-managed child content.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecopages/lit",
3
- "version": "0.2.0-alpha.11",
3
+ "version": "0.2.0-alpha.13",
4
4
  "description": "Ecopages lit integration",
5
5
  "keywords": [
6
6
  "ecopages",
@@ -31,7 +31,7 @@
31
31
  "directory": "packages/integrations/lit"
32
32
  },
33
33
  "peerDependencies": {
34
- "@ecopages/core": "0.2.0-alpha.11",
34
+ "@ecopages/core": "0.2.0-alpha.13",
35
35
  "@lit-labs/ssr": "^3.3.0",
36
36
  "@lit-labs/ssr-client": "^1.1.7",
37
37
  "lit": "^3.2.1"
@@ -3,14 +3,18 @@
3
3
  * @module
4
4
  */
5
5
  import type { ComponentRenderInput, ComponentRenderResult, EcoComponent, EcoPagesElement, IntegrationRendererRenderOptions, RouteRendererBody } from '@ecopages/core';
6
+ import '@lit-labs/ssr/lib/install-global-dom-shim.js';
6
7
  import { IntegrationRenderer, type RenderToResponseContext } from '@ecopages/core/route-renderer/integration-renderer';
7
8
  /**
8
9
  * A renderer for the Lit integration.
9
10
  */
10
11
  export declare class LitRenderer extends IntegrationRenderer<EcoPagesElement> {
11
12
  name: string;
12
- protected shouldRenderPageComponent(): boolean;
13
+ private resolveQueuedBoundaryChildren;
14
+ private resolveQueuedBoundaryHtml;
15
+ private normalizeDeclarativeShadowRootMarkup;
13
16
  private createRenderableMarkup;
17
+ protected shouldRenderPageComponent(): boolean;
14
18
  private renderMarkupToString;
15
19
  private renderValueToString;
16
20
  private isLitManagedComponent;
@@ -21,11 +25,15 @@ export declare class LitRenderer extends IntegrationRenderer<EcoPagesElement> {
21
25
  *
22
26
  * SSR-eligible lazy scripts are preloaded first so custom elements registered
23
27
  * by the component can render their server markup even when the Lit renderer is
24
- * entered through cross-integration marker resolution.
28
+ * entered through cross-integration boundary handoff.
25
29
  *
26
30
  * Includes component-scoped dependency assets when declared.
27
31
  */
28
32
  renderComponent(input: ComponentRenderInput): Promise<ComponentRenderResult>;
33
+ protected createComponentBoundaryRuntime(options: {
34
+ boundaryInput: ComponentRenderInput;
35
+ rendererCache: Map<string, IntegrationRenderer<any>>;
36
+ }): import("@ecopages/core").ComponentBoundaryRuntime;
29
37
  private readonly ssrLazyPreloader;
30
38
  /**
31
39
  * Detects preload failures that are expected for browser-only modules.
@@ -1,23 +1,65 @@
1
+ import "@lit-labs/ssr/lib/install-global-dom-shim.js";
1
2
  import { IntegrationRenderer } from "@ecopages/core/route-renderer/integration-renderer";
2
3
  import { render } from "@lit-labs/ssr";
3
4
  import { html as staticHtml, unsafeStatic } from "lit/static-html.js";
4
5
  import { LitSsrLazyPreloader } from "./lit-ssr-lazy-preloader.js";
5
6
  import { PLUGIN_NAME } from "./lit.plugin.js";
6
7
  const HTML_TEMPLATE_SLOT_MARKER = "<--content-->";
8
+ const COMPONENT_CHILDREN_SLOT_MARKER = "<!--eco-lit-component-children-->";
9
+ const ESCAPED_COMPONENT_CHILDREN_SLOT_MARKER = "&lt;!--eco-lit-component-children--&gt;";
10
+ const DOUBLE_ESCAPED_COMPONENT_CHILDREN_SLOT_MARKER = "&amp;lt;!--eco-lit-component-children--&amp;gt;";
11
+ const DUPLICATE_DECLARATIVE_SHADOW_ROOT_ATTRIBUTE = /\sshadowroot=(['"])(open|closed)\1(?=\sshadowrootmode=\1\2\1)/g;
7
12
  class LitRenderer extends IntegrationRenderer {
8
13
  name = PLUGIN_NAME;
9
- shouldRenderPageComponent() {
10
- return false;
14
+ async resolveQueuedBoundaryChildren(children, queuedResolutionsByToken, resolveToken) {
15
+ if (children === void 0) {
16
+ return void 0;
17
+ }
18
+ let renderedChildren = typeof children === "string" ? children : await this.renderValueToString(children);
19
+ renderedChildren = await this.resolveQueuedBoundaryTokens(
20
+ renderedChildren,
21
+ queuedResolutionsByToken,
22
+ resolveToken
23
+ );
24
+ return renderedChildren;
25
+ }
26
+ async resolveQueuedBoundaryHtml(html, runtimeContext) {
27
+ const queuedBoundaryResolution = await this.resolveRendererOwnedQueuedBoundaryHtml({
28
+ html,
29
+ runtimeContext,
30
+ queueLabel: "Lit",
31
+ renderQueuedChildren: async (children, _runtimeContext, queuedResolutionsByToken, resolveToken) => {
32
+ const renderedChildren = await this.resolveQueuedBoundaryChildren(
33
+ children,
34
+ queuedResolutionsByToken,
35
+ resolveToken
36
+ );
37
+ return {
38
+ assets: [],
39
+ html: renderedChildren
40
+ };
41
+ }
42
+ });
43
+ return {
44
+ html: queuedBoundaryResolution.html,
45
+ assets: queuedBoundaryResolution.assets.length > 0 ? queuedBoundaryResolution.assets : void 0
46
+ };
47
+ }
48
+ normalizeDeclarativeShadowRootMarkup(markup) {
49
+ return markup.replace(DUPLICATE_DECLARATIVE_SHADOW_ROOT_ATTRIBUTE, "");
11
50
  }
12
51
  createRenderableMarkup(markup) {
13
52
  return staticHtml`${unsafeStatic(markup)}`;
14
53
  }
54
+ shouldRenderPageComponent() {
55
+ return false;
56
+ }
15
57
  async renderMarkupToString(markup) {
16
58
  let renderedHtml = "";
17
59
  for (const chunk of render(this.createRenderableMarkup(markup))) {
18
60
  renderedHtml += chunk;
19
61
  }
20
- return renderedHtml;
62
+ return this.normalizeDeclarativeShadowRootMarkup(renderedHtml);
21
63
  }
22
64
  async renderValueToString(value) {
23
65
  if (typeof value === "string") {
@@ -27,14 +69,23 @@ class LitRenderer extends IntegrationRenderer {
27
69
  for (const chunk of render(value)) {
28
70
  renderedHtml += chunk;
29
71
  }
30
- return renderedHtml;
72
+ return this.normalizeDeclarativeShadowRootMarkup(renderedHtml);
31
73
  }
32
74
  isLitManagedComponent(component) {
33
75
  return component?.config?.integration === this.name || component?.config?.__eco?.integration === this.name;
34
76
  }
35
77
  injectRenderedChildren(template, renderedChildren) {
78
+ for (const marker of [
79
+ COMPONENT_CHILDREN_SLOT_MARKER,
80
+ ESCAPED_COMPONENT_CHILDREN_SLOT_MARKER,
81
+ DOUBLE_ESCAPED_COMPONENT_CHILDREN_SLOT_MARKER
82
+ ]) {
83
+ if (template.includes(marker)) {
84
+ return template.split(marker).join(renderedChildren);
85
+ }
86
+ }
36
87
  if (template.includes(HTML_TEMPLATE_SLOT_MARKER)) {
37
- return template.replace(HTML_TEMPLATE_SLOT_MARKER, renderedChildren);
88
+ return template.split(HTML_TEMPLATE_SLOT_MARKER).join(renderedChildren);
38
89
  }
39
90
  if (template.includes("</body>")) {
40
91
  return template.replace("</body>", `${renderedChildren}</body>`);
@@ -66,27 +117,51 @@ class LitRenderer extends IntegrationRenderer {
66
117
  *
67
118
  * SSR-eligible lazy scripts are preloaded first so custom elements registered
68
119
  * by the component can render their server markup even when the Lit renderer is
69
- * entered through cross-integration marker resolution.
120
+ * entered through cross-integration boundary handoff.
70
121
  *
71
122
  * Includes component-scoped dependency assets when declared.
72
123
  */
73
124
  async renderComponent(input) {
74
125
  await this.preloadSsrLazyScripts([input.component]);
75
126
  const component = input.component;
76
- const props = input.children === void 0 ? input.props : { ...input.props, children: input.children };
127
+ let renderedChildren;
128
+ if (input.children !== void 0) {
129
+ renderedChildren = typeof input.children === "string" ? input.children : await this.renderValueToString(input.children);
130
+ }
131
+ let props = input.props;
132
+ if (renderedChildren !== void 0) {
133
+ props = {
134
+ ...input.props,
135
+ children: COMPONENT_CHILDREN_SLOT_MARKER
136
+ };
137
+ }
77
138
  const content = await component(props);
78
- const html = await this.renderValueToString(content);
139
+ const renderedHtml = await this.renderValueToString(content);
140
+ const html = renderedChildren === void 0 ? renderedHtml : this.injectRenderedChildren(renderedHtml, renderedChildren);
141
+ const queuedBoundaryResolution = await this.resolveQueuedBoundaryHtml(
142
+ html,
143
+ this.getQueuedBoundaryRuntime(input)
144
+ );
79
145
  const hasDependencies = Boolean(input.component.config?.dependencies);
80
146
  const canResolveAssets = typeof this.assetProcessingService?.processDependencies === "function";
81
147
  const assets = hasDependencies && canResolveAssets ? await this.processComponentDependencies([input.component]) : void 0;
82
148
  return {
83
- html,
149
+ html: queuedBoundaryResolution.html,
84
150
  canAttachAttributes: true,
85
- rootTag: this.getRootTagName(html),
151
+ rootTag: this.getRootTagName(queuedBoundaryResolution.html),
86
152
  integrationName: this.name,
87
- assets
153
+ assets: this.htmlTransformer.dedupeProcessedAssets([
154
+ ...assets ?? [],
155
+ ...queuedBoundaryResolution.assets ?? []
156
+ ])
88
157
  };
89
158
  }
159
+ createComponentBoundaryRuntime(options) {
160
+ return this.createQueuedBoundaryRuntime({
161
+ boundaryInput: options.boundaryInput,
162
+ rendererCache: options.rendererCache
163
+ });
164
+ }
90
165
  ssrLazyPreloader = new LitSsrLazyPreloader({
91
166
  resolveDependencyPath: this.resolveDependencyPath.bind(this),
92
167
  processDependencies: this.assetProcessingService?.processDependencies?.bind(this.assetProcessingService)
@@ -137,24 +212,24 @@ class LitRenderer extends IntegrationRenderer {
137
212
  }) {
138
213
  try {
139
214
  await this.preloadSsrLazyScripts([Page, Layout]);
140
- const pageContent = await Page({ params, query, ...props, locals });
141
- const pageHtml = await this.renderValueToString(pageContent);
142
- const children = Layout ? this.isLitManagedComponent(Layout) ? await this.renderValueToString(
143
- await Layout({
144
- children: pageContent,
145
- locals
146
- })
147
- ) : await Layout({
148
- children: pageHtml,
149
- locals
150
- }) : pageHtml;
151
- const renderedChildren = this.isLitManagedComponent(HtmlTemplate) ? await this.renderMarkupToString(String(children)) : String(children);
152
- return this.DOC_TYPE + await this.renderHtmlTemplate({
153
- HtmlTemplate,
215
+ return await this.renderPageWithDocumentShell({
216
+ page: {
217
+ component: Page,
218
+ props: {
219
+ params,
220
+ query,
221
+ ...props,
222
+ locals
223
+ }
224
+ },
225
+ layout: Layout ? {
226
+ component: Layout,
227
+ props: locals ? { locals } : {}
228
+ } : void 0,
229
+ htmlTemplate: HtmlTemplate,
154
230
  metadata,
155
231
  pageProps: props || {},
156
- renderedChildren,
157
- isLitManagedHtmlTemplate: this.isLitManagedComponent(HtmlTemplate)
232
+ transformDocumentHtml: (html) => this.normalizeDeclarativeShadowRootMarkup(html)
158
233
  });
159
234
  } catch (error) {
160
235
  throw this.createRenderError("Error rendering page", error);
@@ -162,42 +237,41 @@ class LitRenderer extends IntegrationRenderer {
162
237
  }
163
238
  async renderToResponse(view, props, ctx) {
164
239
  try {
240
+ if (ctx.partial) {
241
+ return this.renderPartialViewResponse({
242
+ view,
243
+ props,
244
+ ctx,
245
+ transformHtml: (html) => this.normalizeDeclarativeShadowRootMarkup(html)
246
+ });
247
+ }
165
248
  const viewConfig = view.config;
166
249
  const Layout = viewConfig?.layout;
167
- const HtmlTemplate = ctx.partial ? void 0 : await this.getHtmlTemplate();
168
- const metadata = ctx.partial || !HtmlTemplate ? void 0 : view.metadata ? await view.metadata({
169
- params: {},
170
- query: {},
171
- props,
172
- appConfig: this.appConfig
173
- }) : this.appConfig.defaultMetadata;
250
+ const HtmlTemplate = await this.getHtmlTemplate();
251
+ const metadata = await this.resolveViewMetadata(view, props);
174
252
  await this.preloadSsrLazyScripts([view, Layout]);
175
- if (!ctx.partial) {
176
- await this.prepareViewDependencies(view, Layout);
177
- }
178
- const viewFn = view;
179
- const renderExecution = await this.captureHtmlRender(async () => {
180
- const pageContent = await viewFn(props);
181
- const pageHtml = await this.renderValueToString(pageContent);
182
- if (ctx.partial) {
183
- return await this.renderMarkupToString(pageHtml);
184
- }
185
- const children = Layout ? this.isLitManagedComponent(Layout) ? await this.renderValueToString(await Layout({ children: pageContent })) : await Layout({ children: pageHtml }) : pageHtml;
186
- const renderedChildren = this.isLitManagedComponent(HtmlTemplate) ? await this.renderMarkupToString(String(children)) : String(children);
187
- return this.DOC_TYPE + await this.renderHtmlTemplate({
188
- HtmlTemplate,
189
- metadata: metadata ?? this.appConfig.defaultMetadata,
190
- pageProps: props ?? {},
191
- renderedChildren,
192
- isLitManagedHtmlTemplate: this.isLitManagedComponent(HtmlTemplate)
193
- });
253
+ await this.prepareViewDependencies(view, Layout);
254
+ const pageRender = await this.renderComponentBoundary({
255
+ component: view,
256
+ props: props ?? {}
257
+ });
258
+ const layoutRender = Layout ? await this.renderComponentBoundary({
259
+ component: Layout,
260
+ props: {},
261
+ children: pageRender.html
262
+ }) : void 0;
263
+ const documentRender = await this.renderComponentBoundary({
264
+ component: HtmlTemplate,
265
+ props: {
266
+ metadata,
267
+ pageProps: props ?? {}
268
+ },
269
+ children: layoutRender?.html ?? pageRender.html
194
270
  });
195
- const componentsToResolve = ctx.partial ? [view] : [HtmlTemplate, Layout, view].filter(Boolean);
196
- const body = await this.finalizeCapturedHtmlRender({
197
- html: renderExecution.html,
198
- componentsToResolve,
199
- graphContext: renderExecution.graphContext,
200
- partial: ctx.partial
271
+ this.appendProcessedDependencies(pageRender.assets, layoutRender?.assets, documentRender.assets);
272
+ const body = await this.finalizeResolvedHtml({
273
+ html: `${this.DOC_TYPE}${this.normalizeDeclarativeShadowRootMarkup(documentRender.html)}`,
274
+ partial: false
201
275
  });
202
276
  return this.createHtmlResponse(body, ctx);
203
277
  } catch (error) {
@@ -2,9 +2,8 @@
2
2
  * This module contains the Lit plugin
3
3
  * @module
4
4
  */
5
- import '@lit-labs/ssr/lib/install-global-dom-shim.js';
6
5
  import './console.js';
7
- import { IntegrationPlugin, type ComponentBoundaryPolicyInput, type IntegrationPluginConfig } from '@ecopages/core/plugins/integration-plugin';
6
+ import { IntegrationPlugin, type IntegrationPluginConfig } from '@ecopages/core/plugins/integration-plugin';
8
7
  import { type AssetDefinition } from '@ecopages/core/services/asset-processing-service';
9
8
  import { LitRenderer } from './lit-renderer.js';
10
9
  /**
@@ -19,17 +18,6 @@ export declare class LitPlugin extends IntegrationPlugin {
19
18
  renderer: typeof LitRenderer;
20
19
  constructor(options?: Omit<IntegrationPluginConfig, 'name'>);
21
20
  getDependencies(): AssetDefinition[];
22
- /**
23
- * Declares Lit's cross-integration boundary deferral rule.
24
- *
25
- * When a non-Lit render pass enters a Lit component boundary, the boundary is
26
- * deferred into marker-graph resolution so Lit SSR can render custom elements,
27
- * declarative shadow DOM, and other Lit-owned output through the Lit renderer.
28
- *
29
- * @param input Boundary metadata for the active render pass.
30
- * @returns `true` when the boundary should be deferred into the marker pass.
31
- */
32
- shouldDeferComponentBoundary(input: ComponentBoundaryPolicyInput): boolean;
33
21
  }
34
22
  /**
35
23
  * Factory function to create a Lit plugin instance
package/src/lit.plugin.js CHANGED
@@ -1,8 +1,5 @@
1
- import "@lit-labs/ssr/lib/install-global-dom-shim.js";
2
1
  import "./console.js";
3
- import {
4
- IntegrationPlugin
5
- } from "@ecopages/core/plugins/integration-plugin";
2
+ import { IntegrationPlugin } from "@ecopages/core/plugins/integration-plugin";
6
3
  import { AssetFactory } from "@ecopages/core/services/asset-processing-service";
7
4
  import { litElementHydrateScript } from "./lit-element-hydrate.js";
8
5
  import { LitRenderer } from "./lit-renderer.js";
@@ -42,19 +39,6 @@ class LitPlugin extends IntegrationPlugin {
42
39
  })
43
40
  ];
44
41
  }
45
- /**
46
- * Declares Lit's cross-integration boundary deferral rule.
47
- *
48
- * When a non-Lit render pass enters a Lit component boundary, the boundary is
49
- * deferred into marker-graph resolution so Lit SSR can render custom elements,
50
- * declarative shadow DOM, and other Lit-owned output through the Lit renderer.
51
- *
52
- * @param input Boundary metadata for the active render pass.
53
- * @returns `true` when the boundary should be deferred into the marker pass.
54
- */
55
- shouldDeferComponentBoundary(input) {
56
- return input.targetIntegration === this.name && input.currentIntegration !== this.name;
57
- }
58
42
  }
59
43
  function litPlugin(options) {
60
44
  return new LitPlugin(options);