@ecopages/react 0.2.1 → 0.3.1

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 (167) hide show
  1. package/README.md +179 -35
  2. package/package.json +78 -33
  3. package/src/app/client-only.js +10 -0
  4. package/src/app/dynamic.js +9 -0
  5. package/src/bundling/bootstrap-package-paths.d.ts +5 -0
  6. package/src/bundling/bootstrap-package-paths.js +5 -0
  7. package/src/bundling/bundle.d.ts +78 -0
  8. package/src/bundling/bundle.js +130 -0
  9. package/src/bundling/runtime-alias-map.d.ts +12 -0
  10. package/src/bundling/runtime-alias-map.js +110 -0
  11. package/src/bundling/runtime-bundle.d.ts +70 -0
  12. package/src/bundling/runtime-bundle.js +233 -0
  13. package/src/{utils/react-dom-runtime-interop-plugin.d.ts → bundling/runtime-interop-plugin.d.ts} +1 -1
  14. package/src/bundling/runtime-interop-plugin.js +32 -0
  15. package/src/bundling/runtime-mode.d.ts +1 -0
  16. package/src/bundling/runtime-mode.js +10 -0
  17. package/src/bundling/runtime-modules.d.ts +21 -0
  18. package/src/bundling/runtime-modules.js +28 -0
  19. package/src/client-graph/ast-transform-eco-page.d.ts +10 -0
  20. package/src/client-graph/ast-transform-eco-page.js +71 -0
  21. package/src/client-graph/ast-transform-import-edits.d.ts +13 -0
  22. package/src/client-graph/ast-transform-import-edits.js +75 -0
  23. package/src/client-graph/ast-transform-walk-imports.d.ts +20 -0
  24. package/src/client-graph/ast-transform-walk-imports.js +105 -0
  25. package/src/client-graph/ast-transform.d.ts +12 -0
  26. package/src/client-graph/ast-transform.js +91 -0
  27. package/src/client-graph/ast-walk.d.ts +10 -0
  28. package/src/client-graph/ast-walk.js +26 -0
  29. package/src/client-graph/boundary-plugin-read-file-inline.d.ts +7 -0
  30. package/src/client-graph/boundary-plugin-read-file-inline.js +43 -0
  31. package/src/client-graph/boundary-plugin-transform.d.ts +16 -0
  32. package/src/client-graph/boundary-plugin-transform.js +31 -0
  33. package/src/client-graph/boundary-plugin.d.ts +23 -0
  34. package/src/client-graph/boundary-plugin.js +177 -0
  35. package/src/client-graph/component-config-traversal.d.ts +36 -0
  36. package/src/client-graph/component-config-traversal.js +77 -0
  37. package/src/{utils → client-graph}/declared-modules.d.ts +1 -1
  38. package/src/client-graph/declared-modules.js +69 -0
  39. package/src/client-graph/module-classification.d.ts +3 -0
  40. package/src/client-graph/module-classification.js +43 -0
  41. package/src/client-graph/reachability-analyzer.d.ts +26 -0
  42. package/src/client-graph/reachability-analyzer.js +55 -0
  43. package/src/client-graph/reachability-client-roots.d.ts +8 -0
  44. package/src/client-graph/reachability-client-roots.js +59 -0
  45. package/src/client-graph/reachability-export-names.d.ts +5 -0
  46. package/src/client-graph/reachability-export-names.js +34 -0
  47. package/src/client-graph/reachability-fallback-roots.d.ts +3 -0
  48. package/src/client-graph/reachability-fallback-roots.js +71 -0
  49. package/src/client-graph/reachability-preload.d.ts +10 -0
  50. package/src/client-graph/reachability-preload.js +30 -0
  51. package/src/client-graph/reachability-top-level-index.d.ts +14 -0
  52. package/src/client-graph/reachability-top-level-index.js +93 -0
  53. package/src/client-graph/reachability-traverse.d.ts +7 -0
  54. package/src/client-graph/reachability-traverse.js +201 -0
  55. package/src/client-graph/specifier-classification.d.ts +74 -0
  56. package/src/client-graph/specifier-classification.js +191 -0
  57. package/src/content/collection-component-cache.d.ts +25 -0
  58. package/src/content/collection-component-cache.js +62 -0
  59. package/src/{router-adapter.d.ts → contracts/router-adapter.d.ts} +7 -14
  60. package/src/contracts/router-adapter.js +6 -0
  61. package/src/dev-transform/react-dev-transform-contributor.d.ts +17 -0
  62. package/src/dev-transform/react-dev-transform-contributor.js +22 -0
  63. package/src/hmr/hmr-strategy-process.d.ts +34 -0
  64. package/src/hmr/hmr-strategy-process.js +83 -0
  65. package/src/hmr/hmr-strategy.d.ts +67 -0
  66. package/src/hmr/hmr-strategy.js +306 -0
  67. package/src/{services/react-hmr-page-metadata-cache.d.ts → hmr/page-metadata-cache.d.ts} +6 -2
  68. package/src/hmr/page-metadata-cache.js +44 -0
  69. package/src/hmr/react-hmr-dev-transform-plugins.d.ts +22 -0
  70. package/src/hmr/react-hmr-dev-transform-plugins.js +19 -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 +81 -0
  74. package/src/hydration/browser/island-hydration.js +247 -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/compiled-page-hydration-options.js +1 -0
  83. package/src/hydration/hydration-asset.d.ts +128 -0
  84. package/src/hydration/hydration-asset.js +388 -0
  85. package/src/hydration/hydration-page-entry-source.d.ts +5 -0
  86. package/src/hydration/hydration-page-entry-source.js +87 -0
  87. package/src/hydration/hydration-script-compiler.d.ts +47 -0
  88. package/src/hydration/hydration-script-compiler.js +213 -0
  89. package/src/hydration/page-data-manifest.d.ts +58 -0
  90. package/src/hydration/page-data-manifest.js +97 -0
  91. package/src/hydration/page-data-reader.d.ts +17 -0
  92. package/src/hydration/page-data-reader.js +31 -0
  93. package/src/hydration/page-payload.d.ts +68 -0
  94. package/src/hydration/page-payload.js +106 -0
  95. package/src/hydration/serialize-page-data-script.d.ts +27 -0
  96. package/src/hydration/serialize-page-data-script.js +30 -0
  97. package/src/mdx/mdx-config-dependency.d.ts +37 -0
  98. package/src/mdx/mdx-config-dependency.js +107 -0
  99. package/src/mdx/mdx-loader-plugin.d.ts +8 -0
  100. package/src/mdx/mdx-loader-plugin.js +11 -0
  101. package/src/page/page-module.d.ts +46 -0
  102. package/src/page/page-module.js +74 -0
  103. package/src/plugin/react.constants.d.ts +1 -0
  104. package/src/plugin/react.constants.js +1 -0
  105. package/src/plugin/react.plugin.d.ts +71 -0
  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.d.ts +11 -0
  114. package/src/render/eco-embed.js +9 -0
  115. package/src/render/errors.d.ts +16 -0
  116. package/src/render/errors.js +23 -0
  117. package/src/render/html-boundary.js +59 -0
  118. package/src/render/layout-compose.d.ts +66 -0
  119. package/src/render/layout-compose.js +134 -0
  120. package/src/render/react-renderer.d.ts +114 -0
  121. package/src/render/react-renderer.js +490 -0
  122. package/src/render/react-runtime.d.ts +13 -0
  123. package/src/render/react-runtime.js +26 -0
  124. package/src/render/unified-layout-composition.d.ts +22 -0
  125. package/src/render/unified-layout-composition.js +31 -0
  126. package/src/runtime/use-sync-external-store-with-selector.d.ts +3 -0
  127. package/src/runtime/use-sync-external-store-with-selector.js +54 -0
  128. package/CHANGELOG.md +0 -29
  129. package/src/react-hmr-strategy.d.ts +0 -148
  130. package/src/react-hmr-strategy.js +0 -313
  131. package/src/react-renderer.d.ts +0 -213
  132. package/src/react-renderer.js +0 -654
  133. package/src/react.plugin.d.ts +0 -147
  134. package/src/react.plugin.js +0 -146
  135. package/src/router-adapter.js +0 -0
  136. package/src/services/react-bundle.service.d.ts +0 -47
  137. package/src/services/react-bundle.service.js +0 -93
  138. package/src/services/react-hmr-page-metadata-cache.js +0 -35
  139. package/src/services/react-hydration-asset.service.d.ts +0 -76
  140. package/src/services/react-hydration-asset.service.js +0 -210
  141. package/src/services/react-page-module.service.d.ts +0 -83
  142. package/src/services/react-page-module.service.js +0 -162
  143. package/src/services/react-runtime-bundle.service.d.ts +0 -40
  144. package/src/services/react-runtime-bundle.service.js +0 -130
  145. package/src/utils/client-graph-boundary-plugin.d.ts +0 -43
  146. package/src/utils/client-graph-boundary-plugin.js +0 -494
  147. package/src/utils/client-only.js +0 -19
  148. package/src/utils/declared-modules.js +0 -59
  149. package/src/utils/dynamic.js +0 -12
  150. package/src/utils/hmr-scripts.d.ts +0 -18
  151. package/src/utils/hmr-scripts.js +0 -31
  152. package/src/utils/html-boundary.js +0 -55
  153. package/src/utils/hydration-scripts.d.ts +0 -86
  154. package/src/utils/hydration-scripts.js +0 -285
  155. package/src/utils/reachability-analyzer.d.ts +0 -66
  156. package/src/utils/reachability-analyzer.js +0 -339
  157. package/src/utils/react-dom-runtime-interop-plugin.js +0 -29
  158. package/src/utils/react-mdx-loader-plugin.d.ts +0 -3
  159. package/src/utils/react-mdx-loader-plugin.js +0 -45
  160. package/src/utils/react-runtime-specifier-map.d.ts +0 -6
  161. package/src/utils/react-runtime-specifier-map.js +0 -37
  162. package/src/utils/use-sync-external-store-shim-plugin.d.ts +0 -5
  163. package/src/utils/use-sync-external-store-shim-plugin.js +0 -41
  164. /package/src/{utils → app}/client-only.d.ts +0 -0
  165. /package/src/{utils → app}/dynamic.d.ts +0 -0
  166. /package/src/{declarations.d.ts → contracts/declarations.d.ts} +0 -0
  167. /package/src/{utils → render}/html-boundary.d.ts +0 -0
