@ecopages/react 0.2.0-beta.12 → 0.2.0-beta.14

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 (29) hide show
  1. package/README.md +12 -0
  2. package/package.json +20 -7
  3. package/src/layout-compose.d.ts +58 -0
  4. package/src/layout-compose.js +98 -0
  5. package/src/react-hmr-strategy.js +1 -1
  6. package/src/react-renderer.d.ts +31 -4
  7. package/src/react-renderer.js +157 -45
  8. package/src/react.plugin.js +1 -1
  9. package/src/serialize-page-data-script.d.ts +8 -0
  10. package/src/serialize-page-data-script.js +10 -0
  11. package/src/services/react-mdx-config-dependency.service.js +5 -7
  12. package/src/services/react-page-payload.service.js +2 -2
  13. package/src/utils/client-graph-ast-transform.d.ts +18 -0
  14. package/src/utils/client-graph-ast-transform.js +335 -0
  15. package/src/utils/client-graph-boundary-plugin.d.ts +1 -12
  16. package/src/utils/client-graph-boundary-plugin.js +8 -454
  17. package/src/utils/client-graph-reachability.d.ts +75 -0
  18. package/src/utils/client-graph-reachability.js +148 -0
  19. package/src/utils/component-config-traversal.js +4 -2
  20. package/src/utils/hydration-scripts.d.ts +2 -0
  21. package/src/utils/hydration-scripts.js +27 -14
  22. package/src/utils/react-mdx-loader-plugin.js +1 -1
  23. package/node_modules/@ecopages/mdx-core/package.json +0 -22
  24. package/node_modules/@ecopages/mdx-core/src/index.d.ts +0 -2
  25. package/node_modules/@ecopages/mdx-core/src/index.js +0 -18
  26. package/node_modules/@ecopages/mdx-core/src/mdx-loader-plugin.d.ts +0 -11
  27. package/node_modules/@ecopages/mdx-core/src/mdx-loader-plugin.js +0 -44
  28. package/node_modules/@ecopages/mdx-core/src/mdx-utils.d.ts +0 -29
  29. package/node_modules/@ecopages/mdx-core/src/mdx-utils.js +0 -55
