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