@ecopages/react 0.2.0-rc.1 → 0.2.0-rc.11

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 (86) hide show
  1. package/README.md +68 -14
  2. package/package.json +17 -5
  3. package/src/app/client-only.js +3 -8
  4. package/src/bundling/bundle.js +4 -1
  5. package/src/bundling/runtime-alias-map.d.ts +0 -1
  6. package/src/bundling/runtime-alias-map.js +1 -4
  7. package/src/bundling/runtime-bundle.d.ts +9 -2
  8. package/src/bundling/runtime-bundle.js +27 -16
  9. package/src/bundling/runtime-modules.d.ts +21 -0
  10. package/src/bundling/runtime-modules.js +25 -0
  11. package/src/client-graph/ast-transform-eco-page.d.ts +10 -0
  12. package/src/client-graph/ast-transform-eco-page.js +71 -0
  13. package/src/client-graph/ast-transform-import-edits.d.ts +13 -0
  14. package/src/client-graph/ast-transform-import-edits.js +75 -0
  15. package/src/client-graph/ast-transform-walk-imports.d.ts +19 -0
  16. package/src/client-graph/ast-transform-walk-imports.js +103 -0
  17. package/src/client-graph/ast-transform.d.ts +1 -7
  18. package/src/client-graph/ast-transform.js +42 -378
  19. package/src/client-graph/ast-walk.d.ts +10 -0
  20. package/src/client-graph/ast-walk.js +26 -0
  21. package/src/client-graph/boundary-cache.js +1 -1
  22. package/src/client-graph/boundary-plugin-read-file-inline.d.ts +8 -0
  23. package/src/client-graph/boundary-plugin-read-file-inline.js +45 -0
  24. package/src/client-graph/boundary-plugin-transform.d.ts +18 -0
  25. package/src/client-graph/boundary-plugin-transform.js +61 -0
  26. package/src/client-graph/boundary-plugin.d.ts +0 -20
  27. package/src/client-graph/boundary-plugin.js +16 -98
  28. package/src/client-graph/reachability-analyzer.d.ts +4 -36
  29. package/src/client-graph/reachability-analyzer.js +23 -483
  30. package/src/client-graph/reachability-client-roots.d.ts +8 -0
  31. package/src/client-graph/reachability-client-roots.js +59 -0
  32. package/src/client-graph/reachability-export-names.d.ts +5 -0
  33. package/src/client-graph/reachability-export-names.js +34 -0
  34. package/src/client-graph/reachability-fallback-roots.d.ts +3 -0
  35. package/src/client-graph/reachability-fallback-roots.js +71 -0
  36. package/src/client-graph/reachability-preload.d.ts +10 -0
  37. package/src/client-graph/reachability-preload.js +30 -0
  38. package/src/client-graph/reachability-top-level-index.d.ts +14 -0
  39. package/src/client-graph/reachability-top-level-index.js +93 -0
  40. package/src/client-graph/reachability-traverse.d.ts +7 -0
  41. package/src/client-graph/reachability-traverse.js +201 -0
  42. package/src/client-graph/specifier-classification.d.ts +3 -2
  43. package/src/client-graph/specifier-classification.js +6 -2
  44. package/src/content/collection-component-cache.d.ts +25 -0
  45. package/src/content/collection-component-cache.js +62 -0
  46. package/src/hmr/hmr-strategy-process.d.ts +34 -0
  47. package/src/hmr/hmr-strategy-process.js +83 -0
  48. package/src/hmr/hmr-strategy.d.ts +3 -0
  49. package/src/hmr/hmr-strategy.js +47 -86
  50. package/src/hmr/react-hmr-dev-transform-plugins.d.ts +1 -0
  51. package/src/hmr/react-hmr-dev-transform-plugins.js +4 -1
  52. package/src/hydration/browser/island-hydration.d.ts +82 -0
  53. package/src/hydration/browser/island-hydration.js +247 -0
  54. package/src/hydration/browser/page-hydration-hmr.d.ts +11 -0
  55. package/src/hydration/browser/page-hydration-hmr.js +56 -0
  56. package/src/hydration/browser/page-hydration-module.d.ts +8 -0
  57. package/src/hydration/browser/page-hydration-module.js +17 -0
  58. package/src/hydration/browser/page-hydration.d.ts +54 -0
  59. package/src/hydration/browser/page-hydration.js +177 -0
  60. package/src/hydration/compiled-page-hydration-options.d.ts +20 -0
  61. package/src/hydration/hydration-asset.d.ts +21 -1
  62. package/src/hydration/hydration-asset.js +54 -24
  63. package/src/hydration/hydration-lifecycle.test.browser.d.ts +1 -0
  64. package/src/hydration/hydration-lifecycle.test.browser.js +409 -0
  65. package/src/hydration/hydration-page-entry-source.d.ts +5 -0
  66. package/src/hydration/hydration-page-entry-source.js +87 -0
  67. package/src/hydration/hydration-script-compiler.d.ts +48 -0
  68. package/src/hydration/hydration-script-compiler.js +213 -0
  69. package/src/hydration/page-data-reader.d.ts +0 -14
  70. package/src/hydration/page-data-reader.js +1 -47
  71. package/src/mdx/mdx-loader-plugin.d.ts +5 -1
  72. package/src/mdx/mdx-loader-plugin.js +4 -2
  73. package/src/plugin/react.plugin.js +8 -1
  74. package/src/plugin/react.types.d.ts +11 -2
  75. package/src/render/component-ssr.d.ts +17 -0
  76. package/src/render/component-ssr.js +19 -1
  77. package/src/render/react-renderer.d.ts +2 -1
  78. package/src/render/react-renderer.js +5 -1
  79. package/src/render/unified-layout-composition.d.ts +2 -0
  80. package/src/render/unified-layout-composition.js +5 -1
  81. package/src/hmr/hmr-scripts.d.ts +0 -18
  82. package/src/hmr/hmr-scripts.js +0 -44
  83. package/src/hydration/hydration-scripts.d.ts +0 -104
  84. package/src/hydration/hydration-scripts.js +0 -483
  85. package/src/hydration/hydration-scripts.test.browser.js +0 -199
  86. /package/src/hydration/{hydration-scripts.test.browser.d.ts → compiled-page-hydration-options.js} +0 -0
