@ecopages/react 0.2.1-beta.0 → 0.3.0

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 (181) hide show
  1. package/README.md +164 -28
  2. package/package.json +67 -30
  3. package/src/app/client-only.js +10 -0
  4. package/src/app/dynamic.js +9 -0
  5. package/src/app/dynamic.test.browser.js +38 -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} +29 -11
  9. package/src/bundling/bundle.js +130 -0
  10. package/src/bundling/runtime-alias-map.d.ts +12 -0
  11. package/src/bundling/runtime-alias-map.js +110 -0
  12. package/src/bundling/runtime-bundle.d.ts +70 -0
  13. package/src/bundling/runtime-bundle.js +233 -0
  14. package/src/{utils/react-dom-runtime-interop-plugin.d.ts → bundling/runtime-interop-plugin.d.ts} +1 -1
  15. package/src/bundling/runtime-interop-plugin.js +32 -0
  16. package/src/bundling/runtime-mode.d.ts +1 -0
  17. package/src/bundling/runtime-mode.js +10 -0
  18. package/src/bundling/runtime-modules.d.ts +21 -0
  19. package/src/bundling/runtime-modules.js +28 -0
  20. package/src/client-graph/ast-transform-eco-page.d.ts +10 -0
  21. package/src/client-graph/ast-transform-eco-page.js +71 -0
  22. package/src/client-graph/ast-transform-import-edits.d.ts +13 -0
  23. package/src/client-graph/ast-transform-import-edits.js +75 -0
  24. package/src/client-graph/ast-transform-walk-imports.d.ts +20 -0
  25. package/src/client-graph/ast-transform-walk-imports.js +105 -0
  26. package/src/client-graph/ast-transform.d.ts +12 -0
  27. package/src/client-graph/ast-transform.js +91 -0
  28. package/src/client-graph/ast-walk.d.ts +10 -0
  29. package/src/client-graph/ast-walk.js +26 -0
  30. package/src/client-graph/boundary-plugin-read-file-inline.d.ts +7 -0
  31. package/src/client-graph/boundary-plugin-read-file-inline.js +43 -0
  32. package/src/client-graph/boundary-plugin-transform.d.ts +16 -0
  33. package/src/client-graph/boundary-plugin-transform.js +31 -0
  34. package/src/client-graph/boundary-plugin.d.ts +23 -0
  35. package/src/client-graph/boundary-plugin.js +177 -0
  36. package/src/client-graph/component-config-traversal.js +77 -0
  37. package/src/client-graph/declared-modules.js +69 -0
  38. package/src/client-graph/module-classification.d.ts +3 -0
  39. package/src/client-graph/module-classification.js +43 -0
  40. package/src/client-graph/reachability-analyzer.d.ts +26 -0
  41. package/src/client-graph/reachability-analyzer.js +55 -0
  42. package/src/client-graph/reachability-client-roots.d.ts +8 -0
  43. package/src/client-graph/reachability-client-roots.js +59 -0
  44. package/src/client-graph/reachability-export-names.d.ts +5 -0
  45. package/src/client-graph/reachability-export-names.js +34 -0
  46. package/src/client-graph/reachability-fallback-roots.d.ts +3 -0
  47. package/src/client-graph/reachability-fallback-roots.js +71 -0
  48. package/src/client-graph/reachability-preload.d.ts +10 -0
  49. package/src/client-graph/reachability-preload.js +30 -0
  50. package/src/client-graph/reachability-top-level-index.d.ts +14 -0
  51. package/src/client-graph/reachability-top-level-index.js +93 -0
  52. package/src/client-graph/reachability-traverse.d.ts +7 -0
  53. package/src/client-graph/reachability-traverse.js +201 -0
  54. package/src/client-graph/specifier-classification.d.ts +74 -0
  55. package/src/client-graph/specifier-classification.js +191 -0
  56. package/src/content/collection-component-cache.d.ts +25 -0
  57. package/src/content/collection-component-cache.js +62 -0
  58. package/src/contracts/router-adapter.js +6 -0
  59. package/src/dev-transform/react-dev-transform-contributor.d.ts +17 -0
  60. package/src/dev-transform/react-dev-transform-contributor.js +22 -0
  61. package/src/hmr/hmr-strategy-process.d.ts +34 -0
  62. package/src/hmr/hmr-strategy-process.js +83 -0
  63. package/src/hmr/hmr-strategy.d.ts +67 -0
  64. package/src/hmr/hmr-strategy.js +306 -0
  65. package/src/{services/react-hmr-page-metadata-cache.d.ts → hmr/page-metadata-cache.d.ts} +5 -1
  66. package/src/hmr/page-metadata-cache.js +44 -0
  67. package/src/hmr/react-hmr-dev-transform-plugins.d.ts +22 -0
  68. package/src/hmr/react-hmr-dev-transform-plugins.js +19 -0
  69. package/src/hydration/assert-no-bare-ecopages-imports.d.ts +4 -0
  70. package/src/hydration/assert-no-bare-ecopages-imports.js +7 -0
  71. package/src/hydration/bootstrap-imports.d.ts +25 -0
  72. package/src/hydration/bootstrap-imports.js +33 -0
  73. package/src/hydration/browser/island-hydration.d.ts +82 -0
  74. package/src/hydration/browser/island-hydration.js +251 -0
  75. package/src/hydration/browser/page-hydration-hmr.d.ts +11 -0
  76. package/src/hydration/browser/page-hydration-hmr.js +56 -0
  77. package/src/hydration/browser/page-hydration-module.d.ts +8 -0
  78. package/src/hydration/browser/page-hydration-module.js +17 -0
  79. package/src/hydration/browser/page-hydration.d.ts +54 -0
  80. package/src/hydration/browser/page-hydration.js +177 -0
  81. package/src/hydration/compiled-page-hydration-options.d.ts +20 -0
  82. package/src/hydration/hydration-asset.d.ts +127 -0
  83. package/src/hydration/hydration-asset.js +379 -0
  84. package/src/hydration/hydration-lifecycle.test.browser.d.ts +1 -0
  85. package/src/hydration/hydration-lifecycle.test.browser.js +461 -0
  86. package/src/hydration/hydration-page-entry-source.d.ts +5 -0
  87. package/src/hydration/hydration-page-entry-source.js +87 -0
  88. package/src/hydration/hydration-script-compiler.d.ts +48 -0
  89. package/src/hydration/hydration-script-compiler.js +213 -0
  90. package/src/hydration/page-data-manifest.d.ts +58 -0
  91. package/src/hydration/page-data-manifest.js +97 -0
  92. package/src/hydration/page-data-reader.d.ts +17 -0
  93. package/src/hydration/page-data-reader.js +31 -0
  94. package/src/hydration/page-payload.d.ts +68 -0
  95. package/src/hydration/page-payload.js +106 -0
  96. package/src/hydration/serialize-page-data-script.d.ts +27 -0
  97. package/src/hydration/serialize-page-data-script.js +30 -0
  98. package/src/{services/react-mdx-config-dependency.service.d.ts → mdx/mdx-config-dependency.d.ts} +9 -8
  99. package/src/mdx/mdx-config-dependency.js +107 -0
  100. package/src/mdx/mdx-loader-plugin.d.ts +8 -0
  101. package/src/mdx/mdx-loader-plugin.js +11 -0
  102. package/src/page/page-module.d.ts +46 -0
  103. package/src/page/page-module.js +74 -0
  104. package/src/plugin/react.constants.js +1 -0
  105. package/src/{react.plugin.d.ts → plugin/react.plugin.d.ts} +21 -27
  106. package/src/plugin/react.plugin.js +210 -0
  107. package/src/plugin/react.types.d.ts +106 -0
  108. package/src/plugin/react.types.js +1 -0
  109. package/src/render/component-ownership.d.ts +49 -0
  110. package/src/render/component-ownership.js +47 -0
  111. package/src/render/component-ssr.d.ts +59 -0
  112. package/src/render/component-ssr.js +76 -0
  113. package/src/render/eco-embed.js +9 -0
  114. package/src/render/errors.d.ts +16 -0
  115. package/src/render/errors.js +23 -0
  116. package/src/render/html-boundary.js +59 -0
  117. package/src/render/layout-compose.d.ts +66 -0
  118. package/src/render/layout-compose.js +134 -0
  119. package/src/render/react-renderer.d.ts +114 -0
  120. package/src/render/react-renderer.js +490 -0
  121. package/src/render/react-runtime.d.ts +13 -0
  122. package/src/render/react-runtime.js +26 -0
  123. package/src/render/unified-layout-composition.d.ts +22 -0
  124. package/src/render/unified-layout-composition.js +31 -0
  125. package/src/runtime/use-sync-external-store-with-selector.js +52 -54
  126. package/src/eco-embed.js +0 -11
  127. package/src/react-hmr-strategy.d.ts +0 -230
  128. package/src/react-hmr-strategy.js +0 -657
  129. package/src/react-renderer.d.ts +0 -233
  130. package/src/react-renderer.js +0 -622
  131. package/src/react.constants.js +0 -4
  132. package/src/react.plugin.js +0 -205
  133. package/src/react.types.d.ts +0 -88
  134. package/src/react.types.js +0 -0
  135. package/src/router-adapter.js +0 -0
  136. package/src/services/pages-index.d.ts +0 -64
  137. package/src/services/pages-index.js +0 -73
  138. package/src/services/react-bundle.service.js +0 -86
  139. package/src/services/react-hmr-page-metadata-cache.js +0 -35
  140. package/src/services/react-hydration-asset.service.d.ts +0 -84
  141. package/src/services/react-hydration-asset.service.js +0 -217
  142. package/src/services/react-mdx-config-dependency.service.js +0 -122
  143. package/src/services/react-page-module.service.d.ts +0 -88
  144. package/src/services/react-page-module.service.js +0 -166
  145. package/src/services/react-page-payload.service.d.ts +0 -46
  146. package/src/services/react-page-payload.service.js +0 -67
  147. package/src/services/react-runtime-bundle.service.d.ts +0 -45
  148. package/src/services/react-runtime-bundle.service.js +0 -186
  149. package/src/utils/client-graph-boundary-cache.d.ts +0 -108
  150. package/src/utils/client-graph-boundary-cache.js +0 -116
  151. package/src/utils/client-graph-boundary-plugin.d.ts +0 -51
  152. package/src/utils/client-graph-boundary-plugin.js +0 -552
  153. package/src/utils/client-only.js +0 -19
  154. package/src/utils/component-config-traversal.js +0 -54
  155. package/src/utils/declared-modules.js +0 -47
  156. package/src/utils/dynamic.js +0 -12
  157. package/src/utils/dynamic.test.browser.js +0 -33
  158. package/src/utils/hmr-scripts.d.ts +0 -18
  159. package/src/utils/hmr-scripts.js +0 -31
  160. package/src/utils/html-boundary.js +0 -55
  161. package/src/utils/hydration-scripts.d.ts +0 -92
  162. package/src/utils/hydration-scripts.js +0 -358
  163. package/src/utils/hydration-scripts.test.browser.js +0 -198
  164. package/src/utils/reachability-analyzer.d.ts +0 -66
  165. package/src/utils/reachability-analyzer.js +0 -339
  166. package/src/utils/react-dom-runtime-interop-plugin.js +0 -38
  167. package/src/utils/react-mdx-loader-plugin.d.ts +0 -3
  168. package/src/utils/react-mdx-loader-plugin.js +0 -45
  169. package/src/utils/react-runtime-alias-map.d.ts +0 -8
  170. package/src/utils/react-runtime-alias-map.js +0 -90
  171. /package/src/{utils → app}/client-only.d.ts +0 -0
  172. /package/src/{utils → app}/dynamic.d.ts +0 -0
  173. /package/src/{utils → app}/dynamic.test.browser.d.ts +0 -0
  174. /package/src/{utils → client-graph}/component-config-traversal.d.ts +0 -0
  175. /package/src/{utils → client-graph}/declared-modules.d.ts +0 -0
  176. /package/src/{declarations.d.ts → contracts/declarations.d.ts} +0 -0
  177. /package/src/{router-adapter.d.ts → contracts/router-adapter.d.ts} +0 -0
  178. /package/src/{utils/hydration-scripts.test.browser.d.ts → hydration/compiled-page-hydration-options.js} +0 -0
  179. /package/src/{react.constants.d.ts → plugin/react.constants.d.ts} +0 -0
  180. /package/src/{eco-embed.d.ts → render/eco-embed.d.ts} +0 -0
  181. /package/src/{utils → render}/html-boundary.d.ts +0 -0
