@ecopages/react 0.2.0-beta.31 → 0.2.0-beta.33
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 +5 -1
- package/package.json +4 -4
- package/src/app/client-only.js +13 -17
- package/src/app/dynamic.js +7 -10
- package/src/app/dynamic.test.browser.js +27 -29
- package/src/bundling/bootstrap-package-paths.js +5 -6
- package/src/bundling/bundle.js +115 -114
- package/src/bundling/discover-layout-runtime-modules.js +166 -155
- package/src/bundling/runtime-alias-map.d.ts +4 -1
- package/src/bundling/runtime-alias-map.js +107 -95
- package/src/bundling/runtime-bundle.js +250 -274
- package/src/bundling/runtime-interop-plugin.js +31 -35
- package/src/bundling/runtime-mode.js +10 -5
- package/src/bundling/runtime-modules.js +46 -35
- package/src/client-graph/ast-transform.js +415 -305
- package/src/client-graph/boundary-cache.js +146 -104
- package/src/client-graph/boundary-plugin.js +122 -104
- package/src/client-graph/component-config-traversal.js +70 -49
- package/src/client-graph/declared-modules.js +63 -41
- package/src/client-graph/reachability-analyzer.js +506 -313
- package/src/client-graph/specifier-classification.js +209 -141
- 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 +22 -9
- package/src/hmr/hmr-strategy.d.ts +12 -181
- package/src/hmr/hmr-strategy.js +321 -650
- package/src/hmr/page-metadata-cache.js +43 -40
- 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.js +6 -6
- package/src/hydration/bootstrap-imports.js +30 -20
- package/src/hydration/hydration-asset.js +216 -213
- package/src/hydration/hydration-scripts.d.ts +4 -0
- package/src/hydration/hydration-scripts.js +199 -66
- package/src/hydration/hydration-scripts.test.browser.js +163 -168
- package/src/hydration/page-data-manifest.js +89 -45
- package/src/hydration/page-data-reader.js +45 -32
- package/src/hydration/page-payload.js +101 -99
- package/src/hydration/serialize-page-data-script.js +27 -28
- package/src/mdx/mdx-config-dependency.js +108 -112
- package/src/mdx/mdx-loader-plugin.js +7 -10
- package/src/page/page-module.js +72 -66
- package/src/plugin/react.constants.js +1 -4
- package/src/plugin/react.plugin.d.ts +1 -0
- package/src/plugin/react.plugin.js +227 -216
- package/src/plugin/react.types.js +1 -0
- package/src/render/component-ownership.js +42 -18
- package/src/render/component-ssr.js +168 -190
- package/src/render/eco-embed.js +8 -10
- package/src/render/errors.js +21 -16
- package/src/render/html-boundary.js +55 -51
- package/src/render/layout-compose.js +122 -94
- package/src/render/react-renderer.js +433 -468
- package/src/render/react-runtime.js +25 -20
- package/src/render/unified-layout-composition.js +74 -85
- package/src/runtime/use-sync-external-store-with-selector.js +52 -54
|
@@ -1,342 +1,535 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @module ReachabilityAnalyzer
|
|
3
|
+
*
|
|
4
|
+
* This module is responsible for performing static analysis on Ecopages client components
|
|
5
|
+
* using the Oxc AST parser. It computes a strict "reachability graph" of all JavaScript/TypeScript
|
|
6
|
+
* dependencies (imports, variables, functions, and classes) that begin from explicit client roots.
|
|
7
|
+
*
|
|
8
|
+
* In Ecopages, "client roots" are defined as the `render`, `errorBoundary`, or `loadingFallback`
|
|
9
|
+
* properties passed into `eco.page()`, `eco.layout()`, or `eco.component()`. By tracing the execution path from
|
|
10
|
+
* these roots, the analyzer determines exactly which modules and bindings are actually needed
|
|
11
|
+
* by the browser to hydrate the page, and which imports are unused on the client (and thus can be pruned).
|
|
12
|
+
*/
|
|
13
|
+
import { parseSync } from 'oxc-parser';
|
|
14
|
+
import { extname } from 'node:path';
|
|
15
|
+
/**
|
|
16
|
+
* Determines the appropriate parser language configuration for a given file name.
|
|
17
|
+
*
|
|
18
|
+
* @param filename - The absolute or relative path to the file.
|
|
19
|
+
* @returns The Oxc parser language dialect to use ('js', 'jsx', 'ts', or 'tsx').
|
|
20
|
+
*/
|
|
21
|
+
export function parserLanguageForFile(filename) {
|
|
22
|
+
const extension = extname(filename).toLowerCase();
|
|
23
|
+
if (extension === '.tsx')
|
|
24
|
+
return 'tsx';
|
|
25
|
+
if (extension === '.ts')
|
|
26
|
+
return 'ts';
|
|
27
|
+
if (extension === '.jsx')
|
|
28
|
+
return 'jsx';
|
|
29
|
+
return 'js';
|
|
9
30
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Analyzes a module using Oxc AST and extracts a strict reachability graph
|
|
33
|
+
* starting from client roots (`render`, `errorBoundary`, `loadingFallback` of `eco.page`, `eco.layout`, or `eco.component`).
|
|
34
|
+
*
|
|
35
|
+
* @param source - Raw source string of the module.
|
|
36
|
+
* @param filename - Absolute or relative path to the module file.
|
|
37
|
+
* @param program - Optional pre-parsed Oxc program AST. When supplied, the
|
|
38
|
+
* internal `parseSync` call is skipped entirely (avoids double-parsing).
|
|
39
|
+
* @param explicitlyRequestedExports - Optional named export filter propagated
|
|
40
|
+
* from a downstream importer when this module is only partially reachable.
|
|
41
|
+
*/
|
|
42
|
+
export function analyzeReachability(source, filename, program, explicitlyRequestedExports) {
|
|
43
|
+
/**
|
|
44
|
+
* AST Resolution
|
|
45
|
+
*
|
|
46
|
+
* If the caller already has a parsed AST (e.g. from a prior `parseSync` call in the same
|
|
47
|
+
* pipeline), we reuse it directly to avoid double-parsing the same source text.
|
|
48
|
+
* Otherwise we parse here and return early with an empty "unanalyzed" result on failure.
|
|
49
|
+
*/
|
|
50
|
+
let resolvedProgram;
|
|
51
|
+
if (program) {
|
|
52
|
+
resolvedProgram = program;
|
|
29
53
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (statement.importKind === "type") {
|
|
38
|
-
continue;
|
|
39
|
-
}
|
|
40
|
-
const specifier = statement.source.value;
|
|
41
|
-
const bindings = /* @__PURE__ */ new Map();
|
|
42
|
-
if (!statement.specifiers || statement.specifiers.length === 0) {
|
|
43
|
-
topLevelImports.push({ node: statement, specifier, bindings, isSideEffect: true });
|
|
44
|
-
} else {
|
|
45
|
-
for (const spec of statement.specifiers) {
|
|
46
|
-
if (spec.type === "ImportDefaultSpecifier") {
|
|
47
|
-
bindings.set(spec.local.name, "default");
|
|
48
|
-
} else if (spec.type === "ImportNamespaceSpecifier") {
|
|
49
|
-
bindings.set(spec.local.name, "*");
|
|
50
|
-
} else if (spec.type === "ImportSpecifier") {
|
|
51
|
-
const importedName = spec.imported.type === "Identifier" ? spec.imported.name : spec.imported.value;
|
|
52
|
-
bindings.set(spec.local.name, importedName);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
topLevelImports.push({ node: statement, specifier, bindings, isSideEffect: false });
|
|
56
|
-
}
|
|
57
|
-
} else if (statement.type === "VariableDeclaration") {
|
|
58
|
-
for (const decl of statement.declarations) {
|
|
59
|
-
if (decl.id.type === "Identifier") {
|
|
60
|
-
topLevelDeclarations.set(decl.id.name, statement);
|
|
61
|
-
checkPotentialClientRoot(decl.init);
|
|
54
|
+
else {
|
|
55
|
+
let result;
|
|
56
|
+
try {
|
|
57
|
+
result = parseSync(filename, source, {
|
|
58
|
+
sourceType: 'module',
|
|
59
|
+
lang: parserLanguageForFile(filename),
|
|
60
|
+
});
|
|
62
61
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (decl.type === "FunctionDeclaration" || decl.type === "ClassDeclaration") {
|
|
72
|
-
if (decl.id && decl.id.type === "Identifier") {
|
|
73
|
-
topLevelDeclarations.set(decl.id.name, statement);
|
|
74
|
-
}
|
|
75
|
-
} else if (decl.type === "VariableDeclaration") {
|
|
76
|
-
for (const v of decl.declarations) {
|
|
77
|
-
if (v.id.type === "Identifier") {
|
|
78
|
-
topLevelDeclarations.set(v.id.name, statement);
|
|
79
|
-
checkPotentialClientRoot(v.init);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
62
|
+
catch {
|
|
63
|
+
return {
|
|
64
|
+
reachableImports: new Map(),
|
|
65
|
+
reachableDeclarations: new Set(),
|
|
66
|
+
unreachableSideEffectImports: [],
|
|
67
|
+
isFallbackRoots: true,
|
|
68
|
+
analyzed: false,
|
|
69
|
+
};
|
|
82
70
|
}
|
|
83
|
-
|
|
84
|
-
} else if (statement.type === "ExportDefaultDeclaration") {
|
|
85
|
-
checkPotentialClientRoot(statement.declaration);
|
|
86
|
-
} else if (statement.type === "ExpressionStatement") {
|
|
87
|
-
checkPotentialClientRoot(statement.expression);
|
|
88
|
-
} else if (statement.type === "ExportAllDeclaration" && statement.source) {
|
|
89
|
-
potentialClientRoots.push(statement);
|
|
71
|
+
resolvedProgram = result.program;
|
|
90
72
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Top-level statement scan
|
|
75
|
+
*
|
|
76
|
+
* Make a single pass over the top-level AST body to build two indexes:
|
|
77
|
+
*
|
|
78
|
+
* - `topLevelImports`: every `import` declaration found in this module, keyed by specifier.
|
|
79
|
+
* Each entry carries a `bindings` map from local alias → imported name so that later
|
|
80
|
+
* identifier lookups can resolve `import { readFile as rf } from 'node:fs'` correctly.
|
|
81
|
+
*
|
|
82
|
+
* - `topLevelDeclarations`: every locally declared variable, function, or class so that
|
|
83
|
+
* the BFS traversal can follow identifier references into their definition nodes.
|
|
84
|
+
*
|
|
85
|
+
* As declarations are catalogued, `checkPotentialClientRoot()` is called to detect whether
|
|
86
|
+
* any of them are `eco.page(…)` or `eco.component(…)` calls — the seed nodes for the graph.
|
|
87
|
+
*/
|
|
88
|
+
const topLevelImports = [];
|
|
89
|
+
const topLevelDeclarations = new Map();
|
|
90
|
+
const potentialClientRoots = [];
|
|
91
|
+
for (const statement of resolvedProgram.body) {
|
|
92
|
+
if (statement.type === 'ImportDeclaration') {
|
|
93
|
+
if (statement.importKind === 'type') {
|
|
94
|
+
continue;
|
|
112
95
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
potentialClientRoots.push(node);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
function getExportedName(specifier) {
|
|
121
|
-
if (specifier?.exported?.type === "Identifier") return specifier.exported.name;
|
|
122
|
-
if (typeof specifier?.exported?.value === "string") return specifier.exported.value;
|
|
123
|
-
if (specifier?.local?.type === "Identifier") return specifier.local.name;
|
|
124
|
-
if (typeof specifier?.local?.value === "string") return specifier.local.value;
|
|
125
|
-
return void 0;
|
|
126
|
-
}
|
|
127
|
-
function getReexportedImportName(specifier) {
|
|
128
|
-
if (specifier?.local?.type === "Identifier") return specifier.local.name;
|
|
129
|
-
if (typeof specifier?.local?.value === "string") return specifier.local.value;
|
|
130
|
-
if (specifier?.imported?.type === "Identifier") return specifier.imported.name;
|
|
131
|
-
if (typeof specifier?.imported?.value === "string") return specifier.imported.value;
|
|
132
|
-
return getExportedName(specifier);
|
|
133
|
-
}
|
|
134
|
-
function getLocalExportName(specifier) {
|
|
135
|
-
if (specifier?.local?.type === "Identifier") return specifier.local.name;
|
|
136
|
-
if (typeof specifier?.local?.value === "string") return specifier.local.value;
|
|
137
|
-
return void 0;
|
|
138
|
-
}
|
|
139
|
-
function isExplicitlyRequestedExport(name) {
|
|
140
|
-
if (explicitlyRequestedExports === "*") return true;
|
|
141
|
-
return explicitlyRequestedExports?.has(name) ?? false;
|
|
142
|
-
}
|
|
143
|
-
let isFallbackRoots = false;
|
|
144
|
-
if (potentialClientRoots.length === 0) {
|
|
145
|
-
if (explicitlyRequestedExports) {
|
|
146
|
-
for (const node of resolvedProgram.body) {
|
|
147
|
-
if (node.type === "ExportNamedDeclaration") {
|
|
148
|
-
const exportNode = node;
|
|
149
|
-
if (exportNode.source && exportNode.specifiers?.length) {
|
|
150
|
-
const hasRequestedReexport = exportNode.specifiers.some((specifier) => {
|
|
151
|
-
const exportedName = getExportedName(specifier);
|
|
152
|
-
return exportedName ? isExplicitlyRequestedExport(exportedName) : false;
|
|
153
|
-
});
|
|
154
|
-
if (hasRequestedReexport) {
|
|
155
|
-
potentialClientRoots.push(node);
|
|
96
|
+
const specifier = statement.source.value;
|
|
97
|
+
const bindings = new Map();
|
|
98
|
+
if (!statement.specifiers || statement.specifiers.length === 0) {
|
|
99
|
+
topLevelImports.push({ node: statement, specifier, bindings, isSideEffect: true });
|
|
156
100
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
101
|
+
else {
|
|
102
|
+
for (const spec of statement.specifiers) {
|
|
103
|
+
if (spec.type === 'ImportDefaultSpecifier') {
|
|
104
|
+
bindings.set(spec.local.name, 'default');
|
|
105
|
+
}
|
|
106
|
+
else if (spec.type === 'ImportNamespaceSpecifier') {
|
|
107
|
+
bindings.set(spec.local.name, '*');
|
|
108
|
+
}
|
|
109
|
+
else if (spec.type === 'ImportSpecifier') {
|
|
110
|
+
const importedName = spec.imported.type === 'Identifier' ? spec.imported.name : spec.imported.value;
|
|
111
|
+
bindings.set(spec.local.name, importedName);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
topLevelImports.push({ node: statement, specifier, bindings, isSideEffect: false });
|
|
163
115
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
potentialClientRoots.push(node);
|
|
116
|
+
}
|
|
117
|
+
else if (statement.type === 'VariableDeclaration') {
|
|
118
|
+
for (const decl of statement.declarations) {
|
|
119
|
+
if (decl.id.type === 'Identifier') {
|
|
120
|
+
topLevelDeclarations.set(decl.id.name, statement);
|
|
121
|
+
checkPotentialClientRoot(decl.init);
|
|
122
|
+
}
|
|
172
123
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const exportedName = getExportedName(specifier);
|
|
178
|
-
return exportedName ? isExplicitlyRequestedExport(exportedName) : false;
|
|
179
|
-
});
|
|
180
|
-
if (hasRequestedSpecifier) {
|
|
181
|
-
potentialClientRoots.push(node);
|
|
124
|
+
}
|
|
125
|
+
else if (statement.type === 'FunctionDeclaration' || statement.type === 'ClassDeclaration') {
|
|
126
|
+
if (statement.id && statement.id.type === 'Identifier') {
|
|
127
|
+
topLevelDeclarations.set(statement.id.name, statement);
|
|
182
128
|
}
|
|
183
|
-
}
|
|
184
|
-
} else if (node.type === "ExportDefaultDeclaration") {
|
|
185
|
-
if (isExplicitlyRequestedExport("default")) {
|
|
186
|
-
potentialClientRoots.push(node);
|
|
187
|
-
}
|
|
188
|
-
} else if (node.type === "ExportAllDeclaration") {
|
|
189
|
-
if (explicitlyRequestedExports === "*") {
|
|
190
|
-
potentialClientRoots.push(node);
|
|
191
|
-
}
|
|
192
129
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
130
|
+
else if (statement.type === 'ExportNamedDeclaration') {
|
|
131
|
+
if (statement.declaration) {
|
|
132
|
+
const decl = statement.declaration;
|
|
133
|
+
if (decl.type === 'FunctionDeclaration' || decl.type === 'ClassDeclaration') {
|
|
134
|
+
if (decl.id && decl.id.type === 'Identifier') {
|
|
135
|
+
topLevelDeclarations.set(decl.id.name, statement);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
else if (decl.type === 'VariableDeclaration') {
|
|
139
|
+
for (const v of decl.declarations) {
|
|
140
|
+
if (v.id.type === 'Identifier') {
|
|
141
|
+
topLevelDeclarations.set(v.id.name, statement);
|
|
142
|
+
checkPotentialClientRoot(v.init);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else if (statement.type === 'ExportDefaultDeclaration') {
|
|
149
|
+
checkPotentialClientRoot(statement.declaration);
|
|
150
|
+
}
|
|
151
|
+
else if (statement.type === 'ExpressionStatement') {
|
|
152
|
+
checkPotentialClientRoot(statement.expression);
|
|
153
|
+
}
|
|
154
|
+
else if (statement.type === 'ExportAllDeclaration' && statement.source) {
|
|
155
|
+
/**
|
|
156
|
+
* `export * from '...'` unconditionally re-exports every binding from the
|
|
157
|
+
* source module. Because the re-exported bindings do not create local
|
|
158
|
+
* identifiers in this file, the BFS traverser would never encounter them
|
|
159
|
+
* naturally. We therefore always seed these nodes into `potentialClientRoots`
|
|
160
|
+
* so that `traverse` can mark the source specifier as fully reachable ('*').
|
|
161
|
+
*/
|
|
162
|
+
potentialClientRoots.push(statement);
|
|
199
163
|
}
|
|
200
|
-
}
|
|
201
164
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
165
|
+
/**
|
|
166
|
+
* Inspects a node to determine if it represents an Ecopages client root declaration.
|
|
167
|
+
*
|
|
168
|
+
* @param node - The AST node to inspect.
|
|
169
|
+
*/
|
|
170
|
+
function checkPotentialClientRoot(node) {
|
|
171
|
+
if (!node || typeof node !== 'object')
|
|
172
|
+
return;
|
|
173
|
+
if (node.type === 'CallExpression' &&
|
|
174
|
+
node.callee.type === 'MemberExpression') {
|
|
175
|
+
const obj = node.callee.object;
|
|
176
|
+
const prop = node.callee.property;
|
|
177
|
+
if (obj.type === 'Identifier' &&
|
|
178
|
+
obj.name === 'eco' &&
|
|
179
|
+
prop.type === 'Identifier' &&
|
|
180
|
+
(prop.name === 'page' ||
|
|
181
|
+
prop.name === 'component' ||
|
|
182
|
+
prop.name === 'layout')) {
|
|
183
|
+
potentialClientRoots.push(node.callee);
|
|
184
|
+
const arg = node.arguments[0];
|
|
185
|
+
if (arg && arg.type === 'ObjectExpression') {
|
|
186
|
+
for (const prop of arg.properties) {
|
|
187
|
+
if (prop.type === 'Property' &&
|
|
188
|
+
prop.key.type === 'Identifier') {
|
|
189
|
+
if ([
|
|
190
|
+
'render',
|
|
191
|
+
'errorBoundary',
|
|
192
|
+
'loadingFallback',
|
|
193
|
+
'clientScripts',
|
|
194
|
+
'dependencies',
|
|
195
|
+
].includes(prop.key.name)) {
|
|
196
|
+
potentialClientRoots.push(prop.value);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
else if (node.type === 'CallExpression' &&
|
|
204
|
+
node.callee.type === 'Identifier' &&
|
|
205
|
+
node.callee.name === 'dynamic') {
|
|
206
|
+
potentialClientRoots.push(node);
|
|
207
|
+
}
|
|
218
208
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
209
|
+
/**
|
|
210
|
+
* Resolves the externally visible export name from an export specifier.
|
|
211
|
+
*
|
|
212
|
+
* @param specifier - Oxc export specifier node.
|
|
213
|
+
* @returns The exported binding name when available.
|
|
214
|
+
*/
|
|
215
|
+
function getExportedName(specifier) {
|
|
216
|
+
if (specifier?.exported?.type === 'Identifier')
|
|
217
|
+
return specifier.exported.name;
|
|
218
|
+
if (typeof specifier?.exported?.value === 'string')
|
|
219
|
+
return specifier.exported.value;
|
|
220
|
+
if (specifier?.local?.type === 'Identifier')
|
|
221
|
+
return specifier.local.name;
|
|
222
|
+
if (typeof specifier?.local?.value === 'string')
|
|
223
|
+
return specifier.local.value;
|
|
224
|
+
return undefined;
|
|
227
225
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
226
|
+
/**
|
|
227
|
+
* Resolves the imported binding name represented by a re-export specifier.
|
|
228
|
+
*
|
|
229
|
+
* @param specifier - Oxc export specifier node.
|
|
230
|
+
* @returns The source-module binding name that should be marked reachable.
|
|
231
|
+
*/
|
|
232
|
+
function getReexportedImportName(specifier) {
|
|
233
|
+
if (specifier?.local?.type === 'Identifier')
|
|
234
|
+
return specifier.local.name;
|
|
235
|
+
if (typeof specifier?.local?.value === 'string')
|
|
236
|
+
return specifier.local.value;
|
|
237
|
+
if (specifier?.imported?.type === 'Identifier')
|
|
238
|
+
return specifier.imported.name;
|
|
239
|
+
if (typeof specifier?.imported?.value === 'string')
|
|
240
|
+
return specifier.imported.value;
|
|
241
|
+
return getExportedName(specifier);
|
|
232
242
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
243
|
+
/**
|
|
244
|
+
* Resolves the local identifier used by a local export list entry.
|
|
245
|
+
*
|
|
246
|
+
* @param specifier - Oxc export specifier node.
|
|
247
|
+
* @returns The local symbol name referenced by the export list.
|
|
248
|
+
*/
|
|
249
|
+
function getLocalExportName(specifier) {
|
|
250
|
+
if (specifier?.local?.type === 'Identifier')
|
|
251
|
+
return specifier.local.name;
|
|
252
|
+
if (typeof specifier?.local?.value === 'string')
|
|
253
|
+
return specifier.local.value;
|
|
254
|
+
return undefined;
|
|
241
255
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
256
|
+
/**
|
|
257
|
+
* Checks whether a named export is part of the explicitly requested subset.
|
|
258
|
+
*
|
|
259
|
+
* @param name - Export name to test.
|
|
260
|
+
* @returns True when the export should seed or continue traversal.
|
|
261
|
+
*/
|
|
262
|
+
function isExplicitlyRequestedExport(name) {
|
|
263
|
+
if (explicitlyRequestedExports === '*')
|
|
264
|
+
return true;
|
|
265
|
+
return explicitlyRequestedExports?.has(name) ?? false;
|
|
246
266
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
267
|
+
/**
|
|
268
|
+
* Client root resolution (fallback mode)
|
|
269
|
+
*
|
|
270
|
+
* If *no* `eco.page`/`eco.component` call was found in the file, we fall back to treating
|
|
271
|
+
* every exported declaration as a potential client root. This covers utility modules that
|
|
272
|
+
* don't use Ecopages conventions (plain React components, shared helpers, etc.)
|
|
273
|
+
*
|
|
274
|
+
* In fallback mode the analysis is intentionally permissive — the `isFallbackRoots` flag
|
|
275
|
+
* propagates to callers so they know not to hard-fail on reachable forbidden imports
|
|
276
|
+
* (since we cannot be 100% certain about the execution boundary).
|
|
277
|
+
*/
|
|
278
|
+
let isFallbackRoots = false;
|
|
279
|
+
if (potentialClientRoots.length === 0) {
|
|
280
|
+
if (explicitlyRequestedExports) {
|
|
281
|
+
for (const node of resolvedProgram.body) {
|
|
282
|
+
if (node.type === 'ExportNamedDeclaration') {
|
|
283
|
+
const exportNode = node;
|
|
284
|
+
if (exportNode.source && exportNode.specifiers?.length) {
|
|
285
|
+
const hasRequestedReexport = exportNode.specifiers.some((specifier) => {
|
|
286
|
+
const exportedName = getExportedName(specifier);
|
|
287
|
+
return exportedName ? isExplicitlyRequestedExport(exportedName) : false;
|
|
288
|
+
});
|
|
289
|
+
if (hasRequestedReexport) {
|
|
290
|
+
potentialClientRoots.push(node);
|
|
291
|
+
}
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
if (exportNode.declaration?.type === 'FunctionDeclaration' ||
|
|
295
|
+
exportNode.declaration?.type === 'ClassDeclaration') {
|
|
296
|
+
const declarationName = exportNode.declaration.id?.name;
|
|
297
|
+
if (declarationName && isExplicitlyRequestedExport(declarationName)) {
|
|
298
|
+
potentialClientRoots.push(node);
|
|
299
|
+
}
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
if (exportNode.declaration?.type === 'VariableDeclaration') {
|
|
303
|
+
const hasRequestedDeclaration = exportNode.declaration.declarations.some((declaration) => declaration.id?.type === 'Identifier' &&
|
|
304
|
+
isExplicitlyRequestedExport(declaration.id.name));
|
|
305
|
+
if (hasRequestedDeclaration) {
|
|
306
|
+
potentialClientRoots.push(node);
|
|
307
|
+
}
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
if (exportNode.specifiers?.length) {
|
|
311
|
+
const hasRequestedSpecifier = exportNode.specifiers.some((specifier) => {
|
|
312
|
+
const exportedName = getExportedName(specifier);
|
|
313
|
+
return exportedName ? isExplicitlyRequestedExport(exportedName) : false;
|
|
314
|
+
});
|
|
315
|
+
if (hasRequestedSpecifier) {
|
|
316
|
+
potentialClientRoots.push(node);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
else if (node.type === 'ExportDefaultDeclaration') {
|
|
321
|
+
if (isExplicitlyRequestedExport('default')) {
|
|
322
|
+
potentialClientRoots.push(node);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
else if (node.type === 'ExportAllDeclaration') {
|
|
326
|
+
if (explicitlyRequestedExports === '*') {
|
|
327
|
+
potentialClientRoots.push(node);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
isFallbackRoots = true;
|
|
334
|
+
for (const node of resolvedProgram.body) {
|
|
335
|
+
if (node.type === 'ExportNamedDeclaration' ||
|
|
336
|
+
node.type === 'ExportDefaultDeclaration' ||
|
|
337
|
+
node.type === 'ExportAllDeclaration') {
|
|
338
|
+
potentialClientRoots.push(node);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
252
341
|
}
|
|
253
|
-
}
|
|
254
|
-
return;
|
|
255
342
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
343
|
+
/**
|
|
344
|
+
* BFS reachability traversal
|
|
345
|
+
*
|
|
346
|
+
* Starting from the seed nodes collected above, we perform a breadth-first walk of the AST.
|
|
347
|
+
* Every identifier encountered is checked against `topLevelDeclarations` (to enqueue further
|
|
348
|
+
* nodes) and `topLevelImports` (to mark the referenced binding as reachable).
|
|
349
|
+
*
|
|
350
|
+
* `visitedNodes` guards against infinite cycles in recursive or mutually-recursive declarations.
|
|
351
|
+
*/
|
|
352
|
+
const reachableImports = new Map();
|
|
353
|
+
const reachableDeclarations = new Set();
|
|
354
|
+
const queue = [...potentialClientRoots];
|
|
355
|
+
const visitedNodes = new Set();
|
|
356
|
+
/**
|
|
357
|
+
* Registers an imported binding as reachable in the client graph.
|
|
358
|
+
*
|
|
359
|
+
* @param specifier - The module specifier from which the binding is imported.
|
|
360
|
+
* @param importedName - The specific named export being imported, or '*' for namespace imports.
|
|
361
|
+
*/
|
|
362
|
+
function markImportReachable(specifier, importedName) {
|
|
363
|
+
let current = reachableImports.get(specifier);
|
|
364
|
+
if (current === '*')
|
|
365
|
+
return;
|
|
366
|
+
if (importedName === '*') {
|
|
367
|
+
reachableImports.set(specifier, '*');
|
|
261
368
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
369
|
+
else {
|
|
370
|
+
if (!current) {
|
|
371
|
+
current = new Set();
|
|
372
|
+
reachableImports.set(specifier, current);
|
|
373
|
+
}
|
|
374
|
+
current.add(importedName);
|
|
265
375
|
}
|
|
266
|
-
}
|
|
267
|
-
return;
|
|
268
376
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
}
|
|
288
|
-
return;
|
|
289
|
-
} else if (node.type === "JSXIdentifier") {
|
|
290
|
-
if (/^[A-Z]/.test(node.name) && !currentScope.has(node.name)) {
|
|
291
|
-
checkIdentifier(node.name);
|
|
292
|
-
}
|
|
293
|
-
} else if (node.type === "JSXMemberExpression") {
|
|
294
|
-
traverse(node.object, currentScope);
|
|
295
|
-
return;
|
|
296
|
-
} else if (node.type === "CallExpression" && node.callee.type === "Identifier" && node.callee.name === "dynamic") {
|
|
297
|
-
const arg = node.arguments[0];
|
|
298
|
-
if (arg && (arg.type === "ArrowFunctionExpression" || arg.type === "FunctionExpression")) {
|
|
299
|
-
const body = arg.body;
|
|
300
|
-
if (body.type === "ImportExpression" && body.source.type === "Literal") {
|
|
301
|
-
markImportReachable(body.source.value, "*");
|
|
377
|
+
/**
|
|
378
|
+
* Traces an identifier to its origin declaration, enqueuing it for deep traversal if it resolves
|
|
379
|
+
* to a local module-level declaration, or marking it as a reachable import if it originates from another module.
|
|
380
|
+
*
|
|
381
|
+
* @param name - The identifier name to check.
|
|
382
|
+
*/
|
|
383
|
+
function checkIdentifier(name) {
|
|
384
|
+
if (topLevelDeclarations.has(name)) {
|
|
385
|
+
const declNode = topLevelDeclarations.get(name);
|
|
386
|
+
if (!reachableDeclarations.has(declNode)) {
|
|
387
|
+
reachableDeclarations.add(declNode);
|
|
388
|
+
queue.push(declNode);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
for (const imp of topLevelImports) {
|
|
392
|
+
if (imp.bindings.has(name)) {
|
|
393
|
+
markImportReachable(imp.specifier, imp.bindings.get(name));
|
|
394
|
+
}
|
|
302
395
|
}
|
|
303
|
-
}
|
|
304
|
-
} else if (node.type === "ImportExpression" && node.source.type === "Literal") {
|
|
305
|
-
markImportReachable(node.source.value, "*");
|
|
306
396
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
397
|
+
/**
|
|
398
|
+
* Recursively walks down an AST node to discover referenced variables and function calls,
|
|
399
|
+
* building out the reachability graph.
|
|
400
|
+
*
|
|
401
|
+
* @param node - The Oxc AST node to traverse. Typed as `any` because Oxc lacks a unified iterable node type.
|
|
402
|
+
* @param localScope - A set of identifiers that shadow module-level declarations within the current lexical scope.
|
|
403
|
+
*/
|
|
404
|
+
function traverse(node, localScope) {
|
|
405
|
+
if (!node || typeof node !== 'object')
|
|
406
|
+
return;
|
|
407
|
+
if (visitedNodes.has(node))
|
|
408
|
+
return;
|
|
409
|
+
visitedNodes.add(node);
|
|
410
|
+
if (Array.isArray(node)) {
|
|
411
|
+
for (const child of node)
|
|
412
|
+
traverse(child, localScope);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const currentScope = localScope;
|
|
416
|
+
/**
|
|
417
|
+
* `export * from '...'` nodes are seeded into the BFS queue by the top-level scan.
|
|
418
|
+
* When the traverser reaches one here we immediately mark the entire source module
|
|
419
|
+
* as reachable ('*'), then stop — there are no local identifiers to follow.
|
|
420
|
+
*/
|
|
421
|
+
if (node.type === 'ExportAllDeclaration' && typeof node.source?.value === 'string') {
|
|
422
|
+
markImportReachable(node.source.value, '*');
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
if (node.type === 'ExportNamedDeclaration' && typeof node.source?.value === 'string') {
|
|
426
|
+
for (const specifier of node.specifiers ?? []) {
|
|
427
|
+
const importedName = getReexportedImportName(specifier);
|
|
428
|
+
if (importedName) {
|
|
429
|
+
markImportReachable(node.source.value, importedName);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
if (node.type === 'ExportNamedDeclaration' &&
|
|
435
|
+
!node.source &&
|
|
436
|
+
explicitlyRequestedExports &&
|
|
437
|
+
node.specifiers?.length) {
|
|
438
|
+
for (const specifier of node.specifiers) {
|
|
439
|
+
const exportedName = getExportedName(specifier);
|
|
440
|
+
if (!exportedName || !isExplicitlyRequestedExport(exportedName)) {
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
const localName = getLocalExportName(specifier);
|
|
444
|
+
if (localName && !currentScope.has(localName)) {
|
|
445
|
+
checkIdentifier(localName);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (node.type === 'Identifier' || (node.type === 'JSXIdentifier' && /^[A-Z]/.test(node.name))) {
|
|
451
|
+
if (!currentScope.has(node.name)) {
|
|
452
|
+
checkIdentifier(node.name);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
else if (node.type === 'MemberExpression') {
|
|
456
|
+
traverse(node.object, currentScope);
|
|
457
|
+
if (node.computed) {
|
|
458
|
+
traverse(node.property, currentScope);
|
|
459
|
+
}
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
else if (node.type === 'Property') {
|
|
463
|
+
if (node.computed)
|
|
464
|
+
traverse(node.key, currentScope);
|
|
465
|
+
traverse(node.value, currentScope);
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
else if (node.type === 'JSXOpeningElement' || node.type === 'JSXClosingElement') {
|
|
469
|
+
traverse(node.name, currentScope);
|
|
470
|
+
if (node.attributes) {
|
|
471
|
+
for (const attr of node.attributes)
|
|
472
|
+
traverse(attr, currentScope);
|
|
473
|
+
}
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
else if (node.type === 'JSXIdentifier') {
|
|
477
|
+
if (/^[A-Z]/.test(node.name) && !currentScope.has(node.name)) {
|
|
478
|
+
checkIdentifier(node.name);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
else if (node.type === 'JSXMemberExpression') {
|
|
482
|
+
traverse(node.object, currentScope);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
else if (node.type === 'CallExpression' &&
|
|
486
|
+
node.callee.type === 'Identifier' &&
|
|
487
|
+
node.callee.name === 'dynamic') {
|
|
488
|
+
const arg = node.arguments[0];
|
|
489
|
+
if (arg && (arg.type === 'ArrowFunctionExpression' || arg.type === 'FunctionExpression')) {
|
|
490
|
+
const body = arg.body;
|
|
491
|
+
if (body.type === 'ImportExpression' && body.source.type === 'Literal') {
|
|
492
|
+
markImportReachable(body.source.value, '*');
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
else if (node.type === 'ImportExpression' && node.source.type === 'Literal') {
|
|
497
|
+
markImportReachable(node.source.value, '*');
|
|
498
|
+
}
|
|
499
|
+
if (node.type === 'ArrowFunctionExpression' ||
|
|
500
|
+
node.type === 'FunctionExpression' ||
|
|
501
|
+
node.type === 'FunctionDeclaration') {
|
|
502
|
+
const newScope = new Set(currentScope);
|
|
503
|
+
if (node.id && node.id.type === 'Identifier')
|
|
504
|
+
newScope.add(node.id.name);
|
|
505
|
+
if (node.params && node.params.items) {
|
|
506
|
+
for (const p of node.params.items) {
|
|
507
|
+
if (p.pattern && p.pattern.type === 'Identifier') {
|
|
508
|
+
newScope.add(p.pattern.name);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
traverse(node.body, newScope);
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
for (const key in node) {
|
|
516
|
+
if (key !== 'type' && key !== 'start' && key !== 'end') {
|
|
517
|
+
traverse(node[key], currentScope);
|
|
518
|
+
}
|
|
315
519
|
}
|
|
316
|
-
}
|
|
317
|
-
traverse(node.body, newScope);
|
|
318
|
-
return;
|
|
319
520
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
traverse(
|
|
323
|
-
}
|
|
521
|
+
while (queue.length > 0) {
|
|
522
|
+
const root = queue.shift();
|
|
523
|
+
traverse(root, new Set());
|
|
324
524
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
isFallbackRoots,
|
|
336
|
-
analyzed: true
|
|
337
|
-
};
|
|
525
|
+
const unreachableSideEffectImports = topLevelImports
|
|
526
|
+
.filter((imp) => imp.isSideEffect && !reachableImports.has(imp.specifier))
|
|
527
|
+
.map((imp) => imp.node);
|
|
528
|
+
return {
|
|
529
|
+
reachableImports,
|
|
530
|
+
reachableDeclarations,
|
|
531
|
+
unreachableSideEffectImports,
|
|
532
|
+
isFallbackRoots,
|
|
533
|
+
analyzed: true,
|
|
534
|
+
};
|
|
338
535
|
}
|
|
339
|
-
export {
|
|
340
|
-
analyzeReachability,
|
|
341
|
-
parserLanguageForFile
|
|
342
|
-
};
|