@@ -0,0 +1,103 @@
1
+ /**
2
+ * AST walk that prunes forbidden imports and rewrites named import surfaces.
3
+ */
4
+ import { isServerOnlySpecifier, toModuleBaseSpecifier } from './specifier-classification.js';
5
+ import { walkAstNodes } from './ast-walk.js';
6
+ import { buildImportDeclarationRewrite } from './ast-transform-import-edits.js';
7
+ function processSpecifier(specifier, allowedMap) {
8
+ const explicitRules = allowedMap.get(toModuleBaseSpecifier(specifier));
9
+ if (isServerOnlySpecifier(specifier) && !explicitRules) {
10
+ return { allowed: false };
11
+ }
12
+ return { allowed: true, rules: explicitRules ?? '*' };
13
+ }
14
+ function pruneForbiddenSpecifier(node, ctx, specifier, message, replacement) {
15
+ const reachableRules = ctx.reachability.reachableImports.get(specifier);
16
+ if (reachableRules && !ctx.reachability.isFallbackRoots) {
17
+ throw new Error(`[Ecopages Client Reachability] ${message} at ${ctx.filename}:${node.start}. This import is explicitly reachable from the React render function.`);
18
+ }
19
+ ctx.edits.push({ start: node.start, end: node.end, replacement });
20
+ }
21
+ function handleImportDeclaration(node, ctx) {
22
+ const specifier = node.source.value;
23
+ const reachableRules = ctx.reachability.reachableImports.get(specifier);
24
+ const { allowed, rules } = processSpecifier(specifier, ctx.allowedMap);
25
+ if (!allowed) {
26
+ pruneForbiddenSpecifier(node, ctx, specifier, `Forbidden client import '${specifier}'`, '');
27
+ return true;
28
+ }
29
+ if (rules instanceof Set && node.specifiers && node.specifiers.length > 0) {
30
+ const rewrite = buildImportDeclarationRewrite(node, specifier, rules);
31
+ if (rewrite) {
32
+ ctx.edits.push(rewrite);
33
+ }
34
+ return true;
35
+ }
36
+ if (!reachableRules && (!node.specifiers || node.specifiers.length === 0)) {
37
+ ctx.edits.push({ start: node.start, end: node.end, replacement: '' });
38
+ }
39
+ return true;
40
+ }
41
+ function handleExportNamedDeclaration(node, ctx) {
42
+ const specifier = node.source.value;
43
+ const { allowed } = processSpecifier(specifier, ctx.allowedMap);
44
+ if (!allowed) {
45
+ pruneForbiddenSpecifier(node, ctx, specifier, `Forbidden client export from '${specifier}'`, '');
46
+ }
47
+ return true;
48
+ }
49
+ function handleExportAllDeclaration(node, ctx) {
50
+ const specifier = node.source.value;
51
+ const { allowed } = processSpecifier(specifier, ctx.allowedMap);
52
+ if (!allowed) {
53
+ pruneForbiddenSpecifier(node, ctx, specifier, `Forbidden client export * from '${specifier}'`, '');
54
+ }
55
+ return true;
56
+ }
57
+ function handleImportExpression(node, ctx) {
58
+ if (!node.source?.value)
59
+ return false;
60
+ const specifier = node.source.value;
61
+ const { allowed } = processSpecifier(specifier, ctx.allowedMap);
62
+ if (!allowed) {
63
+ pruneForbiddenSpecifier(node, ctx, specifier, `Forbidden dynamic import('${specifier}')`, 'Promise.resolve({})');
64
+ }
65
+ return true;
66
+ }
67
+ function handleRequireCall(node, ctx) {
68
+ const arg = node.arguments?.[0];
69
+ if (!arg || (arg.type !== 'StringLiteral' && arg.type !== 'Literal') || typeof arg.value !== 'string') {
70
+ return;
71
+ }
72
+ const specifier = arg.value;
73
+ const { allowed } = processSpecifier(specifier, ctx.allowedMap);
74
+ if (!allowed) {
75
+ pruneForbiddenSpecifier(node, ctx, specifier, `Forbidden require('${specifier}')`, '({})');
76
+ }
77
+ }
78
+ /**
79
+ * Walks a parsed program and collects import-pruning edits.
80
+ */
81
+ export function collectImportTransformEdits(program, ctx) {
82
+ const edits = [];
83
+ const fullCtx = { ...ctx, edits };
84
+ walkAstNodes(program, (node) => {
85
+ if (node.type === 'ImportDeclaration') {
86
+ return handleImportDeclaration(node, fullCtx);
87
+ }
88
+ if (node.type === 'ExportNamedDeclaration' && node.source) {
89
+ return handleExportNamedDeclaration(node, fullCtx);
90
+ }
91
+ if (node.type === 'ExportAllDeclaration' && node.source) {
92
+ return handleExportAllDeclaration(node, fullCtx);
93
+ }
94
+ if (node.type === 'ImportExpression') {
95
+ return handleImportExpression(node, fullCtx);
96
+ }
97
+ if (node.type === 'CallExpression' && node.callee?.type === 'Identifier' && node.callee.name === 'require') {
98
+ handleRequireCall(node, fullCtx);
99
+ }
100
+ return false;
101
+ });
102
+ return edits;
103
+ }
@@ -5,14 +5,8 @@ import type { RequestedExportRules } from './boundary-cache.js';
5
5
  /**
6
6
  * Parses a module using Oxc AST and surgically removes forbidden imports.
7
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
8
  */
