@ecopages/react 0.2.1-beta.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/README.md +164 -28
  2. package/package.json +67 -30
  3. package/src/app/client-only.js +10 -0
  4. package/src/app/dynamic.js +9 -0
  5. package/src/app/dynamic.test.browser.js +38 -0
  6. package/src/bundling/bootstrap-package-paths.d.ts +5 -0
  7. package/src/bundling/bootstrap-package-paths.js +5 -0
  8. package/src/{services/react-bundle.service.d.ts → bundling/bundle.d.ts} +29 -11
  9. package/src/bundling/bundle.js +130 -0
  10. package/src/bundling/runtime-alias-map.d.ts +12 -0
  11. package/src/bundling/runtime-alias-map.js +110 -0
  12. package/src/bundling/runtime-bundle.d.ts +70 -0
  13. package/src/bundling/runtime-bundle.js +233 -0
  14. package/src/{utils/react-dom-runtime-interop-plugin.d.ts → bundling/runtime-interop-plugin.d.ts} +1 -1
  15. package/src/bundling/runtime-interop-plugin.js +32 -0
  16. package/src/bundling/runtime-mode.d.ts +1 -0
  17. package/src/bundling/runtime-mode.js +10 -0
  18. package/src/bundling/runtime-modules.d.ts +21 -0
  19. package/src/bundling/runtime-modules.js +28 -0
  20. package/src/client-graph/ast-transform-eco-page.d.ts +10 -0
  21. package/src/client-graph/ast-transform-eco-page.js +71 -0
  22. package/src/client-graph/ast-transform-import-edits.d.ts +13 -0
  23. package/src/client-graph/ast-transform-import-edits.js +75 -0
  24. package/src/client-graph/ast-transform-walk-imports.d.ts +20 -0
  25. package/src/client-graph/ast-transform-walk-imports.js +105 -0
  26. package/src/client-graph/ast-transform.d.ts +12 -0
  27. package/src/client-graph/ast-transform.js +91 -0
  28. package/src/client-graph/ast-walk.d.ts +10 -0
  29. package/src/client-graph/ast-walk.js +26 -0
  30. package/src/client-graph/boundary-plugin-read-file-inline.d.ts +7 -0
  31. package/src/client-graph/boundary-plugin-read-file-inline.js +43 -0
  32. package/src/client-graph/boundary-plugin-transform.d.ts +16 -0
  33. package/src/client-graph/boundary-plugin-transform.js +31 -0
  34. package/src/client-graph/boundary-plugin.d.ts +23 -0
  35. package/src/client-graph/boundary-plugin.js +177 -0
  36. package/src/client-graph/component-config-traversal.js +77 -0
  37. package/src/client-graph/declared-modules.js +69 -0
  38. package/src/client-graph/module-classification.d.ts +3 -0
  39. package/src/client-graph/module-classification.js +43 -0
  40. package/src/client-graph/reachability-analyzer.d.ts +26 -0
  41. package/src/client-graph/reachability-analyzer.js +55 -0
  42. package/src/client-graph/reachability-client-roots.d.ts +8 -0
  43. package/src/client-graph/reachability-client-roots.js +59 -0
  44. package/src/client-graph/reachability-export-names.d.ts +5 -0
  45. package/src/client-graph/reachability-export-names.js +34 -0
  46. package/src/client-graph/reachability-fallback-roots.d.ts +3 -0
  47. package/src/client-graph/reachability-fallback-roots.js +71 -0
  48. package/src/client-graph/reachability-preload.d.ts +10 -0
  49. package/src/client-graph/reachability-preload.js +30 -0
  50. package/src/client-graph/reachability-top-level-index.d.ts +14 -0
  51. package/src/client-graph/reachability-top-level-index.js +93 -0
  52. package/src/client-graph/reachability-traverse.d.ts +7 -0
  53. package/src/client-graph/reachability-traverse.js +201 -0
  54. package/src/client-graph/specifier-classification.d.ts +74 -0
  55. package/src/client-graph/specifier-classification.js +191 -0
  56. package/src/content/collection-component-cache.d.ts +25 -0
  57. package/src/content/collection-component-cache.js +62 -0
  58. package/src/contracts/router-adapter.js +6 -0
  59. package/src/dev-transform/react-dev-transform-contributor.d.ts +17 -0
  60. package/src/dev-transform/react-dev-transform-contributor.js +22 -0
  61. package/src/hmr/hmr-strategy-process.d.ts +34 -0
  62. package/src/hmr/hmr-strategy-process.js +83 -0
  63. package/src/hmr/hmr-strategy.d.ts +67 -0
  64. package/src/hmr/hmr-strategy.js +306 -0
  65. package/src/{services/react-hmr-page-metadata-cache.d.ts → hmr/page-metadata-cache.d.ts} +5 -1
  66. package/src/hmr/page-metadata-cache.js +44 -0
  67. package/src/hmr/react-hmr-dev-transform-plugins.d.ts +22 -0
  68. package/src/hmr/react-hmr-dev-transform-plugins.js +19 -0
  69. package/src/hydration/assert-no-bare-ecopages-imports.d.ts +4 -0
  70. package/src/hydration/assert-no-bare-ecopages-imports.js +7 -0
  71. package/src/hydration/bootstrap-imports.d.ts +25 -0
  72. package/src/hydration/bootstrap-imports.js +33 -0
  73. package/src/hydration/browser/island-hydration.d.ts +82 -0
  74. package/src/hydration/browser/island-hydration.js +251 -0
  75. package/src/hydration/browser/page-hydration-hmr.d.ts +11 -0
  76. package/src/hydration/browser/page-hydration-hmr.js +56 -0
  77. package/src/hydration/browser/page-hydration-module.d.ts +8 -0
  78. package/src/hydration/browser/page-hydration-module.js +17 -0
  79. package/src/hydration/browser/page-hydration.d.ts +54 -0
  80. package/src/hydration/browser/page-hydration.js +177 -0
  81. package/src/hydration/compiled-page-hydration-options.d.ts +20 -0
  82. package/src/hydration/hydration-asset.d.ts +127 -0
  83. package/src/hydration/hydration-asset.js +379 -0
  84. package/src/hydration/hydration-lifecycle.test.browser.d.ts +1 -0
  85. package/src/hydration/hydration-lifecycle.test.browser.js +461 -0
  86. package/src/hydration/hydration-page-entry-source.d.ts +5 -0
  87. package/src/hydration/hydration-page-entry-source.js +87 -0
  88. package/src/hydration/hydration-script-compiler.d.ts +48 -0
  89. package/src/hydration/hydration-script-compiler.js +213 -0
  90. package/src/hydration/page-data-manifest.d.ts +58 -0
  91. package/src/hydration/page-data-manifest.js +97 -0
  92. package/src/hydration/page-data-reader.d.ts +17 -0
  93. package/src/hydration/page-data-reader.js +31 -0
  94. package/src/hydration/page-payload.d.ts +68 -0
  95. package/src/hydration/page-payload.js +106 -0
  96. package/src/hydration/serialize-page-data-script.d.ts +27 -0
  97. package/src/hydration/serialize-page-data-script.js +30 -0
  98. package/src/{services/react-mdx-config-dependency.service.d.ts → mdx/mdx-config-dependency.d.ts} +9 -8
  99. package/src/mdx/mdx-config-dependency.js +107 -0
  100. package/src/mdx/mdx-loader-plugin.d.ts +8 -0
  101. package/src/mdx/mdx-loader-plugin.js +11 -0
  102. package/src/page/page-module.d.ts +46 -0
  103. package/src/page/page-module.js +74 -0
  104. package/src/plugin/react.constants.js +1 -0
  105. package/src/{react.plugin.d.ts → plugin/react.plugin.d.ts} +21 -27
  106. package/src/plugin/react.plugin.js +210 -0
  107. package/src/plugin/react.types.d.ts +106 -0
  108. package/src/plugin/react.types.js +1 -0
  109. package/src/render/component-ownership.d.ts +49 -0
  110. package/src/render/component-ownership.js +47 -0
  111. package/src/render/component-ssr.d.ts +59 -0
  112. package/src/render/component-ssr.js +76 -0
  113. package/src/render/eco-embed.js +9 -0
  114. package/src/render/errors.d.ts +16 -0
  115. package/src/render/errors.js +23 -0
  116. package/src/render/html-boundary.js +59 -0
  117. package/src/render/layout-compose.d.ts +66 -0
  118. package/src/render/layout-compose.js +134 -0
  119. package/src/render/react-renderer.d.ts +114 -0
  120. package/src/render/react-renderer.js +490 -0
  121. package/src/render/react-runtime.d.ts +13 -0
  122. package/src/render/react-runtime.js +26 -0
  123. package/src/render/unified-layout-composition.d.ts +22 -0
  124. package/src/render/unified-layout-composition.js +31 -0
  125. package/src/runtime/use-sync-external-store-with-selector.js +52 -54
  126. package/src/eco-embed.js +0 -11
  127. package/src/react-hmr-strategy.d.ts +0 -230
  128. package/src/react-hmr-strategy.js +0 -657
  129. package/src/react-renderer.d.ts +0 -233
  130. package/src/react-renderer.js +0 -622
  131. package/src/react.constants.js +0 -4
  132. package/src/react.plugin.js +0 -205
  133. package/src/react.types.d.ts +0 -88
  134. package/src/react.types.js +0 -0
  135. package/src/router-adapter.js +0 -0
  136. package/src/services/pages-index.d.ts +0 -64
  137. package/src/services/pages-index.js +0 -73
  138. package/src/services/react-bundle.service.js +0 -86
  139. package/src/services/react-hmr-page-metadata-cache.js +0 -35
  140. package/src/services/react-hydration-asset.service.d.ts +0 -84
  141. package/src/services/react-hydration-asset.service.js +0 -217
  142. package/src/services/react-mdx-config-dependency.service.js +0 -122
  143. package/src/services/react-page-module.service.d.ts +0 -88
  144. package/src/services/react-page-module.service.js +0 -166
  145. package/src/services/react-page-payload.service.d.ts +0 -46
  146. package/src/services/react-page-payload.service.js +0 -67
  147. package/src/services/react-runtime-bundle.service.d.ts +0 -45
  148. package/src/services/react-runtime-bundle.service.js +0 -186
  149. package/src/utils/client-graph-boundary-cache.d.ts +0 -108
  150. package/src/utils/client-graph-boundary-cache.js +0 -116
  151. package/src/utils/client-graph-boundary-plugin.d.ts +0 -51
  152. package/src/utils/client-graph-boundary-plugin.js +0 -552
  153. package/src/utils/client-only.js +0 -19
  154. package/src/utils/component-config-traversal.js +0 -54
  155. package/src/utils/declared-modules.js +0 -47
  156. package/src/utils/dynamic.js +0 -12
  157. package/src/utils/dynamic.test.browser.js +0 -33
  158. package/src/utils/hmr-scripts.d.ts +0 -18
  159. package/src/utils/hmr-scripts.js +0 -31
  160. package/src/utils/html-boundary.js +0 -55
  161. package/src/utils/hydration-scripts.d.ts +0 -92
  162. package/src/utils/hydration-scripts.js +0 -358
  163. package/src/utils/hydration-scripts.test.browser.js +0 -198
  164. package/src/utils/reachability-analyzer.d.ts +0 -66
  165. package/src/utils/reachability-analyzer.js +0 -339
  166. package/src/utils/react-dom-runtime-interop-plugin.js +0 -38
  167. package/src/utils/react-mdx-loader-plugin.d.ts +0 -3
  168. package/src/utils/react-mdx-loader-plugin.js +0 -45
  169. package/src/utils/react-runtime-alias-map.d.ts +0 -8
  170. package/src/utils/react-runtime-alias-map.js +0 -90
  171. /package/src/{utils → app}/client-only.d.ts +0 -0
  172. /package/src/{utils → app}/dynamic.d.ts +0 -0
  173. /package/src/{utils → app}/dynamic.test.browser.d.ts +0 -0
  174. /package/src/{utils → client-graph}/component-config-traversal.d.ts +0 -0
  175. /package/src/{utils → client-graph}/declared-modules.d.ts +0 -0
  176. /package/src/{declarations.d.ts → contracts/declarations.d.ts} +0 -0
  177. /package/src/{router-adapter.d.ts → contracts/router-adapter.d.ts} +0 -0
  178. /package/src/{utils/hydration-scripts.test.browser.d.ts → hydration/compiled-page-hydration-options.js} +0 -0
  179. /package/src/{react.constants.d.ts → plugin/react.constants.d.ts} +0 -0
  180. /package/src/{eco-embed.d.ts → render/eco-embed.d.ts} +0 -0
  181. /package/src/{utils → render}/html-boundary.d.ts +0 -0
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Strips server-only `eco.page(...)` option keys from browser-bound modules.
3
+ */
4
+ import { walkAstNodes } from './ast-walk.js';
5
+ import { applySourceEdits } from './ast-transform-import-edits.js';
6
+ const SERVER_ONLY_ECO_PAGE_OPTION_KEYS = new Set([
7
+ 'cache',
8
+ 'middleware',
9
+ 'dependencies',
10
+ 'requires',
11
+ 'metadata',
12
+ 'staticProps',
13
+ 'staticPaths',
14
+ ]);
15
+ function isEcoPageOptionsCall(node) {
16
+ return (node.type === 'CallExpression' &&
17
+ node.callee?.type === 'MemberExpression' &&
18
+ node.callee.object?.type === 'Identifier' &&
19
+ node.callee.object.name === 'eco' &&
20
+ node.callee.property?.type === 'Identifier' &&
21
+ node.callee.property.name === 'page' &&
22
+ node.arguments?.[0]?.type === 'ObjectExpression');
23
+ }
24
+ function collectEcoPageOptionStripEdit(node, source, edits) {
25
+ if (!isEcoPageOptionsCall(node))
26
+ return;
27
+ const objectExpression = node.arguments[0];
28
+ const keptProperties = [];
29
+ let removedProperty = false;
30
+ for (const property of objectExpression.properties ?? []) {
31
+ if (property?.type === 'Property') {
32
+ const keyName = getObjectPropertyKeyName(property.key);
33
+ if (keyName && SERVER_ONLY_ECO_PAGE_OPTION_KEYS.has(keyName)) {
34
+ removedProperty = true;
35
+ continue;
36
+ }
37
+ }
38
+ keptProperties.push(source.slice(property.start, property.end));
39
+ }
40
+ if (!removedProperty)
41
+ return;
42
+ const replacement = keptProperties.length > 0 ? `{ ${keptProperties.join(', ')} }` : '{}';
43
+ edits.push({
44
+ start: objectExpression.start,
45
+ end: objectExpression.end,
46
+ replacement,
47
+ });
48
+ }
49
+ function getObjectPropertyKeyName(node) {
50
+ if (!node)
51
+ return undefined;
52
+ if (node.type === 'Identifier')
53
+ return node.name;
54
+ if (node.type === 'StringLiteral' || node.type === 'Literal') {
55
+ return typeof node.value === 'string' ? node.value : undefined;
56
+ }
57
+ return undefined;
58
+ }
59
+ /**
60
+ * Removes server-only `eco.page(...)` options from browser-bound modules.
61
+ */
62
+ export function stripServerOnlyEcoPageOptions(source, program) {
63
+ const edits = [];
64
+ walkAstNodes(program, (node) => {
65
+ collectEcoPageOptionStripEdit(node, source, edits);
66
+ });
67
+ if (edits.length === 0) {
68
+ return { transformed: source, modified: false };
69
+ }
70
+ return { transformed: applySourceEdits(source, edits), modified: true };
71
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Builds source edits for import declaration specifier pruning.
3
+ */
4
+ export type SourceEdit = {
5
+ start: number;
6
+ end: number;
7
+ replacement: string;
8
+ };
9
+ /**
10
+ * Applies range replacements from last-to-first so earlier offsets stay valid.
11
+ */
12
+ export declare function applySourceEdits(source: string, edits: SourceEdit[]): string;
13
+ export declare function buildImportDeclarationRewrite(node: any, specifier: string, rules: Set<string>): SourceEdit | undefined;
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Builds source edits for import declaration specifier pruning.
3
+ */
4
+ /**
5
+ * Applies range replacements from last-to-first so earlier offsets stay valid.
6
+ */
7
+ export function applySourceEdits(source, edits) {
8
+ edits.sort((a, b) => b.start - a.start);
9
+ let transformed = source;
10
+ for (const edit of edits) {
11
+ transformed = transformed.slice(0, edit.start) + edit.replacement + transformed.slice(edit.end);
12
+ }
13
+ return transformed;
14
+ }
15
+ function collectKeptImportSpecifiers(node, rules) {
16
+ const kept = { keptSpecifierCount: 0, namedImportNodes: [] };
17
+ for (const spec of node.specifiers) {
18
+ if (spec.type === 'ImportSpecifier') {
19
+ appendKeptNamedSpecifier(spec, rules, kept);
20
+ }
21
+ else if (spec.type === 'ImportDefaultSpecifier') {
22
+ appendKeptDefaultSpecifier(spec, rules, kept);
23
+ }
24
+ else if (spec.type === 'ImportNamespaceSpecifier') {
25
+ appendKeptNamespaceSpecifier(spec, rules, kept);
26
+ }
27
+ }
28
+ return kept;
29
+ }
30
+ function appendKeptNamedSpecifier(spec, rules, kept) {
31
+ const importedName = spec.imported.type === 'Identifier' ? spec.imported.name : spec.imported.value;
32
+ if (!rules.has(importedName))
33
+ return;
34
+ kept.keptSpecifierCount += 1;
35
+ const localName = spec.local?.name;
36
+ kept.namedImportNodes.push(localName && localName !== importedName ? `${importedName} as ${localName}` : importedName);
37
+ }
38
+ function appendKeptDefaultSpecifier(spec, rules, kept) {
39
+ if (!rules.has('default'))
40
+ return;
41
+ kept.keptSpecifierCount += 1;
42
+ kept.defaultImportLocal = spec.local.name;
43
+ }
44
+ function appendKeptNamespaceSpecifier(spec, rules, kept) {
45
+ if (!rules.has('*'))
46
+ return;
47
+ kept.keptSpecifierCount += 1;
48
+ kept.namespaceImportLocal = spec.local.name;
49
+ }
50
+ export function buildImportDeclarationRewrite(node, specifier, rules) {
51
+ const { keptSpecifierCount, defaultImportLocal, namespaceImportLocal, namedImportNodes } = collectKeptImportSpecifiers(node, rules);
52
+ if (keptSpecifierCount === 0) {
53
+ return { start: node.start, end: node.end, replacement: '' };
54
+ }
55
+ if (keptSpecifierCount >= node.specifiers.length) {
56
+ return undefined;
57
+ }
58
+ const newDeclaration = formatPrunedImportDeclaration(specifier, defaultImportLocal, namespaceImportLocal, namedImportNodes);
59
+ return { start: node.start, end: node.end, replacement: newDeclaration };
60
+ }
61
+ function formatPrunedImportDeclaration(specifier, defaultImportLocal, namespaceImportLocal, namedImportNodes) {
62
+ if (defaultImportLocal && namespaceImportLocal) {
63
+ return `import ${defaultImportLocal}, * as ${namespaceImportLocal} from '${specifier}';`;
64
+ }
65
+ if (namespaceImportLocal) {
66
+ return `import * as ${namespaceImportLocal} from '${specifier}';`;
67
+ }
68
+ if (defaultImportLocal && namedImportNodes.length > 0) {
69
+ return `import ${defaultImportLocal}, { ${namedImportNodes.join(', ')} } from '${specifier}';`;
70
+ }
71
+ if (defaultImportLocal) {
72
+ return `import ${defaultImportLocal} from '${specifier}';`;
73
+ }
74
+ return `import { ${namedImportNodes.join(', ')} } from '${specifier}';`;
75
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * AST walk that prunes forbidden imports and rewrites named import surfaces.
3
+ */
4
+ import type { ReachabilityResult } from './reachability-analyzer.js';
5
+ import { type SourceEdit } from './ast-transform-import-edits.js';
6
+ export type ProcessSpecifierResult = {
7
+ allowed: boolean;
8
+ rules?: Set<string> | '*';
9
+ };
10
+ export type WalkImportsContext = {
11
+ filename: string;
12
+ reachability: ReachabilityResult;
13
+ edits: SourceEdit[];
14
+ allowedMap: Map<string, Set<string> | '*'>;
15
+ preserveUnreachableSideEffectImports: boolean;
16
+ };
17
+ /**
18
+ * Walks a parsed program and collects import-pruning edits.
19
+ */
20
+ export declare function collectImportTransformEdits(program: any, ctx: Omit<WalkImportsContext, 'edits'>): SourceEdit[];
@@ -0,0 +1,105 @@
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 (!ctx.preserveUnreachableSideEffectImports &&
37
+ !reachableRules &&
38
+ (!node.specifiers || node.specifiers.length === 0)) {
39
+ ctx.edits.push({ start: node.start, end: node.end, replacement: '' });
40
+ }
41
+ return true;
42
+ }
43
+ function handleExportNamedDeclaration(node, ctx) {
44
+ const specifier = node.source.value;
45
+ const { allowed } = processSpecifier(specifier, ctx.allowedMap);
46
+ if (!allowed) {
47
+ pruneForbiddenSpecifier(node, ctx, specifier, `Forbidden client export from '${specifier}'`, '');
48
+ }
49
+ return true;
50
+ }
51
+ function handleExportAllDeclaration(node, ctx) {
52
+ const specifier = node.source.value;
53
+ const { allowed } = processSpecifier(specifier, ctx.allowedMap);
54
+ if (!allowed) {
55
+ pruneForbiddenSpecifier(node, ctx, specifier, `Forbidden client export * from '${specifier}'`, '');
56
+ }
57
+ return true;
58
+ }
59
+ function handleImportExpression(node, ctx) {
60
+ if (!node.source?.value)
61
+ return false;
62
+ const specifier = node.source.value;
63
+ const { allowed } = processSpecifier(specifier, ctx.allowedMap);
64
+ if (!allowed) {
65
+ pruneForbiddenSpecifier(node, ctx, specifier, `Forbidden dynamic import('${specifier}')`, 'Promise.resolve({})');
66
+ }
67
+ return true;
68
+ }
69
+ function handleRequireCall(node, ctx) {
70
+ const arg = node.arguments?.[0];
71
+ if (!arg || (arg.type !== 'StringLiteral' && arg.type !== 'Literal') || typeof arg.value !== 'string') {
72
+ return;
73
+ }
74
+ const specifier = arg.value;
75
+ const { allowed } = processSpecifier(specifier, ctx.allowedMap);
76
+ if (!allowed) {
77
+ pruneForbiddenSpecifier(node, ctx, specifier, `Forbidden require('${specifier}')`, '({})');
78
+ }
79
+ }
80
+ /**
81
+ * Walks a parsed program and collects import-pruning edits.
82
+ */
83
+ export function collectImportTransformEdits(program, ctx) {
84
+ const edits = [];
85
+ const fullCtx = { ...ctx, edits };
86
+ walkAstNodes(program, (node) => {
87
+ if (node.type === 'ImportDeclaration') {
88
+ return handleImportDeclaration(node, fullCtx);
89
+ }
90
+ if (node.type === 'ExportNamedDeclaration' && node.source) {
91
+ return handleExportNamedDeclaration(node, fullCtx);
92
+ }
93
+ if (node.type === 'ExportAllDeclaration' && node.source) {
94
+ return handleExportAllDeclaration(node, fullCtx);
95
+ }
96
+ if (node.type === 'ImportExpression') {
97
+ return handleImportExpression(node, fullCtx);
98
+ }
99
+ if (node.type === 'CallExpression' && node.callee?.type === 'Identifier' && node.callee.name === 'require') {
100
+ handleRequireCall(node, fullCtx);
101
+ }
102
+ return false;
103
+ });
104
+ return edits;
105
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * AST transforms for the client graph boundary plugin.
3
+ */
4
+ import { type RequestedExportRules } from './specifier-classification.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
+ export declare function transformModuleImports(source: string, filename: string, globallyAllowed: Map<string, Set<string> | '*'>, requestedExports: Map<string, RequestedExportRules>, stripServerOnlyPageOptions?: boolean, preserveUnreachableSideEffectImports?: boolean): {
10
+ transformed: string;
11
+ modified: boolean;
12
+ };
@@ -0,0 +1,91 @@
1
+ /**
2
+ * AST transforms for the client graph boundary plugin.
3
+ */
4
+ import { parseModuleSource } from '@ecopages/core/build';
5
+ import { analyzeReachability } from './reachability-analyzer.js';
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);
18
+ }
19
+ }
20
+ }
21
+ });
22
+ return localDeclared;
23
+ }
24
+ function getImportedOrReexportedSource(statement) {
25
+ if (statement.type === 'ImportDeclaration') {
26
+ return statement.source.value;
27
+ }
28
+ if ((statement.type === 'ExportNamedDeclaration' || statement.type === 'ExportAllDeclaration') &&
29
+ statement.source) {
30
+ return statement.source.value;
31
+ }
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);
44
+ }
45
+ }
46
+ /**
47
+ * Parses a module using Oxc AST and surgically removes forbidden imports.
48
+ * Filters down to the exact specifiers requested via `{namedImport}` syntax.
49
+ */
50
+ export function transformModuleImports(source, filename, globallyAllowed, requestedExports, stripServerOnlyPageOptions = false, preserveUnreachableSideEffectImports = false) {
51
+ let result;
52
+ try {
53
+ result = parseModuleSource(filename, source);
54
+ }
55
+ catch {
56
+ return { transformed: source, modified: false };
57
+ }
58
+ const { program } = result;
59
+ const localDeclared = collectLocalDeclaredModules(program);
60
+ const locallyAllowed = parseDeclaredModules(localDeclared);
61
+ const allowedMap = mergeDeclaredModulesMap(globallyAllowed, locallyAllowed);
62
+ const explicitRequestedExports = requestedExports.get(normalizeRequestedExportsKey(filename));
63
+ const reachability = analyzeReachability(source, filename, program, explicitRequestedExports);
64
+ propagateRequestedExportRules(program, filename, reachability, requestedExports);
65
+ const edits = collectImportTransformEdits(program, {
66
+ filename,
67
+ reachability,
68
+ allowedMap,
69
+ preserveUnreachableSideEffectImports,
70
+ });
71
+ if (edits.length === 0) {
72
+ return stripServerOnlyPageOptions
73
+ ? stripServerOnlyEcoPageOptions(source, program)
74
+ : { transformed: source, modified: false };
75
+ }
76
+ const transformed = applySourceEdits(source, edits);
77
+ let reparsedResult;
78
+ try {
79
+ reparsedResult = parseModuleSource(filename, transformed);
80
+ }
81
+ catch {
82
+ return { transformed, modified: true };
83
+ }
84
+ const strippedPageOptions = stripServerOnlyPageOptions
85
+ ? stripServerOnlyEcoPageOptions(transformed, reparsedResult.program)
86
+ : { transformed, modified: false };
87
+ if (strippedPageOptions.modified) {
88
+ return strippedPageOptions;
89
+ }
90
+ return { transformed, modified: true };
91
+ }
@@ -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
+ }
@@ -0,0 +1,7 @@
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
+ };
@@ -0,0 +1,43 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { dirname, resolve, sep } from 'node:path';
3
+ const READ_FILE_SYNC_PATTERN = /\bfs\.readFileSync\s*\(\s*path\.resolve\s*\(\s*(['"`])([^'"`\n]+)\1\s*\)\s*,\s*['"`]utf-?8['"`]\s*\)/g;
4
+ function inferProjectRootFromSourcePath(filePath) {
5
+ const parts = filePath.split(/[\\/]/);
6
+ const srcIndex = parts.lastIndexOf('src');
7
+ if (srcIndex <= 0)
8
+ return undefined;
9
+ return parts.slice(0, srcIndex).join(sep);
10
+ }
11
+ function resolveReadFileCandidate(relPath, sourcePath, absWorkingDir) {
12
+ const sourceDir = dirname(sourcePath);
13
+ const inferredProjectRoot = inferProjectRootFromSourcePath(sourcePath);
14
+ const candidates = [
15
+ resolve(absWorkingDir, relPath),
16
+ resolve(process.cwd(), relPath),
17
+ resolve(sourceDir, relPath),
18
+ ...(inferredProjectRoot ? [resolve(inferredProjectRoot, relPath)] : []),
19
+ ];
20
+ return candidates.find((candidate) => existsSync(candidate));
21
+ }
22
+ /**
23
+ * Inlines `fs.readFileSync(path.resolve(...), 'utf8')` calls when the target file exists.
24
+ */
25
+ export function inlineReadFileSyncCalls(source, sourcePath, absWorkingDir) {
26
+ if (!source.includes('readFileSync')) {
27
+ return { transformed: source, modified: false };
28
+ }
29
+ let modified = false;
30
+ const transformed = source.replace(READ_FILE_SYNC_PATTERN, (_match, _q, relPath) => {
31
+ modified = true;
32
+ try {
33
+ const absolutePath = resolveReadFileCandidate(relPath, sourcePath, absWorkingDir);
34
+ if (!absolutePath)
35
+ return '""';
36
+ return JSON.stringify(readFileSync(absolutePath, 'utf-8'));
37
+ }
38
+ catch {
39
+ return '""';
40
+ }
41
+ });
42
+ return { transformed, modified };
43
+ }
@@ -0,0 +1,16 @@
1
+ import type { EcoBuildOnLoadResult } from '@ecopages/core/integration';
2
+ import type { ClientGraphModuleKind } from './module-classification.js';
3
+ import type { RequestedExportRules } from './specifier-classification.js';
4
+ export type TransformClientGraphModuleOptions = {
5
+ filePath: string;
6
+ source: string;
7
+ absWorkingDir: string;
8
+ category: ClientGraphModuleKind;
9
+ globallyDeclaredSources: Map<string, Set<string> | '*'>;
10
+ requestedExports: Map<string, RequestedExportRules>;
11
+ preserveUnreachableSideEffectImports?: boolean;
12
+ };
13
+ /**
14
+ * Applies read-file inlining and import transforms for one client-graph module.
15
+ */
16
+ export declare function transformClientGraphModule(options: TransformClientGraphModuleOptions): EcoBuildOnLoadResult | undefined;
@@ -0,0 +1,31 @@
1
+ import { extname } from 'node:path';
2
+ import { recordModuleTransformProfile } from '@ecopages/core/build';
3
+ import { inlineReadFileSyncCalls } from './boundary-plugin-read-file-inline.js';
4
+ import { transformModuleImports } from './ast-transform.js';
5
+ import { isPageOrLayoutEntry } from './module-classification.js';
6
+ function toLoaderResult(filePath, contents) {
7
+ const ext = extname(filePath).slice(1);
8
+ return { contents, loader: ext };
9
+ }
10
+ /**
11
+ * Applies read-file inlining and import transforms for one client-graph module.
12
+ */
13
+ export function transformClientGraphModule(options) {
14
+ const readFileInlining = inlineReadFileSyncCalls(options.source, options.filePath, options.absWorkingDir);
15
+ let transformed = readFileInlining.transformed;
16
+ let modified = readFileInlining.modified;
17
+ const analysisStartedAt = performance.now();
18
+ const { transformed: oxcTransformed, modified: importsModified } = transformModuleImports(transformed, options.filePath, options.globallyDeclaredSources, options.requestedExports, isPageOrLayoutEntry(options.filePath), Boolean(options.preserveUnreachableSideEffectImports) && !isPageOrLayoutEntry(options.filePath));
19
+ recordModuleTransformProfile({
20
+ category: options.category,
21
+ phase: 'analysis',
22
+ ms: performance.now() - analysisStartedAt,
23
+ });
24
+ if (importsModified) {
25
+ modified = true;
26
+ transformed = oxcTransformed;
27
+ }
28
+ if (!modified)
29
+ return undefined;
30
+ return toLoaderResult(options.filePath, transformed);
31
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @module ClientGraphBoundaryPlugin
3
+ *
4
+ * Build plugin securing the Ecopages isomorphic compilation pipeline.
5
+ */
6
+ import type { EcoBuildPlugin } from '@ecopages/core/integration';
7
+ /**
8
+ * Configuration options for the Client Graph Boundary build plugin.
9
+ */
10
+ type ClientGraphBoundaryOptions = {
11
+ projectRoot?: string;
12
+ absWorkingDir?: string;
13
+ entrypointFile?: string;
14
+ groupedEntrypointFiles?: readonly string[];
15
+ groupedDeclaredModules?: readonly string[];
16
+ declaredModules?: readonly string[];
17
+ alwaysAllowSpecifiers?: string[];
18
+ };
19
+ /**
20
+ * Instantiates the client graph boundary build plugin.
21
+ */
22
+ export declare function createClientGraphBoundaryPlugin(options?: ClientGraphBoundaryOptions): EcoBuildPlugin;
23
+ export {};