@@ -1,86 +0,0 @@
1
- /**
2
- * Hydration script generators for React pages.
3
- * These functions create the client-side scripts that hydrate React components.
4
- * @module
5
- */
6
- import type { ReactRouterAdapter } from '../router-adapter.js';
7
- /**
8
- * Options for generating a hydration script.
9
- */
10
- export type HydrationScriptOptions = {
11
- /** The import path for the bundled page component */
12
- importPath: string;
13
- /** Direct import path for React runtime module */
14
- reactImportPath: string;
15
- /** Direct import path for react-dom/client runtime module */
16
- reactDomClientImportPath: string;
17
- /** Direct import path for router runtime module */
18
- routerImportPath?: string;
19
- /** Whether running in development mode with HMR support */
20
- isDevelopment: boolean;
21
- /** Whether the source file is an MDX file */
22
- isMdx: boolean;
23
- /** Optional router adapter for SPA navigation */
24
- router?: ReactRouterAdapter;
25
- };
26
- export type IslandHydrationScriptOptions = {
27
- /** Bundled browser module path for the island component. */
28
- importPath: string;
29
- /** Browser import path for React runtime. */
30
- reactImportPath: string;
31
- /** Browser import path for react-dom/client runtime. */
32
- reactDomClientImportPath: string;
33
- /** Selector that resolves to all SSR root elements for this island component. */
34
- targetSelector: string;
35
- /** Optional stable component id used to resolve named exports reliably. */
36
- componentRef?: string;
37
- /** Optional source file hint used as fallback for component resolution. */
38
- componentFile?: string;
39
- /** Enables development-oriented non-minified output. */
40
- isDevelopment: boolean;
41
- };
42
- /**
43
- * Creates a hydration script for client-side React hydration.
44
- *
45
- * Why this dispatcher exists:
46
- * the runtime matrix is small but behaviorally different across development vs
47
- * production and router vs non-router pages. Keeping that branch here preserves
48
- * a compact public API while allowing each emitted script to stay focused.
49
- *
50
- * Selection rules:
51
- * - development uses readable scripts with HMR hooks
52
- * - production uses minified equivalents
53
- * - router presence decides whether page updates flow through the router runtime
54
- * or rebuild directly from the page module
55
- *
56
- * @param options - Configuration options for script generation
57
- * @returns The generated hydration script as a string
58
- */
59
- export declare function createHydrationScript(options: HydrationScriptOptions): string;
60
- /**
61
- * Creates the client bootstrap for component-level React islands.
62
- *
63
- * The island runtime intentionally uses `createRoot()` (not `hydrateRoot()`) and
64
- * mounts into the SSR element identified by `targetSelector`.
65
- *
66
- * Rationale:
67
- * - No synthetic wrapper element is introduced in SSR output.
68
- * - DOM structure remains identical to authored component markup.
69
- * - Runtime ownership is isolated per island instance.
70
- *
71
- * Generated script behavior:
72
- * - resolves the component export by metadata (`componentRef`, `componentFile`)
73
- * before falling back to default/first function export
74
- * - selects island root using `targetSelector`
75
- * - replaces the SSR host with a dedicated client-owned container
76
- * - creates a fresh React root and renders with serialized `props`
77
- *
78
- * Why it remounts instead of hydrating:
79
- * island SSR intentionally avoids synthetic wrapper elements. The runtime swaps
80
- * the authored SSR node for a dedicated client-owned container before mounting
81
- * so the server markup stays clean while the client still gets a stable root.
82
- *
83
- * @param options Island script generation options.
84
- * @returns Browser-executable JavaScript module source.
85
- */
86
- export declare function createIslandHydrationScript(options: IslandHydrationScriptOptions): string;
@@ -1,285 +0,0 @@
1
- function getImportStatement(importPath, isMdx) {
2
- return isMdx ? `import * as MDXModule from "${importPath}";
3
- const Page = MDXModule.default;
4
- if (MDXModule.config) Page.config = MDXModule.config;` : `import Page from "${importPath}";`;
5
- }
6
- function getHmrImportStatement(isMdx) {
7
- return isMdx ? "const NewPage = newModule.default; if (newModule.config) NewPage.config = newModule.config;" : "const NewPage = newModule.default;";
8
- }
9
- function getComponentType(isMdx) {
10
- return isMdx ? "MDX" : "React";
11
- }
12
- function getDevPageRootCleanupScript() {
13
- return `window.__ECO_PAGES__ = window.__ECO_PAGES__ || {};
14
- window.__ECO_PAGES__.react = window.__ECO_PAGES__.react || {};
15
- window.__ECO_PAGES__.react.cleanupPageRoot = () => {
16
- const activeRoot = window.__ECO_PAGES__.react?.pageRoot || root;
17
- if (!activeRoot) {
18
- window.__ECO_PAGES__.react.pageRoot = null;
19
- window.__ECO_PAGES__?.navigation?.releaseOwnership?.("react-router");
20
- delete window.__ECO_PAGES__.page;
21
- return;
22
- }
23
- window.__ECO_PAGES__.react.pageRoot = null;
24
- window.__ECO_PAGES__?.navigation?.releaseOwnership?.("react-router");
25
- delete window.__ECO_PAGES__.page;
26
- root = null;
27
- activeRoot.unmount();
28
- };`;
29
- }
30
- function getProdPageRootCleanupScript() {
31
- return 'window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.cleanupPageRoot=()=>{const a=window.__ECO_PAGES__.react?.pageRoot||root;if(!a){window.__ECO_PAGES__.react.pageRoot=null;window.__ECO_PAGES__?.navigation?.releaseOwnership?.("react-router");delete window.__ECO_PAGES__.page;return}window.__ECO_PAGES__.react.pageRoot=null;window.__ECO_PAGES__?.navigation?.releaseOwnership?.("react-router");delete window.__ECO_PAGES__.page;root=null;a.unmount()};';
32
- }
33
- function getDevRouterBootstrapRegistrationScript() {
34
- return `const currentOwnerState = window.__ECO_PAGES__?.navigation?.getOwnerState?.();
35
- if (!(currentOwnerState?.owner === "react-router" && currentOwnerState.canHandleSpaNavigation)) {
36
- window.__ECO_PAGES__?.navigation?.register({
37
- owner: "react-router",
38
- cleanupBeforeHandoff: async () => {
39
- window.__ECO_PAGES__?.react?.cleanupPageRoot?.();
40
- }
41
- });
42
- window.__ECO_PAGES__?.navigation?.claimOwnership?.("react-router");
43
- }`;
44
- }
45
- function getProdRouterBootstrapRegistrationScript() {
46
- return 'const o=window.__ECO_PAGES__?.navigation?.getOwnerState?.();if(!(o?.owner==="react-router"&&o.canHandleSpaNavigation)){window.__ECO_PAGES__?.navigation?.register({owner:"react-router",cleanupBeforeHandoff:async()=>{window.__ECO_PAGES__?.react?.cleanupPageRoot?.()}});window.__ECO_PAGES__?.navigation?.claimOwnership?.("react-router")}';
47
- }
48
- function createDevScriptWithRouter(options) {
49
- const { importPath, isMdx, router, reactImportPath, reactDomClientImportPath, routerImportPath } = options;
50
- const { components, getRouterProps } = router;
51
- if (!routerImportPath) {
52
- throw new Error("routerImportPath is required when router adapter is configured");
53
- }
54
- return `
55
- import { hydrateRoot } from "${reactDomClientImportPath}";
56
- import { createElement } from "${reactImportPath}";
57
- import { ${components.router}, ${components.pageContent} } from "${routerImportPath}";
58
- ${getImportStatement(importPath, isMdx)}
59
-
60
- window.__ECO_PAGES__ = window.__ECO_PAGES__ || {};
61
- window.__ECO_PAGES__.hmrHandlers = window.__ECO_PAGES__.hmrHandlers || {};
62
- window.__ECO_PAGES__.react = window.__ECO_PAGES__.react || {};
63
- window.__ECO_PAGES__.react.pageRoot = window.__ECO_PAGES__.react.pageRoot || null;
64
- let root = window.__ECO_PAGES__.react.pageRoot;
65
- ${getDevPageRootCleanupScript()}
66
- ${getDevRouterBootstrapRegistrationScript()}
67
-
68
- const getPageData = () => {
69
- const el = document.getElementById("__ECO_PAGE_DATA__");
70
- if (el?.textContent) {
71
- try { return JSON.parse(el.textContent); } catch {}
72
- }
73
- return {};
74
- };
75
-
76
- const props = getPageData();
77
-
78
- window.__ECO_PAGES__.page = {
79
- module: "${importPath}",
80
- props
81
- };
82
-
83
- const createTree = (Component, props) => {
84
- const pageContent = createElement(${components.pageContent});
85
- return createElement(${components.router}, ${getRouterProps("Component", "props")}, pageContent);
86
- };
87
-
88
- const mount = () => {
89
- if (window.__ECO_PAGES__.react?.pageRoot) {
90
- root = window.__ECO_PAGES__.react.pageRoot;
91
- root.render(createTree(Page, props));
92
- } else {
93
- root = hydrateRoot(document.body, createTree(Page, props), {
94
- onRecoverableError: (err) => console.warn("[ecopages] Hydration error:", err)
95
- });
96
- window.__ECO_PAGES__.react.pageRoot = root;
97
- }
98
- window.__ECO_PAGES__.hmrHandlers["${importPath}"] = async (newUrl) => {
99
- try {
100
- const newModule = await import(newUrl);
101
- const nextProps = getPageData();
102
- ${getHmrImportStatement(isMdx)}
103
- window.__ECO_PAGES__.page = {
104
- module: "${importPath}",
105
- props: nextProps
106
- };
107
- root.render(createTree(NewPage, nextProps));
108
- if (window.__ECO_PAGES__?.navigation?.getOwnerState().owner === "react-router") {
109
- console.log("[ecopages] ${getComponentType(isMdx)} component updated via router");
110
- } else {
111
- console.log("[ecopages] ${getComponentType(isMdx)} component updated");
112
- }
113
- } catch (e) {
114
- console.error("[ecopages] Failed to hot-reload ${getComponentType(isMdx)} component:", e);
115
- }
116
- };
117
- };
118
-
119
- if (document.readyState === "loading") {
120
- document.addEventListener("DOMContentLoaded", mount);
121
- } else {
122
- mount();
123
- }
124
- `.trim();
125
- }
126
- function createDevScriptWithoutRouter(options) {
127
- const { importPath, isMdx, reactImportPath, reactDomClientImportPath } = options;
128
- return `
129
- import { hydrateRoot } from "${reactDomClientImportPath}";
130
- import { createElement } from "${reactImportPath}";
131
- ${getImportStatement(importPath, isMdx)}
132
-
133
- window.__ECO_PAGES__ = window.__ECO_PAGES__ || {};
134
- window.__ECO_PAGES__.hmrHandlers = window.__ECO_PAGES__.hmrHandlers || {};
135
- window.__ECO_PAGES__.react = window.__ECO_PAGES__.react || {};
136
- window.__ECO_PAGES__.react.pageRoot = window.__ECO_PAGES__.react.pageRoot || null;
137
- let root = window.__ECO_PAGES__.react.pageRoot;
138
- ${getDevPageRootCleanupScript()}
139
-
140
- const getPageData = () => {
141
- const el = document.getElementById("__ECO_PAGE_DATA__");
142
- if (el?.textContent) {
143
- try { return JSON.parse(el.textContent); } catch {}
144
- }
145
- return {};
146
- };
147
-
148
- const props = getPageData();
149
-
150
- window.__ECO_PAGES__.page = {
151
- module: "${importPath}",
152
- props
153
- };
154
-
155
- const createTree = (Component, props) => {
156
- const Layout = Component.config?.layout;
157
- const pageElement = createElement(Component, props);
158
- const layoutProps = props?.locals ? { locals: props.locals } : null;
159
- return Layout ? createElement(Layout, layoutProps, pageElement) : pageElement;
160
- };
161
-
162
- const mount = () => {
163
- if (window.__ECO_PAGES__.react?.pageRoot) {
164
- root = window.__ECO_PAGES__.react.pageRoot;
165
- root.render(createTree(Page, props));
166
- } else {
167
- root = hydrateRoot(document.body, createTree(Page, props), {
168
- onRecoverableError: (err) => console.warn("[ecopages] Hydration error:", err)
169
- });
170
- window.__ECO_PAGES__.react.pageRoot = root;
171
- }
172
- window.__ECO_PAGES__.hmrHandlers["${importPath}"] = async (newUrl) => {
173
- try {
174
- const newModule = await import(newUrl);
175
- ${getHmrImportStatement(isMdx)}
176
- root.render(createTree(NewPage, props));
177
- console.log("[ecopages] ${getComponentType(isMdx)} component updated");
178
- } catch (e) {
179
- console.error("[ecopages] Failed to hot-reload ${getComponentType(isMdx)} component:", e);
180
- }
181
- };
182
- };
183
-
184
- if (document.readyState === "loading") {
185
- document.addEventListener("DOMContentLoaded", mount);
186
- } else {
187
- mount();
188
- }
189
- `.trim();
190
- }
191
- function createProdScriptWithRouter(options) {
192
- const { importPath, isMdx, router, reactImportPath, reactDomClientImportPath, routerImportPath } = options;
193
- const { components, getRouterProps } = router;
194
- if (!routerImportPath) {
195
- throw new Error("routerImportPath is required when router adapter is configured");
196
- }
197
- if (isMdx) {
198
- return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import{${components.router} as R,${components.pageContent} as PC}from"${routerImportPath}";import*as M from"${importPath}";const P=M.default;if(M.config)P.config=M.config;window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}${getProdRouterBootstrapRegistrationScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const pr=gd();window.__ECO_PAGES__.page={module:"${importPath}",props:pr};const ct=(C,p)=>ce(R,${getRouterProps("C", "p")},ce(PC));const m=()=>{if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;root.render(ct(P,pr));return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()`;
199
- }
200
- return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import{${components.router} as R,${components.pageContent} as PC}from"${routerImportPath}";import P from"${importPath}";window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}${getProdRouterBootstrapRegistrationScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const pr=gd();window.__ECO_PAGES__.page={module:"${importPath}",props:pr};const ct=(C,p)=>ce(R,${getRouterProps("C", "p")},ce(PC));const m=()=>{if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;root.render(ct(P,pr));return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()`;
201
- }
202
- function createProdScriptWithoutRouter(options) {
203
- const { importPath, isMdx, reactImportPath, reactDomClientImportPath } = options;
204
- if (isMdx) {
205
- return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import*as M from"${importPath}";const P=M.default;if(M.config)P.config=M.config;window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const pr=gd();window.__ECO_PAGES__.page={module:"${importPath}",props:pr};const ct=(C,p)=>{const L=C.config?.layout;const pe=ce(C,p);const lp=p?.locals?{locals:p.locals}:null;return L?ce(L,lp,pe):pe};const m=()=>{if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;root.render(ct(P,pr));return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()`;
206
- }
207
- return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import P from"${importPath}";window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const pr=gd();window.__ECO_PAGES__.page={module:"${importPath}",props:pr};const ct=(C,p)=>{const L=C.config?.layout;const pe=ce(C,p);const lp=p?.locals?{locals:p.locals}:null;return L?ce(L,lp,pe):pe};const m=()=>{if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;root.render(ct(P,pr));return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()`;
208
- }
209
- function createHydrationScript(options) {
210
- const { isDevelopment, router } = options;
211
- if (isDevelopment) {
212
- return router ? createDevScriptWithRouter(options) : createDevScriptWithoutRouter(options);
213
- }
214
- return router ? createProdScriptWithRouter(options) : createProdScriptWithoutRouter(options);
215
- }
216
- function createIslandHydrationScript(options) {
217
- const targetSelector = JSON.stringify(options.targetSelector);
218
- const componentRef = JSON.stringify(options.componentRef ?? "");
219
- const componentFile = JSON.stringify(options.componentFile ?? "");
220
- if (options.isDevelopment) {
221
- return `
222
- import { createRoot } from "${options.reactDomClientImportPath}";
223
- import { createElement } from "${options.reactImportPath}";
224
- import * as ComponentModule from "${options.importPath}";
225
-
226
- const resolveComponent = () => {
227
- const id = ${componentRef};
228
- const file = ${componentFile};
229
- const moduleValues = Object.values(ComponentModule);
230
-
231
- const matchByMetadata = moduleValues.find((entry) => {
232
- if (typeof entry !== "function") return false;
233
- const config = entry.config;
234
- const eco = config?.__eco;
235
- if (!eco) return false;
236
- if (id && eco.id === id) return true;
237
- if (file && eco.file === file) return true;
238
- return false;
239
- });
240
-
241
- if (matchByMetadata && typeof matchByMetadata === "function") {
242
- return matchByMetadata;
243
- }
244
-
245
- const defaultExport = ComponentModule.default;
246
- if (typeof defaultExport === "function") {
247
- return defaultExport;
248
- }
249
-
250
- const firstFunction = moduleValues.find((entry) => typeof entry === "function");
251
- return typeof firstFunction === "function" ? firstFunction : null;
252
- };
253
-
254
- const mount = () => {
255
- const targets = document.querySelectorAll(${targetSelector});
256
- const Component = resolveComponent();
257
- if (!Component || targets.length === 0) {
258
- return;
259
- }
260
- targets.forEach((target) => {
261
- if (!(target instanceof HTMLElement)) {
262
- return;
263
- }
264
- const props = JSON.parse(atob(target.getAttribute("data-eco-props") || "e30="));
265
- const container = document.createElement("eco-island");
266
- container.style.display = "block";
267
- target.replaceWith(container);
268
- const root = createRoot(container);
269
- root.render(createElement(Component, props));
270
- });
271
- };
272
-
273
- if (document.readyState === "loading") {
274
- document.addEventListener("DOMContentLoaded", mount, { once: true });
275
- } else {
276
- mount();
277
- }
278
- `.trim();
279
- }
280
- return `import{createRoot as cr}from"${options.reactDomClientImportPath}";import{createElement as ce}from"${options.reactImportPath}";import*as M from"${options.importPath}";const r=${componentRef};const f=${componentFile};const mv=Object.values(M);const c=mv.find((e)=>{if(typeof e!=="function")return false;const ec=e.config?.__eco;if(!ec)return false;if(r&&ec.id===r)return true;if(f&&ec.file===f)return true;return false;})??(typeof M.default==="function"?M.default:mv.find((e)=>typeof e==="function")??null);const m=()=>{const ts=document.querySelectorAll(${targetSelector});if(!c||ts.length===0)return;ts.forEach((t)=>{if(!(t instanceof HTMLElement))return;const p=JSON.parse(atob(t.getAttribute("data-eco-props")||"e30="));const ct=document.createElement("eco-island");ct.style.display="block";t.replaceWith(ct);cr(ct).render(ce(c,p))})};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m,{once:true}):m()`;
281
- }
282
- export {
283
- createHydrationScript,
284
- createIslandHydrationScript
285
- };
@@ -1,66 +0,0 @@
1
- /**
2
- * @module ReachabilityAnalyzer
3
- *
4
- * This module is responsible for performing static analysis on Ecopages client components
5
- * using the Oxc AST parser. It computes a strict "reachability graph" of all JavaScript/TypeScript
6
- * dependencies (imports, variables, functions, and classes) that begin from explicit client roots.
7
- *
8
- * In Ecopages, "client roots" are defined as the `render`, `errorBoundary`, or `loadingFallback`
9
- * properties passed into `eco.page()` or `eco.component()`. By tracing the execution path from
10
- * these roots, the analyzer determines exactly which modules and bindings are actually needed
11
- * by the browser to hydrate the page, and which imports are unused on the client (and thus can be pruned).
12
- */
13
- import { parseSync } from 'oxc-parser';
14
- type ParserLanguage = 'js' | 'jsx' | 'ts' | 'tsx';
15
- /**
16
- * Determines the appropriate parser language configuration for a given file name.
17
- *
18
- * @param filename - The absolute or relative path to the file.
19
- * @returns The Oxc parser language dialect to use ('js', 'jsx', 'ts', or 'tsx').
20
- */
21
- export declare function parserLanguageForFile(filename: string): ParserLanguage;
22
- /**
23
- * Represents the computed results of a reachability analysis pass.
24
- */
25
- export type ReachabilityResult = {
26
- /**
27
- * Map from import specifier (e.g. 'node:fs', '@/components/Button')
28
- * to a Set of imported bindings, or '*' for namespace imports.
29
- */
30
- reachableImports: Map<string, Set<string> | '*'>;
31
- /**
32
- * AST nodes of top-level declarations that are reachable.
33
- */
34
- reachableDeclarations: Set<unknown>;
35
- unreachableSideEffectImports: unknown[];
36
- /**
37
- * Indicates whether the file had explicit eco client roots, or fell back to treating all exports as roots.
38
- */
39
- isFallbackRoots: boolean;
40
- /**
41
- * Whether the file was successfully parsed and analyzed.
42
- */
43
- analyzed: boolean;
44
- };
45
- /**
46
- * Optional export filter supplied by the client graph boundary when a local
47
- * module is imported through a narrower named-export surface.
48
- *
49
- * `'*'` means the whole module namespace is considered reachable, while a
50
- * `Set` restricts analysis to the named exports that are actually requested by
51
- * downstream client-reachable modules.
52
- */
53
- type ExplicitlyRequestedExports = Set<string> | '*';
54
- /**
55
- * Analyzes a module using Oxc AST and extracts a strict reachability graph
56
- * starting from client roots (`render`, `errorBoundary`, `loadingFallback` of `eco.page` or `eco.component`).
57
- *
58
- * @param source - Raw source string of the module.
59
- * @param filename - Absolute or relative path to the module file.
60
- * @param program - Optional pre-parsed Oxc program AST. When supplied, the
61
- * internal `parseSync` call is skipped entirely (avoids double-parsing).
62
- * @param explicitlyRequestedExports - Optional named export filter propagated
63
- * from a downstream importer when this module is only partially reachable.
64
- */
65
- export declare function analyzeReachability(source: string, filename: string, program?: ReturnType<typeof parseSync>['program'], explicitlyRequestedExports?: ExplicitlyRequestedExports): ReachabilityResult;
66
- export {};