@@ -0,0 +1,18 @@
1
+ /**
2
+ * AST transforms for the client graph boundary plugin.
3
+ */
4
+ import type { RequestedExportRules } from './client-graph-boundary-cache.js';
5
+ /**
6
+ * Parses a module using Oxc AST and surgically removes forbidden imports.
7
+ * Filters down to the exact specifiers requested via `{namedImport}` syntax.
8
+ *
9
+ * @param source - The raw string source content of the module.
10
+ * @param filename - The absolute path of the module.
11
+ * @param globallyAllowed - A map of modules declared globally allowable by the build configuration.
12
+ * @param requestedExports - Local requested-export registry used to propagate named reachability across files.
13
+ * @returns An object containing the transformed string and a boolean indicating if changes occurred.
14
+ */
15
+ export declare function transformModuleImports(source: string, filename: string, globallyAllowed: Map<string, Set<string> | '*'>, requestedExports: Map<string, RequestedExportRules>): {
16
+ transformed: string;
17
+ modified: boolean;
18
+ };
@@ -0,0 +1,335 @@
1
+ import { extname } from "node:path";
2
+ import { cachedParseSync } from "@ecopages/core/cache";
3
+ import { analyzeReachability } from "./reachability-analyzer.js";
4
+ import {
5
+ isBareSpecifier,
6
+ isProjectAliasSpecifier,
7
+ isServerOnlySpecifier,
8
+ toModuleBaseSpecifier,
9
+ parseDeclaredModules,
10
+ mergeDeclaredModulesMap,
11
+ normalizeRequestedExportsKey,
12
+ resolveRequestedExportsKey,
13
+ mergeRequestedExportRules
14
+ } from "./client-graph-reachability.js";
15
+ const SERVER_ONLY_ECO_PAGE_OPTION_KEYS = /* @__PURE__ */ new Set([
16
+ "cache",
17
+ "middleware",
18
+ "requires",
19
+ "metadata",
20
+ "staticProps",
21
+ "staticPaths"
22
+ ]);
23
+ function parserLanguageForFile(filename) {
24
+ const extension = extname(filename).toLowerCase();
25
+ if (extension === ".tsx") return "tsx";
26
+ if (extension === ".ts") return "ts";
27
+ if (extension === ".jsx") return "jsx";
28
+ return "js";
29
+ }
30
+ function getObjectPropertyKeyName(node) {
31
+ if (!node) return void 0;
32
+ if (node.type === "Identifier") return node.name;
33
+ if (node.type === "StringLiteral" || node.type === "Literal") {
34
+ return typeof node.value === "string" ? node.value : void 0;
35
+ }
36
+ return void 0;
37
+ }
38
+ function stripServerOnlyEcoPageOptions(source, program) {
39
+ const edits = [];
40
+ function walk(node) {
41
+ if (!node || typeof node !== "object") return;
42
+ if (Array.isArray(node)) {
43
+ for (const child of node) walk(child);
44
+ return;
45
+ }
46
+ 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") {
47
+ const objectExpression = node.arguments[0];
48
+ const keptProperties = [];
49
+ let removedProperty = false;
50
+ for (const property of objectExpression.properties ?? []) {
51
+ if (property?.type === "Property") {
52
+ const keyName = getObjectPropertyKeyName(property.key);
53
+ if (keyName && SERVER_ONLY_ECO_PAGE_OPTION_KEYS.has(keyName)) {
54
+ removedProperty = true;
55
+ continue;
56
+ }
57
+ }
58
+ keptProperties.push(source.slice(property.start, property.end));
59
+ }
60
+ if (removedProperty) {
61
+ const replacement = keptProperties.length > 0 ? `{ ${keptProperties.join(", ")} }` : "{}";
62
+ edits.push({
63
+ start: objectExpression.start,
64
+ end: objectExpression.end,
65
+ replacement
66
+ });
67
+ }
68
+ }
69
+ for (const key in node) {
70
+ if (key !== "type" && key !== "start" && key !== "end") {
71
+ walk(node[key]);
72
+ }
73
+ }
74
+ }
75
+ walk(program);
76
+ if (edits.length === 0) {
77
+ return { transformed: source, modified: false };
78
+ }
79
+ edits.sort((a, b) => b.start - a.start);
80
+ let transformed = source;
81
+ for (const edit of edits) {
82
+ transformed = transformed.slice(0, edit.start) + edit.replacement + transformed.slice(edit.end);
83
+ }
84
+ return { transformed, modified: true };
85
+ }
86
+ function transformModuleImports(source, filename, globallyAllowed, requestedExports) {
87
+ let result;
88
+ try {
89
+ result = cachedParseSync(filename, source, {
90
+ sourceType: "module",
91
+ lang: parserLanguageForFile(filename)
92
+ });
93
+ } catch {
94
+ return { transformed: source, modified: false };
95
+ }
96
+ const { program } = result;
97
+ const localDeclared = [];
98
+ function walk(node) {
99
+ if (!node || typeof node !== "object") return;
100
+ if (Array.isArray(node)) {
101
+ for (const child of node) walk(child);
102
+ return;
103
+ }
104
+ if (node.type === "Property" && node.key?.name === "modules" && node.value?.type === "ArrayExpression") {
105
+ for (const el of node.value.elements) {
106
+ if ((el.type === "StringLiteral" || el.type === "Literal") && typeof el.value === "string") {
107
+ localDeclared.push(el.value);
108
+ }
109
+ }
110
+ }
111
+ for (const key in node) {
112
+ if (key !== "type" && key !== "start" && key !== "end") {
113
+ walk(node[key]);
114
+ }
115
+ }
116
+ }
117
+ walk(program);
118
+ const locallyAllowed = parseDeclaredModules(localDeclared);
119
+ const allowedMap = mergeDeclaredModulesMap(globallyAllowed, locallyAllowed);
120
+ const explicitRequestedExports = requestedExports.get(normalizeRequestedExportsKey(filename));
121
+ const reachability = analyzeReachability(source, filename, program, explicitRequestedExports);
122
+ for (const statement of program.body) {
123
+ if (statement.type === "ImportDeclaration") {
124
+ const reachableRules = reachability.reachableImports.get(statement.source.value);
125
+ const requestedModuleKey = resolveRequestedExportsKey(filename, statement.source.value);
126
+ if (!requestedModuleKey || !reachableRules) continue;
127
+ mergeRequestedExportRules(requestedExports, requestedModuleKey, reachableRules);
128
+ continue;
129
+ }
130
+ if (statement.type === "ExportNamedDeclaration" && statement.source) {
131
+ const reachableRules = reachability.reachableImports.get(statement.source.value);
132
+ const requestedModuleKey = resolveRequestedExportsKey(filename, statement.source.value);
133
+ if (!requestedModuleKey || !reachableRules) continue;
134
+ mergeRequestedExportRules(requestedExports, requestedModuleKey, reachableRules);
135
+ continue;
136
+ }
137
+ if (statement.type === "ExportAllDeclaration" && statement.source) {
138
+ const reachableRules = reachability.reachableImports.get(statement.source.value);
139
+ const requestedModuleKey = resolveRequestedExportsKey(filename, statement.source.value);
140
+ if (!requestedModuleKey || !reachableRules) continue;
141
+ mergeRequestedExportRules(requestedExports, requestedModuleKey, reachableRules);
142
+ }
143
+ }
144
+ const edits = [];
145
+ function processSpecifier(specifier) {
146
+ const moduleBase = toModuleBaseSpecifier(specifier);
147
+ const explicitRules = allowedMap.get(moduleBase);
148
+ if (isServerOnlySpecifier(specifier)) {
149
+ if (explicitRules) {
150
+ return { allowed: true, rules: explicitRules };
151
+ }
152
+ return { allowed: false };
153
+ }
154
+ if (isProjectAliasSpecifier(specifier)) return { allowed: true, rules: explicitRules ?? "*" };
155
+ if (!isBareSpecifier(specifier)) return { allowed: true, rules: explicitRules ?? "*" };
156
+ return { allowed: true, rules: explicitRules ?? "*" };
157
+ }
158
+ function walkImports(node) {
159
+ if (!node || typeof node !== "object") return;
160
+ if (Array.isArray(node)) {
161
+ for (const child of node) walkImports(child);
162
+ return;
163
+ }
164
+ if (node.type === "ImportDeclaration") {
165
+ const specifier = node.source.value;
166
+ const reachableRules = reachability.reachableImports.get(specifier);
167
+ const { allowed, rules } = processSpecifier(specifier);
168
+ if (!allowed) {
169
+ if (reachableRules && !reachability.isFallbackRoots) {
170
+ throw new Error(
171
+ `[Ecopages Client Reachability] Forbidden client import '${specifier}' at ${filename}:${node.start}. This import is explicitly reachable from the React render function.`
172
+ );
173
+ } else {
174
+ edits.push({ start: node.start, end: node.end, replacement: "" });
175
+ }
176
+ return;
177
+ }
178
+ if (rules instanceof Set && node.specifiers && node.specifiers.length > 0) {
179
+ let keptSpecifierCount = 0;
180
+ let defaultImportLocal;
181
+ let namespaceImportLocal;
182
+ const namedImportNodes = [];
183
+ for (const spec of node.specifiers) {
184
+ if (spec.type === "ImportSpecifier") {
185
+ const importedName = spec.imported.type === "Identifier" ? spec.imported.name : spec.imported.value;
186
+ if (rules.has(importedName)) {
187
+ keptSpecifierCount += 1;
188
+ const localName = spec.local?.name;
189
+ if (localName && localName !== importedName) {
190
+ namedImportNodes.push(`${importedName} as ${localName}`);
191
+ } else {
192
+ namedImportNodes.push(importedName);
193
+ }
194
+ }
195
+ } else if (spec.type === "ImportDefaultSpecifier") {
196
+ if (rules.has("default")) {
197
+ keptSpecifierCount += 1;
198
+ defaultImportLocal = spec.local.name;
199
+ }
200
+ } else if (spec.type === "ImportNamespaceSpecifier") {
201
+ if (rules.has("*")) {
202
+ keptSpecifierCount += 1;
203
+ namespaceImportLocal = spec.local.name;
204
+ }
205
+ }
206
+ }
207
+ if (keptSpecifierCount === 0) {
208
+ edits.push({ start: node.start, end: node.end, replacement: "" });
209
+ } else if (keptSpecifierCount < node.specifiers.length) {
210
+ let newDeclaration = "";
211
+ if (defaultImportLocal && namespaceImportLocal) {
212
+ newDeclaration = `import ${defaultImportLocal}, * as ${namespaceImportLocal} from '${specifier}';`;
213
+ } else if (namespaceImportLocal) {
214
+ newDeclaration = `import * as ${namespaceImportLocal} from '${specifier}';`;
215
+ } else if (defaultImportLocal && namedImportNodes.length > 0) {
216
+ newDeclaration = `import ${defaultImportLocal}, { ${namedImportNodes.join(", ")} } from '${specifier}';`;
217
+ } else if (defaultImportLocal) {
218
+ newDeclaration = `import ${defaultImportLocal} from '${specifier}';`;
219
+ } else {
220
+ newDeclaration = `import { ${namedImportNodes.join(", ")} } from '${specifier}';`;
221
+ }
222
+ edits.push({ start: node.start, end: node.end, replacement: newDeclaration });
223
+ }
224
+ return;
225
+ }
226
+ if (!reachableRules && (!node.specifiers || node.specifiers.length === 0)) {
227
+ edits.push({ start: node.start, end: node.end, replacement: "" });
228
+ }
229
+ return;
230
+ }
231
+ if (node.type === "ExportNamedDeclaration" && node.source) {
232
+ const specifier = node.source.value;
233
+ const { allowed } = processSpecifier(specifier);
234
+ if (!allowed) {
235
+ const reachableRules = reachability.reachableImports.get(specifier);
236
+ if (reachableRules && !reachability.isFallbackRoots) {
237
+ throw new Error(
238
+ `[Ecopages Client Reachability] Forbidden client export from '${specifier}' at ${filename}:${node.start}. This export is explicitly reachable from the React render function.`
239
+ );
240
+ } else {
241
+ edits.push({ start: node.start, end: node.end, replacement: "" });
242
+ }
243
+ }
244
+ return;
245
+ }
246
+ if (node.type === "ExportAllDeclaration" && node.source) {
247
+ const specifier = node.source.value;
248
+ const { allowed } = processSpecifier(specifier);
249
+ if (!allowed) {
250
+ const reachableRules = reachability.reachableImports.get(specifier);
251
+ if (reachableRules && !reachability.isFallbackRoots) {
252
+ throw new Error(
253
+ `[Ecopages Client Reachability] Forbidden client export * from '${specifier}' at ${filename}:${node.start}. This export is explicitly reachable from the React render function.`
254
+ );
255
+ } else {
256
+ edits.push({ start: node.start, end: node.end, replacement: "" });
257
+ }
258
+ }
259
+ return;
260
+ }
261
+ if (node.type === "ImportExpression" && node.source?.value) {
262
+ const specifier = node.source.value;
263
+ const { allowed } = processSpecifier(specifier);
264
+ const reachableRules = reachability.reachableImports.get(specifier);
265
+ if (!reachableRules) {
266
+ if (!allowed) {
267
+ edits.push({ start: node.start, end: node.end, replacement: "Promise.resolve({})" });
268
+ }
269
+ return;
270
+ }
271
+ if (!allowed) {
272
+ if (!reachability.isFallbackRoots) {
273
+ throw new Error(
274
+ `[Ecopages Client Reachability] Forbidden dynamic import('${specifier}') at ${filename}:${node.start}. This import is explicitly reachable from the React render function.`
275
+ );
276
+ } else {
277
+ edits.push({ start: node.start, end: node.end, replacement: "Promise.resolve({})" });
278
+ }
279
+ }
280
+ return;
281
+ }
282
+ if (node.type === "CallExpression" && node.callee?.type === "Identifier" && node.callee.name === "require") {
283
+ const arg = node.arguments?.[0];
284
+ if (arg && (arg.type === "StringLiteral" || arg.type === "Literal") && typeof arg.value === "string") {
285
+ const specifier = arg.value;
286
+ const { allowed } = processSpecifier(specifier);
287
+ const reachableRules = reachability.reachableImports.get(specifier);
288
+ if (!reachableRules) {
289
+ if (!allowed) {
290
+ edits.push({ start: node.start, end: node.end, replacement: "({})" });
291
+ }
292
+ } else if (!allowed) {
293
+ if (!reachability.isFallbackRoots) {
294
+ throw new Error(
295
+ `[Ecopages Client Reachability] Forbidden require('${specifier}') at ${filename}:${node.start}. This import is explicitly reachable from the React render function.`
296
+ );
297
+ } else {
298
+ edits.push({ start: node.start, end: node.end, replacement: "({})" });
299
+ }
300
+ }
301
+ }
302
+ }
303
+ for (const key in node) {
304
+ if (key !== "type" && key !== "start" && key !== "end") {
305
+ walkImports(node[key]);
306
+ }
307
+ }
308
+ }
309
+ walkImports(program);
310
+ if (edits.length === 0) {
311
+ return stripServerOnlyEcoPageOptions(source, program);
312
+ }
313
+ edits.sort((a, b) => b.start - a.start);
314
+ let transformed = source;
315
+ for (const edit of edits) {
316
+ transformed = transformed.slice(0, edit.start) + edit.replacement + transformed.slice(edit.end);
317
+ }
318
+ let reparsedResult;
319
+ try {
320
+ reparsedResult = cachedParseSync(filename, transformed, {
321
+ sourceType: "module",
322
+ lang: parserLanguageForFile(filename)
323
+ });
324
+ } catch {
325
+ return { transformed, modified: true };
326
+ }
327
+ const strippedPageOptions = stripServerOnlyEcoPageOptions(transformed, reparsedResult.program);
328
+ if (strippedPageOptions.modified) {
329
+ return strippedPageOptions;
330
+ }
331
+ return { transformed, modified: true };
332
+ }
333
+ export {
334
+ transformModuleImports
335
+ };
@@ -1,18 +1,7 @@
1
1
  /**
2
2
  * @module ClientGraphBoundaryPlugin
3
3
  *
4
- * This module defines the primary build plugin responsible for securing the Ecopages
5
- * isomorphic compilation pipeline. It ensures that backend-only code, sensitive Node.js APIs,
6
- * and massive server utilities do not accidentally leak into the browser bundle.
7
- *
8
- * It achieves this by intercepting all client module compilation passes and applying the
9
- * `analyzeReachability` AST pass. If a forbidden import (e.g. `node:fs` or `*.server.ts`)
10
- * is completely unreachable from the client component's `render` function, it is surgically
11
- * pruned. If a forbidden import IS reachable, the build is intentionally failed to prevent
12
- * runtime hydration crashes.
13
- *
14
- * Additionally, this plugin provides a build-time transform that statically resolves and
15
- * inlines `fs.readFileSync(path.resolve(...))` calls to prevent server/client data mismatches.
4
+ * Build plugin securing the Ecopages isomorphic compilation pipeline.
16
5
  */
17
6
  import type { EcoBuildPlugin } from '@ecopages/core/plugins/integration-plugin';
18
7
  import { ClientGraphBoundaryCache } from './client-graph-boundary-cache.js';