@ecopages/lit 0.2.0-alpha.10 → 0.2.0-alpha.12

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
@@ -8,12 +8,13 @@ All notable changes to `@ecopages/lit` are documented here.
8
8
 
9
9
  ### Features
10
10
 
11
- - Aligned Lit with the unified orchestration pipeline and the shared lazy dependency and global injector flow.
11
+ - Aligned Lit with the shared orchestration, lazy dependency, and global injector flow.
12
12
 
13
13
  ### Bug Fixes
14
14
 
15
- - Prevented the shared Lit hydrate-support bootstrap from re-running on every browser-router navigation.
16
- - Routed Lit renderer output through Lit's static SSR template pipeline so registered custom elements render declarative shadow DOM during SSR instead of staying as bare host tags.
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.
17
+ - Fixed component-boundary SSR to inject serialized children through both direct Lit interpolation and repeated Lit child slots when Lit is hosted by other integrations.
17
18
 
18
19
  ### Refactoring
19
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecopages/lit",
3
- "version": "0.2.0-alpha.10",
3
+ "version": "0.2.0-alpha.12",
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.10",
34
+ "@ecopages/core": "0.2.0-alpha.12",
35
35
  "@lit-labs/ssr": "^3.3.0",
36
36
  "@lit-labs/ssr-client": "^1.1.7",
37
37
  "lit": "^3.2.1"
@@ -3,17 +3,28 @@
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;
13
+ private normalizeDeclarativeShadowRootMarkup;
12
14
  private createRenderableMarkup;
15
+ protected shouldRenderPageComponent(): boolean;
13
16
  private renderMarkupToString;
17
+ private renderValueToString;
18
+ private isLitManagedComponent;
19
+ private injectRenderedChildren;
20
+ private renderHtmlTemplate;
14
21
  /**
15
22
  * Renders a Lit component boundary for component-level orchestration.
16
23
  *
24
+ * SSR-eligible lazy scripts are preloaded first so custom elements registered
25
+ * by the component can render their server markup even when the Lit renderer is
26
+ * entered through cross-integration marker resolution.
27
+ *
17
28
  * Includes component-scoped dependency assets when declared.
18
29
  */
19
30
  renderComponent(input: ComponentRenderInput): Promise<ComponentRenderResult>;
@@ -1,39 +1,110 @@
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
- import { RenderResultReadable } from "@lit-labs/ssr/lib/render-result-readable.js";
4
4
  import { html as staticHtml, unsafeStatic } from "lit/static-html.js";
5
5
  import { LitSsrLazyPreloader } from "./lit-ssr-lazy-preloader.js";
6
6
  import { PLUGIN_NAME } from "./lit.plugin.js";
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;
14
+ normalizeDeclarativeShadowRootMarkup(markup) {
15
+ return markup.replace(DUPLICATE_DECLARATIVE_SHADOW_ROOT_ATTRIBUTE, "");
16
+ }
9
17
  createRenderableMarkup(markup) {
10
18
  return staticHtml`${unsafeStatic(markup)}`;
11
19
  }