15
- export declare function transformModuleImports(source: string, filename: string, globallyAllowed: Map<string, Set<string> | '*'>, requestedExports: Map<string, RequestedExportRules>, projectRoot?: string, stripServerOnlyPageOptions?: boolean): {
9
+ export declare function transformModuleImports(source: string, filename: string, globallyAllowed: Map<string, Set<string> | '*'>, requestedExports: Map<string, RequestedExportRules>, stripServerOnlyPageOptions?: boolean): {
16
10
  transformed: string;
17
11
  modified: boolean;
18
12
  };
@@ -3,122 +3,51 @@
3
3
  */
4
4
  import { parseModuleSource } from '@ecopages/core/cache';
5
5
  import { analyzeReachability } from './reachability-analyzer.js';
6
- import { isBareSpecifier, isProjectAliasSpecifier, isServerOnlySpecifier, toModuleBaseSpecifier, parseDeclaredModules, mergeDeclaredModulesMap, normalizeRequestedExportsKey, resolveRequestedExportsKey, mergeRequestedExportRules, } from './specifier-classification.js';
7
- const SERVER_ONLY_ECO_PAGE_OPTION_KEYS = new Set([
8
- 'cache',
9
- 'middleware',
10
- 'requires',
11
- 'metadata',
12
- 'staticProps',
13
- 'staticPaths',
14
- ]);
15
- /**
16
- * Extracts a static property key name from an object literal property node.
17
- *
18
- * The client graph boundary rewrite only strips known `eco.page(...)` keys when
19
- * it can prove the property name statically. Computed or otherwise dynamic keys
20
- * are ignored so the transform remains conservative.
21
- *
22
- * @param node - OXC AST node representing an object property key.
23
- * @returns Static property key name when it can be resolved, otherwise `undefined`.
24
- */
25
- function getObjectPropertyKeyName(node) {
26
- if (!node)
27
- return undefined;
28
- if (node.type === 'Identifier')
29
- return node.name;
30
- if (node.type === 'StringLiteral' || node.type === 'Literal') {
31
- return typeof node.value === 'string' ? node.value : undefined;
32
- }
33
- return undefined;
34
- }
35
- /**
36
- * Removes server-only `eco.page(...)` options from browser-bound modules.
37
- *
38
- * Import pruning alone is not sufficient because a page module can still retain
39
- * references to stripped server imports through config fields like `middleware`
40
- * or `metadata`. This pass rewrites the `eco.page(...)` object literal so only
41
- * browser-relevant properties remain.
42
- *
43
- * @param source - Original or already-transformed module source.
44
- * @param program - Parsed OXC program for the same source text.
45
- * @returns Updated source plus a flag indicating whether any rewrite occurred.
46
- */
47
- function stripServerOnlyEcoPageOptions(source, program) {
48
- const edits = [];
49
- function walk(node) {
50
- if (!node || typeof node !== 'object')
51
- return;
52
- if (Array.isArray(node)) {
53
- for (const child of node)
54
- walk(child);
55
- return;
56
- }
57
- if (node.type === 'CallExpression' &&
58
- node.callee?.type === 'MemberExpression' &&
59
- node.callee.object?.type === 'Identifier' &&
60
- node.callee.object.name === 'eco' &&
61
- node.callee.property?.type === 'Identifier' &&
62
- node.callee.property.name === 'page' &&
63
- node.arguments?.[0]?.type === 'ObjectExpression') {
64
- const objectExpression = node.arguments[0];
65
- const keptProperties = [];
66
- let removedProperty = false;
67
- for (const property of objectExpression.properties ?? []) {
68
- if (property?.type === 'Property') {
69
- const keyName = getObjectPropertyKeyName(property.key);
70
- if (keyName && SERVER_ONLY_ECO_PAGE_OPTION_KEYS.has(keyName)) {
71
- removedProperty = true;
72
- continue;
73
- }
6
+ import { stripServerOnlyEcoPageOptions } from './ast-transform-eco-page.js';
7
+ import { collectImportTransformEdits } from './ast-transform-walk-imports.js';
8
+ import { walkAstNodes } from './ast-walk.js';
9
+ import { applySourceEdits } from './ast-transform-import-edits.js';
10
+ import { mergeDeclaredModulesMap, mergeRequestedExportRules, normalizeRequestedExportsKey, parseDeclaredModules, resolveRequestedExportsKey, } from './specifier-classification.js';
11
+ function collectLocalDeclaredModules(program) {
12
+ const localDeclared = [];
13
+ walkAstNodes(program, (node) => {
14
+ if (node.type === 'Property' && node.key?.name === 'modules' && node.value?.type === 'ArrayExpression') {
15
+ for (const el of node.value.elements) {
16
+ if ((el.type === 'StringLiteral' || el.type === 'Literal') && typeof el.value === 'string') {
17
+ localDeclared.push(el.value);
74
18
  }
75
- keptProperties.push(source.slice(property.start, property.end));
76
- }
77
- if (removedProperty) {
78
- const replacement = keptProperties.length > 0 ? `{ ${keptProperties.join(', ')} }` : '{}';
79
- edits.push({
80
- start: objectExpression.start,
81
- end: objectExpression.end,
82
- replacement,
83
- });
84
- }
85
- }
86
- for (const key in node) {
87
- if (key !== 'type' && key !== 'start' && key !== 'end') {
88
- walk(node[key]);
89
19
  }
90
20
  }
21
+ });
22
+ return localDeclared;
23
+ }
24
+ function getImportedOrReexportedSource(statement) {
25
+ if (statement.type === 'ImportDeclaration') {
26
+ return statement.source.value;
91
27
  }
92
- walk(program);
93
- if (edits.length === 0) {
94
- return { transformed: source, modified: false };
28
+ if ((statement.type === 'ExportNamedDeclaration' || statement.type === 'ExportAllDeclaration') &&
29
+ statement.source) {
30
+ return statement.source.value;
95
31
  }
96
- edits.sort((a, b) => b.start - a.start);
97
- let transformed = source;
98
- for (const edit of edits) {
99
- transformed = transformed.slice(0, edit.start) + edit.replacement + transformed.slice(edit.end);
32
+ return undefined;
33
+ }
34
+ function propagateRequestedExportRules(program, filename, reachability, requestedExports) {
35
+ for (const statement of program.body) {
36
+ const source = getImportedOrReexportedSource(statement);
37
+ if (!source)
38
+ continue;
39
+ const reachableRules = reachability.reachableImports.get(source);
40
+ const requestedModuleKey = resolveRequestedExportsKey(filename, source);
41
+ if (!requestedModuleKey || !reachableRules)
42
+ continue;
43
+ mergeRequestedExportRules(requestedExports, requestedModuleKey, reachableRules);
100
44
  }
101
- return { transformed, modified: true };
102
45
  }
103
46
  /**
104
47
  * Parses a module using Oxc AST and surgically removes forbidden imports.
105
48
  * Filters down to the exact specifiers requested via `{namedImport}` syntax.
106
- *
107
- * @param source - The raw string source content of the module.
108
- * @param filename - The absolute path of the module.
109
- * @param globallyAllowed - A map of modules declared globally allowable by the build configuration.
110
- * @param requestedExports - Local requested-export registry used to propagate named reachability across files.
111
- * @returns An object containing the transformed string and a boolean indicating if changes occurred.
112
49
  */
113
- export function transformModuleImports(source, filename, globallyAllowed, requestedExports, projectRoot, stripServerOnlyPageOptions = false) {
114
- /**
115
- * Parse the source
116
- *
117
- * We parse once here and then reuse the resulting `program` AST for both
118
- * the local `modules` declaration walk (step 2) and the reachability analysis
119
- * (step 3). Passing it through avoids a redundant second `parseSync` call inside
120
- * `analyzeReachability`, cutting the per-file OxC work roughly in half.
121
- */
50
+ export function transformModuleImports(source, filename, globallyAllowed, requestedExports, stripServerOnlyPageOptions = false) {
122
51
  let result;
123
52
  try {
124
53
  result = parseModuleSource(filename, source);
@@ -127,288 +56,23 @@ export function transformModuleImports(source, filename, globallyAllowed, reques
127
56
  return { transformed: source, modified: false };
128
57
  }
129
58
  const { program } = result;
130
- const localDeclared = [];
131
- /**
132
- * Collect locally declared modules
133
- *
134
- * Walk the AST looking for `modules: [...]` array properties. These are the
135
- * component-level allowlist declarations that a developer writes inside
136
- * `eco.page({ modules: ['react', '@myorg/ui{Button}'] })` to explicitly opt
137
- * specific packages into the client bundle.
138
- *
139
- * The collected specifiers are merged with the globally configured allowlist
140
- * before any import filtering takes place.
141
- */
142
- function walk(node) {
143
- if (!node || typeof node !== 'object')
144
- return;
145
- if (Array.isArray(node)) {
146
- for (const child of node)
147
- walk(child);
148
- return;
149
- }
150
- if (node.type === 'Property' && node.key?.name === 'modules' && node.value?.type === 'ArrayExpression') {
151
- for (const el of node.value.elements) {
152
- if ((el.type === 'StringLiteral' || el.type === 'Literal') && typeof el.value === 'string') {
153
- localDeclared.push(el.value);
154
- }
155
- }
156
- }
157
- for (const key in node) {
158
- if (key !== 'type' && key !== 'start' && key !== 'end') {
159
- walk(node[key]);
160
- }
161
- }
162
- }
163
- walk(program);
164
- /**
165
- * Merge allowlists and compute reachability
166
- *
167
- * Combine the globally declared modules (from plugin config) with the locally
168
- * declared ones (from the component's `modules` array) into a single authoritative map.
169
- *
170
- * Then run the reachability analysis, passing the already-parsed `program` so the
171
- * analyser skips its own internal `parseSync` call (see step 1 above).
172
- */
59
+ const localDeclared = collectLocalDeclaredModules(program);
173
60
  const locallyAllowed = parseDeclaredModules(localDeclared);
174
61
  const allowedMap = mergeDeclaredModulesMap(globallyAllowed, locallyAllowed);
175
62
  const explicitRequestedExports = requestedExports.get(normalizeRequestedExportsKey(filename));
176
63
  const reachability = analyzeReachability(source, filename, program, explicitRequestedExports);
177
- for (const statement of program.body) {
178
- if (statement.type === 'ImportDeclaration') {
179
- const reachableRules = reachability.reachableImports.get(statement.source.value);
180
- const requestedModuleKey = resolveRequestedExportsKey(filename, statement.source.value);
181
- if (!requestedModuleKey || !reachableRules)
182
- continue;
183
- mergeRequestedExportRules(requestedExports, requestedModuleKey, reachableRules);
184
- continue;
185
- }
186
- if (statement.type === 'ExportNamedDeclaration' && statement.source) {
187
- const reachableRules = reachability.reachableImports.get(statement.source.value);
188
- const requestedModuleKey = resolveRequestedExportsKey(filename, statement.source.value);
189
- if (!requestedModuleKey || !reachableRules)
190
- continue;
191
- mergeRequestedExportRules(requestedExports, requestedModuleKey, reachableRules);
192
- continue;
193
- }
194
- if (statement.type === 'ExportAllDeclaration' && statement.source) {
195
- const reachableRules = reachability.reachableImports.get(statement.source.value);
196
- const requestedModuleKey = resolveRequestedExportsKey(filename, statement.source.value);
197
- if (!requestedModuleKey || !reachableRules)
198
- continue;
199
- mergeRequestedExportRules(requestedExports, requestedModuleKey, reachableRules);
200
- }
201
- }
202
- /**
203
- * Build the edit list
204
- *
205
- * Walk the AST a second time, this time inspecting every import/export/dynamic-import
206
- * node against the combined allowlist and the reachability graph:
207
- *
208
- * - **Forbidden + unreachable** → replace with empty string (pruned).
209
- * - **Forbidden + reachable from a known client root** → throw a build error so the
210
- * developer is forced to resolve the server-client boundary violation explicitly.
211
- * - **Allowed with specific named rules** → surgically rewrite the import to keep only
212
- * the permitted named bindings; the bundler tree-shakes the rest.
213
- * - **Allowed with no restrictions** → left untouched; the bundler handles tree-shaking.
214
- */
215
- const edits = [];
216
- function processSpecifier(specifier) {
217
- const moduleBase = toModuleBaseSpecifier(specifier);
218
- const explicitRules = allowedMap.get(moduleBase);
219
- if (isServerOnlySpecifier(specifier)) {
220
- if (explicitRules) {
221
- return { allowed: true, rules: explicitRules };
222
- }
223
- return { allowed: false };
224
- }
225
- if (isProjectAliasSpecifier(specifier, projectRoot))
226
- return { allowed: true, rules: explicitRules ?? '*' };
227
- if (!isBareSpecifier(specifier))
228
- return { allowed: true, rules: explicitRules ?? '*' };
229
- /** By default, bare specifiers (NPM modules) are allowed entirely. */
230
- return { allowed: true, rules: explicitRules ?? '*' };
231
- }
232
- function walkImports(node) {
233
- if (!node || typeof node !== 'object')
234
- return;
235
- if (Array.isArray(node)) {
236
- for (const child of node)
237
- walkImports(child);
238
- return;
239
- }
240
- if (node.type === 'ImportDeclaration') {
241
- const specifier = node.source.value;
242
- const reachableRules = reachability.reachableImports.get(specifier);
243
- const { allowed, rules } = processSpecifier(specifier);
244
- if (!allowed) {
245
- if (reachableRules && !reachability.isFallbackRoots) {
246
- throw new Error(`[Ecopages Client Reachability] Forbidden client import '${specifier}' at ${filename}:${node.start}. This import is explicitly reachable from the React render function.`);
247
- }
248
- else {
249
- edits.push({ start: node.start, end: node.end, replacement: '' });
250
- }
251
- return;
252
- }
253
- /**
254
- * If it IS allowed by the base specifier, we must check if there are specific named rules.
255
- * If there are specific rules (a Set), we must surgically remove any specifiers that aren't in the rule.
256
- */
257
- if (rules instanceof Set && node.specifiers && node.specifiers.length > 0) {
258
- let keptSpecifierCount = 0;
259
- let defaultImportLocal;
260
- let namespaceImportLocal;
261
- const namedImportNodes = [];
262
- for (const spec of node.specifiers) {
263
- if (spec.type === 'ImportSpecifier') {
264
- const importedName = spec.imported.type === 'Identifier' ? spec.imported.name : spec.imported.value;
265
- if (rules.has(importedName)) {
266
- keptSpecifierCount += 1;
267
- const localName = spec.local?.name;
268
- if (localName && localName !== importedName) {
269
- namedImportNodes.push(`${importedName} as ${localName}`);
270
- }
271
- else {
272
- namedImportNodes.push(importedName);
273
- }
274
- }
275
- }
276
- else if (spec.type === 'ImportDefaultSpecifier') {
277
- if (rules.has('default')) {
278
- keptSpecifierCount += 1;
279
- defaultImportLocal = spec.local.name;
280
- }
281
- }
282
- else if (spec.type === 'ImportNamespaceSpecifier') {
283
- if (rules.has('*')) {
284
- keptSpecifierCount += 1;
285
- namespaceImportLocal = spec.local.name;
286
- }
287
- }
288
- }
289
- if (keptSpecifierCount === 0) {
290
- edits.push({ start: node.start, end: node.end, replacement: '' });
291
- }
292
- else if (keptSpecifierCount < node.specifiers.length) {
293
- let newDeclaration = '';
294
- if (defaultImportLocal && namespaceImportLocal) {
295
- newDeclaration = `import ${defaultImportLocal}, * as ${namespaceImportLocal} from '${specifier}';`;
296
- }
297
- else if (namespaceImportLocal) {
298
- newDeclaration = `import * as ${namespaceImportLocal} from '${specifier}';`;
299
- }
300
- else if (defaultImportLocal && namedImportNodes.length > 0) {
301
- newDeclaration = `import ${defaultImportLocal}, { ${namedImportNodes.join(', ')} } from '${specifier}';`;
302
- }
303
- else if (defaultImportLocal) {
304
- newDeclaration = `import ${defaultImportLocal} from '${specifier}';`;
305
- }
306
- else {
307
- newDeclaration = `import { ${namedImportNodes.join(', ')} } from '${specifier}';`;
308
- }
309
- edits.push({ start: node.start, end: node.end, replacement: newDeclaration });
310
- }
311
- return;
312
- }
313
- /**
314
- * If it IS allowed (globally or all specifiers match) and IS reachable,
315
- * we can safely just leave the import alone.
316
- * ESBuild will natively treeshake any bindings that are actually unused.
317
- *
318
- * However, if it is completely unreachable, and it's a side-effect import
319
- * (no specifiers), we want to proactively prune it.
320
- */
321
- if (!reachableRules && (!node.specifiers || node.specifiers.length === 0)) {
322
- edits.push({ start: node.start, end: node.end, replacement: '' });
323
- }
324
- return;
325
- }
326
- if (node.type === 'ExportNamedDeclaration' && node.source) {
327
- const specifier = node.source.value;
328
- const { allowed } = processSpecifier(specifier);
329
- if (!allowed) {
330
- const reachableRules = reachability.reachableImports.get(specifier);
331
- if (reachableRules && !reachability.isFallbackRoots) {
332
- throw new Error(`[Ecopages Client Reachability] Forbidden client export from '${specifier}' at ${filename}:${node.start}. This export is explicitly reachable from the React render function.`);
333
- }
334
- else {
335
- edits.push({ start: node.start, end: node.end, replacement: '' });
336
- }
337
- }
338
- return;
339
- }
340
- if (node.type === 'ExportAllDeclaration' && node.source) {
341
- const specifier = node.source.value;
342
- const { allowed } = processSpecifier(specifier);
343
- if (!allowed) {
344
- const reachableRules = reachability.reachableImports.get(specifier);
345
- if (reachableRules && !reachability.isFallbackRoots) {
346
- throw new Error(`[Ecopages Client Reachability] Forbidden client export * from '${specifier}' at ${filename}:${node.start}. This export is explicitly reachable from the React render function.`);
347
- }
348
- else {
349
- edits.push({ start: node.start, end: node.end, replacement: '' });
350
- }
351
- }
352
- return;
353
- }
354
- if (node.type === 'ImportExpression' && node.source?.value) {
355
- const specifier = node.source.value;
356
- const { allowed } = processSpecifier(specifier);
357
- const reachableRules = reachability.reachableImports.get(specifier);
358
- if (!reachableRules) {
359
- if (!allowed) {
360
- edits.push({ start: node.start, end: node.end, replacement: 'Promise.resolve({})' });
361
- }
362
- return;
363
- }
364
- if (!allowed) {
365
- if (!reachability.isFallbackRoots) {
366
- throw new Error(`[Ecopages Client Reachability] Forbidden dynamic import('${specifier}') at ${filename}:${node.start}. This import is explicitly reachable from the React render function.`);
367
- }
368
- else {
369
- edits.push({ start: node.start, end: node.end, replacement: 'Promise.resolve({})' });
370
- }
371
- }
372
- return;
373
- }
374
- if (node.type === 'CallExpression' && node.callee?.type === 'Identifier' && node.callee.name === 'require') {
375
- const arg = node.arguments?.[0];
376
- if (arg && (arg.type === 'StringLiteral' || arg.type === 'Literal') && typeof arg.value === 'string') {
377
- const specifier = arg.value;
378
- const { allowed } = processSpecifier(specifier);
379
- const reachableRules = reachability.reachableImports.get(specifier);
380
- if (!reachableRules) {
381
- if (!allowed) {
382
- edits.push({ start: node.start, end: node.end, replacement: '({})' });
383
- }
384
- }
385
- else if (!allowed) {
386
- if (!reachability.isFallbackRoots) {
387
- throw new Error(`[Ecopages Client Reachability] Forbidden require('${specifier}') at ${filename}:${node.start}. This import is explicitly reachable from the React render function.`);
388
- }
389
- else {
390
- edits.push({ start: node.start, end: node.end, replacement: '({})' });
391
- }
392
- }
393
- }
394
- }
395
- for (const key in node) {
396
- if (key !== 'type' && key !== 'start' && key !== 'end') {
397
- walkImports(node[key]);
398
- }
399
- }
400
- }
401
- walkImports(program);
64
+ propagateRequestedExportRules(program, filename, reachability, requestedExports);
65
+ const edits = collectImportTransformEdits(program, {
66
+ filename,
67
+ reachability,
68
+ allowedMap,
69
+ });
402
70
  if (edits.length === 0) {
403
71
  return stripServerOnlyPageOptions
404
72
  ? stripServerOnlyEcoPageOptions(source, program)
405
73
  : { transformed: source, modified: false };
406
74
  }
407
- edits.sort((a, b) => b.start - a.start);
408
- let transformed = source;
409
- for (const edit of edits) {
410
- transformed = transformed.slice(0, edit.start) + edit.replacement + transformed.slice(edit.end);
411
- }
75
+ const transformed = applySourceEdits(source, edits);
412
76
  let reparsedResult;
413
77
  try {
414
78
  reparsedResult = parseModuleSource(filename, transformed);
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Generic depth-first walk over Oxc AST object graphs.
3
+ */
4
+ /**
5
+ * Visits every object node in an Oxc AST tree.
6
+ *
7
+ * @param node - Root AST node or subtree.
8
+ * @param onNode - Called for each object node; return `true` to skip descending into its children.
9
+ */
10
+ export declare function walkAstNodes(node: unknown, onNode: (node: any) => boolean | void): void;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Generic depth-first walk over Oxc AST object graphs.
3
+ */
4
+ /**
5
+ * Visits every object node in an Oxc AST tree.
6
+ *
7
+ * @param node - Root AST node or subtree.
8
+ * @param onNode - Called for each object node; return `true` to skip descending into its children.
9
+ */
10
+ export function walkAstNodes(node, onNode) {
11
+ if (!node || typeof node !== 'object')
12
+ return;
13
+ if (Array.isArray(node)) {
14
+ for (const child of node)
15
+ walkAstNodes(child, onNode);
16
+ return;
17
+ }
18
+ const skipChildren = onNode(node) === true;
19
+ if (skipChildren)
20
+ return;
21
+ for (const key in node) {
22
+ if (key !== 'type' && key !== 'start' && key !== 'end') {
23
+ walkAstNodes(node[key], onNode);
24
+ }
25
+ }
26
+ }
@@ -34,7 +34,7 @@
34
34
  * during the transform is correctly grown on replay against a fresh
35
35
  * registry as well.
36
36
  */
37
- import { rapidhash } from '@ecopages/core/utils/hash';
37
+ import { rapidhash } from '@ecopages/core/hash';
38
38
  const DEFAULT_MAX_ENTRIES = 5_000;
39
39
  /**
40
40
  * Clone a single rule value: `'*'` is a singleton, Sets get a shallow copy.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Inlines `fs.readFileSync(path.resolve(...), 'utf8')` calls when the target file exists.
3
+ */
4
+ export declare function inlineReadFileSyncCalls(source: string, sourcePath: string, absWorkingDir: string): {
5
+ transformed: string;
6
+ modified: boolean;
7
+ inlinedExternalFile: boolean;
8
+ };