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

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,552 +0,0 @@
1
- import { existsSync, readFileSync } from "node:fs";
2
- import { dirname, extname, resolve } from "node:path";
3
- import { cachedParseSync } from "@ecopages/core/cache";
4
- import { ClientGraphBoundaryCache } from "./client-graph-boundary-cache.js";
5
- import { analyzeReachability } from "./reachability-analyzer.js";
6
- const SOURCE_FILE_FILTER = /\.(tsx?|jsx?)$/;
7
- const SERVER_ONLY_ECO_PAGE_OPTION_KEYS = /* @__PURE__ */ new Set([
8
- "cache",
9
- "middleware",
10
- "requires",
11
- "metadata",
12
- "staticProps",
13
- "staticPaths"
14
- ]);
15
- function isBareSpecifier(specifier) {
16
- if (specifier.startsWith(".")) return false;
17
- if (specifier.startsWith("/")) return false;
18
- if (specifier.includes("://")) return false;
19
- return true;
20
- }
21
- function isProjectAliasSpecifier(specifier) {
22
- return specifier.startsWith("@/") || specifier.startsWith("~/") || specifier.startsWith("ecopages:");
23
- }
24
- function isServerOnlySpecifier(specifier) {
25
- if (specifier.startsWith("node:")) return true;
26
- return /(?:^|[/])[^/]+\.server(?:$|\.)/.test(specifier);
27
- }
28
- function toModuleBaseSpecifier(specifier) {
29
- if (!isBareSpecifier(specifier) || specifier.startsWith("node:")) {
30
- return specifier;
31
- }
32
- if (specifier.startsWith("@")) {
33
- const [scope, name2] = specifier.split("/");
34
- if (!scope || !name2) return specifier;
35
- return `${scope}/${name2}`;
36
- }
37
- const [name] = specifier.split("/");
38
- return name ?? specifier;
39
- }
40
- function parseDeclaredModules(moduleDeclarations) {
41
- const map = /* @__PURE__ */ new Map();
42
- for (const declaration of moduleDeclarations ?? []) {
43
- const source = declaration.trim();
44
- if (source.length === 0) continue;
45
- const openBraceIndex = source.indexOf("{");
46
- if (openBraceIndex < 0) {
47
- map.set(toModuleBaseSpecifier(source), "*");
48
- continue;
49
- }
50
- const closeBraceIndex = source.indexOf("}", openBraceIndex);
51
- const rawPkg = source.slice(0, openBraceIndex).trim();
52
- if (rawPkg.length === 0) continue;
53
- const pkg = toModuleBaseSpecifier(rawPkg);
54
- const namedImportsStr = closeBraceIndex > openBraceIndex ? source.slice(openBraceIndex + 1, closeBraceIndex) : source.slice(openBraceIndex + 1);
55
- const namedImports = namedImportsStr.split(",").map((s) => s.trim()).filter(Boolean);
56
- const existing = map.get(pkg);
57
- if (existing === "*") continue;
58
- if (!existing) {
59
- if (namedImports.length === 0) {
60
- map.set(pkg, "*");
61
- } else {
62
- map.set(pkg, new Set(namedImports));
63
- }
64
- } else {
65
- for (const name of namedImports) {
66
- existing.add(name);
67
- }
68
- }
69
- }
70
- return map;
71
- }
72
- function mergeDeclaredModulesMap(a, b) {
73
- const result = new Map(a);
74
- for (const [pkg, imports] of b.entries()) {
75
- const existing = result.get(pkg);
76
- if (existing === "*") continue;
77
- if (imports === "*") {
78
- result.set(pkg, "*");
79
- continue;
80
- }
81
- if (!existing) {
82
- result.set(pkg, imports);
83
- } else {
84
- for (const name of imports) {
85
- existing.add(name);
86
- }
87
- }
88
- }
89
- return result;
90
- }
91
- function parserLanguageForFile(filename) {
92
- const extension = extname(filename).toLowerCase();
93
- if (extension === ".tsx") return "tsx";
94
- if (extension === ".ts") return "ts";
95
- if (extension === ".jsx") return "jsx";
96
- return "js";
97
- }
98
- function getObjectPropertyKeyName(node) {
99
- if (!node) return void 0;
100
- if (node.type === "Identifier") return node.name;
101
- if (node.type === "StringLiteral" || node.type === "Literal") {
102
- return typeof node.value === "string" ? node.value : void 0;
103
- }
104
- return void 0;
105
- }
106
- function stripServerOnlyEcoPageOptions(source, program) {
107
- const edits = [];
108
- function walk(node) {
109
- if (!node || typeof node !== "object") return;
110
- if (Array.isArray(node)) {
111
- for (const child of node) walk(child);
112
- return;
113
- }
114
- if (node.type === "CallExpression" && node.callee?.type === "MemberExpression" && node.callee.object?.type === "Identifier" && node.callee.object.name === "eco" && node.callee.property?.type === "Identifier" && node.callee.property.name === "page" && node.arguments?.[0]?.type === "ObjectExpression") {
115
- const objectExpression = node.arguments[0];
116
- const keptProperties = [];
117
- let removedProperty = false;
118
- for (const property of objectExpression.properties ?? []) {
119
- if (property?.type === "Property") {
120
- const keyName = getObjectPropertyKeyName(property.key);
121
- if (keyName && SERVER_ONLY_ECO_PAGE_OPTION_KEYS.has(keyName)) {
122
- removedProperty = true;
123
- continue;
124
- }
125
- }
126
- keptProperties.push(source.slice(property.start, property.end));
127
- }
128
- if (removedProperty) {
129
- const replacement = keptProperties.length > 0 ? `{ ${keptProperties.join(", ")} }` : "{}";
130
- edits.push({
131
- start: objectExpression.start,
132
- end: objectExpression.end,
133
- replacement
134
- });
135
- }
136
- }
137
- for (const key in node) {
138
- if (key !== "type" && key !== "start" && key !== "end") {
139
- walk(node[key]);
140
- }
141
- }
142
- }
143
- walk(program);
144
- if (edits.length === 0) {
145
- return { transformed: source, modified: false };
146
- }
147
- edits.sort((a, b) => b.start - a.start);
148
- let transformed = source;
149
- for (const edit of edits) {
150
- transformed = transformed.slice(0, edit.start) + edit.replacement + transformed.slice(edit.end);
151
- }
152
- return { transformed, modified: true };
153
- }
154
- function normalizeRequestedExportsKey(pathname) {
155
- let normalized = pathname.replace(/\\/g, "/");
156
- normalized = normalized.replace(/\.(tsx?|jsx?)$/i, "");
157
- if (normalized.endsWith("/index")) {
158
- normalized = normalized.slice(0, -"/index".length);
159
- }
160
- return normalized;
161
- }
162
- function resolveRequestedExportsKey(importer, specifier) {
163
- if (isBareSpecifier(specifier) || isProjectAliasSpecifier(specifier)) {
164
- return void 0;
165
- }
166
- const resolved = specifier.startsWith("/") ? specifier : resolve(dirname(importer), specifier);
167
- return normalizeRequestedExportsKey(resolved);
168
- }
169
- function mergeRequestedExportRules(registry, moduleKey, rules) {
170
- const existing = registry.get(moduleKey);
171
- if (existing === "*") return;
172
- if (rules === "*") {
173
- registry.set(moduleKey, "*");
174
- return;
175
- }
176
- if (!existing) {
177
- registry.set(moduleKey, new Set(rules));
178
- return;
179
- }
180
- for (const rule of rules) {
181
- existing.add(rule);
182
- }
183
- }
184
- function cloneRequestedExportRules(rules) {
185
- return rules === "*" ? rules : new Set(rules);
186
- }
187
- function snapshotRegistry(registry) {
188
- const out = /* @__PURE__ */ new Map();
189
- for (const [key, rules] of registry) {
190
- out.set(key, cloneRequestedExportRules(rules));
191
- }
192
- return out;
193
- }
194
- function diffRequestedExportRules(before, after) {
195
- if (!before) {
196
- return cloneRequestedExportRules(after);
197
- }
198
- if (before === "*") {
199
- return void 0;
200
- }
201
- if (after === "*") {
202
- return "*";
203
- }
204
- const addedRules = /* @__PURE__ */ new Set();
205
- for (const rule of after) {
206
- if (!before.has(rule)) {
207
- addedRules.add(rule);
208
- }
209
- }
210
- return addedRules.size > 0 ? addedRules : void 0;
211
- }
212
- function transformModuleImports(source, filename, globallyAllowed, requestedExports) {
213
- let result;
214
- try {
215
- result = cachedParseSync(filename, source, {
216
- sourceType: "module",
217
- lang: parserLanguageForFile(filename)
218
- });
219
- } catch {
220
- return { transformed: source, modified: false };
221
- }
222
- const { program } = result;
223
- const localDeclared = [];
224
- function walk(node) {
225
- if (!node || typeof node !== "object") return;
226
- if (Array.isArray(node)) {
227
- for (const child of node) walk(child);
228
- return;
229
- }
230
- if (node.type === "Property" && node.key?.name === "modules" && node.value?.type === "ArrayExpression") {
231
- for (const el of node.value.elements) {
232
- if ((el.type === "StringLiteral" || el.type === "Literal") && typeof el.value === "string") {
233
- localDeclared.push(el.value);
234
- }
235
- }
236
- }
237
- for (const key in node) {
238
- if (key !== "type" && key !== "start" && key !== "end") {
239
- walk(node[key]);
240
- }
241
- }
242
- }
243
- walk(program);
244
- const locallyAllowed = parseDeclaredModules(localDeclared);
245
- const allowedMap = mergeDeclaredModulesMap(globallyAllowed, locallyAllowed);
246
- const explicitRequestedExports = requestedExports.get(normalizeRequestedExportsKey(filename));
247
- const reachability = analyzeReachability(source, filename, program, explicitRequestedExports);
248
- for (const statement of program.body) {
249
- if (statement.type === "ImportDeclaration") {
250
- const reachableRules = reachability.reachableImports.get(statement.source.value);
251
- const requestedModuleKey = resolveRequestedExportsKey(filename, statement.source.value);
252
- if (!requestedModuleKey || !reachableRules) continue;
253
- mergeRequestedExportRules(requestedExports, requestedModuleKey, reachableRules);
254
- continue;
255
- }
256
- if (statement.type === "ExportNamedDeclaration" && statement.source) {
257
- const reachableRules = reachability.reachableImports.get(statement.source.value);
258
- const requestedModuleKey = resolveRequestedExportsKey(filename, statement.source.value);
259
- if (!requestedModuleKey || !reachableRules) continue;
260
- mergeRequestedExportRules(requestedExports, requestedModuleKey, reachableRules);
261
- continue;
262
- }
263
- if (statement.type === "ExportAllDeclaration" && statement.source) {
264
- const reachableRules = reachability.reachableImports.get(statement.source.value);
265
- const requestedModuleKey = resolveRequestedExportsKey(filename, statement.source.value);
266
- if (!requestedModuleKey || !reachableRules) continue;
267
- mergeRequestedExportRules(requestedExports, requestedModuleKey, reachableRules);
268
- }
269
- }
270
- const edits = [];
271
- function processSpecifier(specifier) {
272
- const moduleBase = toModuleBaseSpecifier(specifier);
273
- const explicitRules = allowedMap.get(moduleBase);
274
- if (isServerOnlySpecifier(specifier)) {
275
- if (explicitRules) {
276
- return { allowed: true, rules: explicitRules };
277
- }
278
- return { allowed: false };
279
- }
280
- if (isProjectAliasSpecifier(specifier)) return { allowed: true, rules: explicitRules ?? "*" };
281
- if (!isBareSpecifier(specifier)) return { allowed: true, rules: explicitRules ?? "*" };
282
- return { allowed: true, rules: explicitRules ?? "*" };
283
- }
284
- function walkImports(node) {
285
- if (!node || typeof node !== "object") return;
286
- if (Array.isArray(node)) {
287
- for (const child of node) walkImports(child);
288
- return;
289
- }
290
- if (node.type === "ImportDeclaration") {
291
- const specifier = node.source.value;
292
- const reachableRules = reachability.reachableImports.get(specifier);
293
- const { allowed, rules } = processSpecifier(specifier);
294
- if (!allowed) {
295
- if (reachableRules && !reachability.isFallbackRoots) {
296
- throw new Error(
297
- `[Ecopages Client Reachability] Forbidden client import '${specifier}' at ${filename}:${node.start}. This import is explicitly reachable from the React render function.`
298
- );
299
- } else {
300
- edits.push({ start: node.start, end: node.end, replacement: "" });
301
- }
302
- return;
303
- }
304
- if (rules instanceof Set && node.specifiers && node.specifiers.length > 0) {
305
- let keptSpecifierCount = 0;
306
- let defaultImportLocal;
307
- let namespaceImportLocal;
308
- const namedImportNodes = [];
309
- for (const spec of node.specifiers) {
310
- if (spec.type === "ImportSpecifier") {
311
- const importedName = spec.imported.type === "Identifier" ? spec.imported.name : spec.imported.value;
312
- if (rules.has(importedName)) {
313
- keptSpecifierCount += 1;
314
- const localName = spec.local?.name;
315
- if (localName && localName !== importedName) {
316
- namedImportNodes.push(`${importedName} as ${localName}`);
317
- } else {
318
- namedImportNodes.push(importedName);
319
- }
320
- }
321
- } else if (spec.type === "ImportDefaultSpecifier") {
322
- if (rules.has("default")) {
323
- keptSpecifierCount += 1;
324
- defaultImportLocal = spec.local.name;
325
- }
326
- } else if (spec.type === "ImportNamespaceSpecifier") {
327
- if (rules.has("*")) {
328
- keptSpecifierCount += 1;
329
- namespaceImportLocal = spec.local.name;
330
- }
331
- }
332
- }
333
- if (keptSpecifierCount === 0) {
334
- edits.push({ start: node.start, end: node.end, replacement: "" });
335
- } else if (keptSpecifierCount < node.specifiers.length) {
336
- let newDeclaration = "";
337
- if (defaultImportLocal && namespaceImportLocal) {
338
- newDeclaration = `import ${defaultImportLocal}, * as ${namespaceImportLocal} from '${specifier}';`;
339
- } else if (namespaceImportLocal) {
340
- newDeclaration = `import * as ${namespaceImportLocal} from '${specifier}';`;
341
- } else if (defaultImportLocal && namedImportNodes.length > 0) {
342
- newDeclaration = `import ${defaultImportLocal}, { ${namedImportNodes.join(", ")} } from '${specifier}';`;
343
- } else if (defaultImportLocal) {
344
- newDeclaration = `import ${defaultImportLocal} from '${specifier}';`;
345
- } else {
346
- newDeclaration = `import { ${namedImportNodes.join(", ")} } from '${specifier}';`;
347
- }
348
- edits.push({ start: node.start, end: node.end, replacement: newDeclaration });
349
- }
350
- return;
351
- }
352
- if (!reachableRules && (!node.specifiers || node.specifiers.length === 0)) {
353
- edits.push({ start: node.start, end: node.end, replacement: "" });
354
- }
355
- return;
356
- }
357
- if (node.type === "ExportNamedDeclaration" && node.source) {
358
- const specifier = node.source.value;
359
- const { allowed } = processSpecifier(specifier);
360
- if (!allowed) {
361
- const reachableRules = reachability.reachableImports.get(specifier);
362
- if (reachableRules && !reachability.isFallbackRoots) {
363
- throw new Error(
364
- `[Ecopages Client Reachability] Forbidden client export from '${specifier}' at ${filename}:${node.start}. This export is explicitly reachable from the React render function.`
365
- );
366
- } else {
367
- edits.push({ start: node.start, end: node.end, replacement: "" });
368
- }
369
- }
370
- return;
371
- }
372
- if (node.type === "ExportAllDeclaration" && node.source) {
373
- const specifier = node.source.value;
374
- const { allowed } = processSpecifier(specifier);
375
- if (!allowed) {
376
- const reachableRules = reachability.reachableImports.get(specifier);
377
- if (reachableRules && !reachability.isFallbackRoots) {
378
- throw new Error(
379
- `[Ecopages Client Reachability] Forbidden client export * from '${specifier}' at ${filename}:${node.start}. This export is explicitly reachable from the React render function.`
380
- );
381
- } else {
382
- edits.push({ start: node.start, end: node.end, replacement: "" });
383
- }
384
- }
385
- return;
386
- }
387
- if (node.type === "ImportExpression" && node.source?.value) {
388
- const specifier = node.source.value;
389
- const { allowed } = processSpecifier(specifier);
390
- const reachableRules = reachability.reachableImports.get(specifier);
391
- if (!reachableRules) {
392
- if (!allowed) {
393
- edits.push({ start: node.start, end: node.end, replacement: "Promise.resolve({})" });
394
- }
395
- return;
396
- }
397
- if (!allowed) {
398
- if (!reachability.isFallbackRoots) {
399
- throw new Error(
400
- `[Ecopages Client Reachability] Forbidden dynamic import('${specifier}') at ${filename}:${node.start}. This import is explicitly reachable from the React render function.`
401
- );
402
- } else {
403
- edits.push({ start: node.start, end: node.end, replacement: "Promise.resolve({})" });
404
- }
405
- }
406
- return;
407
- }
408
- if (node.type === "CallExpression" && node.callee?.type === "Identifier" && node.callee.name === "require") {
409
- const arg = node.arguments?.[0];
410
- if (arg && (arg.type === "StringLiteral" || arg.type === "Literal") && typeof arg.value === "string") {
411
- const specifier = arg.value;
412
- const { allowed } = processSpecifier(specifier);
413
- const reachableRules = reachability.reachableImports.get(specifier);
414
- if (!reachableRules) {
415
- if (!allowed) {
416
- edits.push({ start: node.start, end: node.end, replacement: "({})" });
417
- }
418
- } else if (!allowed) {
419
- if (!reachability.isFallbackRoots) {
420
- throw new Error(
421
- `[Ecopages Client Reachability] Forbidden require('${specifier}') at ${filename}:${node.start}. This import is explicitly reachable from the React render function.`
422
- );
423
- } else {
424
- edits.push({ start: node.start, end: node.end, replacement: "({})" });
425
- }
426
- }
427
- }
428
- }
429
- for (const key in node) {
430
- if (key !== "type" && key !== "start" && key !== "end") {
431
- walkImports(node[key]);
432
- }
433
- }
434
- }
435
- walkImports(program);
436
- if (edits.length === 0) {
437
- return stripServerOnlyEcoPageOptions(source, program);
438
- }
439
- edits.sort((a, b) => b.start - a.start);
440
- let transformed = source;
441
- for (const edit of edits) {
442
- transformed = transformed.slice(0, edit.start) + edit.replacement + transformed.slice(edit.end);
443
- }
444
- let reparsedResult;
445
- try {
446
- reparsedResult = cachedParseSync(filename, transformed, {
447
- sourceType: "module",
448
- lang: parserLanguageForFile(filename)
449
- });
450
- } catch {
451
- return { transformed, modified: true };
452
- }
453
- const strippedPageOptions = stripServerOnlyEcoPageOptions(transformed, reparsedResult.program);
454
- if (strippedPageOptions.modified) {
455
- return strippedPageOptions;
456
- }
457
- return { transformed, modified: true };
458
- }
459
- function createClientGraphBoundaryPlugin(options) {
460
- return {
461
- name: "ecopages-client-graph-boundary",
462
- setup(build) {
463
- const absWorkingDir = options?.absWorkingDir ?? process.cwd();
464
- const cache = options?.cache;
465
- const globallyDeclaredSources = parseDeclaredModules(options?.declaredModules);
466
- const requestedExports = /* @__PURE__ */ new Map();
467
- for (const alwaysAllow of options?.alwaysAllowSpecifiers ?? []) {
468
- globallyDeclaredSources.set(toModuleBaseSpecifier(alwaysAllow), "*");
469
- }
470
- const allowListForCache = Array.from(globallyDeclaredSources.keys()).sort();
471
- build.onLoad({ filter: SOURCE_FILE_FILTER }, (args) => {
472
- let source;
473
- try {
474
- source = readFileSync(args.path, "utf-8");
475
- } catch {
476
- return void 0;
477
- }
478
- if (cache) {
479
- const cached = cache.get(args.path, source, allowListForCache);
480
- if (cached) {
481
- for (const [moduleKey, rules] of cached.rulesAdded) {
482
- mergeRequestedExportRules(requestedExports, moduleKey, rules);
483
- }
484
- if (!cached.modified) return void 0;
485
- const ext2 = extname(args.path).slice(1);
486
- return { contents: cached.transformed, loader: ext2, resolveDir: dirname(args.path) };
487
- }
488
- }
489
- let transformed = source;
490
- let modified = false;
491
- if (source.includes("readFileSync")) {
492
- const readFileTransformed = transformed.replace(
493
- /\bfs\.readFileSync\s*\(\s*path\.resolve\s*\(\s*(['"`])([^'"`\n]+)\1\s*\)\s*,\s*['"`]utf-?8['"`]\s*\)/g,
494
- (_match, _q, relPath) => {
495
- modified = true;
496
- try {
497
- const sourceDir = dirname(args.path);
498
- const srcDirIndex = args.path.lastIndexOf("/src/");
499
- const inferredProjectRoot = srcDirIndex >= 0 ? args.path.slice(0, srcDirIndex) : void 0;
500
- const candidates = [
501
- resolve(absWorkingDir, relPath),
502
- resolve(process.cwd(), relPath),
503
- resolve(sourceDir, relPath),
504
- ...inferredProjectRoot ? [resolve(inferredProjectRoot, relPath)] : []
505
- ];
506
- const absolutePath = candidates.find((candidate) => existsSync(candidate));
507
- if (!absolutePath) return '""';
508
- const content = readFileSync(absolutePath, "utf-8");
509
- return JSON.stringify(content);
510
- } catch {
511
- return '""';
512
- }
513
- }
514
- );
515
- transformed = readFileTransformed;
516
- }
517
- const registryBefore = snapshotRegistry(requestedExports);
518
- const { transformed: oxcTransformed, modified: importsModified } = transformModuleImports(
519
- transformed,
520
- args.path,
521
- globallyDeclaredSources,
522
- requestedExports
523
- );
524
- if (importsModified) {
525
- modified = true;
526
- transformed = oxcTransformed;
527
- }
528
- if (cache) {
529
- const rulesAdded = /* @__PURE__ */ new Map();
530
- for (const [key, afterRules] of requestedExports) {
531
- const beforeRules = registryBefore.get(key);
532
- const diff = diffRequestedExportRules(beforeRules, afterRules);
533
- if (!diff) continue;
534
- rulesAdded.set(key, diff);
535
- }
536
- const entry = {
537
- transformed,
538
- modified,
539
- rulesAdded
540
- };
541
- cache.set(args.path, source, allowListForCache, entry);
542
- }
543
- if (!modified) return void 0;
544
- const ext = extname(args.path).slice(1);
545
- return { contents: transformed, loader: ext, resolveDir: dirname(args.path) };
546
- });
547
- }
548
- };
549
- }
550
- export {
551
- createClientGraphBoundaryPlugin
552
- };
@@ -1,19 +0,0 @@
1
- import { useEffect, useState } from "react";
2
- const useIsClient = () => {
3
- const [isClient, setIsClient] = useState(false);
4
- useEffect(() => {
5
- setIsClient(true);
6
- }, []);
7
- return isClient;
8
- };
9
- const ClientOnly = ({ children, fallback = null }) => {
10
- const isClient = useIsClient();
11
- if (!isClient) {
12
- return fallback;
13
- }
14
- return children;
15
- };
16
- export {
17
- ClientOnly,
18
- useIsClient
19
- };
@@ -1,54 +0,0 @@
1
- function walkConfigTree(config, visitor, visited = /* @__PURE__ */ new Set()) {
2
- if (!config || visited.has(config)) {
3
- return;
4
- }
5
- visited.add(config);
6
- visitor(config);
7
- if (config.layout?.config) {
8
- walkConfigTree(config.layout.config, visitor, visited);
9
- }
10
- for (const component of config.dependencies?.components ?? []) {
11
- walkConfigTree(component.config, visitor, visited);
12
- }
13
- }
14
- function walkConfigForest(configs, visitor) {
15
- const visited = /* @__PURE__ */ new Set();
16
- for (const config of configs) {
17
- walkConfigTree(config, visitor, visited);
18
- }
19
- }
20
- function collectFromConfigTree(config, collector) {
21
- const values = [];
22
- walkConfigTree(config, (node) => {
23
- values.push(...collector(node));
24
- });
25
- return values;
26
- }
27
- function collectFromConfigForest(configs, collector) {
28
- const values = [];
29
- walkConfigForest(configs, (node) => {
30
- values.push(...collector(node));
31
- });
32
- return values;
33
- }
34
- function someInConfigTree(config, predicate) {
35
- let matched = false;
36
- walkConfigTree(config, (node) => {
37
- if (matched) {
38
- return;
39
- }
40
- matched = predicate(node);
41
- });
42
- return matched;
43
- }
44
- function getComponentConfigs(components) {
45
- return components.map((component) => component.config);
46
- }
47
- export {
48
- collectFromConfigForest,
49
- collectFromConfigTree,
50
- getComponentConfigs,
51
- someInConfigTree,
52
- walkConfigForest,
53
- walkConfigTree
54
- };
@@ -1,47 +0,0 @@
1
- import { collectFromConfigTree } from "./component-config-traversal.js";
2
- function parseDeclaredModuleSource(value) {
3
- const source = value.trim();
4
- if (source.length === 0) return void 0;
5
- const openBraceIndex = source.indexOf("{");
6
- if (openBraceIndex < 0) return source;
7
- const from = source.slice(0, openBraceIndex).trim();
8
- return from.length > 0 ? from : void 0;
9
- }
10
- function normalizeDeclaredModuleSources(modules) {
11
- const seen = /* @__PURE__ */ new Set();
12
- for (const declaration of modules ?? []) {
13
- const from = parseDeclaredModuleSource(declaration);
14
- if (from) {
15
- seen.add(from);
16
- }
17
- }
18
- return Array.from(seen);
19
- }
20
- function collectDeclaredModulesInConfig(config) {
21
- return collectFromConfigTree(config, (node) => normalizeDeclaredModuleSources(node.dependencies?.modules));
22
- }
23
- function collectPageDeclaredModulesFromModule(pageModule) {
24
- const declarations = [
25
- ...collectDeclaredModulesInConfig(pageModule.default?.config),
26
- ...collectDeclaredModulesInConfig(pageModule.config)
27
- ];
28
- return Array.from(new Set(declarations));
29
- }
30
- async function collectPageDeclaredModules(pagePath) {
31
- try {
32
- const pageModule = await import(
33
- /* @vite-ignore */
34
- pagePath
35
- );
36
- return collectPageDeclaredModulesFromModule(pageModule);
37
- } catch {
38
- return [];
39
- }
40
- }
41
- export {
42
- collectDeclaredModulesInConfig,
43
- collectPageDeclaredModules,
44
- collectPageDeclaredModulesFromModule,
45
- normalizeDeclaredModuleSources,
46
- parseDeclaredModuleSource
47
- };
@@ -1,12 +0,0 @@
1
- import { lazy } from "react";
2
- const NullComponent = () => null;
3
- function dynamic(importFn, options = {}) {
4
- const { ssr = false } = options;
5
- if (ssr || typeof window !== "undefined") {
6
- return lazy(importFn);
7
- }
8
- return NullComponent;
9
- }
10
- export {
11
- dynamic
12
- };