@ecopages/react 0.2.0-beta.4 → 0.2.0-beta.41

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.
Files changed (142) hide show
  1. package/README.md +175 -7
  2. package/package.json +54 -29
  3. package/src/app/client-only.js +15 -0
  4. package/src/app/dynamic.js +9 -0
  5. package/src/app/dynamic.test.browser.js +31 -0
  6. package/src/bundling/bootstrap-package-paths.d.ts +5 -0
  7. package/src/bundling/bootstrap-package-paths.js +5 -0
  8. package/src/{services/react-bundle.service.d.ts → bundling/bundle.d.ts} +23 -8
  9. package/src/bundling/bundle.js +120 -0
  10. package/src/bundling/discover-layout-runtime-modules.d.ts +32 -0
  11. package/src/bundling/discover-layout-runtime-modules.js +191 -0
  12. package/src/bundling/runtime-alias-map.d.ts +13 -0
  13. package/src/bundling/runtime-alias-map.js +113 -0
  14. package/src/{services/react-runtime-bundle.service.d.ts → bundling/runtime-bundle.d.ts} +30 -5
  15. package/src/bundling/runtime-bundle.js +258 -0
  16. package/src/bundling/runtime-interop-plugin.js +34 -0
  17. package/src/bundling/runtime-mode.d.ts +9 -0
  18. package/src/bundling/runtime-mode.js +11 -0
  19. package/src/bundling/runtime-modules.d.ts +29 -0
  20. package/src/bundling/runtime-modules.js +49 -0
  21. package/src/client-graph/ast-transform.d.ts +18 -0
  22. package/src/client-graph/ast-transform.js +426 -0
  23. package/src/{utils/client-graph-boundary-cache.d.ts → client-graph/boundary-cache.d.ts} +23 -13
  24. package/src/client-graph/boundary-cache.js +182 -0
  25. package/src/{utils/client-graph-boundary-plugin.d.ts → client-graph/boundary-plugin.d.ts} +4 -13
  26. package/src/client-graph/boundary-plugin.js +149 -0
  27. package/src/client-graph/component-config-traversal.js +77 -0
  28. package/src/client-graph/declared-modules.js +69 -0
  29. package/src/client-graph/module-classification.d.ts +3 -0
  30. package/src/client-graph/module-classification.js +43 -0
  31. package/src/{utils → client-graph}/reachability-analyzer.d.ts +4 -12
  32. package/src/client-graph/reachability-analyzer.js +515 -0
  33. package/src/client-graph/specifier-classification.d.ts +75 -0
  34. package/src/client-graph/specifier-classification.js +223 -0
  35. package/src/contracts/router-adapter.js +6 -0
  36. package/src/dev-transform/react-dev-transform-contributor.d.ts +17 -0
  37. package/src/dev-transform/react-dev-transform-contributor.js +22 -0
  38. package/src/hmr/hmr-scripts.js +44 -0
  39. package/src/hmr/hmr-strategy.d.ts +74 -0
  40. package/src/hmr/hmr-strategy.js +351 -0
  41. package/src/{services/react-hmr-page-metadata-cache.d.ts → hmr/page-metadata-cache.d.ts} +5 -1
  42. package/src/hmr/page-metadata-cache.js +44 -0
  43. package/src/hmr/react-hmr-dev-transform-plugins.d.ts +23 -0
  44. package/src/hmr/react-hmr-dev-transform-plugins.js +20 -0
  45. package/src/hydration/assert-no-bare-ecopages-imports.d.ts +4 -0
  46. package/src/hydration/assert-no-bare-ecopages-imports.js +7 -0
  47. package/src/hydration/bootstrap-imports.d.ts +25 -0
  48. package/src/hydration/bootstrap-imports.js +33 -0
  49. package/src/hydration/hydration-asset.d.ts +84 -0
  50. package/src/hydration/hydration-asset.js +227 -0
  51. package/src/hydration/hydration-scripts.d.ts +104 -0
  52. package/src/hydration/hydration-scripts.js +483 -0
  53. package/src/hydration/hydration-scripts.test.browser.js +199 -0
  54. package/src/hydration/page-data-manifest.d.ts +58 -0
  55. package/src/hydration/page-data-manifest.js +97 -0
  56. package/src/hydration/page-data-reader.d.ts +31 -0
  57. package/src/hydration/page-data-reader.js +77 -0
  58. package/src/hydration/page-payload.d.ts +67 -0
  59. package/src/hydration/page-payload.js +106 -0
  60. package/src/hydration/serialize-page-data-script.d.ts +27 -0
  61. package/src/hydration/serialize-page-data-script.js +30 -0
  62. package/src/{services/react-mdx-config-dependency.service.d.ts → mdx/mdx-config-dependency.d.ts} +6 -6
  63. package/src/mdx/mdx-config-dependency.js +110 -0
  64. package/src/mdx/mdx-loader-plugin.d.ts +2 -0
  65. package/src/mdx/mdx-loader-plugin.js +8 -0
  66. package/src/page/page-module.d.ts +46 -0
  67. package/src/page/page-module.js +74 -0
  68. package/src/plugin/react.constants.js +1 -0
  69. package/src/{react.plugin.d.ts → plugin/react.plugin.d.ts} +9 -3
  70. package/src/plugin/react.plugin.js +241 -0
  71. package/src/plugin/react.types.d.ts +152 -0
  72. package/src/plugin/react.types.js +1 -0
  73. package/src/render/component-ownership.d.ts +49 -0
  74. package/src/render/component-ownership.js +46 -0
  75. package/src/render/component-ssr.d.ts +113 -0
  76. package/src/render/component-ssr.js +180 -0
  77. package/src/render/eco-embed.js +9 -0
  78. package/src/render/errors.d.ts +16 -0
  79. package/src/render/errors.js +23 -0
  80. package/src/render/html-boundary.js +59 -0
  81. package/src/render/layout-compose.d.ts +66 -0
  82. package/src/render/layout-compose.js +134 -0
  83. package/src/render/react-renderer.d.ts +138 -0
  84. package/src/render/react-renderer.js +448 -0
  85. package/src/render/react-runtime.d.ts +13 -0
  86. package/src/render/react-runtime.js +26 -0
  87. package/src/render/unified-layout-composition.d.ts +53 -0
  88. package/src/render/unified-layout-composition.js +78 -0
  89. package/src/runtime/use-sync-external-store-with-selector.js +52 -54
  90. package/src/eco-embed.js +0 -11
  91. package/src/react-hmr-strategy.d.ts +0 -230
  92. package/src/react-hmr-strategy.js +0 -657
  93. package/src/react-renderer.d.ts +0 -233
  94. package/src/react-renderer.js +0 -622
  95. package/src/react.constants.js +0 -4
  96. package/src/react.plugin.js +0 -205
  97. package/src/react.types.d.ts +0 -88
  98. package/src/react.types.js +0 -0
  99. package/src/router-adapter.js +0 -0
  100. package/src/services/pages-index.d.ts +0 -64
  101. package/src/services/pages-index.js +0 -73
  102. package/src/services/react-bundle.service.js +0 -86
  103. package/src/services/react-hmr-page-metadata-cache.js +0 -35
  104. package/src/services/react-hydration-asset.service.d.ts +0 -84
  105. package/src/services/react-hydration-asset.service.js +0 -217
  106. package/src/services/react-mdx-config-dependency.service.js +0 -122
  107. package/src/services/react-page-module.service.d.ts +0 -88
  108. package/src/services/react-page-module.service.js +0 -166
  109. package/src/services/react-page-payload.service.d.ts +0 -46
  110. package/src/services/react-page-payload.service.js +0 -67
  111. package/src/services/react-runtime-bundle.service.js +0 -186
  112. package/src/utils/client-graph-boundary-cache.js +0 -116
  113. package/src/utils/client-graph-boundary-plugin.js +0 -552
  114. package/src/utils/client-only.js +0 -19
  115. package/src/utils/component-config-traversal.js +0 -54
  116. package/src/utils/declared-modules.js +0 -47
  117. package/src/utils/dynamic.js +0 -12
  118. package/src/utils/dynamic.test.browser.js +0 -33
  119. package/src/utils/hmr-scripts.js +0 -31
  120. package/src/utils/html-boundary.js +0 -55
  121. package/src/utils/hydration-scripts.d.ts +0 -92
  122. package/src/utils/hydration-scripts.js +0 -358
  123. package/src/utils/hydration-scripts.test.browser.js +0 -198
  124. package/src/utils/reachability-analyzer.js +0 -339
  125. package/src/utils/react-dom-runtime-interop-plugin.js +0 -38
  126. package/src/utils/react-mdx-loader-plugin.d.ts +0 -3
  127. package/src/utils/react-mdx-loader-plugin.js +0 -45
  128. package/src/utils/react-runtime-alias-map.d.ts +0 -8
  129. package/src/utils/react-runtime-alias-map.js +0 -90
  130. /package/src/{utils → app}/client-only.d.ts +0 -0
  131. /package/src/{utils → app}/dynamic.d.ts +0 -0
  132. /package/src/{utils → app}/dynamic.test.browser.d.ts +0 -0
  133. /package/src/{utils/react-dom-runtime-interop-plugin.d.ts → bundling/runtime-interop-plugin.d.ts} +0 -0
  134. /package/src/{utils → client-graph}/component-config-traversal.d.ts +0 -0
  135. /package/src/{utils → client-graph}/declared-modules.d.ts +0 -0
  136. /package/src/{declarations.d.ts → contracts/declarations.d.ts} +0 -0
  137. /package/src/{router-adapter.d.ts → contracts/router-adapter.d.ts} +0 -0
  138. /package/src/{utils → hmr}/hmr-scripts.d.ts +0 -0
  139. /package/src/{utils → hydration}/hydration-scripts.test.browser.d.ts +0 -0
  140. /package/src/{react.constants.d.ts → plugin/react.constants.d.ts} +0 -0
  141. /package/src/{eco-embed.d.ts → render/eco-embed.d.ts} +0 -0
  142. /package/src/{utils → render}/html-boundary.d.ts +0 -0
