@ecopages/react 0.2.0-beta.4 → 0.2.0-beta.41
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 +175 -7
- package/package.json +54 -29
- package/src/app/client-only.js +15 -0
- package/src/app/dynamic.js +9 -0
- package/src/app/dynamic.test.browser.js +31 -0
- package/src/bundling/bootstrap-package-paths.d.ts +5 -0
- package/src/bundling/bootstrap-package-paths.js +5 -0
- package/src/{services/react-bundle.service.d.ts → bundling/bundle.d.ts} +23 -8
- package/src/bundling/bundle.js +120 -0
- package/src/bundling/discover-layout-runtime-modules.d.ts +32 -0
- package/src/bundling/discover-layout-runtime-modules.js +191 -0
- package/src/bundling/runtime-alias-map.d.ts +13 -0
- package/src/bundling/runtime-alias-map.js +113 -0
- package/src/{services/react-runtime-bundle.service.d.ts → bundling/runtime-bundle.d.ts} +30 -5
- package/src/bundling/runtime-bundle.js +258 -0
- package/src/bundling/runtime-interop-plugin.js +34 -0
- package/src/bundling/runtime-mode.d.ts +9 -0
- package/src/bundling/runtime-mode.js +11 -0
- package/src/bundling/runtime-modules.d.ts +29 -0
- package/src/bundling/runtime-modules.js +49 -0
- package/src/client-graph/ast-transform.d.ts +18 -0
- package/src/client-graph/ast-transform.js +426 -0
- package/src/{utils/client-graph-boundary-cache.d.ts → client-graph/boundary-cache.d.ts} +23 -13
- package/src/client-graph/boundary-cache.js +182 -0
- package/src/{utils/client-graph-boundary-plugin.d.ts → client-graph/boundary-plugin.d.ts} +4 -13
- package/src/client-graph/boundary-plugin.js +149 -0
- package/src/client-graph/component-config-traversal.js +77 -0
- 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/{utils → client-graph}/reachability-analyzer.d.ts +4 -12
- package/src/client-graph/reachability-analyzer.js +515 -0
- package/src/client-graph/specifier-classification.d.ts +75 -0
- package/src/client-graph/specifier-classification.js +223 -0
- package/src/contracts/router-adapter.js +6 -0
- package/src/dev-transform/react-dev-transform-contributor.d.ts +17 -0
- package/src/dev-transform/react-dev-transform-contributor.js +22 -0
- package/src/hmr/hmr-scripts.js +44 -0
- package/src/hmr/hmr-strategy.d.ts +74 -0
- package/src/hmr/hmr-strategy.js +351 -0
- package/src/{services/react-hmr-page-metadata-cache.d.ts → hmr/page-metadata-cache.d.ts} +5 -1
- package/src/hmr/page-metadata-cache.js +44 -0
- package/src/hmr/react-hmr-dev-transform-plugins.d.ts +23 -0
- package/src/hmr/react-hmr-dev-transform-plugins.js +20 -0
- package/src/hydration/assert-no-bare-ecopages-imports.d.ts +4 -0
- package/src/hydration/assert-no-bare-ecopages-imports.js +7 -0
- package/src/hydration/bootstrap-imports.d.ts +25 -0
- package/src/hydration/bootstrap-imports.js +33 -0
- package/src/hydration/hydration-asset.d.ts +84 -0
- package/src/hydration/hydration-asset.js +227 -0
- package/src/hydration/hydration-scripts.d.ts +104 -0
- package/src/hydration/hydration-scripts.js +483 -0
- package/src/hydration/hydration-scripts.test.browser.js +199 -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 +31 -0
- package/src/hydration/page-data-reader.js +77 -0
- package/src/hydration/page-payload.d.ts +67 -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/{services/react-mdx-config-dependency.service.d.ts → mdx/mdx-config-dependency.d.ts} +6 -6
- package/src/mdx/mdx-config-dependency.js +110 -0
- package/src/mdx/mdx-loader-plugin.d.ts +2 -0
- package/src/mdx/mdx-loader-plugin.js +8 -0
- package/src/page/page-module.d.ts +46 -0
- package/src/page/page-module.js +74 -0
- package/src/plugin/react.constants.js +1 -0
- package/src/{react.plugin.d.ts → plugin/react.plugin.d.ts} +9 -3
- package/src/plugin/react.plugin.js +241 -0
- package/src/plugin/react.types.d.ts +152 -0
- package/src/plugin/react.types.js +1 -0
- package/src/render/component-ownership.d.ts +49 -0
- package/src/render/component-ownership.js +46 -0
- package/src/render/component-ssr.d.ts +113 -0
- package/src/render/component-ssr.js +180 -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 +138 -0
- package/src/render/react-renderer.js +448 -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 +53 -0
- package/src/render/unified-layout-composition.js +78 -0
- package/src/runtime/use-sync-external-store-with-selector.js +52 -54
- package/src/eco-embed.js +0 -11
- package/src/react-hmr-strategy.d.ts +0 -230
- package/src/react-hmr-strategy.js +0 -657
- package/src/react-renderer.d.ts +0 -233
- package/src/react-renderer.js +0 -622
- package/src/react.constants.js +0 -4
- package/src/react.plugin.js +0 -205
- package/src/react.types.d.ts +0 -88
- package/src/react.types.js +0 -0
- package/src/router-adapter.js +0 -0
- package/src/services/pages-index.d.ts +0 -64
- package/src/services/pages-index.js +0 -73
- package/src/services/react-bundle.service.js +0 -86
- package/src/services/react-hmr-page-metadata-cache.js +0 -35
- package/src/services/react-hydration-asset.service.d.ts +0 -84
- package/src/services/react-hydration-asset.service.js +0 -217
- package/src/services/react-mdx-config-dependency.service.js +0 -122
- package/src/services/react-page-module.service.d.ts +0 -88
- package/src/services/react-page-module.service.js +0 -166
- package/src/services/react-page-payload.service.d.ts +0 -46
- package/src/services/react-page-payload.service.js +0 -67
- package/src/services/react-runtime-bundle.service.js +0 -186
- package/src/utils/client-graph-boundary-cache.js +0 -116
- package/src/utils/client-graph-boundary-plugin.js +0 -552
- package/src/utils/client-only.js +0 -19
- package/src/utils/component-config-traversal.js +0 -54
- package/src/utils/declared-modules.js +0 -47
- package/src/utils/dynamic.js +0 -12
- package/src/utils/dynamic.test.browser.js +0 -33
- package/src/utils/hmr-scripts.js +0 -31
- package/src/utils/html-boundary.js +0 -55
- package/src/utils/hydration-scripts.d.ts +0 -92
- package/src/utils/hydration-scripts.js +0 -358
- package/src/utils/hydration-scripts.test.browser.js +0 -198
- package/src/utils/reachability-analyzer.js +0 -339
- package/src/utils/react-dom-runtime-interop-plugin.js +0 -38
- 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-alias-map.d.ts +0 -8
- package/src/utils/react-runtime-alias-map.js +0 -90
- /package/src/{utils → app}/client-only.d.ts +0 -0
- /package/src/{utils → app}/dynamic.d.ts +0 -0
- /package/src/{utils → app}/dynamic.test.browser.d.ts +0 -0
- /package/src/{utils/react-dom-runtime-interop-plugin.d.ts → bundling/runtime-interop-plugin.d.ts} +0 -0
- /package/src/{utils → client-graph}/component-config-traversal.d.ts +0 -0
- /package/src/{utils → client-graph}/declared-modules.d.ts +0 -0
- /package/src/{declarations.d.ts → contracts/declarations.d.ts} +0 -0
- /package/src/{router-adapter.d.ts → contracts/router-adapter.d.ts} +0 -0
- /package/src/{utils → hmr}/hmr-scripts.d.ts +0 -0
- /package/src/{utils → hydration}/hydration-scripts.test.browser.d.ts +0 -0
- /package/src/{react.constants.d.ts → plugin/react.constants.d.ts} +0 -0
- /package/src/{eco-embed.d.ts → render/eco-embed.d.ts} +0 -0
- /package/src/{utils → render}/html-boundary.d.ts +0 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-app persistent cache for `client-graph-boundary` plugin transforms.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* The plugin's `transformModuleImports` function is the second most
|
|
6
|
+
* expensive operation in the React build path (after `parseSync`).
|
|
7
|
+
* It walks the AST to find reachable exports and prunes forbidden
|
|
8
|
+
* imports. On a no-op rebuild (file unchanged, no plugin signature
|
|
9
|
+
* change) the entire walk can be skipped if the inputs are identical.
|
|
10
|
+
*
|
|
11
|
+
* This cache is owned by the {@link ReactPlugin} for the app's lifetime.
|
|
12
|
+
* It persists across HMR rebuilds but is invalidated per-file when the
|
|
13
|
+
* watcher detects a source change. The `requestedExports` registry
|
|
14
|
+
* remains per-build (it's mutated during cross-file propagation and
|
|
15
|
+
* must not accumulate stale state across builds).
|
|
16
|
+
*
|
|
17
|
+
* The cache is content-hashed (via `rapidhash`) so a `touch`/`utimes`
|
|
18
|
+
* does not invalidate a still-valid entry.
|
|
19
|
+
*
|
|
20
|
+
* Cache identity is `(filePath, source, allowListRules, inboundRules)`.
|
|
21
|
+
* Allow-list hashing includes each package's permitted exports, not just
|
|
22
|
+
* package names — otherwise `node:fs{readFileSync}` and
|
|
23
|
+
* `node:fs{writeFileSync}` would collide.
|
|
24
|
+
*
|
|
25
|
+
* Modules that inline external file contents (`readFileSync` rewrite) are
|
|
26
|
+
* not stored: their output depends on files outside the importer source.
|
|
27
|
+
*
|
|
28
|
+
* **`rulesAdded` semantics:** the map holds the **after-state** of
|
|
29
|
+
* every registry key this transform touched — both newly added keys
|
|
30
|
+
* and keys that were grown (Set union) or promoted to `'*'`. On
|
|
31
|
+
* replay the cache applies each entry via the same merge rules the
|
|
32
|
+
* live transform uses (`mergeRequestedExportRules`), so a key that
|
|
33
|
+
* was already in the live registry when the transform ran and grew
|
|
34
|
+
* during the transform is correctly grown on replay against a fresh
|
|
35
|
+
* registry as well.
|
|
36
|
+
*/
|
|
37
|
+
import { rapidhash } from '@ecopages/core/utils/hash';
|
|
38
|
+
const DEFAULT_MAX_ENTRIES = 5_000;
|
|
39
|
+
/**
|
|
40
|
+
* Clone a single rule value: `'*'` is a singleton, Sets get a shallow copy.
|
|
41
|
+
*/
|
|
42
|
+
function cloneRules(rules) {
|
|
43
|
+
return rules instanceof Set ? new Set(rules) : rules;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* LRU-bounded cache for client-graph-boundary transform results.
|
|
47
|
+
*
|
|
48
|
+
* One instance is owned by the React plugin for the app's lifetime and
|
|
49
|
+
* shared across all builds. The cache key is
|
|
50
|
+
* `(filePath, source, allowListRules, inboundRules)` so a file whose
|
|
51
|
+
* source, allow-list export rules, or inbound requested exports change
|
|
52
|
+
* gets a fresh entry.
|
|
53
|
+
*/
|
|
54
|
+
export class ClientGraphBoundaryCache {
|
|
55
|
+
entries = new Map();
|
|
56
|
+
maxEntries;
|
|
57
|
+
hits = 0;
|
|
58
|
+
misses = 0;
|
|
59
|
+
constructor(maxEntries = DEFAULT_MAX_ENTRIES) {
|
|
60
|
+
if (maxEntries <= 0) {
|
|
61
|
+
throw new Error(`ClientGraphBoundaryCache: maxEntries must be > 0, got ${maxEntries}`);
|
|
62
|
+
}
|
|
63
|
+
this.maxEntries = maxEntries;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Look up a cached transform for `filePath`.
|
|
67
|
+
*
|
|
68
|
+
* Returns `undefined` if the source, allow-list rules, or inbound
|
|
69
|
+
* requested exports have changed since the entry was stored.
|
|
70
|
+
*/
|
|
71
|
+
get(filePath, source, allowList, inboundRules) {
|
|
72
|
+
const sourceHash = rapidhash(source);
|
|
73
|
+
const allowListHash = hashAllowList(allowList);
|
|
74
|
+
const inboundRulesHash = hashRequestedExportRules(inboundRules);
|
|
75
|
+
const existing = this.entries.get(filePath);
|
|
76
|
+
if (existing &&
|
|
77
|
+
existing.sourceHash === sourceHash &&
|
|
78
|
+
existing.allowListHash === allowListHash &&
|
|
79
|
+
existing.inboundRulesHash === inboundRulesHash) {
|
|
80
|
+
this.hits += 1;
|
|
81
|
+
this.entries.delete(filePath);
|
|
82
|
+
this.entries.set(filePath, existing);
|
|
83
|
+
return existing;
|
|
84
|
+
}
|
|
85
|
+
this.misses += 1;
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Store a transform result.
|
|
90
|
+
*
|
|
91
|
+
* Evicts the least recently used entry if the cache exceeds its
|
|
92
|
+
* capacity. Defensively copies any `Set`-typed rules in `rulesAdded`
|
|
93
|
+
* so later mutations to the caller's sets cannot corrupt the cache.
|
|
94
|
+
*/
|
|
95
|
+
set(filePath, source, allowList, entry, inboundRules) {
|
|
96
|
+
const sourceHash = rapidhash(source);
|
|
97
|
+
const allowListHash = hashAllowList(allowList);
|
|
98
|
+
const inboundRulesHash = hashRequestedExportRules(inboundRules);
|
|
99
|
+
const rulesAddedCopy = new Map();
|
|
100
|
+
for (const [key, rules] of entry.rulesAdded) {
|
|
101
|
+
rulesAddedCopy.set(key, cloneRules(rules));
|
|
102
|
+
}
|
|
103
|
+
const full = {
|
|
104
|
+
sourceHash,
|
|
105
|
+
allowListHash,
|
|
106
|
+
inboundRulesHash,
|
|
107
|
+
transformed: entry.transformed,
|
|
108
|
+
modified: entry.modified,
|
|
109
|
+
rulesAdded: rulesAddedCopy,
|
|
110
|
+
};
|
|
111
|
+
this.entries.set(filePath, full);
|
|
112
|
+
if (this.entries.size > this.maxEntries) {
|
|
113
|
+
const oldestKey = this.entries.keys().next().value;
|
|
114
|
+
if (oldestKey !== undefined) {
|
|
115
|
+
this.entries.delete(oldestKey);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Invalidate a single file's entry. Call this from the file watcher
|
|
121
|
+
* when `filePath`'s content has changed.
|
|
122
|
+
*/
|
|
123
|
+
invalidate(filePath) {
|
|
124
|
+
this.entries.delete(filePath);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Invalidate every file whose key matches a predicate.
|
|
128
|
+
*/
|
|
129
|
+
invalidateMatching(predicate) {
|
|
130
|
+
let removed = 0;
|
|
131
|
+
for (const key of this.entries.keys()) {
|
|
132
|
+
if (predicate(key)) {
|
|
133
|
+
this.entries.delete(key);
|
|
134
|
+
removed += 1;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return removed;
|
|
138
|
+
}
|
|
139
|
+
/** Clear all entries. */
|
|
140
|
+
clear() {
|
|
141
|
+
this.entries.clear();
|
|
142
|
+
this.hits = 0;
|
|
143
|
+
this.misses = 0;
|
|
144
|
+
}
|
|
145
|
+
/** Current cache size. */
|
|
146
|
+
get size() {
|
|
147
|
+
return this.entries.size;
|
|
148
|
+
}
|
|
149
|
+
/** Hit/miss counters for observability. */
|
|
150
|
+
stats() {
|
|
151
|
+
const total = this.hits + this.misses;
|
|
152
|
+
return {
|
|
153
|
+
hits: this.hits,
|
|
154
|
+
misses: this.misses,
|
|
155
|
+
size: this.entries.size,
|
|
156
|
+
hitRate: total === 0 ? 0 : this.hits / total,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Hash allow-list package names together with each package's permitted exports.
|
|
162
|
+
*/
|
|
163
|
+
export function hashAllowList(allowList) {
|
|
164
|
+
const lines = [];
|
|
165
|
+
for (const packageName of Array.from(allowList.keys()).sort()) {
|
|
166
|
+
const rules = allowList.get(packageName);
|
|
167
|
+
if (rules === '*') {
|
|
168
|
+
lines.push(`${packageName}=*`);
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
const named = rules ? Array.from(rules).sort().join(',') : '';
|
|
172
|
+
lines.push(`${packageName}={${named}}`);
|
|
173
|
+
}
|
|
174
|
+
return rapidhash(lines.join('\n'));
|
|
175
|
+
}
|
|
176
|
+
function hashRequestedExportRules(rules) {
|
|
177
|
+
if (rules === '*')
|
|
178
|
+
return rapidhash('*');
|
|
179
|
+
if (!rules)
|
|
180
|
+
return rapidhash('');
|
|
181
|
+
return rapidhash(Array.from(rules).sort().join('\n'));
|
|
182
|
+
}
|
|
@@ -1,21 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module ClientGraphBoundaryPlugin
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* isomorphic compilation pipeline. It ensures that backend-only code, sensitive Node.js APIs,
|
|
6
|
-
* and massive server utilities do not accidentally leak into the browser bundle.
|
|
7
|
-
*
|
|
8
|
-
* It achieves this by intercepting all client module compilation passes and applying the
|
|
9
|
-
* `analyzeReachability` AST pass. If a forbidden import (e.g. `node:fs` or `*.server.ts`)
|
|
10
|
-
* is completely unreachable from the client component's `render` function, it is surgically
|
|
11
|
-
* pruned. If a forbidden import IS reachable, the build is intentionally failed to prevent
|
|
12
|
-
* runtime hydration crashes.
|
|
13
|
-
*
|
|
14
|
-
* Additionally, this plugin provides a build-time transform that statically resolves and
|
|
15
|
-
* inlines `fs.readFileSync(path.resolve(...))` calls to prevent server/client data mismatches.
|
|
4
|
+
* Build plugin securing the Ecopages isomorphic compilation pipeline.
|
|
16
5
|
*/
|
|
17
6
|
import type { EcoBuildPlugin } from '@ecopages/core/plugins/integration-plugin';
|
|
18
|
-
import { ClientGraphBoundaryCache } from './
|
|
7
|
+
import { ClientGraphBoundaryCache } from './boundary-cache.js';
|
|
19
8
|
/**
|
|
20
9
|
* Configuration options for the Client Graph Boundary build plugin.
|
|
21
10
|
*
|
|
@@ -24,6 +13,8 @@ import { ClientGraphBoundaryCache } from './client-graph-boundary-cache.js';
|
|
|
24
13
|
* from accidentally leaking into the browser compilation step, which would cause immediate crashes.
|
|
25
14
|
*/
|
|
26
15
|
type ClientGraphBoundaryOptions = {
|
|
16
|
+
/** Absolute path to the app project root, used for tsconfig path alias classification. */
|
|
17
|
+
projectRoot?: string;
|
|
27
18
|
/** Absolute path to the current working directory, used as a root fallback for resolving inline file reads. */
|
|
28
19
|
absWorkingDir?: string;
|
|
29
20
|
/**
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module ClientGraphBoundaryPlugin
|
|
3
|
+
*
|
|
4
|
+
* Build plugin securing the Ecopages isomorphic compilation pipeline.
|
|
5
|
+
*/
|
|
6
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
7
|
+
import { dirname, extname, resolve, sep } from 'node:path';
|
|
8
|
+
import { ClientGraphBoundaryCache } from './boundary-cache.js';
|
|
9
|
+
import { parseDeclaredModules, toModuleBaseSpecifier, mergeRequestedExportRules, normalizeRequestedExportsKey, snapshotRegistry, diffRequestedExportRules, } from './specifier-classification.js';
|
|
10
|
+
import { transformModuleImports } from './ast-transform.js';
|
|
11
|
+
import { classifyClientGraphModule, isPageOrLayoutEntry } from './module-classification.js';
|
|
12
|
+
import { recordModuleTransformProfile } from '@ecopages/core/cache';
|
|
13
|
+
const SOURCE_FILE_FILTER = /\.(tsx?|jsx?)$/;
|
|
14
|
+
/**
|
|
15
|
+
* Instantiates the client graph boundary build plugin.
|
|
16
|
+
*
|
|
17
|
+
* @param options - Configuration options for the graph boundary.
|
|
18
|
+
* @returns The resulting `EcoBuildPlugin`.
|
|
19
|
+
*/
|
|
20
|
+
export function createClientGraphBoundaryPlugin(options) {
|
|
21
|
+
return {
|
|
22
|
+
name: 'ecopages-client-graph-boundary',
|
|
23
|
+
setup(build) {
|
|
24
|
+
const absWorkingDir = options?.absWorkingDir ?? process.cwd();
|
|
25
|
+
const cache = options?.cache;
|
|
26
|
+
const globallyDeclaredSources = parseDeclaredModules(options?.declaredModules);
|
|
27
|
+
const requestedExports = new Map();
|
|
28
|
+
for (const alwaysAllow of options?.alwaysAllowSpecifiers ?? []) {
|
|
29
|
+
globallyDeclaredSources.set(toModuleBaseSpecifier(alwaysAllow), '*');
|
|
30
|
+
}
|
|
31
|
+
build.onLoad({ filter: SOURCE_FILE_FILTER }, (args) => {
|
|
32
|
+
const category = classifyClientGraphModule(args.path, options?.projectRoot);
|
|
33
|
+
if (category === 'package' || category === 'vendored' || category === 'virtual') {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
let source;
|
|
37
|
+
try {
|
|
38
|
+
source = readFileSync(args.path, 'utf-8');
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Fast path: if the cache has a transform result for this
|
|
45
|
+
* exact (filePath, source, allowListRules, inboundRules) tuple,
|
|
46
|
+
* replay the captured `rulesAdded` into the live registry and
|
|
47
|
+
* return the cached transformed source.
|
|
48
|
+
*/
|
|
49
|
+
const inboundRules = requestedExports.get(normalizeRequestedExportsKey(args.path));
|
|
50
|
+
if (cache) {
|
|
51
|
+
const cacheStartedAt = performance.now();
|
|
52
|
+
const cached = cache.get(args.path, source, globallyDeclaredSources, inboundRules);
|
|
53
|
+
recordModuleTransformProfile({
|
|
54
|
+
category,
|
|
55
|
+
phase: 'cache-lookup',
|
|
56
|
+
ms: performance.now() - cacheStartedAt,
|
|
57
|
+
cacheHit: Boolean(cached),
|
|
58
|
+
});
|
|
59
|
+
if (cached) {
|
|
60
|
+
for (const [moduleKey, rules] of cached.rulesAdded) {
|
|
61
|
+
mergeRequestedExportRules(requestedExports, moduleKey, rules);
|
|
62
|
+
}
|
|
63
|
+
if (!cached.modified)
|
|
64
|
+
return undefined;
|
|
65
|
+
const ext = extname(args.path).slice(1);
|
|
66
|
+
return { contents: cached.transformed, loader: ext, resolveDir: dirname(args.path) };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
let transformed = source;
|
|
70
|
+
let modified = false;
|
|
71
|
+
let inlinedExternalFile = false;
|
|
72
|
+
if (source.includes('readFileSync')) {
|
|
73
|
+
const readFileTransformed = transformed.replace(/\bfs\.readFileSync\s*\(\s*path\.resolve\s*\(\s*(['"`])([^'"`\n]+)\1\s*\)\s*,\s*['"`]utf-?8['"`]\s*\)/g, (_match, _q, relPath) => {
|
|
74
|
+
modified = true;
|
|
75
|
+
inlinedExternalFile = true;
|
|
76
|
+
try {
|
|
77
|
+
const sourceDir = dirname(args.path);
|
|
78
|
+
const inferredProjectRoot = inferProjectRootFromSourcePath(args.path);
|
|
79
|
+
const candidates = [
|
|
80
|
+
resolve(absWorkingDir, relPath),
|
|
81
|
+
resolve(process.cwd(), relPath),
|
|
82
|
+
resolve(sourceDir, relPath),
|
|
83
|
+
...(inferredProjectRoot ? [resolve(inferredProjectRoot, relPath)] : []),
|
|
84
|
+
];
|
|
85
|
+
const absolutePath = candidates.find((candidate) => existsSync(candidate));
|
|
86
|
+
if (!absolutePath)
|
|
87
|
+
return '""';
|
|
88
|
+
const content = readFileSync(absolutePath, 'utf-8');
|
|
89
|
+
return JSON.stringify(content);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return '""';
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
transformed = readFileTransformed;
|
|
96
|
+
}
|
|
97
|
+
// Snapshot the live registry so we can diff per-key after
|
|
98
|
+
// the transform. We must capture the **after-state** of
|
|
99
|
+
// every key the transform touched — including keys that
|
|
100
|
+
// already existed and grew via Set union or were promoted
|
|
101
|
+
// to `'*'`. A snapshot keyed only on newly-added entries
|
|
102
|
+
// would under-populate the registry on cache hit.
|
|
103
|
+
const registryBefore = snapshotRegistry(requestedExports);
|
|
104
|
+
const analysisStartedAt = performance.now();
|
|
105
|
+
const { transformed: oxcTransformed, modified: importsModified } = transformModuleImports(transformed, args.path, globallyDeclaredSources, requestedExports, options?.projectRoot, isPageOrLayoutEntry(args.path));
|
|
106
|
+
recordModuleTransformProfile({
|
|
107
|
+
category,
|
|
108
|
+
phase: 'analysis',
|
|
109
|
+
ms: performance.now() - analysisStartedAt,
|
|
110
|
+
});
|
|
111
|
+
if (importsModified) {
|
|
112
|
+
modified = true;
|
|
113
|
+
transformed = oxcTransformed;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Skip persistent cache when the transform inlined another file:
|
|
117
|
+
* output depends on content outside the importer source hash.
|
|
118
|
+
*/
|
|
119
|
+
if (cache && !inlinedExternalFile) {
|
|
120
|
+
const rulesAdded = new Map();
|
|
121
|
+
for (const [key, afterRules] of requestedExports) {
|
|
122
|
+
const beforeRules = registryBefore.get(key);
|
|
123
|
+
const diff = diffRequestedExportRules(beforeRules, afterRules);
|
|
124
|
+
if (!diff)
|
|
125
|
+
continue;
|
|
126
|
+
rulesAdded.set(key, diff);
|
|
127
|
+
}
|
|
128
|
+
const entry = {
|
|
129
|
+
transformed,
|
|
130
|
+
modified,
|
|
131
|
+
rulesAdded,
|
|
132
|
+
};
|
|
133
|
+
cache.set(args.path, source, globallyDeclaredSources, entry, inboundRules);
|
|
134
|
+
}
|
|
135
|
+
if (!modified)
|
|
136
|
+
return undefined;
|
|
137
|
+
const ext = extname(args.path).slice(1);
|
|
138
|
+
return { contents: transformed, loader: ext, resolveDir: dirname(args.path) };
|
|
139
|
+
});
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function inferProjectRootFromSourcePath(filePath) {
|
|
144
|
+
const parts = filePath.split(/[\\/]/);
|
|
145
|
+
const srcIndex = parts.lastIndexOf('src');
|
|
146
|
+
if (srcIndex <= 0)
|
|
147
|
+
return undefined;
|
|
148
|
+
return parts.slice(0, srcIndex).join(sep);
|
|
149
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Walks a component config tree once, including nested layout configs and
|
|
3
|
+
* dependency component configs.
|
|
4
|
+
*
|
|
5
|
+
* The shared React integration code performs several different analyses over the
|
|
6
|
+
* same config graph. Centralizing the traversal keeps cycle handling and graph
|
|
7
|
+
* shape assumptions in one place instead of repeating them in the renderer and
|
|
8
|
+
* services.
|
|
9
|
+
*/
|
|
10
|
+
export function walkConfigTree(config, visitor, visited = new Set()) {
|
|
11
|
+
if (!config || visited.has(config)) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
visited.add(config);
|
|
15
|
+
visitor(config);
|
|
16
|
+
for (const layout of config.layouts ?? []) {
|
|
17
|
+
if (layout?.config) {
|
|
18
|
+
walkConfigTree(layout.config, visitor, visited);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
for (const component of config.dependencies?.components ?? []) {
|
|
22
|
+
walkConfigTree(component.config, visitor, visited);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Walks a forest of root component configs using one shared visited set.
|
|
27
|
+
*
|
|
28
|
+
* This is useful when a page contributes multiple config roots, such as a page
|
|
29
|
+
* config plus a resolved layout config, and duplicate nested nodes should still
|
|
30
|
+
* be processed only once.
|
|
31
|
+
*/
|
|
32
|
+
export function walkConfigForest(configs, visitor) {
|
|
33
|
+
const visited = new Set();
|
|
34
|
+
for (const config of configs) {
|
|
35
|
+
walkConfigTree(config, visitor, visited);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Collects values from a config tree while preserving the shared traversal and
|
|
40
|
+
* cycle protection behavior used across the React integration.
|
|
41
|
+
*/
|
|
42
|
+
export function collectFromConfigTree(config, collector) {
|
|
43
|
+
const values = [];
|
|
44
|
+
walkConfigTree(config, (node) => {
|
|
45
|
+
values.push(...collector(node));
|
|
46
|
+
});
|
|
47
|
+
return values;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Collects values from multiple config roots with one shared visited set.
|
|
51
|
+
*/
|
|
52
|
+
export function collectFromConfigForest(configs, collector) {
|
|
53
|
+
const values = [];
|
|
54
|
+
walkConfigForest(configs, (node) => {
|
|
55
|
+
values.push(...collector(node));
|
|
56
|
+
});
|
|
57
|
+
return values;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Returns true when any node in the config tree matches the predicate.
|
|
61
|
+
*/
|
|
62
|
+
export function someInConfigTree(config, predicate) {
|
|
63
|
+
let matched = false;
|
|
64
|
+
walkConfigTree(config, (node) => {
|
|
65
|
+
if (matched) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
matched = predicate(node);
|
|
69
|
+
});
|
|
70
|
+
return matched;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Reads config roots from partial components while tolerating undefined config.
|
|
74
|
+
*/
|
|
75
|
+
export function getComponentConfigs(components) {
|
|
76
|
+
return components.map((component) => component.config);
|
|
77
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared utilities for collecting declared module sources from component configs.
|
|
3
|
+
* Used by both the production ReactRenderer and the HMR strategy to ensure
|
|
4
|
+
* the client-graph-boundary plugin receives a consistent set of allowed modules.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { collectFromConfigTree } from './component-config-traversal.js';
|
|
9
|
+
/**
|
|
10
|
+
* Extracts the module source (package name) from a declared module string,
|
|
11
|
+
* stripping any `{namedImport,...}` grammar.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* parseDeclaredModuleSource('@ecopages/image-processor/component/react{EcoImage}')
|
|
15
|
+
* // → '@ecopages/image-processor/component/react'
|
|
16
|
+
*/
|
|
17
|
+
export function parseDeclaredModuleSource(value) {
|
|
18
|
+
const source = value.trim();
|
|
19
|
+
if (source.length === 0)
|
|
20
|
+
return undefined;
|
|
21
|
+
const openBraceIndex = source.indexOf('{');
|
|
22
|
+
if (openBraceIndex < 0)
|
|
23
|
+
return source;
|
|
24
|
+
const from = source.slice(0, openBraceIndex).trim();
|
|
25
|
+
return from.length > 0 ? from : undefined;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Normalizes an array of declared module strings into unique source paths.
|
|
29
|
+
*/
|
|
30
|
+
export function normalizeDeclaredModuleSources(modules) {
|
|
31
|
+
const seen = new Set();
|
|
32
|
+
for (const declaration of modules ?? []) {
|
|
33
|
+
const from = parseDeclaredModuleSource(declaration);
|
|
34
|
+
if (from) {
|
|
35
|
+
seen.add(from);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return Array.from(seen);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Recursively walks a component config tree (including layouts and nested
|
|
42
|
+
* `dependencies.components`) to collect all declared module sources.
|
|
43
|
+
*/
|
|
44
|
+
export function collectDeclaredModulesInConfig(config) {
|
|
45
|
+
return collectFromConfigTree(config, (node) => normalizeDeclaredModuleSources(node.dependencies?.modules));
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Collects declared module sources from an already imported page module.
|
|
49
|
+
*/
|
|
50
|
+
export function collectPageDeclaredModulesFromModule(pageModule) {
|
|
51
|
+
const declarations = [
|
|
52
|
+
...collectDeclaredModulesInConfig(pageModule.default?.config),
|
|
53
|
+
...collectDeclaredModulesInConfig(pageModule.config),
|
|
54
|
+
];
|
|
55
|
+
return Array.from(new Set(declarations));
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Imports a page entrypoint and collects all transitively declared module sources
|
|
59
|
+
* from its config, layout config, and nested component configs.
|
|
60
|
+
*/
|
|
61
|
+
export async function collectPageDeclaredModules(pagePath) {
|
|
62
|
+
try {
|
|
63
|
+
const pageModule = (await import(/* @vite-ignore */ pagePath));
|
|
64
|
+
return collectPageDeclaredModulesFromModule(pageModule);
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export type ClientGraphModuleKind = 'app' | 'workspace' | 'package' | 'vendored' | 'virtual';
|
|
2
|
+
export declare function classifyClientGraphModule(filePath: string, projectRoot?: string): ClientGraphModuleKind;
|
|
3
|
+
export declare function isPageOrLayoutEntry(filePath: string): boolean;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
/**
|
|
3
|
+
* Splits a file path into OS-agnostic segments.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* Bundlers may pass POSIX or Windows separators; classification must not depend
|
|
7
|
+
* on the host's `path.sep` alone.
|
|
8
|
+
*/
|
|
9
|
+
function pathSegments(filePath) {
|
|
10
|
+
return filePath.split(/[\\/]+/).filter(Boolean);
|
|
11
|
+
}
|
|
12
|
+
function hasPathSegment(filePath, segment) {
|
|
13
|
+
return pathSegments(filePath).includes(segment);
|
|
14
|
+
}
|
|
15
|
+
function hasPathSuffix(filePath, segments) {
|
|
16
|
+
const parts = pathSegments(filePath);
|
|
17
|
+
for (let index = 0; index <= parts.length - segments.length; index += 1) {
|
|
18
|
+
if (segments.every((segment, offset) => parts[index + offset] === segment)) {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
export function classifyClientGraphModule(filePath, projectRoot) {
|
|
25
|
+
if (filePath.startsWith('\0') || filePath.includes('?'))
|
|
26
|
+
return 'virtual';
|
|
27
|
+
if (hasPathSegment(filePath, 'node_modules'))
|
|
28
|
+
return 'package';
|
|
29
|
+
if (hasPathSegment(filePath, '.eco') || hasPathSuffix(filePath, ['assets', 'vendors']))
|
|
30
|
+
return 'vendored';
|
|
31
|
+
if (projectRoot) {
|
|
32
|
+
const resolvedRoot = path.resolve(projectRoot);
|
|
33
|
+
const resolvedFile = path.resolve(filePath);
|
|
34
|
+
if (resolvedFile === resolvedRoot || resolvedFile.startsWith(`${resolvedRoot}${path.sep}`)) {
|
|
35
|
+
return 'app';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return 'workspace';
|
|
39
|
+
}
|
|
40
|
+
export function isPageOrLayoutEntry(filePath) {
|
|
41
|
+
const segments = pathSegments(filePath);
|
|
42
|
+
return segments.includes('pages') || segments.includes('layouts');
|
|
43
|
+
}
|
|
@@ -6,19 +6,11 @@
|
|
|
6
6
|
* dependencies (imports, variables, functions, and classes) that begin from explicit client roots.
|
|
7
7
|
*
|
|
8
8
|
* In Ecopages, "client roots" are defined as the `render`, `errorBoundary`, or `loadingFallback`
|
|
9
|
-
* properties passed into `eco.page()` or `eco.component()`. By tracing the execution path from
|
|
9
|
+
* properties passed into `eco.page()`, `eco.layout()`, or `eco.component()`. By tracing the execution path from
|
|
10
10
|
* these roots, the analyzer determines exactly which modules and bindings are actually needed
|
|
11
11
|
* by the browser to hydrate the page, and which imports are unused on the client (and thus can be pruned).
|
|
12
12
|
*/
|
|
13
|
-
import {
|
|
14
|
-
type ParserLanguage = 'js' | 'jsx' | 'ts' | 'tsx';
|
|
15
|
-
/**
|
|
16
|
-
* Determines the appropriate parser language configuration for a given file name.
|
|
17
|
-
*
|
|
18
|
-
* @param filename - The absolute or relative path to the file.
|
|
19
|
-
* @returns The Oxc parser language dialect to use ('js', 'jsx', 'ts', or 'tsx').
|
|
20
|
-
*/
|
|
21
|
-
export declare function parserLanguageForFile(filename: string): ParserLanguage;
|
|
13
|
+
import type { ParseResult } from 'oxc-parser';
|
|
22
14
|
/**
|
|
23
15
|
* Represents the computed results of a reachability analysis pass.
|
|
24
16
|
*/
|
|
@@ -53,7 +45,7 @@ export type ReachabilityResult = {
|
|
|
53
45
|
type ExplicitlyRequestedExports = Set<string> | '*';
|
|
54
46
|
/**
|
|
55
47
|
* Analyzes a module using Oxc AST and extracts a strict reachability graph
|
|
56
|
-
* starting from client roots (`render`, `errorBoundary`, `loadingFallback` of `eco.page` or `eco.component`).
|
|
48
|
+
* starting from client roots (`render`, `errorBoundary`, `loadingFallback` of `eco.page`, `eco.layout`, or `eco.component`).
|
|
57
49
|
*
|
|
58
50
|
* @param source - Raw source string of the module.
|
|
59
51
|
* @param filename - Absolute or relative path to the module file.
|
|
@@ -62,5 +54,5 @@ type ExplicitlyRequestedExports = Set<string> | '*';
|
|
|
62
54
|
* @param explicitlyRequestedExports - Optional named export filter propagated
|
|
63
55
|
* from a downstream importer when this module is only partially reachable.
|
|
64
56
|
*/
|
|
65
|
-
export declare function analyzeReachability(source: string, filename: string, program?:
|
|
57
|
+
export declare function analyzeReachability(source: string, filename: string, program?: ParseResult['program'], explicitlyRequestedExports?: ExplicitlyRequestedExports): ReachabilityResult;
|
|
66
58
|
export {};
|