@ecopages/react 0.2.0-rc.6 → 0.2.0-rc.8
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.
- package/README.md +21 -4
- package/package.json +5 -4
- package/src/app/client-only.js +3 -8
- package/src/client-graph/ast-transform-eco-page.d.ts +10 -0
- package/src/client-graph/ast-transform-eco-page.js +71 -0
- package/src/client-graph/ast-transform-import-edits.d.ts +13 -0
- package/src/client-graph/ast-transform-import-edits.js +75 -0
- package/src/client-graph/ast-transform-walk-imports.d.ts +19 -0
- package/src/client-graph/ast-transform-walk-imports.js +103 -0
- package/src/client-graph/ast-transform.d.ts +1 -7
- package/src/client-graph/ast-transform.js +42 -379
- package/src/client-graph/ast-walk.d.ts +10 -0
- package/src/client-graph/ast-walk.js +26 -0
- package/src/client-graph/boundary-plugin-read-file-inline.d.ts +8 -0
- package/src/client-graph/boundary-plugin-read-file-inline.js +45 -0
- package/src/client-graph/boundary-plugin-transform.d.ts +18 -0
- package/src/client-graph/boundary-plugin-transform.js +61 -0
- package/src/client-graph/boundary-plugin.d.ts +0 -20
- package/src/client-graph/boundary-plugin.js +16 -98
- package/src/client-graph/reachability-analyzer.d.ts +2 -43
- package/src/client-graph/reachability-analyzer.js +20 -518
- package/src/client-graph/reachability-client-roots.d.ts +8 -0
- package/src/client-graph/reachability-client-roots.js +59 -0
- package/src/client-graph/reachability-export-names.d.ts +5 -0
- package/src/client-graph/reachability-export-names.js +34 -0
- package/src/client-graph/reachability-fallback-roots.d.ts +3 -0
- package/src/client-graph/reachability-fallback-roots.js +71 -0
- package/src/client-graph/reachability-preload.d.ts +10 -0
- package/src/client-graph/reachability-preload.js +30 -0
- package/src/client-graph/reachability-top-level-index.d.ts +14 -0
- package/src/client-graph/reachability-top-level-index.js +93 -0
- package/src/client-graph/reachability-traverse.d.ts +7 -0
- package/src/client-graph/reachability-traverse.js +201 -0
- package/src/hmr/hmr-strategy-process.d.ts +34 -0
- package/src/hmr/hmr-strategy-process.js +83 -0
- package/src/hmr/hmr-strategy.d.ts +1 -0
- package/src/hmr/hmr-strategy.js +42 -84
- package/src/hydration/browser/island-hydration.d.ts +82 -0
- package/src/hydration/browser/island-hydration.js +247 -0
- package/src/hydration/browser/page-hydration-hmr.d.ts +11 -0
- package/src/hydration/browser/page-hydration-hmr.js +56 -0
- package/src/hydration/browser/page-hydration-module.d.ts +8 -0
- package/src/hydration/browser/page-hydration-module.js +17 -0
- package/src/hydration/browser/page-hydration.d.ts +54 -0
- package/src/hydration/browser/page-hydration.js +177 -0
- package/src/hydration/compiled-page-hydration-options.d.ts +20 -0
- package/src/hydration/hydration-asset.d.ts +20 -1
- package/src/hydration/hydration-asset.js +42 -24
- package/src/hydration/hydration-lifecycle.test.browser.d.ts +1 -0
- package/src/hydration/hydration-lifecycle.test.browser.js +409 -0
- package/src/hydration/hydration-page-entry-source.d.ts +5 -0
- package/src/hydration/hydration-page-entry-source.js +87 -0
- package/src/hydration/hydration-script-compiler.d.ts +48 -0
- package/src/hydration/hydration-script-compiler.js +213 -0
- package/src/hydration/page-data-reader.d.ts +0 -14
- package/src/hydration/page-data-reader.js +1 -47
- package/src/render/component-ssr.d.ts +17 -0
- package/src/render/component-ssr.js +19 -1
- package/src/render/react-renderer.d.ts +2 -1
- package/src/render/react-renderer.js +5 -1
- package/src/render/unified-layout-composition.d.ts +2 -0
- package/src/render/unified-layout-composition.js +5 -1
- package/src/hydration/hydration-scripts.d.ts +0 -112
- package/src/hydration/hydration-scripts.js +0 -517
- package/src/hydration/hydration-scripts.test.browser.js +0 -199
- /package/src/hydration/{hydration-scripts.test.browser.d.ts → compiled-page-hydration-options.js} +0 -0
package/README.md
CHANGED
|
@@ -31,7 +31,7 @@ For component-level islands, Ecopages React uses this contract:
|
|
|
31
31
|
|
|
32
32
|
- SSR output preserves the authored DOM structure (no unnecessary wrapper elements).
|
|
33
33
|
- A stable island host contract is stamped on the component SSR root via core (`data-eco-island`, `data-eco-island-integration`, `data-eco-component-id`, optional `data-eco-component-key` / `data-eco-props`).
|
|
34
|
-
- The island runtime
|
|
34
|
+
- The island runtime keeps the SSR host and its children in place and hydrates that host with `hydrateRoot()`. The host uses `display: contents` so it does not introduce an extra layout box. Full-page hydration paths also use `hydrateRoot()`.
|
|
35
35
|
|
|
36
36
|
> [!TIP]
|
|
37
37
|
> **Full React SPA Routing:**
|
|
@@ -75,6 +75,7 @@ Important:
|
|
|
75
75
|
|
|
76
76
|
- Components that may render foreign children must declare those children in `config.dependencies.components`.
|
|
77
77
|
- Ecopages validates mixed-renderer ownership from declared dependencies during render preparation. It does not infer every foreign subtree from rendered HTML alone.
|
|
78
|
+
- Route-level dependency resolvers can supply Components at request time; React includes those resolved roots when deciding whether a Foreign Subtree needs handoff.
|
|
78
79
|
- React still keeps its own child transport and hydration rules for React-owned subtrees.
|
|
79
80
|
|
|
80
81
|
## Server and Client Graph Contract
|
|
@@ -203,7 +204,7 @@ Hydration must rebuild the same tree the server rendered.
|
|
|
203
204
|
|
|
204
205
|
That applies to both:
|
|
205
206
|
|
|
206
|
-
-
|
|
207
|
+
- page hydration lifecycle in [src/hydration/browser/page-hydration.ts](src/hydration/browser/page-hydration.ts)
|
|
207
208
|
- router-backed hydration in [../../react-router/src/router.ts](../../react-router/src/router.ts)
|
|
208
209
|
|
|
209
210
|
If the page render receives `locals` on the server and the layout also depends on those values, the client must pass the same serialized `locals` into the layout during hydration. Otherwise React will detect a mismatch.
|
|
@@ -214,7 +215,7 @@ The main regression coverage lives in:
|
|
|
214
215
|
|
|
215
216
|
- [src/client-graph/boundary-plugin.test.ts](src/client-graph/boundary-plugin.test.ts): verifies server-only `eco.page(...)` options are stripped from browser bundles.
|
|
216
217
|
- [src/render/react-renderer.locals.test.ts](src/render/react-renderer.locals.test.ts): verifies only declared `requires` keys are serialized into hydration payloads.
|
|
217
|
-
- [src/hydration/hydration-
|
|
218
|
+
- [src/hydration/hydration-script-compiler.test.ts](src/hydration/hydration-script-compiler.test.ts): verifies editable browser sources compile to readable and minified entries.
|
|
218
219
|
- [../../react-router/test/hmr-reload.test.browser.ts](../../react-router/test/hmr-reload.test.browser.ts): verifies router-backed layout hydration receives `locals` with `persistLayouts` both enabled and disabled.
|
|
219
220
|
|
|
220
221
|
If you change the AST transform or hydration flow, update the corresponding tests in the same change.
|
|
@@ -243,7 +244,23 @@ The fix is **shared browser runtime vendors**: selected npm packages are built o
|
|
|
243
244
|
|
|
244
245
|
In `ecopages dev`, page modules are served from `/assets/__eco_dev__/` and should import `/assets/vendors/*` for shared packages instead of inlining `node_modules`. Kitchen-sink routes `/vendor-share/a` and `/vendor-share/b` (with `runtimeModules: ['zod']`) are the reference fixture; see `playground/kitchen-sink/e2e/shared-vendors.test.e2e.ts`.
|
|
245
246
|
|
|
246
|
-
The Ecopages dev toolbar **Islands** app inspects `data-eco-island`, `data-eco-component-id`, `data-eco-props`, and `window.__ECO_PAGES__.islandRoots` during development.
|
|
247
|
+
The Ecopages dev toolbar **Islands** app inspects `data-eco-island`, `data-eco-component-id`, `data-eco-props`, and `window.__ECO_PAGES__.islandRoots` during development. React component islands are emitted as `<eco-island style="display:contents">` hosts during SSR and hydrated in place; the client adds `data-eco-hydrated` after the initial commit. `display: contents` keeps the host from introducing a layout box while preserving it as the hydration and diagnostics boundary.
|
|
248
|
+
|
|
249
|
+
The browser lifecycle is maintained as typed code in `src/hydration/browser/`.
|
|
250
|
+
Production island bootstraps are generated from that source by the server-side
|
|
251
|
+
Rolldown compiler in `src/hydration/hydration-script-compiler.ts`, which keeps
|
|
252
|
+
React, ReactDOM, and the component entry as external browser imports. This keeps
|
|
253
|
+
the generated script compact without requiring a second handwritten minified
|
|
254
|
+
implementation.
|
|
255
|
+
|
|
256
|
+
Page HMR registers before initial preload, but publishes a replacement component
|
|
257
|
+
only after its preload succeeds. Each handler belongs to the page generation in
|
|
258
|
+
which it was registered. Navigation cleanup invalidates both pending updates and
|
|
259
|
+
later calls to retained handlers, preventing updates to a replacement page root.
|
|
260
|
+
|
|
261
|
+
Hydration preserves the SSR DOM when the server and client trees match. A
|
|
262
|
+
component that renders different markup in the browser can still produce a
|
|
263
|
+
normal React hydration mismatch or layout shift.
|
|
247
264
|
|
|
248
265
|
Auto-discovery removes the need to hand-maintain `runtimeModules` for the common case — a query-client layout that imports `@tanstack/react-query` is enough when discovery is configured correctly.
|
|
249
266
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecopages/react",
|
|
3
|
-
"version": "0.2.0-rc.
|
|
3
|
+
"version": "0.2.0-rc.8",
|
|
4
4
|
"description": "React integration for Ecopages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ecopages",
|
|
@@ -104,19 +104,20 @@
|
|
|
104
104
|
"directory": "packages/integrations/react"
|
|
105
105
|
},
|
|
106
106
|
"peerDependencies": {
|
|
107
|
-
"@ecopages/core": "0.2.0-rc.
|
|
107
|
+
"@ecopages/core": "0.2.0-rc.8",
|
|
108
108
|
"@types/react": "^19",
|
|
109
109
|
"@types/react-dom": "^19",
|
|
110
110
|
"react": "^19",
|
|
111
111
|
"react-dom": "^19"
|
|
112
112
|
},
|
|
113
113
|
"dependencies": {
|
|
114
|
-
"@ecopages/file-system": "0.2.0-rc.
|
|
114
|
+
"@ecopages/file-system": "0.2.0-rc.8",
|
|
115
115
|
"@ecopages/logger": "^0.2.3",
|
|
116
|
-
"@ecopages/mdx": "0.2.0-rc.
|
|
116
|
+
"@ecopages/mdx": "0.2.0-rc.8",
|
|
117
117
|
"@mdx-js/mdx": "^3.1.1",
|
|
118
118
|
"oxc-parser": "^0.141.0",
|
|
119
119
|
"oxc-transform": "^0.141.0",
|
|
120
|
+
"rolldown": "^1.1.0",
|
|
120
121
|
"source-map": "^0.8.0",
|
|
121
122
|
"vfile": "^6.0.3"
|
|
122
123
|
}
|
package/src/app/client-only.js
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
useEffect(() => {
|
|
5
|
-
setIsClient(true);
|
|
6
|
-
}, []);
|
|
7
|
-
return isClient;
|
|
8
|
-
};
|
|
1
|
+
import { useSyncExternalStore } from 'react';
|
|
2
|
+
const emptySubscribe = () => () => { };
|
|
3
|
+
export const useIsClient = () => useSyncExternalStore(emptySubscribe, () => true, () => false);
|
|
9
4
|
export const ClientOnly = ({ children, fallback = null }) => {
|
|
10
5
|
const isClient = useIsClient();
|
|
11
6
|
if (!isClient) {
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Strips server-only `eco.page(...)` option keys from browser-bound modules.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Removes server-only `eco.page(...)` options from browser-bound modules.
|
|
6
|
+
*/
|
|
7
|
+
export declare function stripServerOnlyEcoPageOptions(source: string, program: any): {
|
|
8
|
+
transformed: string;
|
|
9
|
+
modified: boolean;
|
|
10
|
+
};
|
|
@@ -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,19 @@
|
|
|
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
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Walks a parsed program and collects import-pruning edits.
|
|
18
|
+
*/
|
|
19
|
+
export declare function collectImportTransformEdits(program: any, ctx: Omit<WalkImportsContext, 'edits'>): SourceEdit[];
|
|
@@ -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>,
|
|
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
|
};
|