@ecopages/react 0.2.0-rc.6 → 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/README.md +21 -4
- package/package.json +5 -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/island-hydration.d.ts +82 -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/hydration-asset.d.ts +20 -1
- package/src/hydration/hydration-asset.js +42 -24
- package/src/hydration/hydration-lifecycle.test.browser.d.ts +1 -0
- package/src/hydration/hydration-lifecycle.test.browser.js +409 -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 +48 -0
- package/src/hydration/hydration-script-compiler.js +213 -0
- package/src/hydration/page-data-reader.d.ts +0 -14
- package/src/hydration/page-data-reader.js +1 -47
- package/src/render/component-ssr.d.ts +17 -0
- package/src/render/component-ssr.js +19 -1
- package/src/render/react-renderer.d.ts +2 -1
- package/src/render/react-renderer.js +5 -1
- package/src/render/unified-layout-composition.d.ts +2 -0
- package/src/render/unified-layout-composition.js +5 -1
- package/src/hydration/hydration-scripts.d.ts +0 -112
- package/src/hydration/hydration-scripts.js +0 -517
- package/src/hydration/hydration-scripts.test.browser.js +0 -199
- /package/src/hydration/{hydration-scripts.test.browser.d.ts → compiled-page-hydration-options.js} +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/cache';
|
|
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,34 @@
|
|
|
1
|
+
import type { HmrAction } from '@ecopages/core/hmr/hmr-strategy';
|
|
2
|
+
import type { ReactHmrBuildTarget } from './react-hmr-dev-transform-plugins.js';
|
|
3
|
+
export type HmrProcessContext = {
|
|
4
|
+
resolvedFilePath: string;
|
|
5
|
+
watchedFiles: Map<string, string>;
|
|
6
|
+
isLayoutFile: (filePath: string) => boolean;
|
|
7
|
+
isPageEntrypoint: (filePath: string) => boolean;
|
|
8
|
+
isReactEntrypoint: (filePath: string) => boolean;
|
|
9
|
+
ownsWatchedEntrypoint: (filePath: string) => boolean;
|
|
10
|
+
isDevTransformOutputUrl: (outputUrl: string) => boolean;
|
|
11
|
+
collectReactPageBuildTargets: () => Promise<ReactHmrBuildTarget[]>;
|
|
12
|
+
hasLayoutOwnedDependencyTarget: (changedFilePath: string, requestedTargets: ReactHmrBuildTarget[]) => Promise<boolean>;
|
|
13
|
+
resolveBuildTargets: (requestedTargets: ReactHmrBuildTarget[], changedFilePath: string) => Promise<ReactHmrBuildTarget[]>;
|
|
14
|
+
partitionBuildTargets: (requestedTargets: ReactHmrBuildTarget[], groupedPageTargets: ReactHmrBuildTarget[]) => {
|
|
15
|
+
pageTargets: ReactHmrBuildTarget[];
|
|
16
|
+
nonPageTargets: ReactHmrBuildTarget[];
|
|
17
|
+
};
|
|
18
|
+
queueDevTransformOutputUpdates: (targets: readonly ReactHmrBuildTarget[], requestedOutputUrls: ReadonlySet<string>, updates: string[]) => void;
|
|
19
|
+
markOwnedEntrypoint: (filePath: string) => void;
|
|
20
|
+
getDependencyEntrypoints: (filePath: string) => Set<string>;
|
|
21
|
+
};
|
|
22
|
+
export type DependencyScanResult = {
|
|
23
|
+
affectedEntrypoints: Map<string, string>;
|
|
24
|
+
hasOwnedLayoutDependencyHit: boolean;
|
|
25
|
+
shouldAbort: boolean;
|
|
26
|
+
};
|
|
27
|
+
export declare function scanDependencyHits(ctx: HmrProcessContext, changedEntrypointOutput?: string): DependencyScanResult;
|
|
28
|
+
export declare function resolveRequestedTargets(ctx: HmrProcessContext, changedEntrypointOutput: string | undefined, isLayout: boolean, isChangedPageEntrypoint: boolean, hasOwnedLayoutDependencyHit: boolean, affectedEntrypoints: Map<string, string>, hasDependencyHits: boolean): Promise<{
|
|
29
|
+
requestedTargets: ReactHmrBuildTarget[];
|
|
30
|
+
hasLayoutOwnedRequestedTarget: boolean;
|
|
31
|
+
}>;
|
|
32
|
+
export declare function collectHmrUpdateUrls(ctx: HmrProcessContext, requestedTargets: ReactHmrBuildTarget[], pageTargets: ReactHmrBuildTarget[], nonPageTargets: ReactHmrBuildTarget[]): string[];
|
|
33
|
+
export declare function buildHmrAction(requiresLayoutRefresh: boolean, updates: string[]): HmrAction;
|
|
34
|
+
export declare function ensureWatchedEntrypointOwnership(ctx: HmrProcessContext, changedEntrypointOutput: string | undefined): 'continue' | 'abort';
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
export function scanDependencyHits(ctx, changedEntrypointOutput) {
|
|
3
|
+
const dependencyHits = ctx.getDependencyEntrypoints(ctx.resolvedFilePath);
|
|
4
|
+
const affectedEntrypoints = new Map();
|
|
5
|
+
let hasOwnedLayoutDependencyHit = false;
|
|
6
|
+
if (dependencyHits.size === 0 || changedEntrypointOutput) {
|
|
7
|
+
return { affectedEntrypoints, hasOwnedLayoutDependencyHit, shouldAbort: false };
|
|
8
|
+
}
|
|
9
|
+
for (const entrypoint of dependencyHits) {
|
|
10
|
+
const resolvedEntrypoint = path.resolve(entrypoint);
|
|
11
|
+
const outputUrl = ctx.watchedFiles.get(resolvedEntrypoint);
|
|
12
|
+
if (outputUrl && (ctx.ownsWatchedEntrypoint(resolvedEntrypoint) || ctx.isDevTransformOutputUrl(outputUrl))) {
|
|
13
|
+
affectedEntrypoints.set(resolvedEntrypoint, outputUrl);
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
if (ctx.isLayoutFile(resolvedEntrypoint) && ctx.ownsWatchedEntrypoint(resolvedEntrypoint)) {
|
|
17
|
+
hasOwnedLayoutDependencyHit = true;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const shouldAbort = affectedEntrypoints.size === 0 && !hasOwnedLayoutDependencyHit && !ctx.isLayoutFile(ctx.resolvedFilePath);
|
|
21
|
+
return { affectedEntrypoints, hasOwnedLayoutDependencyHit, shouldAbort };
|
|
22
|
+
}
|
|
23
|
+
export async function resolveRequestedTargets(ctx, changedEntrypointOutput, isLayout, isChangedPageEntrypoint, hasOwnedLayoutDependencyHit, affectedEntrypoints, hasDependencyHits) {
|
|
24
|
+
let layoutOwnedPageTargets = [];
|
|
25
|
+
let hasLayoutOwnedRequestedTarget = false;
|
|
26
|
+
if (changedEntrypointOutput && !isLayout && !isChangedPageEntrypoint) {
|
|
27
|
+
layoutOwnedPageTargets = await ctx.collectReactPageBuildTargets();
|
|
28
|
+
hasLayoutOwnedRequestedTarget = await ctx.hasLayoutOwnedDependencyTarget(ctx.resolvedFilePath, layoutOwnedPageTargets);
|
|
29
|
+
}
|
|
30
|
+
const requestedTargets = changedEntrypointOutput
|
|
31
|
+
? hasLayoutOwnedRequestedTarget
|
|
32
|
+
? [{ entrypointPath: ctx.resolvedFilePath, outputUrl: changedEntrypointOutput }, ...layoutOwnedPageTargets]
|
|
33
|
+
: [{ entrypointPath: ctx.resolvedFilePath, outputUrl: changedEntrypointOutput }]
|
|
34
|
+
: hasOwnedLayoutDependencyHit
|
|
35
|
+
? await ctx.collectReactPageBuildTargets()
|
|
36
|
+
: hasDependencyHits
|
|
37
|
+
? Array.from(affectedEntrypoints, ([entrypointPath, outputUrl]) => ({ entrypointPath, outputUrl }))
|
|
38
|
+
: Array.from(ctx.watchedFiles, ([entrypointPath, outputUrl]) => ({ entrypointPath, outputUrl }));
|
|
39
|
+
return { requestedTargets, hasLayoutOwnedRequestedTarget };
|
|
40
|
+
}
|
|
41
|
+
export function collectHmrUpdateUrls(ctx, requestedTargets, pageTargets, nonPageTargets) {
|
|
42
|
+
const updates = [];
|
|
43
|
+
const requestedOutputUrls = new Set(requestedTargets.map((target) => target.outputUrl));
|
|
44
|
+
ctx.queueDevTransformOutputUpdates(pageTargets, requestedOutputUrls, updates);
|
|
45
|
+
for (const { outputUrl } of nonPageTargets) {
|
|
46
|
+
if (!requestedOutputUrls.has(outputUrl)) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (ctx.isDevTransformOutputUrl(outputUrl)) {
|
|
50
|
+
updates.push(outputUrl);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return updates;
|
|
54
|
+
}
|
|
55
|
+
export function buildHmrAction(requiresLayoutRefresh, updates) {
|
|
56
|
+
if (requiresLayoutRefresh) {
|
|
57
|
+
return {
|
|
58
|
+
type: 'broadcast',
|
|
59
|
+
events: [{ type: 'layout-update' }],
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if (updates.length > 0) {
|
|
63
|
+
return {
|
|
64
|
+
type: 'broadcast',
|
|
65
|
+
events: updates.map((path) => ({
|
|
66
|
+
type: 'update',
|
|
67
|
+
path,
|
|
68
|
+
timestamp: Date.now(),
|
|
69
|
+
})),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return { type: 'none' };
|
|
73
|
+
}
|
|
74
|
+
export function ensureWatchedEntrypointOwnership(ctx, changedEntrypointOutput) {
|
|
75
|
+
if (!changedEntrypointOutput || ctx.ownsWatchedEntrypoint(ctx.resolvedFilePath)) {
|
|
76
|
+
return 'continue';
|
|
77
|
+
}
|
|
78
|
+
if (ctx.isReactEntrypoint(ctx.resolvedFilePath)) {
|
|
79
|
+
ctx.markOwnedEntrypoint(ctx.resolvedFilePath);
|
|
80
|
+
return 'continue';
|
|
81
|
+
}
|
|
82
|
+
return 'abort';
|
|
83
|
+
}
|