20
+ shouldRenderPageComponent() {
21
+ return false;
22
+ }
12
23
  async renderMarkupToString(markup) {
13
24
  let renderedHtml = "";
14
25
  for (const chunk of render(this.createRenderableMarkup(markup))) {
15
26
  renderedHtml += chunk;
16
27
  }
17
- return renderedHtml;
28
+ return this.normalizeDeclarativeShadowRootMarkup(renderedHtml);
29
+ }
30
+ async renderValueToString(value) {
31
+ if (typeof value === "string") {
32
+ return await this.renderMarkupToString(value);
33
+ }
34
+ let renderedHtml = "";
35
+ for (const chunk of render(value)) {
36
+ renderedHtml += chunk;
37
+ }
38
+ return this.normalizeDeclarativeShadowRootMarkup(renderedHtml);
39
+ }
40
+ isLitManagedComponent(component) {
41
+ return component?.config?.integration === this.name || component?.config?.__eco?.integration === this.name;
42
+ }
43
+ injectRenderedChildren(template, renderedChildren) {
44
+ for (const marker of [
45
+ COMPONENT_CHILDREN_SLOT_MARKER,
46
+ ESCAPED_COMPONENT_CHILDREN_SLOT_MARKER,
47
+ DOUBLE_ESCAPED_COMPONENT_CHILDREN_SLOT_MARKER
48
+ ]) {
49
+ if (template.includes(marker)) {
50
+ return template.split(marker).join(renderedChildren);
51
+ }
52
+ }
53
+ if (template.includes(HTML_TEMPLATE_SLOT_MARKER)) {
54
+ return template.split(HTML_TEMPLATE_SLOT_MARKER).join(renderedChildren);
55
+ }
56
+ if (template.includes("</body>")) {
57
+ return template.replace("</body>", `${renderedChildren}</body>`);
58
+ }
59
+ if (template.includes("</html>")) {
60
+ return template.replace("</html>", `${renderedChildren}</html>`);
61
+ }
62
+ return `${template}${renderedChildren}`;
63
+ }
64
+ async renderHtmlTemplate(options) {
65
+ if (!options.isLitManagedHtmlTemplate) {
66
+ return String(
67
+ await options.HtmlTemplate({
68
+ metadata: options.metadata,
69
+ children: options.renderedChildren,
70
+ pageProps: options.pageProps
71
+ })
72
+ );
73
+ }
74
+ const template = await options.HtmlTemplate({
75
+ metadata: options.metadata,
76
+ children: HTML_TEMPLATE_SLOT_MARKER,
77
+ pageProps: options.pageProps
78
+ });
79
+ return this.injectRenderedChildren(String(template), options.renderedChildren);
18
80
  }
19
81
  /**
20
82
  * Renders a Lit component boundary for component-level orchestration.
21
83
  *
84
+ * SSR-eligible lazy scripts are preloaded first so custom elements registered
85
+ * by the component can render their server markup even when the Lit renderer is
86
+ * entered through cross-integration marker resolution.
87
+ *
22
88
  * Includes component-scoped dependency assets when declared.
23
89
  */