@@ -0,0 +1,379 @@
1
+ /**
2
+ * Hydration asset creation service for React integration.
3
+ *
4
+ * Builds the asset definitions required for client-side React rendering — both at
5
+ * the page level and the component island level.
6
+ *
7
+ * @module
8
+ */
9
+ import path from 'node:path';
10
+ import { contentHash } from '@ecopages/core/build';
11
+ import { RESOLVED_ASSETS_DIR } from '@ecopages/core/build';
12
+ import { isReactProductionRuntime } from '../bundling/runtime-mode.js';
13
+ import { APP_BROWSER_CLIENT_BUNDLE_ID, AssetFactory, createAppBrowserClientEntry, } from '@ecopages/core/integration';
14
+ import { IslandHydrationScriptCompiler, PageHydrationScriptCompiler } from './hydration-script-compiler.js';
15
+ import { resolveHydrationBootstrapImports } from './bootstrap-imports.js';
16
+ import { collectDeclaredModulesInConfig } from '../client-graph/declared-modules.js';
17
+ import { hasPagePreloadExport } from '../client-graph/reachability-analyzer.js';
18
+ import { fileSystem } from '@ecopages/file-system';
19
+ /**
20
+ * Derives the stable component key used by asset grouping and host selectors.
21
+ *
22
+ * @remarks
23
+ * The key identifies the component entry. Individual SSR instances receive a
24
+ * separate component ID, so repeated uses share browser assets without sharing
25
+ * a React root.
26
+ *
27
+ * @param componentFile - Absolute source path of the React component.
28
+ * @param config - Optional component identity used to distinguish entries.
29
+ * @returns Stable key used in selectors and asset names.
30
+ */
31
+ export function getIslandComponentKey(componentFile, config) {
32
+ return BigInt(`0x${contentHash(`${componentFile}:${config?.identity?.id ?? ''}`, 16)}`).toString(10);
33
+ }
34
+ /**
35
+ * Manages the creation of client-side hydration assets for React pages and component islands.
36
+ */
37
+ export class HydrationAssetService {
38
+ config;
39
+ islandScriptCompiler = new IslandHydrationScriptCompiler();
40
+ pageScriptCompiler = new PageHydrationScriptCompiler();
41
+ static ROUTER_PAGE_GROUPED_BUNDLE_ID = APP_BROWSER_CLIENT_BUNDLE_ID;
42
+ constructor(config) {
43
+ this.config = config;
44
+ }
45
+ getIslandBundleName(componentFile) {
46
+ return `ecopages-react-island-${BigInt(`0x${contentHash(componentFile, 16)}`).toString()}`;
47
+ }
48
+ getIslandHydrationName(bundleName, componentKey) {
49
+ return `${bundleName}-hydration-${componentKey}`;
50
+ }
51
+ getRouterPageGroupedEntryName(pagePath) {
52
+ const relativePath = path.relative(this.config.srcDir, pagePath);
53
+ return relativePath
54
+ .replace(/\.(tsx?|jsx?|mdx?)$/, '')
55
+ .replace(/[\\/]+/g, '__')
56
+ .replace(/\[([^\]]+)\]/g, '_$1_');
57
+ }
58
+ /**
59
+ * Resolves the browser import path used for a React-owned page or island module.
60
+ *
61
+ * @remarks
62
+ * When HMR is enabled, registers the source file as an HMR entrypoint and returns
63
+ * that URL. Otherwise returns the static resolved-assets path for the generated
64
+ * asset name.
65
+ */
66
+ async resolveAssetImportPath(pagePath, assetName) {
67
+ const hmrManager = this.config.assetProcessingService?.getHmrManager();
68
+ if (hmrManager?.isEnabled()) {
69
+ return hmrManager.registerEntrypoint(pagePath);
70
+ }
71
+ return `/${path
72
+ .join(RESOLVED_ASSETS_DIR, path.relative(this.config.srcDir, pagePath))
73
+ .replace(path.basename(pagePath), `${assetName}.js`)
74
+ .replace(/\\/g, '/')}`;
75
+ }
76
+ /**
77
+ * Creates the page-owned route entry asset for hydration and client navigation.
78
+ *
79
+ * @remarks
80
+ * The entry is compiled asynchronously from the editable browser lifecycle.
81
+ * Production router pages may be grouped into the Page Browser Graph, while
82
+ * HMR entries remain separate ESM scripts with browser-resolvable imports.
83
+ */
84
+ async createPageDependencies(options) {
85
+ const { pagePath, componentName, importPath, pageModuleUrlExpression, bundleOptions, hmrEnabled, useBrowserRuntimeImports, isMdx, hasPagePreload, } = options;
86
+ const runtimeImports = this.config.bundleService.getRuntimeImports();
87
+ const groupedBundle = !hmrEnabled && this.config.routerAdapter
88
+ ? {
89
+ id: HydrationAssetService.ROUTER_PAGE_GROUPED_BUNDLE_ID,
90
+ entryName: this.getRouterPageGroupedEntryName(pagePath),
91
+ }
92
+ : undefined;
93
+ const bootstrapImports = resolveHydrationBootstrapImports({
94
+ useBrowserRuntimeImports,
95
+ runtimeImports,
96
+ routerAdapterImportPath: this.config.routerAdapter?.bundle.importPath,
97
+ });
98
+ const pageHydrationScript = await this.pageScriptCompiler.compile({
99
+ importPath: hmrEnabled ? importPath : pagePath,
100
+ pageModuleUrlExpression,
101
+ scriptId: componentName,
102
+ reactImportPath: bootstrapImports.reactImportPath,
103
+ reactDomClientImportPath: bootstrapImports.reactDomClientImportPath,
104
+ routerImportPath: bootstrapImports.routerImportPath,
105
+ layoutComposeImportPath: bootstrapImports.layoutComposeImportPath,
106
+ pageLayoutNormalizationImportPath: bootstrapImports.pageLayoutNormalizationImportPath,
107
+ routerComponents: this.config.routerAdapter?.components,
108
+ routerPropsExpression: this.config.routerAdapter?.getRouterProps('Page', 'props'),
109
+ hmrEnabled,
110
+ isMdx,
111
+ hasPagePreload: hasPagePreload === true,
112
+ minify: !hmrEnabled && isReactProductionRuntime(),
113
+ });
114
+ return [
115
+ AssetFactory.createContentScript({
116
+ position: 'head',
117
+ content: pageHydrationScript,
118
+ name: componentName,
119
+ packageRole: 'page-script',
120
+ /**
121
+ * @remarks
122
+ * HMR bootstraps stay unbundled write-through ESM. Helper imports must
123
+ * already be browser-resolvable vendor URLs from
124
+ * {@link resolveHydrationBootstrapImports}.
125
+ */
126
+ bundle: !hmrEnabled,
127
+ groupedBundle,
128
+ bundleOptions,
129
+ attributes: {
130
+ type: 'module',
131
+ defer: '',
132
+ 'data-eco-rerun': 'true',
133
+ 'data-eco-script-id': componentName,
134
+ ...(this.config.routerAdapter ? { 'data-eco-page-bootstrap': 'react-router' } : {}),
135
+ 'data-eco-persist': 'true',
136
+ },
137
+ }),
138
+ ];
139
+ }
140
+ /**
141
+ * Builds island and lazy-entry browser assets in one grouped multi-entry build.
142
+ *
143
+ * @remarks
144
+ * Production reads island module URLs from processed grouped-build outputs.
145
+ * HMR keeps per-file transform URLs and does not start a disk bundle.
146
+ */
147
+ async buildPageClientRenderAssets(input) {
148
+ if (!this.config.assetProcessingService) {
149
+ return { assetsByFile: new Map(), processedAssets: [] };
150
+ }
151
+ const uniqueIslands = this.uniqueIslands(input.islands);
152
+ const hmrEnabled = this.config.assetProcessingService.getHmrManager()?.isEnabled() ?? false;
153
+ if (hmrEnabled) {
154
+ return this.buildHmrPageClientRenderAssets(uniqueIslands, input.lazyEntries);
155
+ }
156
+ return this.buildGroupedPageClientRenderAssets(uniqueIslands, input.lazyEntries);
157
+ }
158
+ /** Collects React island hydration entries for the production app browser build. */
159
+ async collectAppBrowserClientBuildEntries(islands) {
160
+ const uniqueIslands = this.uniqueIslands(islands);
161
+ const groupedEntrypointFiles = uniqueIslands.map(({ file }) => file);
162
+ const groupedDeclaredModules = [
163
+ ...new Set(uniqueIslands.flatMap(({ config }) => collectDeclaredModulesInConfig(config))),
164
+ ];
165
+ const entries = [];
166
+ for (const island of uniqueIslands) {
167
+ const componentName = this.getIslandBundleName(island.file);
168
+ const declaredModules = collectDeclaredModulesInConfig(island.config);
169
+ const bundleOptions = await this.config.bundleService.createBundleOptions(componentName, false, declaredModules, {}, island.file, groupedEntrypointFiles, groupedDeclaredModules);
170
+ const componentKey = getIslandComponentKey(island.file, island.config);
171
+ const hydrationName = this.getIslandHydrationName(componentName, componentKey);
172
+ const hydrationScript = await this.createIslandHydrationScript(island.file, island.config, island.file, false);
173
+ entries.push({
174
+ dependency: {
175
+ ...hydrationScript,
176
+ bundle: true,
177
+ groupedBundle: {
178
+ id: APP_BROWSER_CLIENT_BUNDLE_ID,
179
+ entryName: hydrationName,
180
+ },
181
+ bundleOptions,
182
+ },
183
+ islandFile: island.file,
184
+ });
185
+ }
186
+ return entries;
187
+ }
188
+ uniqueIslands(islands) {
189
+ const uniqueIslands = [];
190
+ const seenFiles = new Set();
191
+ for (const island of islands) {
192
+ if (seenFiles.has(island.file)) {
193
+ continue;
194
+ }
195
+ seenFiles.add(island.file);
196
+ uniqueIslands.push(island);
197
+ }
198
+ return uniqueIslands;
199
+ }
200
+ async buildHmrPageClientRenderAssets(uniqueIslands, lazyEntries) {
201
+ const assetsByFile = new Map();
202
+ const processedAssets = [];
203
+ for (const island of uniqueIslands) {
204
+ this.config.hmrPageMetadataCache?.markOwnedEntrypoint(island.file);
205
+ const assets = await this.buildHmrIslandRenderAssets(island.file, island.config);
206
+ assetsByFile.set(island.file, assets);
207
+ processedAssets.push(...assets);
208
+ }
209
+ if (lazyEntries?.length) {
210
+ processedAssets.push(...(await this.config.assetProcessingService.processDependencies(lazyEntries, this.config.assetProcessingIntegration)));
211
+ }
212
+ return { assetsByFile, processedAssets };
213
+ }
214
+ async buildGroupedPageClientRenderAssets(uniqueIslands, lazyEntries) {
215
+ const islandEntries = await this.createGroupedIslandEntries(uniqueIslands);
216
+ const clientEntries = [...islandEntries, ...(lazyEntries ?? [])];
217
+ const processedClientEntries = clientEntries.length > 0
218
+ ? await this.config.assetProcessingService.processDependencies(clientEntries, this.config.assetProcessingIntegration)
219
+ : [];
220
+ const srcUrlByEntryName = this.indexSrcUrlsByEntryName(processedClientEntries);
221
+ const hydrationScripts = await this.createGroupedHydrationScripts(uniqueIslands, srcUrlByEntryName);
222
+ const processedHydration = hydrationScripts.length > 0
223
+ ? await this.config.assetProcessingService.processDependencies(hydrationScripts, this.config.assetProcessingIntegration)
224
+ : [];
225
+ return {
226
+ assetsByFile: this.collectIslandAssetsByFile(uniqueIslands, processedClientEntries, processedHydration),
227
+ processedAssets: [...processedClientEntries, ...processedHydration],
228
+ };
229
+ }
230
+ async createGroupedIslandEntries(uniqueIslands) {
231
+ const islandEntries = [];
232
+ const groupedEntrypointFiles = uniqueIslands.map(({ file }) => file);
233
+ const groupedDeclaredModules = [
234
+ ...new Set(uniqueIslands.flatMap(({ config }) => collectDeclaredModulesInConfig(config))),
235
+ ];
236
+ for (const island of uniqueIslands) {
237
+ const componentName = this.getIslandBundleName(island.file);
238
+ const bundleOptions = await this.config.bundleService.createBundleOptions(componentName, false, collectDeclaredModulesInConfig(island.config), {}, island.file, groupedEntrypointFiles, groupedDeclaredModules);
239
+ islandEntries.push(createAppBrowserClientEntry({
240
+ entryName: componentName,
241
+ importPath: island.file,
242
+ reexport: true,
243
+ packageRole: 'dynamic-chunk',
244
+ bundleOptions,
245
+ attributes: {
246
+ 'data-eco-persist': 'true',
247
+ },
248
+ }));
249
+ }
250
+ return islandEntries;
251
+ }
252
+ indexSrcUrlsByEntryName(processedClientEntries) {
253
+ const srcUrlByEntryName = new Map();
254
+ for (const processed of processedClientEntries) {
255
+ const entryName = processed.groupedBundle?.entryName;
256
+ if (entryName && processed.srcUrl) {
257
+ srcUrlByEntryName.set(entryName, processed.srcUrl);
258
+ }
259
+ }
260
+ return srcUrlByEntryName;
261
+ }
262
+ async createGroupedHydrationScripts(uniqueIslands, srcUrlByEntryName) {
263
+ const hydrationScripts = [];
264
+ for (const island of uniqueIslands) {
265
+ const importPath = srcUrlByEntryName.get(this.getIslandBundleName(island.file));
266
+ if (!importPath) {
267
+ throw new Error(`Missing grouped browser output for island ${island.file}`);
268
+ }
269
+ hydrationScripts.push(await this.createIslandHydrationScript(island.file, island.config, importPath, false));
270
+ }
271
+ return hydrationScripts;
272
+ }
273
+ collectIslandAssetsByFile(uniqueIslands, processedClientEntries, processedHydration) {
274
+ const assetsByFile = new Map();
275
+ const hydrationByName = new Map();
276
+ for (const processed of processedHydration) {
277
+ const name = processed.attributes?.['data-eco-script-id'];
278
+ if (!name) {
279
+ continue;
280
+ }
281
+ const existing = hydrationByName.get(name) ?? [];
282
+ existing.push(processed);
283
+ hydrationByName.set(name, existing);
284
+ }
285
+ for (const island of uniqueIslands) {
286
+ const componentName = this.getIslandBundleName(island.file);
287
+ const hydrationName = this.getIslandHydrationName(componentName, getIslandComponentKey(island.file, island.config));
288
+ const islandAsset = processedClientEntries.find((processed) => processed.groupedBundle?.entryName === componentName);
289
+ assetsByFile.set(island.file, [
290
+ ...(islandAsset ? [islandAsset] : []),
291
+ ...(hydrationByName.get(hydrationName) ?? []),
292
+ ]);
293
+ }
294
+ return assetsByFile;
295
+ }
296
+ async buildHmrIslandRenderAssets(componentFile, config) {
297
+ const importPath = await this.resolveAssetImportPath(componentFile, this.getIslandBundleName(componentFile));
298
+ const hydrationScript = await this.createIslandHydrationScript(componentFile, config, importPath, true);
299
+ return this.config.assetProcessingService.processDependencies([hydrationScript], this.config.assetProcessingIntegration);
300
+ }
301
+ async createIslandHydrationScript(componentFile, config, importPath, hmrEnabled) {
302
+ const componentName = this.getIslandBundleName(componentFile);
303
+ const componentKey = getIslandComponentKey(componentFile, config);
304
+ const hydrationName = this.getIslandHydrationName(componentName, componentKey);
305
+ const runtimeImports = this.config.bundleService.getRuntimeImports();
306
+ const islandHydrationScript = await this.islandScriptCompiler.compile({
307
+ importPath,
308
+ scriptId: hydrationName,
309
+ reactImportPath: runtimeImports.react,
310
+ reactDomClientImportPath: runtimeImports.reactDomClient,
311
+ targetSelector: `[data-eco-component-key="${componentKey}"]`,
312
+ componentRef: config?.identity?.id,
313
+ componentFile,
314
+ minify: !hmrEnabled,
315
+ hmrEnabled,
316
+ });
317
+ return AssetFactory.createContentScript({
318
+ position: 'head',
319
+ content: islandHydrationScript,
320
+ name: hydrationName,
321
+ packageRole: 'keep-separate',
322
+ bundle: false,
323
+ attributes: {
324
+ type: 'module',
325
+ defer: '',
326
+ 'data-eco-rerun': 'true',
327
+ 'data-eco-script-id': hydrationName,
328
+ 'data-eco-persist': 'true',
329
+ },
330
+ });
331
+ }
332
+ /**
333
+ * Creates the page browser graph dependency declarations for a React page.
334
+ *
335
+ * @remarks
336
+ * Chooses shared vendor import URLs when HMR is on, when running a non-production
337
+ * hosted runtime with HMR off, or when a router adapter is configured (router
338
+ * pages always externalize to shared runtimes). Production non-router pages
339
+ * may inline React into the page bundle instead.
340
+ *
341
+ * @param pagePath - Absolute file path of the page
342
+ * @param isMdx - Whether the page is an MDX file
343
+ * @param declaredModules - Explicitly declared browser module specifiers
344
+ */
345
+ async createPageBrowserGraphDependencies(pagePath, isMdx, declaredModules) {
346
+ const componentName = `ecopages-react-${BigInt(`0x${contentHash(pagePath, 16)}`).toString()}`;
347
+ const hmrManager = this.config.assetProcessingService?.getHmrManager();
348
+ const hmrEnabled = hmrManager?.isEnabled() ?? false;
349
+ const productionRuntime = isReactProductionRuntime();
350
+ const usesRouterRuntime = Boolean(this.config.routerAdapter);
351
+ const useBrowserRuntimeImports = hmrEnabled || !productionRuntime || usesRouterRuntime;
352
+ if (hmrEnabled) {
353
+ this.config.hmrPageMetadataCache?.setDeclaredModules(pagePath, declaredModules);
354
+ }
355
+ const importPath = await this.resolveAssetImportPath(pagePath, componentName);
356
+ const pageModuleUrlExpression = hmrEnabled ? JSON.stringify(importPath) : 'import.meta.url';
357
+ let hasPagePreload = false;
358
+ try {
359
+ const source = await fileSystem.readFile(pagePath);
360
+ hasPagePreload = hasPagePreloadExport(source, pagePath);
361
+ }
362
+ catch {
363
+ hasPagePreload = false;
364
+ }
365
+ const bundleOptions = await this.config.bundleService.createBundleOptions(componentName, isMdx, declaredModules, { includeRuntime: !useBrowserRuntimeImports, splitting: usesRouterRuntime }, pagePath);
366
+ const dependencies = await this.createPageDependencies({
367
+ pagePath,
368
+ componentName,
369
+ importPath,
370
+ pageModuleUrlExpression,
371
+ bundleOptions,
372
+ hmrEnabled,
373
+ useBrowserRuntimeImports,
374
+ isMdx,
375
+ hasPagePreload,
376
+ });
377
+ return dependencies;
378
+ }
379
+ }