@ecopages/react 0.2.1 → 0.3.1
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 +179 -35
- package/package.json +78 -33
- package/src/app/client-only.js +10 -0
- package/src/app/dynamic.js +9 -0
- package/src/bundling/bootstrap-package-paths.d.ts +5 -0
- package/src/bundling/bootstrap-package-paths.js +5 -0
- package/src/bundling/bundle.d.ts +78 -0
- package/src/bundling/bundle.js +130 -0
- package/src/bundling/runtime-alias-map.d.ts +12 -0
- package/src/bundling/runtime-alias-map.js +110 -0
- package/src/bundling/runtime-bundle.d.ts +70 -0
- package/src/bundling/runtime-bundle.js +233 -0
- package/src/{utils/react-dom-runtime-interop-plugin.d.ts → bundling/runtime-interop-plugin.d.ts} +1 -1
- package/src/bundling/runtime-interop-plugin.js +32 -0
- package/src/bundling/runtime-mode.d.ts +1 -0
- package/src/bundling/runtime-mode.js +10 -0
- package/src/bundling/runtime-modules.d.ts +21 -0
- package/src/bundling/runtime-modules.js +28 -0
- 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 +20 -0
- package/src/client-graph/ast-transform-walk-imports.js +105 -0
- package/src/client-graph/ast-transform.d.ts +12 -0
- package/src/client-graph/ast-transform.js +91 -0
- 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 +7 -0
- package/src/client-graph/boundary-plugin-read-file-inline.js +43 -0
- package/src/client-graph/boundary-plugin-transform.d.ts +16 -0
- package/src/client-graph/boundary-plugin-transform.js +31 -0
- package/src/client-graph/boundary-plugin.d.ts +23 -0
- package/src/client-graph/boundary-plugin.js +177 -0
- package/src/client-graph/component-config-traversal.d.ts +36 -0
- package/src/client-graph/component-config-traversal.js +77 -0
- package/src/{utils → client-graph}/declared-modules.d.ts +1 -1
- package/src/client-graph/declared-modules.js +69 -0
- package/src/client-graph/module-classification.d.ts +3 -0
- package/src/client-graph/module-classification.js +43 -0
- package/src/client-graph/reachability-analyzer.d.ts +26 -0
- package/src/client-graph/reachability-analyzer.js +55 -0
- 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/client-graph/specifier-classification.d.ts +74 -0
- package/src/client-graph/specifier-classification.js +191 -0
- package/src/content/collection-component-cache.d.ts +25 -0
- package/src/content/collection-component-cache.js +62 -0
- package/src/{router-adapter.d.ts → contracts/router-adapter.d.ts} +7 -14
- 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-strategy-process.d.ts +34 -0
- package/src/hmr/hmr-strategy-process.js +83 -0
- package/src/hmr/hmr-strategy.d.ts +67 -0
- package/src/hmr/hmr-strategy.js +306 -0
- package/src/{services/react-hmr-page-metadata-cache.d.ts → hmr/page-metadata-cache.d.ts} +6 -2
- package/src/hmr/page-metadata-cache.js +44 -0
- package/src/hmr/react-hmr-dev-transform-plugins.d.ts +22 -0
- package/src/hmr/react-hmr-dev-transform-plugins.js +19 -0
- package/src/hydration/bootstrap-imports.d.ts +25 -0
- package/src/hydration/bootstrap-imports.js +33 -0
- package/src/hydration/browser/island-hydration.d.ts +81 -0
- package/src/hydration/browser/island-hydration.js +247 -0
- package/src/hydration/browser/page-hydration-hmr.d.ts +11 -0
- package/src/hydration/browser/page-hydration-hmr.js +56 -0
- package/src/hydration/browser/page-hydration-module.d.ts +8 -0
- package/src/hydration/browser/page-hydration-module.js +17 -0
- package/src/hydration/browser/page-hydration.d.ts +54 -0
- package/src/hydration/browser/page-hydration.js +177 -0
- package/src/hydration/compiled-page-hydration-options.d.ts +20 -0
- package/src/hydration/compiled-page-hydration-options.js +1 -0
- package/src/hydration/hydration-asset.d.ts +128 -0
- package/src/hydration/hydration-asset.js +388 -0
- package/src/hydration/hydration-page-entry-source.d.ts +5 -0
- package/src/hydration/hydration-page-entry-source.js +87 -0
- package/src/hydration/hydration-script-compiler.d.ts +47 -0
- package/src/hydration/hydration-script-compiler.js +213 -0
- package/src/hydration/page-data-manifest.d.ts +58 -0
- package/src/hydration/page-data-manifest.js +97 -0
- package/src/hydration/page-data-reader.d.ts +17 -0
- package/src/hydration/page-data-reader.js +31 -0
- package/src/hydration/page-payload.d.ts +68 -0
- package/src/hydration/page-payload.js +106 -0
- package/src/hydration/serialize-page-data-script.d.ts +27 -0
- package/src/hydration/serialize-page-data-script.js +30 -0
- package/src/mdx/mdx-config-dependency.d.ts +37 -0
- package/src/mdx/mdx-config-dependency.js +107 -0
- package/src/mdx/mdx-loader-plugin.d.ts +8 -0
- package/src/mdx/mdx-loader-plugin.js +11 -0
- package/src/page/page-module.d.ts +46 -0
- package/src/page/page-module.js +74 -0
- package/src/plugin/react.constants.d.ts +1 -0
- package/src/plugin/react.constants.js +1 -0
- package/src/plugin/react.plugin.d.ts +71 -0
- package/src/plugin/react.plugin.js +210 -0
- package/src/plugin/react.types.d.ts +106 -0
- package/src/plugin/react.types.js +1 -0
- package/src/render/component-ownership.d.ts +49 -0
- package/src/render/component-ownership.js +47 -0
- package/src/render/component-ssr.d.ts +59 -0
- package/src/render/component-ssr.js +76 -0
- package/src/render/eco-embed.d.ts +11 -0
- package/src/render/eco-embed.js +9 -0
- package/src/render/errors.d.ts +16 -0
- package/src/render/errors.js +23 -0
- package/src/render/html-boundary.js +59 -0
- package/src/render/layout-compose.d.ts +66 -0
- package/src/render/layout-compose.js +134 -0
- package/src/render/react-renderer.d.ts +114 -0
- package/src/render/react-renderer.js +490 -0
- package/src/render/react-runtime.d.ts +13 -0
- package/src/render/react-runtime.js +26 -0
- package/src/render/unified-layout-composition.d.ts +22 -0
- package/src/render/unified-layout-composition.js +31 -0
- package/src/runtime/use-sync-external-store-with-selector.d.ts +3 -0
- package/src/runtime/use-sync-external-store-with-selector.js +54 -0
- package/CHANGELOG.md +0 -29
- package/src/react-hmr-strategy.d.ts +0 -148
- package/src/react-hmr-strategy.js +0 -313
- package/src/react-renderer.d.ts +0 -213
- package/src/react-renderer.js +0 -654
- package/src/react.plugin.d.ts +0 -147
- package/src/react.plugin.js +0 -146
- package/src/router-adapter.js +0 -0
- package/src/services/react-bundle.service.d.ts +0 -47
- package/src/services/react-bundle.service.js +0 -93
- package/src/services/react-hmr-page-metadata-cache.js +0 -35
- package/src/services/react-hydration-asset.service.d.ts +0 -76
- package/src/services/react-hydration-asset.service.js +0 -210
- package/src/services/react-page-module.service.d.ts +0 -83
- package/src/services/react-page-module.service.js +0 -162
- package/src/services/react-runtime-bundle.service.d.ts +0 -40
- package/src/services/react-runtime-bundle.service.js +0 -130
- package/src/utils/client-graph-boundary-plugin.d.ts +0 -43
- package/src/utils/client-graph-boundary-plugin.js +0 -494
- package/src/utils/client-only.js +0 -19
- package/src/utils/declared-modules.js +0 -59
- package/src/utils/dynamic.js +0 -12
- package/src/utils/hmr-scripts.d.ts +0 -18
- package/src/utils/hmr-scripts.js +0 -31
- package/src/utils/html-boundary.js +0 -55
- package/src/utils/hydration-scripts.d.ts +0 -86
- package/src/utils/hydration-scripts.js +0 -285
- package/src/utils/reachability-analyzer.d.ts +0 -66
- package/src/utils/reachability-analyzer.js +0 -339
- package/src/utils/react-dom-runtime-interop-plugin.js +0 -29
- package/src/utils/react-mdx-loader-plugin.d.ts +0 -3
- package/src/utils/react-mdx-loader-plugin.js +0 -45
- package/src/utils/react-runtime-specifier-map.d.ts +0 -6
- package/src/utils/react-runtime-specifier-map.js +0 -37
- package/src/utils/use-sync-external-store-shim-plugin.d.ts +0 -5
- package/src/utils/use-sync-external-store-shim-plugin.js +0 -41
- /package/src/{utils → app}/client-only.d.ts +0 -0
- /package/src/{utils → app}/dynamic.d.ts +0 -0
- /package/src/{declarations.d.ts → contracts/declarations.d.ts} +0 -0
- /package/src/{utils → render}/html-boundary.d.ts +0 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { getExportedName, isExplicitlyRequestedExport, } from './reachability-export-names.js';
|
|
2
|
+
function pushFunctionOrClassExportRoot(exportNode, declaration, potentialClientRoots, explicitlyRequestedExports) {
|
|
3
|
+
if (declaration?.type !== 'FunctionDeclaration' && declaration?.type !== 'ClassDeclaration') {
|
|
4
|
+
return false;
|
|
5
|
+
}
|
|
6
|
+
const declarationName = declaration.id?.name;
|
|
7
|
+
if (declarationName && isExplicitlyRequestedExport(declarationName, explicitlyRequestedExports)) {
|
|
8
|
+
potentialClientRoots.push(exportNode);
|
|
9
|
+
}
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
function hasRequestedVariableExport(declaration, explicitlyRequestedExports) {
|
|
13
|
+
return declaration.declarations.some((decl) => decl.id?.type === 'Identifier' && isExplicitlyRequestedExport(decl.id.name, explicitlyRequestedExports));
|
|
14
|
+
}
|
|
15
|
+
function hasRequestedExportSpecifier(exportNode, explicitlyRequestedExports) {
|
|
16
|
+
return (exportNode.specifiers?.some((specifier) => {
|
|
17
|
+
const exportedName = getExportedName(specifier);
|
|
18
|
+
return exportedName ? isExplicitlyRequestedExport(exportedName, explicitlyRequestedExports) : false;
|
|
19
|
+
}) ?? false);
|
|
20
|
+
}
|
|
21
|
+
function pushExportNamedFallbackRoots(exportNode, potentialClientRoots, explicitlyRequestedExports) {
|
|
22
|
+
if (exportNode.source && exportNode.specifiers?.length) {
|
|
23
|
+
if (hasRequestedExportSpecifier(exportNode, explicitlyRequestedExports)) {
|
|
24
|
+
potentialClientRoots.push(exportNode);
|
|
25
|
+
}
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const declaration = exportNode.declaration;
|
|
29
|
+
if (pushFunctionOrClassExportRoot(exportNode, declaration, potentialClientRoots, explicitlyRequestedExports)) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (declaration?.type === 'VariableDeclaration' &&
|
|
33
|
+
hasRequestedVariableExport(declaration, explicitlyRequestedExports)) {
|
|
34
|
+
potentialClientRoots.push(exportNode);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (hasRequestedExportSpecifier(exportNode, explicitlyRequestedExports)) {
|
|
38
|
+
potentialClientRoots.push(exportNode);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export function resolveFallbackClientRoots(program, potentialClientRoots, explicitlyRequestedExports) {
|
|
42
|
+
if (potentialClientRoots.length > 0) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
if (explicitlyRequestedExports) {
|
|
46
|
+
for (const node of program.body) {
|
|
47
|
+
if (node.type === 'ExportNamedDeclaration') {
|
|
48
|
+
pushExportNamedFallbackRoots(node, potentialClientRoots, explicitlyRequestedExports);
|
|
49
|
+
}
|
|
50
|
+
else if (node.type === 'ExportDefaultDeclaration') {
|
|
51
|
+
if (isExplicitlyRequestedExport('default', explicitlyRequestedExports)) {
|
|
52
|
+
potentialClientRoots.push(node);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else if (node.type === 'ExportAllDeclaration') {
|
|
56
|
+
if (explicitlyRequestedExports === '*') {
|
|
57
|
+
potentialClientRoots.push(node);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
for (const node of program.body) {
|
|
64
|
+
if (node.type === 'ExportNamedDeclaration' ||
|
|
65
|
+
node.type === 'ExportDefaultDeclaration' ||
|
|
66
|
+
node.type === 'ExportAllDeclaration') {
|
|
67
|
+
potentialClientRoots.push(node);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ExportNamedDeclaration } from 'oxc-parser';
|
|
2
|
+
export declare function isPreloadExportStatement(statement: ExportNamedDeclaration): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* Returns whether a module exports a named `preload` binding.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Hydration entries only wire `preload` when this is true, so Pages without
|
|
8
|
+
* that export do not produce a missing-named-import warning.
|
|
9
|
+
*/
|
|
10
|
+
export declare function hasPagePreloadExport(source: string, filename: string): boolean;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { parseModuleSource } from '@ecopages/core/build';
|
|
2
|
+
export function isPreloadExportStatement(statement) {
|
|
3
|
+
const declaration = statement.declaration;
|
|
4
|
+
if (declaration?.type === 'FunctionDeclaration' || declaration?.type === 'ClassDeclaration') {
|
|
5
|
+
return declaration.id?.type === 'Identifier' && declaration.id.name === 'preload';
|
|
6
|
+
}
|
|
7
|
+
if (declaration?.type === 'VariableDeclaration') {
|
|
8
|
+
return (declaration.declarations?.some((declarator) => declarator.id.type === 'Identifier' && declarator.id.name === 'preload') ?? false);
|
|
9
|
+
}
|
|
10
|
+
return statement.specifiers.some((specifier) => {
|
|
11
|
+
const exported = specifier.exported;
|
|
12
|
+
return exported.type === 'Identifier' ? exported.name === 'preload' : exported.value === 'preload';
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Returns whether a module exports a named `preload` binding.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* Hydration entries only wire `preload` when this is true, so Pages without
|
|
20
|
+
* that export do not produce a missing-named-import warning.
|
|
21
|
+
*/
|
|
22
|
+
export function hasPagePreloadExport(source, filename) {
|
|
23
|
+
try {
|
|
24
|
+
const { program } = parseModuleSource(filename, source);
|
|
25
|
+
return program.body.some((statement) => statement.type === 'ExportNamedDeclaration' && isPreloadExportStatement(statement));
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ParseResult } from 'oxc-parser';
|
|
2
|
+
import { type ClientRootScanState } from './reachability-client-roots.js';
|
|
3
|
+
export type TopLevelImportEntry = {
|
|
4
|
+
node: unknown;
|
|
5
|
+
specifier: string;
|
|
6
|
+
bindings: Map<string, string>;
|
|
7
|
+
isSideEffect: boolean;
|
|
8
|
+
};
|
|
9
|
+
export type TopLevelIndex = {
|
|
10
|
+
topLevelImports: TopLevelImportEntry[];
|
|
11
|
+
topLevelDeclarations: Map<string, unknown>;
|
|
12
|
+
clientRootState: ClientRootScanState;
|
|
13
|
+
};
|
|
14
|
+
export declare function buildTopLevelIndex(program: ParseResult['program']): TopLevelIndex;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { checkPotentialClientRoot, createClientRootScanState, } from './reachability-client-roots.js';
|
|
2
|
+
import { isPreloadExportStatement } from './reachability-preload.js';
|
|
3
|
+
function indexImportDeclaration(statement, topLevelImports) {
|
|
4
|
+
if (statement.importKind === 'type') {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
const specifier = statement.source.value;
|
|
8
|
+
const bindings = new Map();
|
|
9
|
+
if (!statement.specifiers || statement.specifiers.length === 0) {
|
|
10
|
+
topLevelImports.push({ node: statement, specifier, bindings, isSideEffect: true });
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
for (const spec of statement.specifiers) {
|
|
14
|
+
if (spec.type === 'ImportDefaultSpecifier') {
|
|
15
|
+
bindings.set(spec.local.name, 'default');
|
|
16
|
+
}
|
|
17
|
+
else if (spec.type === 'ImportNamespaceSpecifier') {
|
|
18
|
+
bindings.set(spec.local.name, '*');
|
|
19
|
+
}
|
|
20
|
+
else if (spec.type === 'ImportSpecifier') {
|
|
21
|
+
const importedName = spec.imported.type === 'Identifier' ? spec.imported.name : spec.imported.value;
|
|
22
|
+
bindings.set(spec.local.name, importedName);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
topLevelImports.push({ node: statement, specifier, bindings, isSideEffect: false });
|
|
26
|
+
}
|
|
27
|
+
function indexExportNamedDeclaration(statement, index) {
|
|
28
|
+
const decl = statement.declaration;
|
|
29
|
+
if (isPreloadExportStatement(statement)) {
|
|
30
|
+
index.clientRootState.pagePreloadRoots.push(statement);
|
|
31
|
+
}
|
|
32
|
+
if (!decl) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (decl.type === 'FunctionDeclaration' || decl.type === 'ClassDeclaration') {
|
|
36
|
+
if (decl.id?.type === 'Identifier') {
|
|
37
|
+
index.topLevelDeclarations.set(decl.id.name, statement);
|
|
38
|
+
}
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (decl.type === 'VariableDeclaration') {
|
|
42
|
+
for (const v of decl.declarations) {
|
|
43
|
+
if (v.id.type === 'Identifier') {
|
|
44
|
+
index.topLevelDeclarations.set(v.id.name, statement);
|
|
45
|
+
checkPotentialClientRoot(v.init, index.clientRootState);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export function buildTopLevelIndex(program) {
|
|
51
|
+
const index = {
|
|
52
|
+
topLevelImports: [],
|
|
53
|
+
topLevelDeclarations: new Map(),
|
|
54
|
+
clientRootState: createClientRootScanState(),
|
|
55
|
+
};
|
|
56
|
+
for (const statement of program.body) {
|
|
57
|
+
if (statement.type === 'ImportDeclaration') {
|
|
58
|
+
indexImportDeclaration(statement, index.topLevelImports);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (statement.type === 'VariableDeclaration') {
|
|
62
|
+
for (const decl of statement.declarations) {
|
|
63
|
+
if (decl.id.type === 'Identifier') {
|
|
64
|
+
index.topLevelDeclarations.set(decl.id.name, statement);
|
|
65
|
+
checkPotentialClientRoot(decl.init, index.clientRootState);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (statement.type === 'FunctionDeclaration' || statement.type === 'ClassDeclaration') {
|
|
71
|
+
if (statement.id?.type === 'Identifier') {
|
|
72
|
+
index.topLevelDeclarations.set(statement.id.name, statement);
|
|
73
|
+
}
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (statement.type === 'ExportNamedDeclaration') {
|
|
77
|
+
indexExportNamedDeclaration(statement, index);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (statement.type === 'ExportDefaultDeclaration') {
|
|
81
|
+
checkPotentialClientRoot(statement.declaration, index.clientRootState);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (statement.type === 'ExpressionStatement') {
|
|
85
|
+
checkPotentialClientRoot(statement.expression, index.clientRootState);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (statement.type === 'ExportAllDeclaration' && statement.source) {
|
|
89
|
+
index.clientRootState.potentialClientRoots.push(statement);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return index;
|
|
93
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type ExplicitlyRequestedExports } from './reachability-export-names.js';
|
|
2
|
+
import type { TopLevelImportEntry } from './reachability-top-level-index.js';
|
|
3
|
+
export type ReachabilityTraversalResult = {
|
|
4
|
+
reachableImports: Map<string, Set<string> | '*'>;
|
|
5
|
+
reachableDeclarations: Set<unknown>;
|
|
6
|
+
};
|
|
7
|
+
export declare function runReachabilityTraversal(roots: unknown[], topLevelImports: TopLevelImportEntry[], topLevelDeclarations: Map<string, unknown>, explicitlyRequestedExports?: ExplicitlyRequestedExports): ReachabilityTraversalResult;
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { getExportedName, getLocalExportName, getReexportedImportName, isExplicitlyRequestedExport, } from './reachability-export-names.js';
|
|
2
|
+
function markImportReachable(reachableImports, specifier, importedName) {
|
|
3
|
+
let current = reachableImports.get(specifier);
|
|
4
|
+
if (current === '*')
|
|
5
|
+
return;
|
|
6
|
+
if (importedName === '*') {
|
|
7
|
+
reachableImports.set(specifier, '*');
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
if (!current) {
|
|
11
|
+
current = new Set();
|
|
12
|
+
reachableImports.set(specifier, current);
|
|
13
|
+
}
|
|
14
|
+
current.add(importedName);
|
|
15
|
+
}
|
|
16
|
+
function createTraversalContext(topLevelImports, topLevelDeclarations, explicitlyRequestedExports) {
|
|
17
|
+
const reachableImports = new Map();
|
|
18
|
+
const reachableDeclarations = new Set();
|
|
19
|
+
const queue = [];
|
|
20
|
+
const visitedNodes = new Set();
|
|
21
|
+
function checkIdentifier(name) {
|
|
22
|
+
if (topLevelDeclarations.has(name)) {
|
|
23
|
+
const declNode = topLevelDeclarations.get(name);
|
|
24
|
+
if (!reachableDeclarations.has(declNode)) {
|
|
25
|
+
reachableDeclarations.add(declNode);
|
|
26
|
+
queue.push(declNode);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
for (const imp of topLevelImports) {
|
|
30
|
+
if (imp.bindings.has(name)) {
|
|
31
|
+
markImportReachable(reachableImports, imp.specifier, imp.bindings.get(name));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
reachableImports,
|
|
37
|
+
reachableDeclarations,
|
|
38
|
+
queue,
|
|
39
|
+
visitedNodes,
|
|
40
|
+
checkIdentifier,
|
|
41
|
+
explicitlyRequestedExports,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function visitExportAll(node, ctx) {
|
|
45
|
+
if (typeof node.source?.value !== 'string')
|
|
46
|
+
return false;
|
|
47
|
+
markImportReachable(ctx.reachableImports, node.source.value, '*');
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
function visitExportNamed(node, ctx, localScope) {
|
|
51
|
+
if (typeof node.source?.value === 'string') {
|
|
52
|
+
for (const specifier of node.specifiers ?? []) {
|
|
53
|
+
const importedName = getReexportedImportName(specifier);
|
|
54
|
+
if (importedName) {
|
|
55
|
+
markImportReachable(ctx.reachableImports, node.source.value, importedName);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
if (node.source || !ctx.explicitlyRequestedExports || !node.specifiers?.length) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
for (const specifier of node.specifiers) {
|
|
64
|
+
const exportedName = getExportedName(specifier);
|
|
65
|
+
if (!exportedName || !isExplicitlyRequestedExport(exportedName, ctx.explicitlyRequestedExports)) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const localName = getLocalExportName(specifier);
|
|
69
|
+
if (localName && !localScope.has(localName)) {
|
|
70
|
+
ctx.checkIdentifier(localName);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
function visitIdentifier(node, ctx, localScope) {
|
|
76
|
+
if (!localScope.has(node.name)) {
|
|
77
|
+
ctx.checkIdentifier(node.name);
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
function visitJsxIdentifier(node, ctx, localScope) {
|
|
82
|
+
if (/^[A-Z]/.test(node.name) && !localScope.has(node.name)) {
|
|
83
|
+
ctx.checkIdentifier(node.name);
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
function visitMemberExpression(node, ctx, localScope) {
|
|
88
|
+
traverseNode(node.object, ctx, localScope);
|
|
89
|
+
if (node.computed) {
|
|
90
|
+
traverseNode(node.property, ctx, localScope);
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
function visitProperty(node, ctx, localScope) {
|
|
95
|
+
if (node.computed)
|
|
96
|
+
traverseNode(node.key, ctx, localScope);
|
|
97
|
+
traverseNode(node.value, ctx, localScope);
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
function visitJsxElement(node, ctx, localScope) {
|
|
101
|
+
traverseNode(node.name, ctx, localScope);
|
|
102
|
+
if (node.attributes) {
|
|
103
|
+
for (const attr of node.attributes)
|
|
104
|
+
traverseNode(attr, ctx, localScope);
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
function visitJsxMemberExpression(node, ctx, localScope) {
|
|
109
|
+
traverseNode(node.object, ctx, localScope);
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
function visitCallExpression(node, ctx) {
|
|
113
|
+
if (node.callee?.type !== 'Identifier') {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
if (node.callee.name === 'dynamic') {
|
|
117
|
+
const arg = node.arguments[0];
|
|
118
|
+
if (arg && (arg.type === 'ArrowFunctionExpression' || arg.type === 'FunctionExpression')) {
|
|
119
|
+
const body = arg.body;
|
|
120
|
+
if (body.type === 'ImportExpression' && body.source.type === 'Literal') {
|
|
121
|
+
markImportReachable(ctx.reachableImports, body.source.value, '*');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
if (node.callee.name === 'require') {
|
|
127
|
+
const arg = node.arguments?.[0];
|
|
128
|
+
if (arg && (arg.type === 'StringLiteral' || arg.type === 'Literal') && typeof arg.value === 'string') {
|
|
129
|
+
markImportReachable(ctx.reachableImports, arg.value, '*');
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
function visitImportExpression(node, ctx) {
|
|
135
|
+
if (node.source.type === 'Literal') {
|
|
136
|
+
markImportReachable(ctx.reachableImports, node.source.value, '*');
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
function visitFunctionNode(node, ctx, localScope) {
|
|
141
|
+
const newScope = new Set(localScope);
|
|
142
|
+
if (node.id?.type === 'Identifier')
|
|
143
|
+
newScope.add(node.id.name);
|
|
144
|
+
if (node.params?.items) {
|
|
145
|
+
for (const p of node.params.items) {
|
|
146
|
+
if (p.pattern?.type === 'Identifier') {
|
|
147
|
+
newScope.add(p.pattern.name);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
traverseNode(node.body, ctx, newScope);
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
const NODE_VISITORS = new Map([
|
|
155
|
+
['ExportAllDeclaration', visitExportAll],
|
|
156
|
+
['ExportNamedDeclaration', visitExportNamed],
|
|
157
|
+
['Identifier', visitIdentifier],
|
|
158
|
+
['JSXIdentifier', visitJsxIdentifier],
|
|
159
|
+
['MemberExpression', visitMemberExpression],
|
|
160
|
+
['Property', visitProperty],
|
|
161
|
+
['JSXOpeningElement', visitJsxElement],
|
|
162
|
+
['JSXClosingElement', visitJsxElement],
|
|
163
|
+
['JSXMemberExpression', visitJsxMemberExpression],
|
|
164
|
+
['CallExpression', visitCallExpression],
|
|
165
|
+
['ImportExpression', visitImportExpression],
|
|
166
|
+
['ArrowFunctionExpression', visitFunctionNode],
|
|
167
|
+
['FunctionExpression', visitFunctionNode],
|
|
168
|
+
['FunctionDeclaration', visitFunctionNode],
|
|
169
|
+
]);
|
|
170
|
+
function traverseNode(node, ctx, localScope) {
|
|
171
|
+
if (!node || typeof node !== 'object')
|
|
172
|
+
return;
|
|
173
|
+
if (ctx.visitedNodes.has(node))
|
|
174
|
+
return;
|
|
175
|
+
ctx.visitedNodes.add(node);
|
|
176
|
+
if (Array.isArray(node)) {
|
|
177
|
+
for (const child of node)
|
|
178
|
+
traverseNode(child, ctx, localScope);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const visitor = NODE_VISITORS.get(node.type);
|
|
182
|
+
if (visitor?.(node, ctx, localScope))
|
|
183
|
+
return;
|
|
184
|
+
for (const key in node) {
|
|
185
|
+
if (key !== 'type' && key !== 'start' && key !== 'end') {
|
|
186
|
+
traverseNode(node[key], ctx, localScope);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
export function runReachabilityTraversal(roots, topLevelImports, topLevelDeclarations, explicitlyRequestedExports) {
|
|
191
|
+
const ctx = createTraversalContext(topLevelImports, topLevelDeclarations, explicitlyRequestedExports);
|
|
192
|
+
ctx.queue.push(...roots);
|
|
193
|
+
while (ctx.queue.length > 0) {
|
|
194
|
+
const root = ctx.queue.shift();
|
|
195
|
+
traverseNode(root, ctx, new Set());
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
reachableImports: ctx.reachableImports,
|
|
199
|
+
reachableDeclarations: ctx.reachableDeclarations,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Specifier classification and requested-export registry helpers for the client graph boundary.
|
|
3
|
+
*/
|
|
4
|
+
export type RequestedExportRules = Set<string> | '*';
|
|
5
|
+
export declare function isBareSpecifier(specifier: string): boolean;
|
|
6
|
+
export declare function isProjectAliasSpecifier(specifier: string, projectRoot?: string): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Determines whether a specifier should be treated as server-only.
|
|
9
|
+
*
|
|
10
|
+
* This covers Node built-ins, content collection `/server` virtual modules,
|
|
11
|
+
* and local module conventions such as `.server.ts` and extensionless imports
|
|
12
|
+
* that resolve to `.server.*` files.
|
|
13
|
+
*
|
|
14
|
+
* @param specifier - Raw import specifier from the module source.
|
|
15
|
+
* @returns True when the import must never become client-reachable.
|
|
16
|
+
*/
|
|
17
|
+
export declare function isServerOnlySpecifier(specifier: string): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Strips down a deep path module specifier to its foundational root package name.
|
|
20
|
+
*
|
|
21
|
+
* When checking against the "allowed modules" whitelist, a component might import something deeply
|
|
22
|
+
* nested like `lodash/fp/map` or `@myorg/ui/button`. However, the user configuration only whitelists
|
|
23
|
+
* the root `lodash` or `@myorg/ui` package. This normalizer ensures we are comparing apples to apples
|
|
24
|
+
* by extracting the base package scope before checking the authorization list.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* toModuleBaseSpecifier('@scope/package/deep/file') -> '@scope/package'
|
|
28
|
+
* toModuleBaseSpecifier('lodash/cloneDeep') -> 'lodash'
|
|
29
|
+
* toModuleBaseSpecifier('node:fs') -> 'node:fs'
|
|
30
|
+
*
|
|
31
|
+
* @param specifier - The raw import specifier from the code.
|
|
32
|
+
* @returns The root package name, preserving scoped npm organizations.
|
|
33
|
+
*/
|
|
34
|
+
export declare function toModuleBaseSpecifier(specifier: string): string;
|
|
35
|
+
/**
|
|
36
|
+
* Parses the grammar syntax of declared modules.
|
|
37
|
+
* Handles patterns like `@pkg/name` and `@pkg/name{namedImport,anotherImport}`
|
|
38
|
+
* returning a map of base packages to their explicitly allowed specifiers.
|
|
39
|
+
*
|
|
40
|
+
* @param moduleDeclarations - A list of module declaration strings.
|
|
41
|
+
* @returns A structured map of allowed packages and their named exports.
|
|
42
|
+
*/
|
|
43
|
+
export declare function parseDeclaredModules(moduleDeclarations: readonly string[] | undefined): Map<string, Set<string> | '*'>;
|
|
44
|
+
/**
|
|
45
|
+
* Merges two declared module maps, combining their allowable specific scopes.
|
|
46
|
+
*
|
|
47
|
+
* @param a - First map of declared modules.
|
|
48
|
+
* @param b - Second map of declared modules to merge into the first.
|
|
49
|
+
* @returns A unified map of both declarations.
|
|
50
|
+
*/
|
|
51
|
+
export declare function mergeDeclaredModulesMap(a: Map<string, Set<string> | '*'>, b: Map<string, Set<string> | '*'>): Map<string, Set<string> | '*'>;
|
|
52
|
+
export declare function normalizeRequestedExportsKey(pathname: string): string;
|
|
53
|
+
/**
|
|
54
|
+
* Resolves a local import specifier into a requested-export registry key.
|
|
55
|
+
*
|
|
56
|
+
* Bare package specifiers and project aliases are intentionally ignored because
|
|
57
|
+
* requested-export propagation is only used for cross-file local reachability.
|
|
58
|
+
*
|
|
59
|
+
* @param importer - Absolute path of the importing module.
|
|
60
|
+
* @param specifier - Raw import or re-export specifier.
|
|
61
|
+
* @returns Registry key for a local dependency, or `undefined` when not applicable.
|
|
62
|
+
*/
|
|
63
|
+
export declare function resolveRequestedExportsKey(importer: string, specifier: string): string | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* Merges newly discovered requested-export rules into the local propagation registry.
|
|
66
|
+
*
|
|
67
|
+
* Once a module is promoted to `'*'`, it stays fully reachable for the remainder
|
|
68
|
+
* of the transform pass.
|
|
69
|
+
*
|
|
70
|
+
* @param registry - Cross-module requested-export registry.
|
|
71
|
+
* @param moduleKey - Normalized local module key.
|
|
72
|
+
* @param rules - Newly observed reachable export rules for the module.
|
|
73
|
+
*/
|
|
74
|
+
export declare function mergeRequestedExportRules(registry: Map<string, RequestedExportRules>, moduleKey: string, rules: Set<string> | '*'): void;
|