24
90
  async renderComponent(input) {
91
+ await this.preloadSsrLazyScripts([input.component]);
25
92
  const component = input.component;
26
- const props = input.children === void 0 ? input.props : { ...input.props, children: input.children };
93
+ const renderedChildren = input.children === void 0 ? void 0 : typeof input.children === "string" ? input.children : await this.renderValueToString(input.children);
94
+ const props = renderedChildren === void 0 ? input.props : {
95
+ ...input.props,
96
+ children: COMPONENT_CHILDREN_SLOT_MARKER
97
+ };
27
98
  const content = await component(props);
28
- const markup = String(content);
29
- const html = await this.renderMarkupToString(markup);
99
+ const renderedHtml = await this.renderValueToString(content);
100
+ const html = renderedChildren === void 0 ? renderedHtml : this.injectRenderedChildren(renderedHtml, renderedChildren);
30
101
  const hasDependencies = Boolean(input.component.config?.dependencies);
31
102
  const canResolveAssets = typeof this.assetProcessingService?.processDependencies === "function";
32
103
  const assets = hasDependencies && canResolveAssets ? await this.processComponentDependencies([input.component]) : void 0;
33
104
  return {
34
105
  html,
35
106
  canAttachAttributes: true,
36
- rootTag: this.getRootTagName(markup),
107
+ rootTag: this.getRootTagName(html),
37
108
  integrationName: this.name,
38
109
  assets
39
110
  };
@@ -89,26 +160,24 @@ class LitRenderer extends IntegrationRenderer {
89
160
  try {
90
161
  await this.preloadSsrLazyScripts([Page, Layout]);
91
162
  const pageContent = await Page({ params, query, ...props, locals });
92
- const children = Layout ? await Layout(
93
- {
163
+ const pageHtml = await this.renderValueToString(pageContent);
164
+ const children = Layout ? this.isLitManagedComponent(Layout) ? await this.renderValueToString(
165
+ await Layout({
94
166
  children: pageContent,
95
167
  locals
96
- }
97
- ) : pageContent;
98
- const template = await HtmlTemplate({
168
+ })
169
+ ) : await Layout({
170
+ children: pageHtml,
171
+ locals
172
+ }) : pageHtml;
173
+ const renderedChildren = this.normalizeDeclarativeShadowRootMarkup(String(children));
174
+ return this.DOC_TYPE + await this.renderHtmlTemplate({
175
+ HtmlTemplate,
99
176
  metadata,
100
- children: "<--content-->",
101
- pageProps: props || {}
177
+ pageProps: props || {},
178
+ renderedChildren,
179
+ isLitManagedHtmlTemplate: this.isLitManagedComponent(HtmlTemplate)
102
180
  });
103
- const [templateStart, templateEnd] = template.split("<--content-->");
104
- const DOC_TYPE = this.DOC_TYPE;
105
- function* streamBody() {
106
- yield DOC_TYPE;
107
- yield templateStart;
108
- yield* render(staticHtml`${unsafeStatic(children)}`);
109
- yield templateEnd;
110
- }
111
- return new RenderResultReadable(streamBody());
112
181
  } catch (error) {
113
182
  throw this.createRenderError("Error rendering page", error);
114
183
  }
@@ -117,45 +186,42 @@ class LitRenderer extends IntegrationRenderer {
117
186
  try {
118
187
  const viewConfig = view.config;
119
188
  const Layout = viewConfig?.layout;
120
- await this.preloadSsrLazyScripts([view, Layout]);
121
- const viewFn = view;
122
- const pageContent = await viewFn(props);
123
- if (ctx.partial) {
124
- function* streamBody2() {
125
- yield* render(staticHtml`${unsafeStatic(pageContent)}`);
126
- }
127
- const readable = new RenderResultReadable(streamBody2());
128
- return this.createHtmlResponse(readable, ctx);
129
- }
130
- const DOC_TYPE = this.DOC_TYPE;
131
- const children = Layout ? await Layout({ children: pageContent }) : pageContent;
132
- const HtmlTemplate = await this.getHtmlTemplate();
133
- const metadata = view.metadata ? await view.metadata({
189
+ const HtmlTemplate = ctx.partial ? void 0 : await this.getHtmlTemplate();
190
+ const metadata = ctx.partial || !HtmlTemplate ? void 0 : view.metadata ? await view.metadata({
134
191
  params: {},
135
192
  query: {},
136
193
  props,
137
194
  appConfig: this.appConfig
138
195
  }) : this.appConfig.defaultMetadata;
139
- await this.prepareViewDependencies(view, Layout);
140
- const template = await HtmlTemplate({
141
- metadata,
142
- children: "<--content-->",
143
- pageProps: props
144
- });
145
- const [templateStart, templateEnd] = template.split("<--content-->");
146
- function* streamBody() {
147
- yield DOC_TYPE;
148
- yield templateStart;
149
- yield* render(staticHtml`${unsafeStatic(children)}`);
150
- yield templateEnd;
196
+ await this.preloadSsrLazyScripts([view, Layout]);
197
+ if (!ctx.partial) {
198
+ await this.prepareViewDependencies(view, Layout);
151
199
  }
152
- const stream = new RenderResultReadable(streamBody());
153
- const transformedResponse = await this.htmlTransformer.transform(
154
- new Response(stream, {
155
- headers: { "Content-Type": "text/html" }
156
- })
157
- );
158
- return this.createHtmlResponse(transformedResponse.body, ctx);
200
+ const viewFn = view;
201
+ const renderExecution = await this.captureHtmlRender(async () => {
202
+ const pageContent = await viewFn(props);
203
+ const pageHtml = await this.renderValueToString(pageContent);
204
+ if (ctx.partial) {
205
+ return this.normalizeDeclarativeShadowRootMarkup(pageHtml);
206
+ }
207
+ const children = Layout ? this.isLitManagedComponent(Layout) ? await this.renderValueToString(await Layout({ children: pageContent })) : await Layout({ children: pageHtml }) : pageHtml;
208
+ const renderedChildren = this.normalizeDeclarativeShadowRootMarkup(String(children));
209
+ return this.DOC_TYPE + await this.renderHtmlTemplate({
210
+ HtmlTemplate,
211
+ metadata: metadata ?? this.appConfig.defaultMetadata,
212
+ pageProps: props ?? {},
213
+ renderedChildren,
214
+ isLitManagedHtmlTemplate: this.isLitManagedComponent(HtmlTemplate)
215
+ });
216
+ });
217
+ const componentsToResolve = ctx.partial ? [view] : [HtmlTemplate, Layout, view].filter(Boolean);
218
+ const body = await this.finalizeCapturedHtmlRender({
219
+ html: renderExecution.html,
220
+ componentsToResolve,
221
+ graphContext: renderExecution.graphContext,
222
+ partial: ctx.partial
223
+ });
224
+ return this.createHtmlResponse(body, ctx);
159
225
  } catch (error) {
160
226
  throw this.createRenderError("Error rendering view", error);
161
227
  }
@@ -6,6 +6,7 @@ type ProcessDependencies = (dependencies: AssetDefinition[], integrationName: st
6
6
  export interface LitSsrLazyPreloaderOptions {
7
7
  resolveDependencyPath: (componentDir: string, sourcePath: string) => string;
8
8
  processDependencies?: ProcessDependencies;
9
+ preferSourceImports?: boolean;
9
10
  }
10
11
  /**
11
12
  * Encapsulates SSR lazy script preload behavior for Lit components.
@@ -17,10 +18,11 @@ export interface LitSsrLazyPreloaderOptions {
17
18
  export declare class LitSsrLazyPreloader {
18
19
  private readonly resolveDependencyPath;
19
20
  private readonly processDependencies?;
21
+ private readonly preferSourceImports;
20
22
  private readonly ssrPreloadedScripts;
21
23
  private readonly ssrPreloadFailedScripts;
22
24
  private readonly ssrPreloadEntrypointCache;
23
- constructor({ resolveDependencyPath, processDependencies }: LitSsrLazyPreloaderOptions);
25
+ constructor({ resolveDependencyPath, processDependencies, preferSourceImports }: LitSsrLazyPreloaderOptions);
24
26
  /**
25
27
  * Detects preload failures that are expected for browser-only modules.
26
28
  */
@@ -4,12 +4,14 @@ import { pathToFileURL } from "node:url";
4
4
  class LitSsrLazyPreloader {
5
5
  resolveDependencyPath;
6
6
  processDependencies;
7
+ preferSourceImports;
7
8
  ssrPreloadedScripts = /* @__PURE__ */ new Set();
8
9
  ssrPreloadFailedScripts = /* @__PURE__ */ new Set();
9
10
  ssrPreloadEntrypointCache = /* @__PURE__ */ new Map();
10
- constructor({ resolveDependencyPath, processDependencies }) {
11
+ constructor({ resolveDependencyPath, processDependencies, preferSourceImports }) {
11
12
  this.resolveDependencyPath = resolveDependencyPath;
12
13
  this.processDependencies = processDependencies;
14
+ this.preferSourceImports = preferSourceImports ?? typeof Bun !== "undefined";
13
15
  }
14
16
  /**
15
17
  * Detects preload failures that are expected for browser-only modules.
@@ -87,7 +89,10 @@ class LitSsrLazyPreloader {
87
89
  return;
88
90
  }
89
91
  try {
90
- await import(pathToFileURL(preloadEntrypoint).href);
92
+ await import(
93
+ /* @vite-ignore */
94
+ pathToFileURL(preloadEntrypoint).href
95
+ );
91
96
  this.ssrPreloadedScripts.add(scriptPath);
92
97
  } catch (error) {
93
98
  this.ssrPreloadFailedScripts.add(scriptPath);
@@ -112,7 +117,12 @@ class LitSsrLazyPreloader {
112
117
  if (cachedEntrypoint) {
113
118
  return cachedEntrypoint;
114
119
  }
120
+ if (this.preferSourceImports) {
121
+ this.ssrPreloadEntrypointCache.set(scriptPath, scriptPath);
122
+ return scriptPath;
123
+ }
115
124
  if (!this.processDependencies) {
125
+ this.ssrPreloadEntrypointCache.set(scriptPath, scriptPath);
116
126
  return scriptPath;
117
127
  }
118
128
  try {
@@ -140,6 +150,7 @@ class LitSsrLazyPreloader {
140
150
  if (process.env.ECOPAGES_DEBUG === "true") {
141
151
  console.warn(`[ecopages][lit] Failed to resolve SSR preload entrypoint for: ${scriptPath}`, error);
142
152
  }
153
+ this.ssrPreloadEntrypointCache.set(scriptPath, scriptPath);
143
154
  return scriptPath;
144
155
  }
145
156
  }
@@ -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
- import './console';
7
- import { IntegrationPlugin, type IntegrationPluginConfig } from '@ecopages/core/plugins/integration-plugin';
5
+ import './console.js';
6
+ import { IntegrationPlugin, type ComponentBoundaryPolicyInput, 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,6 +18,17 @@ export declare class LitPlugin extends IntegrationPlugin {
19
18
  renderer: typeof LitRenderer;
20
19
  constructor(options?: Omit<IntegrationPluginConfig, 'name'>);
21
20
  getDependencies(): AssetDefinition[];
21
+ /**
22
+ * Declares Lit's cross-integration boundary deferral rule.
23
+ *
24
+ * When a non-Lit render pass enters a Lit component boundary, the boundary is
25
+ * deferred into marker-graph resolution so Lit SSR can render custom elements,
26
+ * declarative shadow DOM, and other Lit-owned output through the Lit renderer.
27
+ *
28
+ * @param input Boundary metadata for the active render pass.
29
+ * @returns `true` when the boundary should be deferred into the marker pass.
30
+ */
31
+ shouldDeferComponentBoundary(input: ComponentBoundaryPolicyInput): boolean;
22
32
  }
23
33
  /**
24
34
  * Factory function to create a Lit plugin instance
package/src/lit.plugin.js CHANGED
@@ -1,6 +1,7 @@
1
- import "@lit-labs/ssr/lib/install-global-dom-shim.js";
2
- import "./console";
3
- import { IntegrationPlugin } from "@ecopages/core/plugins/integration-plugin";
1
+ import "./console.js";
2
+ import {
3
+ IntegrationPlugin
4
+ } from "@ecopages/core/plugins/integration-plugin";
4
5
  import { AssetFactory } from "@ecopages/core/services/asset-processing-service";
5
6
  import { litElementHydrateScript } from "./lit-element-hydrate.js";
6
7
  import { LitRenderer } from "./lit-renderer.js";
@@ -40,6 +41,19 @@ class LitPlugin extends IntegrationPlugin {
40
41
  })
41
42
  ];
42
43
  }
44
+ /**
45
+ * Declares Lit's cross-integration boundary deferral rule.
46
+ *
47
+ * When a non-Lit render pass enters a Lit component boundary, the boundary is
48
+ * deferred into marker-graph resolution so Lit SSR can render custom elements,
49
+ * declarative shadow DOM, and other Lit-owned output through the Lit renderer.
50
+ *
51
+ * @param input Boundary metadata for the active render pass.
52
+ * @returns `true` when the boundary should be deferred into the marker pass.
53
+ */
54
+ shouldDeferComponentBoundary(input) {
55
+ return input.targetIntegration === this.name && input.currentIntegration !== this.name;
56
+ }
43
57
  }
44
58
  function litPlugin(options) {
45
59
  return new LitPlugin(options);