@ecopages/react 0.2.0-beta.4 → 0.2.0-beta.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +175 -7
- package/package.json +54 -29
- package/src/app/client-only.js +15 -0
- package/src/app/dynamic.js +9 -0
- package/src/app/dynamic.test.browser.js +31 -0
- package/src/bundling/bootstrap-package-paths.d.ts +5 -0
- package/src/bundling/bootstrap-package-paths.js +5 -0
- package/src/{services/react-bundle.service.d.ts → bundling/bundle.d.ts} +23 -8
- package/src/bundling/bundle.js +120 -0
- package/src/bundling/discover-layout-runtime-modules.d.ts +32 -0
- package/src/bundling/discover-layout-runtime-modules.js +191 -0
- package/src/bundling/runtime-alias-map.d.ts +13 -0
- package/src/bundling/runtime-alias-map.js +113 -0
- package/src/{services/react-runtime-bundle.service.d.ts → bundling/runtime-bundle.d.ts} +30 -5
- package/src/bundling/runtime-bundle.js +258 -0
- package/src/bundling/runtime-interop-plugin.js +34 -0
- package/src/bundling/runtime-mode.d.ts +9 -0
- package/src/bundling/runtime-mode.js +11 -0
- package/src/bundling/runtime-modules.d.ts +29 -0
- package/src/bundling/runtime-modules.js +49 -0
- package/src/client-graph/ast-transform.d.ts +18 -0
- package/src/client-graph/ast-transform.js +426 -0
- package/src/{utils/client-graph-boundary-cache.d.ts → client-graph/boundary-cache.d.ts} +23 -13
- package/src/client-graph/boundary-cache.js +182 -0
- package/src/{utils/client-graph-boundary-plugin.d.ts → client-graph/boundary-plugin.d.ts} +4 -13
- package/src/client-graph/boundary-plugin.js +149 -0
- package/src/client-graph/component-config-traversal.js +77 -0
- package/src/client-graph/declared-modules.js +69 -0
- package/src/client-graph/module-classification.d.ts +3 -0
- package/src/client-graph/module-classification.js +43 -0
- package/src/{utils → client-graph}/reachability-analyzer.d.ts +4 -12
- package/src/client-graph/reachability-analyzer.js +515 -0
- package/src/client-graph/specifier-classification.d.ts +75 -0
- package/src/client-graph/specifier-classification.js +223 -0
- package/src/contracts/router-adapter.js +6 -0
- package/src/dev-transform/react-dev-transform-contributor.d.ts +17 -0
- package/src/dev-transform/react-dev-transform-contributor.js +22 -0
- package/src/hmr/hmr-scripts.js +44 -0
- package/src/hmr/hmr-strategy.d.ts +74 -0
- package/src/hmr/hmr-strategy.js +351 -0
- package/src/{services/react-hmr-page-metadata-cache.d.ts → hmr/page-metadata-cache.d.ts} +5 -1
- package/src/hmr/page-metadata-cache.js +44 -0
- package/src/hmr/react-hmr-dev-transform-plugins.d.ts +23 -0
- package/src/hmr/react-hmr-dev-transform-plugins.js +20 -0
- package/src/hydration/assert-no-bare-ecopages-imports.d.ts +4 -0
- package/src/hydration/assert-no-bare-ecopages-imports.js +7 -0
- package/src/hydration/bootstrap-imports.d.ts +25 -0
- package/src/hydration/bootstrap-imports.js +33 -0
- package/src/hydration/hydration-asset.d.ts +84 -0
- package/src/hydration/hydration-asset.js +227 -0
- package/src/hydration/hydration-scripts.d.ts +104 -0
- package/src/hydration/hydration-scripts.js +483 -0
- package/src/hydration/hydration-scripts.test.browser.js +199 -0
- package/src/hydration/page-data-manifest.d.ts +58 -0
- package/src/hydration/page-data-manifest.js +97 -0
- package/src/hydration/page-data-reader.d.ts +31 -0
- package/src/hydration/page-data-reader.js +77 -0
- package/src/hydration/page-payload.d.ts +67 -0
- package/src/hydration/page-payload.js +106 -0
- package/src/hydration/serialize-page-data-script.d.ts +27 -0
- package/src/hydration/serialize-page-data-script.js +30 -0
- package/src/{services/react-mdx-config-dependency.service.d.ts → mdx/mdx-config-dependency.d.ts} +6 -6
- package/src/mdx/mdx-config-dependency.js +110 -0
- package/src/mdx/mdx-loader-plugin.d.ts +2 -0
- package/src/mdx/mdx-loader-plugin.js +8 -0
- package/src/page/page-module.d.ts +46 -0
- package/src/page/page-module.js +74 -0
- package/src/plugin/react.constants.js +1 -0
- package/src/{react.plugin.d.ts → plugin/react.plugin.d.ts} +9 -3
- package/src/plugin/react.plugin.js +241 -0
- package/src/plugin/react.types.d.ts +152 -0
- package/src/plugin/react.types.js +1 -0
- package/src/render/component-ownership.d.ts +49 -0
- package/src/render/component-ownership.js +46 -0
- package/src/render/component-ssr.d.ts +113 -0
- package/src/render/component-ssr.js +180 -0
- package/src/render/eco-embed.js +9 -0
- package/src/render/errors.d.ts +16 -0
- package/src/render/errors.js +23 -0
- package/src/render/html-boundary.js +59 -0
- package/src/render/layout-compose.d.ts +66 -0
- package/src/render/layout-compose.js +134 -0
- package/src/render/react-renderer.d.ts +138 -0
- package/src/render/react-renderer.js +448 -0
- package/src/render/react-runtime.d.ts +13 -0
- package/src/render/react-runtime.js +26 -0
- package/src/render/unified-layout-composition.d.ts +53 -0
- package/src/render/unified-layout-composition.js +78 -0
- package/src/runtime/use-sync-external-store-with-selector.js +52 -54
- package/src/eco-embed.js +0 -11
- package/src/react-hmr-strategy.d.ts +0 -230
- package/src/react-hmr-strategy.js +0 -657
- package/src/react-renderer.d.ts +0 -233
- package/src/react-renderer.js +0 -622
- package/src/react.constants.js +0 -4
- package/src/react.plugin.js +0 -205
- package/src/react.types.d.ts +0 -88
- package/src/react.types.js +0 -0
- package/src/router-adapter.js +0 -0
- package/src/services/pages-index.d.ts +0 -64
- package/src/services/pages-index.js +0 -73
- package/src/services/react-bundle.service.js +0 -86
- package/src/services/react-hmr-page-metadata-cache.js +0 -35
- package/src/services/react-hydration-asset.service.d.ts +0 -84
- package/src/services/react-hydration-asset.service.js +0 -217
- package/src/services/react-mdx-config-dependency.service.js +0 -122
- package/src/services/react-page-module.service.d.ts +0 -88
- package/src/services/react-page-module.service.js +0 -166
- package/src/services/react-page-payload.service.d.ts +0 -46
- package/src/services/react-page-payload.service.js +0 -67
- package/src/services/react-runtime-bundle.service.js +0 -186
- package/src/utils/client-graph-boundary-cache.js +0 -116
- package/src/utils/client-graph-boundary-plugin.js +0 -552
- package/src/utils/client-only.js +0 -19
- package/src/utils/component-config-traversal.js +0 -54
- package/src/utils/declared-modules.js +0 -47
- package/src/utils/dynamic.js +0 -12
- package/src/utils/dynamic.test.browser.js +0 -33
- package/src/utils/hmr-scripts.js +0 -31
- package/src/utils/html-boundary.js +0 -55
- package/src/utils/hydration-scripts.d.ts +0 -92
- package/src/utils/hydration-scripts.js +0 -358
- package/src/utils/hydration-scripts.test.browser.js +0 -198
- package/src/utils/reachability-analyzer.js +0 -339
- package/src/utils/react-dom-runtime-interop-plugin.js +0 -38
- package/src/utils/react-mdx-loader-plugin.d.ts +0 -3
- package/src/utils/react-mdx-loader-plugin.js +0 -45
- package/src/utils/react-runtime-alias-map.d.ts +0 -8
- package/src/utils/react-runtime-alias-map.js +0 -90
- /package/src/{utils → app}/client-only.d.ts +0 -0
- /package/src/{utils → app}/dynamic.d.ts +0 -0
- /package/src/{utils → app}/dynamic.test.browser.d.ts +0 -0
- /package/src/{utils/react-dom-runtime-interop-plugin.d.ts → bundling/runtime-interop-plugin.d.ts} +0 -0
- /package/src/{utils → client-graph}/component-config-traversal.d.ts +0 -0
- /package/src/{utils → client-graph}/declared-modules.d.ts +0 -0
- /package/src/{declarations.d.ts → contracts/declarations.d.ts} +0 -0
- /package/src/{router-adapter.d.ts → contracts/router-adapter.d.ts} +0 -0
- /package/src/{utils → hmr}/hmr-scripts.d.ts +0 -0
- /package/src/{utils → hydration}/hydration-scripts.test.browser.d.ts +0 -0
- /package/src/{react.constants.d.ts → plugin/react.constants.d.ts} +0 -0
- /package/src/{eco-embed.d.ts → render/eco-embed.d.ts} +0 -0
- /package/src/{utils → render}/html-boundary.d.ts +0 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Production runtime check for React bundle and hydration policy.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Matches core `isProductionRuntime` (`NODE_ENV === 'production'`) without a new
|
|
6
|
+
* `@ecopages/core` export. Keeping this boundary shared ensures hosted-development
|
|
7
|
+
* runtime imports and production bundle optimizations use the same definition.
|
|
8
|
+
*/
|
|
9
|
+
export declare function isReactProductionRuntime(): boolean;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Production runtime check for React bundle and hydration policy.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Matches core `isProductionRuntime` (`NODE_ENV === 'production'`) without a new
|
|
6
|
+
* `@ecopages/core` export. Keeping this boundary shared ensures hosted-development
|
|
7
|
+
* runtime imports and production bundle optimizations use the same definition.
|
|
8
|
+
*/
|
|
9
|
+
export function isReactProductionRuntime() {
|
|
10
|
+
return process.env.NODE_ENV === 'production';
|
|
11
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalizes `reactPlugin({ runtimeModules })` entries for vendor bundling.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
export type ReactPluginRuntimeModule = string | {
|
|
7
|
+
specifier: string;
|
|
8
|
+
outputName?: string;
|
|
9
|
+
externals?: string[];
|
|
10
|
+
};
|
|
11
|
+
export type ResolvedReactPluginRuntimeModule = {
|
|
12
|
+
specifier: string;
|
|
13
|
+
outputName: string;
|
|
14
|
+
externals: string[];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Builds a stable vendor filename stem from a package specifier.
|
|
18
|
+
*/
|
|
19
|
+
export declare function resolveReactPluginRuntimeModuleSlug(specifier: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Resolves user-facing runtime module declarations into vendor bundle config.
|
|
22
|
+
*/
|
|
23
|
+
export declare function resolveReactPluginRuntimeModules(modules: ReactPluginRuntimeModule[] | undefined): ResolvedReactPluginRuntimeModule[];
|
|
24
|
+
/**
|
|
25
|
+
* Merges auto-discovered layout npm imports with explicit `runtimeModules` entries.
|
|
26
|
+
*
|
|
27
|
+
* @remarks Manual entries win when both sources declare the same specifier.
|
|
28
|
+
*/
|
|
29
|
+
export declare function mergeReactPluginRuntimeModules(manualModules: ResolvedReactPluginRuntimeModule[], autoSpecifiers: string[]): ResolvedReactPluginRuntimeModule[];
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalizes `reactPlugin({ runtimeModules })` entries for vendor bundling.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Builds a stable vendor filename stem from a package specifier.
|
|
8
|
+
*/
|
|
9
|
+
export function resolveReactPluginRuntimeModuleSlug(specifier) {
|
|
10
|
+
return specifier.replace(/^@/, '').replace(/\//g, '-');
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Resolves user-facing runtime module declarations into vendor bundle config.
|
|
14
|
+
*/
|
|
15
|
+
export function resolveReactPluginRuntimeModules(modules) {
|
|
16
|
+
return (modules ?? []).map((entry) => {
|
|
17
|
+
if (typeof entry === 'string') {
|
|
18
|
+
return {
|
|
19
|
+
specifier: entry,
|
|
20
|
+
outputName: resolveReactPluginRuntimeModuleSlug(entry),
|
|
21
|
+
externals: [],
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
specifier: entry.specifier,
|
|
26
|
+
outputName: entry.outputName ?? resolveReactPluginRuntimeModuleSlug(entry.specifier),
|
|
27
|
+
externals: entry.externals ?? [],
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Merges auto-discovered layout npm imports with explicit `runtimeModules` entries.
|
|
33
|
+
*
|
|
34
|
+
* @remarks Manual entries win when both sources declare the same specifier.
|
|
35
|
+
*/
|
|
36
|
+
export function mergeReactPluginRuntimeModules(manualModules, autoSpecifiers) {
|
|
37
|
+
const merged = new Map();
|
|
38
|
+
for (const specifier of autoSpecifiers) {
|
|
39
|
+
merged.set(specifier, {
|
|
40
|
+
specifier,
|
|
41
|
+
outputName: resolveReactPluginRuntimeModuleSlug(specifier),
|
|
42
|
+
externals: [],
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
for (const entry of manualModules) {
|
|
46
|
+
merged.set(entry.specifier, entry);
|
|
47
|
+
}
|
|
48
|
+
return [...merged.values()];
|
|
49
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AST transforms for the client graph boundary plugin.
|
|
3
|
+
*/
|
|
4
|
+
import type { RequestedExportRules } from './boundary-cache.js';
|
|
5
|
+
/**
|
|
6
|
+
* Parses a module using Oxc AST and surgically removes forbidden imports.
|
|
7
|
+
* Filters down to the exact specifiers requested via `{namedImport}` syntax.
|
|
8
|
+
*
|
|
9
|
+
* @param source - The raw string source content of the module.
|
|
10
|
+
* @param filename - The absolute path of the module.
|
|
11
|
+
* @param globallyAllowed - A map of modules declared globally allowable by the build configuration.
|
|
12
|
+
* @param requestedExports - Local requested-export registry used to propagate named reachability across files.
|
|
13
|
+
* @returns An object containing the transformed string and a boolean indicating if changes occurred.
|
|
14
|
+
*/
|
|
15
|
+
export declare function transformModuleImports(source: string, filename: string, globallyAllowed: Map<string, Set<string> | '*'>, requestedExports: Map<string, RequestedExportRules>, projectRoot?: string, stripServerOnlyPageOptions?: boolean): {
|
|
16
|
+
transformed: string;
|
|
17
|
+
modified: boolean;
|
|
18
|
+
};
|
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AST transforms for the client graph boundary plugin.
|
|
3
|
+
*/
|
|
4
|
+
import { parseModuleSource } from '@ecopages/core/cache';
|
|
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
|
+
}
|
|
74
|
+
}
|
|
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
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
walk(program);
|
|
93
|
+
if (edits.length === 0) {
|
|
94
|
+
return { transformed: source, modified: false };
|
|
95
|
+
}
|
|
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);
|
|
100
|
+
}
|
|
101
|
+
return { transformed, modified: true };
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Parses a module using Oxc AST and surgically removes forbidden imports.
|
|
105
|
+
* 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
|
+
*/
|
|
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
|
+
*/
|
|
122
|
+
let result;
|
|
123
|
+
try {
|
|
124
|
+
result = parseModuleSource(filename, source);
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
return { transformed: source, modified: false };
|
|
128
|
+
}
|
|
129
|
+
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
|
+
*/
|
|
173
|
+
const locallyAllowed = parseDeclaredModules(localDeclared);
|
|
174
|
+
const allowedMap = mergeDeclaredModulesMap(globallyAllowed, locallyAllowed);
|
|
175
|
+
const explicitRequestedExports = requestedExports.get(normalizeRequestedExportsKey(filename));
|
|
176
|
+
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);
|
|
402
|
+
if (edits.length === 0) {
|
|
403
|
+
return stripServerOnlyPageOptions
|
|
404
|
+
? stripServerOnlyEcoPageOptions(source, program)
|
|
405
|
+
: { transformed: source, modified: false };
|
|
406
|
+
}
|
|
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
|
+
}
|
|
412
|
+
let reparsedResult;
|
|
413
|
+
try {
|
|
414
|
+
reparsedResult = parseModuleSource(filename, transformed);
|
|
415
|
+
}
|
|
416
|
+
catch {
|
|
417
|
+
return { transformed, modified: true };
|
|
418
|
+
}
|
|
419
|
+
const strippedPageOptions = stripServerOnlyPageOptions
|
|
420
|
+
? stripServerOnlyEcoPageOptions(transformed, reparsedResult.program)
|
|
421
|
+
: { transformed, modified: false };
|
|
422
|
+
if (strippedPageOptions.modified) {
|
|
423
|
+
return strippedPageOptions;
|
|
424
|
+
}
|
|
425
|
+
return { transformed, modified: true };
|
|
426
|
+
}
|
|
@@ -17,6 +17,14 @@
|
|
|
17
17
|
* The cache is content-hashed (via `rapidhash`) so a `touch`/`utimes`
|
|
18
18
|
* does not invalidate a still-valid entry.
|
|
19
19
|
*
|
|
20
|
+
* Cache identity is `(filePath, source, allowListRules, inboundRules)`.
|
|
21
|
+
* Allow-list hashing includes each package's permitted exports, not just
|
|
22
|
+
* package names — otherwise `node:fs{readFileSync}` and
|
|
23
|
+
* `node:fs{writeFileSync}` would collide.
|
|
24
|
+
*
|
|
25
|
+
* Modules that inline external file contents (`readFileSync` rewrite) are
|
|
26
|
+
* not stored: their output depends on files outside the importer source.
|
|
27
|
+
*
|
|
20
28
|
* **`rulesAdded` semantics:** the map holds the **after-state** of
|
|
21
29
|
* every registry key this transform touched — both newly added keys
|
|
22
30
|
* and keys that were grown (Set union) or promoted to `'*'`. On
|
|
@@ -27,11 +35,15 @@
|
|
|
27
35
|
* registry as well.
|
|
28
36
|
*/
|
|
29
37
|
export type RequestedExportRules = Set<string> | '*';
|
|
38
|
+
/** Globally declared modules with their permitted export rules. */
|
|
39
|
+
export type ClientGraphAllowList = ReadonlyMap<string, RequestedExportRules>;
|
|
30
40
|
export type CachedTransform = {
|
|
31
41
|
/** Hash of the source string at the time the transform was cached. */
|
|
32
42
|
sourceHash: number | bigint;
|
|
33
43
|
/** Hash of the globally-allowed modules map at the time the transform was cached. */
|
|
34
44
|
allowListHash: number | bigint;
|
|
45
|
+
/** Hash of the requested export rules already propagated to this module. */
|
|
46
|
+
inboundRulesHash: number | bigint;
|
|
35
47
|
/** The transformed source (or original if `modified` is false). */
|
|
36
48
|
transformed: string;
|
|
37
49
|
/** Whether the transform changed the source. */
|
|
@@ -53,9 +65,10 @@ export type CachedTransform = {
|
|
|
53
65
|
* LRU-bounded cache for client-graph-boundary transform results.
|
|
54
66
|
*
|
|
55
67
|
* One instance is owned by the React plugin for the app's lifetime and
|
|
56
|
-
* shared across all builds. The cache key is
|
|
57
|
-
*
|
|
58
|
-
*
|
|
68
|
+
* shared across all builds. The cache key is
|
|
69
|
+
* `(filePath, source, allowListRules, inboundRules)` so a file whose
|
|
70
|
+
* source, allow-list export rules, or inbound requested exports change
|
|
71
|
+
* gets a fresh entry.
|
|
59
72
|
*/
|
|
60
73
|
export declare class ClientGraphBoundaryCache {
|
|
61
74
|
private readonly entries;
|
|
@@ -66,11 +79,10 @@ export declare class ClientGraphBoundaryCache {
|
|
|
66
79
|
/**
|
|
67
80
|
* Look up a cached transform for `filePath`.
|
|
68
81
|
*
|
|
69
|
-
* Returns `undefined` if the source
|
|
70
|
-
* the entry was stored.
|
|
71
|
-
* transform in that case and calling `set` to update the entry.
|
|
82
|
+
* Returns `undefined` if the source, allow-list rules, or inbound
|
|
83
|
+
* requested exports have changed since the entry was stored.
|
|
72
84
|
*/
|
|
73
|
-
get(filePath: string, source: string,
|
|
85
|
+
get(filePath: string, source: string, allowList: ClientGraphAllowList, inboundRules?: RequestedExportRules): CachedTransform | undefined;
|
|
74
86
|
/**
|
|
75
87
|
* Store a transform result.
|
|
76
88
|
*
|
|
@@ -78,15 +90,14 @@ export declare class ClientGraphBoundaryCache {
|
|
|
78
90
|
* capacity. Defensively copies any `Set`-typed rules in `rulesAdded`
|
|
79
91
|
* so later mutations to the caller's sets cannot corrupt the cache.
|
|
80
92
|
*/
|
|
81
|
-
set(filePath: string, source: string,
|
|
93
|
+
set(filePath: string, source: string, allowList: ClientGraphAllowList, entry: Omit<CachedTransform, 'sourceHash' | 'allowListHash' | 'inboundRulesHash'>, inboundRules?: RequestedExportRules): void;
|
|
82
94
|
/**
|
|
83
95
|
* Invalidate a single file's entry. Call this from the file watcher
|
|
84
96
|
* when `filePath`'s content has changed.
|
|
85
97
|
*/
|
|
86
98
|
invalidate(filePath: string): void;
|
|
87
99
|
/**
|
|
88
|
-
* Invalidate every file whose key matches a
|
|
89
|
-
* "anything under `src/pages/` changed" signals.
|
|
100
|
+
* Invalidate every file whose key matches a predicate.
|
|
90
101
|
*/
|
|
91
102
|
invalidateMatching(predicate: (filePath: string) => boolean): number;
|
|
92
103
|
/** Clear all entries. */
|
|
@@ -102,7 +113,6 @@ export declare class ClientGraphBoundaryCache {
|
|
|
102
113
|
};
|
|
103
114
|
}
|
|
104
115
|
/**
|
|
105
|
-
*
|
|
106
|
-
* create their own.
|
|
116
|
+
* Hash allow-list package names together with each package's permitted exports.
|
|
107
117
|
*/
|
|
108
|
-
export declare
|
|
118
|
+
export declare function hashAllowList(allowList: ClientGraphAllowList): number | bigint;
|