@@ -1,622 +0,0 @@
1
- import {
2
- IntegrationRenderer
3
- } from "@ecopages/core/route-renderer/integration-renderer";
4
- import { RESOLVED_ASSETS_DIR } from "@ecopages/core/constants";
5
- import { getAppBuildExecutor } from "@ecopages/core/build/build-adapter";
6
- import { ECO_DOCUMENT_OWNER_ATTRIBUTE } from "@ecopages/core/router/navigation-coordinator";
7
- import { createRequire } from "node:module";
8
- import path from "node:path";
9
- import { REACT_PLUGIN_NAME } from "./react.constants.js";
10
- import { hasSingleRootElement } from "./utils/html-boundary.js";
11
- import { ReactBundleService } from "./services/react-bundle.service.js";
12
- import { ReactHmrPageMetadataCache } from "./services/react-hmr-page-metadata-cache.js";
13
- import { ReactMdxConfigDependencyService } from "./services/react-mdx-config-dependency.service.js";
14
- import { ReactPageModuleService } from "./services/react-page-module.service.js";
15
- import { ReactPagePayloadService } from "./services/react-page-payload.service.js";
16
- import { getReactIslandComponentKey, ReactHydrationAssetService } from "./services/react-hydration-asset.service.js";
17
- class ReactRenderError extends Error {
18
- constructor(message) {
19
- super(message);
20
- this.name = "ReactRenderError";
21
- }
22
- }
23
- class BundleError extends Error {
24
- logs;
25
- constructor(message, logs) {
26
- super(message);
27
- this.name = "BundleError";
28
- this.logs = logs;
29
- }
30
- }
31
- class ReactRenderer extends IntegrationRenderer {
32
- name = REACT_PLUGIN_NAME;
33
- componentDirectory = RESOLVED_ASSETS_DIR;
34
- reactRuntimeModules;
35
- routerAdapter;
36
- mdxCompilerOptions;
37
- mdxExtensions;
38
- hmrPageMetadataCache;
39
- /**
40
- * Enables explicit graph behavior for React page-entry bundling.
41
- *
42
- * When true, page-entry bundles disable AST server-only stripping and rely
43
- * on explicit dependency declarations for browser graph composition.
44
- */
45
- explicitGraphEnabled;
46
- /** @internal */
47
- bundleService;
48
- /** @internal */
49
- pageModuleService;
50
- /** @internal */
51
- hydrationAssetService;
52
- /** @internal */
53
- pagePayloadService;
54
- /** @internal */
55
- mdxConfigDependencyService;
56
- constructor(options) {
57
- const { reactConfig, ...rendererOptions } = options;
58
- super(rendererOptions);
59
- this.routerAdapter = reactConfig?.routerAdapter;
60
- this.mdxCompilerOptions = reactConfig?.mdxCompilerOptions;
61
- this.mdxExtensions = reactConfig?.mdxExtensions ?? [".mdx"];
62
- this.hmrPageMetadataCache = reactConfig?.hmrPageMetadataCache;
63
- this.explicitGraphEnabled = reactConfig?.explicitGraphEnabled ?? false;
64
- this.bundleService = new ReactBundleService({
65
- rootDir: this.appConfig.rootDir,
66
- routerAdapter: this.routerAdapter,
67
- mdxCompilerOptions: this.mdxCompilerOptions,
68
- jsxImportSource: (this.appConfig.integrations ?? []).find((integration) => integration.name === this.name)?.jsxImportSource,
69
- nonReactExtensions: (this.appConfig.integrations ?? []).filter((integration) => integration.name !== this.name).flatMap((integration) => integration.extensions)
70
- });
71
- this.pageModuleService = new ReactPageModuleService({
72
- rootDir: this.appConfig.rootDir,
73
- distDir: this.appConfig.absolutePaths.distDir,
74
- workDir: this.appConfig.absolutePaths.workDir,
75
- buildExecutor: getAppBuildExecutor(this.appConfig),
76
- layoutsDir: this.appConfig.absolutePaths.layoutsDir,
77
- componentsDir: this.appConfig.absolutePaths.componentsDir,
78
- mdxCompilerOptions: this.mdxCompilerOptions,
79
- mdxExtensions: this.mdxExtensions,
80
- integrationName: this.name,
81
- hasRouterAdapter: Boolean(this.routerAdapter)
82
- });
83
- this.hydrationAssetService = new ReactHydrationAssetService({
84
- srcDir: this.appConfig.srcDir,
85
- routerAdapter: this.routerAdapter,
86
- assetProcessingService: this.assetProcessingService,
87
- bundleService: this.bundleService,
88
- hmrPageMetadataCache: this.hmrPageMetadataCache
89
- });
90
- this.pagePayloadService = new ReactPagePayloadService();
91
- this.mdxConfigDependencyService = new ReactMdxConfigDependencyService({
92
- integrationName: this.name,
93
- pageModuleService: this.pageModuleService,
94
- assetProcessingService: this.assetProcessingService
95
- });
96
- }
97
- shouldRenderPageComponent() {
98
- return false;
99
- }
100
- /**
101
- * Reads the declared integration name for a component or layout.
102
- *
103
- * We honor both the explicit `config.integration` override and injected
104
- * `config.__eco.integration` metadata because pages can arrive here through
105
- * authored config as well as build-time component metadata.
106
- */
107
- getComponentIntegration(component) {
108
- return component?.config?.integration ?? component?.config?.__eco?.integration;
109
- }
110
- /**
111
- * Returns whether a component should stay inside the React render lane.
112
- *
113
- * Components without explicit integration metadata are treated as React-owned
114
- * here because this renderer only receives them after the route pipeline has
115
- * already selected the React integration.
116
- */
117
- isReactManagedComponent(component) {
118
- const integration = this.getComponentIntegration(component);
119
- return integration === void 0 || integration === this.name;
120
- }
121
- getComponentRequires(component) {
122
- return component?.requires;
123
- }
124
- getRouterDocumentAttributes() {
125
- if (!this.routerAdapter) {
126
- return void 0;
127
- }
128
- return {
129
- [ECO_DOCUMENT_OWNER_ATTRIBUTE]: "react-router"
130
- };
131
- }
132
- /**
133
- * Commits a framework-agnostic component to React semantics.
134
- *
135
- * This is one of the two real cast boundaries in this file. Core keeps
136
- * `EcoComponent` broad so integrations can share the same public surface; once
137
- * the React renderer is executing, `createElement()` needs a concrete React
138
- * component signature.
139
- */
140
- asReactComponent(component) {
141
- return component;
142
- }
143
- /**
144
- * Commits a mixed-shell component to the string-returning contract required by
145
- * non-React layouts and HTML templates.
146
- *
147
- * This is the second real cast boundary: once we decide a shell is not managed
148
- * by React, we call it directly and require serialized HTML back.
149
- */
150
- asNonReactShellComponent(component) {
151
- return component;
152
- }
153
- resolveReactRuntimeModules() {
154
- const appPackageJsonPath = path.resolve(this.appConfig.rootDir || process.cwd(), "package.json");
155
- try {
156
- const requireFromApp = createRequire(appPackageJsonPath);
157
- return {
158
- react: requireFromApp("react"),
159
- reactDomServer: requireFromApp("react-dom/server")
160
- };
161
- } catch {
162
- const requireFromIntegration = createRequire(import.meta.url);
163
- return {
164
- react: requireFromIntegration("react"),
165
- reactDomServer: requireFromIntegration("react-dom/server")
166
- };
167
- }
168
- }
169
- getReactRuntimeModules() {
170
- this.reactRuntimeModules ??= this.resolveReactRuntimeModules();
171
- return this.reactRuntimeModules;
172
- }
173
- /**
174
- * Appends route hydration assets for a concrete page/view file to the current
175
- * HTML transformer state.
176
- */
177
- async appendHydrationAssetsForFile(filePath) {
178
- if (!filePath) {
179
- return;
180
- }
181
- const pageBrowserGraph = await this.resolvePageBrowserGraphForFile(filePath);
182
- this.mergePageBrowserGraphIntoPagePackage(pageBrowserGraph);
183
- }
184
- /**
185
- * Renders a non-React layout or HTML template and enforces that mixed shells
186
- * return serialized HTML.
187
- *
188
- * The React renderer can compose through another integration's shell, but only
189
- * if that shell yields a string that can be inserted into the final document.
190
- */
191
- async renderNonReactShellComponent(Component, props, label) {
192
- const output = await Component(props);
193
- if (typeof output === "string") {
194
- return output;
195
- }
196
- throw new ReactRenderError(`${label} must return a string when used as a mixed shell for React pages.`);
197
- }
198
- /**
199
- * Renders one React component while preserving already-resolved child HTML.
200
- *
201
- * When nested foreign-subtree resolution has already produced child HTML for this
202
- * component, the child payload must remain raw SSR output rather than a React
203
- * string child, otherwise React would escape it. This helper renders a unique
204
- * token through React and swaps that token back to the resolved HTML
205
- * afterward.
206
- *
207
- * @param input Component render input for the current render step.
208
- * @param context React-specific render context for stable token generation.
209
- * @returns Serialized component HTML with resolved child markup preserved.
210
- */
211
- renderComponentHtml(input, context, runtimeContext) {
212
- const { react, reactDomServer } = this.getReactRuntimeModules();
213
- if (input.children === void 0) {
214
- return this.normalizeUnresolvedMarkerArtifactHtml(
215
- reactDomServer.renderToString(react.createElement(this.asReactComponent(input.component), input.props))
216
- );
217
- }
218
- const resolvedChildHtml = typeof input.children === "string" ? input.children : String(input.children ?? "");
219
- const rawChildrenToken = `__ECO_RAW_HTML_CHILD_${context.componentInstanceId ?? "component"}__`;
220
- if (runtimeContext) {
221
- runtimeContext.rawChildrenToken = rawChildrenToken;
222
- runtimeContext.rawChildrenHtml = resolvedChildHtml;
223
- }
224
- const html = reactDomServer.renderToString(
225
- react.createElement(this.asReactComponent(input.component), input.props, rawChildrenToken)
226
- );
227
- return this.normalizeUnresolvedMarkerArtifactHtml(html.split(rawChildrenToken).join(resolvedChildHtml));
228
- }
229
- /**
230
- * Restores raw child HTML that was temporarily replaced by a token during React SSR.
231
- *
232
- * Queued foreign-subtree resolution may render children through a fragment path before all
233
- * nested integration tokens are resolved. When that happens, React must never see
234
- * the resolved child HTML as a normal string child or it would escape it. The
235
- * runtime context stores the placeholder token and the raw child HTML so the
236
- * fragment render path can reinsert it before foreign-subtree tokens are handled.
237
- */
238
- restoreRuntimeChildHtml(html, runtimeContext) {
239
- if (!runtimeContext?.rawChildrenToken || runtimeContext.rawChildrenHtml === void 0) {
240
- return html;
241
- }
242
- return html.split(runtimeContext.rawChildrenToken).join(runtimeContext.rawChildrenHtml);
243
- }
244
- /**
245
- * Renders queued child content through React and then resolves nested foreign-subtree tokens.
246
- *
247
- * This path is only used for children that were deferred while React rendered the
248
- * parent component. It first restores any raw child HTML placeholders owned by the
249
- * current runtime context, then asks the shared queued foreign-subtree resolver to swap
250
- * foreign integration tokens with their resolved HTML.
251
- */
252
- async renderQueuedChildrenToHtml(children, runtimeContext, queuedResolutionsByToken, resolveToken) {
253
- if (children === void 0) {
254
- return void 0;
255
- }
256
- const { react, reactDomServer } = this.getReactRuntimeModules();
257
- let html = this.normalizeUnresolvedMarkerArtifactHtml(
258
- reactDomServer.renderToString(react.createElement(react.Fragment, null, children))
259
- );
260
- html = this.restoreRuntimeChildHtml(html, runtimeContext);
261
- html = await this.foreignSubtreeExecutionService.resolveQueuedTokens(
262
- html,
263
- queuedResolutionsByToken,
264
- resolveToken
265
- );
266
- return html;
267
- }
268
- /**
269
- * Resolves queued renderer-owned foreign-subtree tokens produced during React component rendering.
270
- *
271
- * React components can enqueue nested foreign subtrees while the parent HTML is being
272
- * rendered. This delegates to the shared renderer-owned queue resolver but keeps
273
- * the React-specific child rendering behavior local so raw child HTML and React's
274
- * fragment rendering semantics stay coordinated.
275
- */
276
- async resolveQueuedForeignSubtreeHtml(html, runtimeContext) {
277
- return this.foreignSubtreeExecutionService.resolveQueuedHtml({
278
- currentIntegrationName: this.name,
279
- html,
280
- runtimeContext,
281
- queueLabel: "React",
282
- getOwningRenderer: (integrationName, rendererCache) => this.getIntegrationRendererForName(integrationName, rendererCache),
283
- applyAttributesToFirstElement: (resolvedHtml, attributes) => this.htmlTransformer.applyAttributesToFirstElement(resolvedHtml, attributes),
284
- dedupeProcessedAssets: (assets) => this.htmlTransformer.dedupeProcessedAssets(assets),
285
- renderQueuedChildren: async (children, currentRuntimeContext, queuedResolutionsByToken, resolveToken) => {
286
- const renderedHtml = await this.renderQueuedChildrenToHtml(
287
- children,
288
- currentRuntimeContext,
289
- queuedResolutionsByToken,
290
- resolveToken
291
- );
292
- return {
293
- assets: [],
294
- html: renderedHtml
295
- };
296
- }
297
- });
298
- }
299
- buildHydrationProps(props) {
300
- if (!props || !Object.prototype.hasOwnProperty.call(props, "locals")) {
301
- return props ?? {};
302
- }
303
- const { locals: _locals, ...hydrationProps } = props;
304
- return hydrationProps;
305
- }
306
- /**
307
- * Builds shared document html contributions for router-backed React pages rendered
308
- * through a non-React HTML shell.
309
- */
310
- buildNonReactDocumentContributions(htmlTemplate, pageProps) {
311
- if (this.isReactManagedComponent(htmlTemplate) || !this.routerAdapter) {
312
- return void 0;
313
- }
314
- return [
315
- {
316
- placement: "head-append",
317
- html: this.pagePayloadService.buildRouterPageDataScript(pageProps)
318
- }
319
- ];
320
- }
321
- /**
322
- * Renders a foreign integration component that participates in React composition.
323
- *
324
- * Non-React components must resolve to serialized HTML so React can embed them as
325
- * mixed-shell children. Any component-owned dependencies still need to flow
326
- * through the shared dependency resolver before queued foreign-subtree tokens are finalized.
327
- */
328
- async renderForeignComponentWithSerializedHtml(input, runtimeContext) {
329
- let props = input.props;
330
- if (input.children !== void 0) {
331
- props = {
332
- ...input.props,
333
- children: typeof input.children === "string" ? input.children : String(input.children ?? "")
334
- };
335
- }
336
- const html = await this.renderNonReactShellComponent(
337
- this.asNonReactShellComponent(input.component),
338
- props,
339
- "Component"
340
- );
341
- const hasDependencies = Boolean(input.component.config?.dependencies);
342
- const canResolveAssets = typeof this.assetProcessingService?.processDependencies === "function";
343
- const assets = hasDependencies && canResolveAssets ? await this.processComponentDependencies([input.component]) : void 0;
344
- const queuedForeignSubtreeResolution = await this.resolveQueuedForeignSubtreeHtml(html, runtimeContext);
345
- const mergedAssets = this.htmlTransformer.dedupeProcessedAssets([
346
- ...assets ?? [],
347
- ...queuedForeignSubtreeResolution.assets
348
- ]);
349
- return {
350
- html: queuedForeignSubtreeResolution.html,
351
- canAttachAttributes: true,
352
- rootTag: this.getRootTagName(queuedForeignSubtreeResolution.html),
353
- integrationName: this.name,
354
- assets: mergedAssets.length > 0 ? mergedAssets : void 0
355
- };
356
- }
357
- /**
358
- * Renders a React-owned component and attaches island hydration metadata when possible.
359
- *
360
- * This path keeps React-owned SSR, queued foreign-subtree resolution, and optional
361
- * island hydration wiring together so the public `renderComponent()` method can
362
- * read as orchestration rather than implementation detail.
363
- */
364
- async renderReactManagedComponent(input, runtimeContext) {
365
- const componentConfig = input.component.config;
366
- const context = {
367
- componentInstanceId: input.integrationContext?.componentInstanceId
368
- };
369
- const hasResolvedChildHtml = input.children !== void 0;
370
- let html = this.renderComponentHtml(input, context, runtimeContext);
371
- const queuedForeignSubtreeResolution = await this.resolveQueuedForeignSubtreeHtml(html, runtimeContext);
372
- html = queuedForeignSubtreeResolution.html;
373
- const canAttachAttributes = hasSingleRootElement(html);
374
- const rootTag = this.getRootTagName(html);
375
- const componentFile = componentConfig?.__eco?.file;
376
- let rootAttributes;
377
- let assets;
378
- if (canAttachAttributes && componentFile && context.componentInstanceId && this.assetProcessingService && !hasResolvedChildHtml) {
379
- const componentInstanceId = context.componentInstanceId;
380
- assets = await this.hydrationAssetService.buildComponentRenderAssets(componentFile, componentConfig);
381
- rootAttributes = {
382
- "data-eco-component-id": componentInstanceId,
383
- "data-eco-component-key": getReactIslandComponentKey(componentFile, componentConfig),
384
- "data-eco-props": btoa(JSON.stringify(this.buildHydrationProps(input.props)))
385
- };
386
- }
387
- const mergedAssets = this.htmlTransformer.dedupeProcessedAssets([
388
- ...assets ?? [],
389
- ...queuedForeignSubtreeResolution.assets
390
- ]);
391
- return {
392
- html,
393
- canAttachAttributes,
394
- rootTag,
395
- integrationName: this.name,
396
- rootAttributes,
397
- assets: mergedAssets.length > 0 ? mergedAssets : void 0
398
- };
399
- }
400
- /**
401
- * Renders a React component for component-level orchestration.
402
- *
403
- * Behavior:
404
- * - SSR always returns the component's own root HTML (no synthetic wrapper).
405
- * - When an explicit component instance id is provided, a stable
406
- * `data-eco-component-id` attribute is attached so island hydration can target it.
407
- * - Without an explicit instance id, component renders remain plain SSR output.
408
- * - When resolved child HTML is provided, that foreign subtree is treated as a pure SSR
409
- * composition step and does not emit hydration assets for the parent wrapper.
410
- *
411
- * This preserves DOM shape for global CSS/layout selectors while keeping a
412
- * deterministic mount target per component instance.
413
- */
414
- async renderComponent(input) {
415
- const runtimeContext = this.getQueuedForeignSubtreeResolutionContext(input);
416
- if (!this.isReactManagedComponent(input.component)) {
417
- return this.renderForeignComponentWithSerializedHtml(input, runtimeContext);
418
- }
419
- return this.renderReactManagedComponent(input, runtimeContext);
420
- }
421
- createForeignChildRuntime(options) {
422
- return this.createQueuedForeignSubtreeExecutionRuntime({
423
- renderInput: options.renderInput,
424
- rendererCache: options.rendererCache,
425
- createRuntimeContext: (integrationContext, rendererCache) => ({
426
- rendererCache,
427
- componentInstanceScope: integrationContext.componentInstanceId,
428
- nextForeignSubtreeId: 0,
429
- queuedResolutions: [],
430
- rawChildrenToken: void 0,
431
- rawChildrenHtml: void 0
432
- })
433
- });
434
- }
435
- /**
436
- * Checks if the given file path corresponds to an MDX file based on configured extensions.
437
- * @param filePath - The file path to check
438
- * @returns True if the file is an MDX file
439
- */
440
- isMdxFile(filePath) {
441
- return this.pageModuleService.isMdxFile(filePath);
442
- }
443
- usesIntegrationPageImporter(file) {
444
- return this.pageModuleService.isMdxFile(file);
445
- }
446
- async importIntegrationPageFile(file, options) {
447
- return await this.pageModuleService.importMdxPageFile(file, options);
448
- }
449
- normalizeImportedPageFile(file, pageModule) {
450
- const reactModule = pageModule;
451
- const { default: Page, getMetadata, config } = reactModule;
452
- if (this.pageModuleService.isMdxFile(file) && config) {
453
- Page.config = config;
454
- }
455
- return {
456
- ...pageModule,
457
- default: Page,
458
- getMetadata,
459
- config
460
- };
461
- }
462
- async collectPageBrowserGraphContribution(context) {
463
- try {
464
- const { file: pagePath, pageModule } = context;
465
- const shouldHydrate = this.explicitGraphEnabled ? true : this.pageModuleService.shouldHydratePage(pageModule);
466
- if (!shouldHydrate) {
467
- return { assets: [] };
468
- }
469
- const isMdx = this.pageModuleService.isMdxFile(pagePath);
470
- const declaredModules = this.pageModuleService.collectPageDeclaredModules(pageModule);
471
- const dependencies = await this.hydrationAssetService.createPageBrowserGraphDependencies(
472
- pagePath,
473
- isMdx,
474
- declaredModules
475
- );
476
- const assets = [];
477
- if (isMdx) {
478
- const mdxConfigAssets = await this.mdxConfigDependencyService.processMdxConfigDependencies({
479
- pagePath,
480
- config: pageModule.config,
481
- processComponentDependencies: async (components) => await this.processComponentDependencies(components)
482
- });
483
- assets.push(...mdxConfigAssets);
484
- }
485
- return { dependencies, assets };
486
- } catch (error) {
487
- if (error instanceof BundleError) {
488
- console.error("[ecopages] Bundle errors:", error.logs);
489
- }
490
- throw new ReactRenderError(
491
- `Failed to generate hydration script: ${error instanceof Error ? error.message : String(error)}`
492
- );
493
- }
494
- }
495
- /**
496
- * Renders a full route response for the filesystem page pipeline.
497
- *
498
- * This path receives already-resolved route metadata, layout, locals, and HTML
499
- * template instances from the shared renderer orchestration. Its main job is to
500
- * serialize only the browser-safe page payload, compose the mixed React/non-
501
- * React shell tree, and hand the result back as a document body.
502
- */
503
- async render({
504
- params,
505
- query,
506
- props,
507
- locals,
508
- pageLocals,
509
- metadata,
510
- Page,
511
- Layout,
512
- HtmlTemplate,
513
- pageProps
514
- }) {
515
- try {
516
- const safeLocals = this.pagePayloadService.getSerializableLocals(locals, this.getComponentRequires(Page));
517
- const allPageProps = this.pagePayloadService.buildSerializedPageProps({
518
- pageProps,
519
- params,
520
- query,
521
- safeLocals
522
- });
523
- return await this.renderPageWithDocumentShell({
524
- page: {
525
- component: Page,
526
- props: { params, query, ...props, locals: pageLocals }
527
- },
528
- layout: Layout ? {
529
- component: Layout,
530
- props: locals ? { locals } : {}
531
- } : void 0,
532
- htmlTemplate: HtmlTemplate,
533
- metadata,
534
- pageProps: allPageProps
535
- });
536
- } catch (error) {
537
- throw this.createRenderError("Failed to render component", error);
538
- }
539
- }
540
- getHtmlDocumentContributions(options) {
541
- if (options.partial || !options.renderOptions) {
542
- return void 0;
543
- }
544
- const safeLocals = this.pagePayloadService.getSerializableLocals(
545
- options.renderOptions.locals,
546
- this.getComponentRequires(options.renderOptions.Page)
547
- );
548
- const allPageProps = this.pagePayloadService.buildSerializedPageProps({
549
- pageProps: options.renderOptions.pageProps,
550
- params: options.renderOptions.params,
551
- query: options.renderOptions.query,
552
- safeLocals
553
- });
554
- return this.buildNonReactDocumentContributions(options.renderOptions.HtmlTemplate, allPageProps);
555
- }
556
- getDocumentAttributes() {
557
- return this.getRouterDocumentAttributes();
558
- }
559
- /**
560
- * Renders an arbitrary React view through the application's HTML shell.
561
- *
562
- * Unlike route rendering, this path starts from a single component rather than a
563
- * page module discovered by the router. It still needs to resolve metadata,
564
- * layout dependencies, and hydration assets so direct `ctx.render()` calls match
565
- * normal page responses.
566
- */
567
- async renderToResponse(view, props, ctx) {
568
- try {
569
- const { react, reactDomServer } = this.getReactRuntimeModules();
570
- const viewConfig = view.config;
571
- const Layout = viewConfig?.layout;
572
- const ViewComponent = this.asReactComponent(view);
573
- const normalizedProps = props ?? {};
574
- if (ctx.partial) {
575
- return this.renderPartialViewResponse({
576
- view,
577
- props,
578
- ctx,
579
- renderInline: async () => await reactDomServer.renderToReadableStream(
580
- react.createElement(ViewComponent, normalizedProps)
581
- )
582
- });
583
- }
584
- const HtmlTemplate = await this.getHtmlTemplate();
585
- const metadata = await this.resolveViewMetadata(view, props);
586
- await this.prepareViewDependencies(view, Layout);
587
- await this.appendHydrationAssetsForFile(viewConfig?.__eco?.file);
588
- const viewRender = await this.renderComponentWithForeignChildren({
589
- component: view,
590
- props: normalizedProps
591
- });
592
- const layoutRender = Layout ? await this.renderComponentWithForeignChildren({
593
- component: Layout,
594
- props: {},
595
- children: viewRender.html
596
- }) : void 0;
597
- const documentRender = await this.renderComponentWithForeignChildren({
598
- component: HtmlTemplate,
599
- props: {
600
- metadata,
601
- pageProps: normalizedProps
602
- },
603
- children: layoutRender?.html ?? viewRender.html
604
- });
605
- this.appendProcessedDependencies(viewRender.assets, layoutRender?.assets, documentRender.assets);
606
- const transformedHtml = await this.finalizeResolvedHtml({
607
- html: `${this.DOC_TYPE}${documentRender.html}`,
608
- partial: false,
609
- documentAttributes: this.getRouterDocumentAttributes(),
610
- htmlContributions: this.buildNonReactDocumentContributions(HtmlTemplate, normalizedProps)
611
- });
612
- return this.createHtmlResponse(transformedHtml, ctx);
613
- } catch (error) {
614
- throw this.createRenderError("Failed to render view", error);
615
- }
616
- }
617
- }
618
- export {
619
- BundleError,
620
- ReactRenderError,
621
- ReactRenderer
622
- };
@@ -1,4 +0,0 @@
1
- const REACT_PLUGIN_NAME = "react";
2
- export {
3
- REACT_PLUGIN_NAME
4
- };