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