@ecopages/react 0.2.0-beta.3 → 0.2.0-beta.30
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 +168 -6
- package/package.json +51 -26
- package/src/bundling/bootstrap-package-paths.d.ts +5 -0
- package/src/bundling/bootstrap-package-paths.js +6 -0
- package/src/{services/react-bundle.service.d.ts → bundling/bundle.d.ts} +21 -8
- package/src/{services/react-bundle.service.js → bundling/bundle.js} +53 -21
- package/src/bundling/discover-layout-runtime-modules.d.ts +32 -0
- package/src/bundling/discover-layout-runtime-modules.js +180 -0
- package/src/bundling/runtime-alias-map.d.ts +10 -0
- package/src/{utils/react-runtime-alias-map.js → bundling/runtime-alias-map.js} +13 -2
- package/src/{services/react-runtime-bundle.service.d.ts → bundling/runtime-bundle.d.ts} +30 -5
- package/src/{services/react-runtime-bundle.service.js → bundling/runtime-bundle.js} +104 -8
- package/src/bundling/runtime-mode.d.ts +9 -0
- package/src/bundling/runtime-mode.js +6 -0
- package/src/bundling/runtime-modules.d.ts +29 -0
- package/src/bundling/runtime-modules.js +38 -0
- package/src/client-graph/ast-transform.d.ts +18 -0
- package/src/{utils/client-graph-boundary-plugin.js → client-graph/ast-transform.js} +15 -232
- package/src/{utils/client-graph-boundary-cache.d.ts → client-graph/boundary-cache.d.ts} +0 -5
- package/src/{utils/client-graph-boundary-cache.js → client-graph/boundary-cache.js} +1 -3
- package/src/{utils/client-graph-boundary-plugin.d.ts → client-graph/boundary-plugin.d.ts} +4 -13
- package/src/client-graph/boundary-plugin.js +107 -0
- package/src/{utils → client-graph}/component-config-traversal.js +4 -2
- package/src/{utils → client-graph}/reachability-analyzer.d.ts +2 -2
- package/src/{utils → client-graph}/reachability-analyzer.js +4 -1
- package/src/client-graph/specifier-classification.d.ts +75 -0
- package/src/client-graph/specifier-classification.js +155 -0
- package/src/{react-hmr-strategy.d.ts → hmr/hmr-strategy.d.ts} +19 -6
- package/src/{react-hmr-strategy.js → hmr/hmr-strategy.js} +154 -133
- package/src/{services/react-hmr-page-metadata-cache.d.ts → hmr/page-metadata-cache.d.ts} +5 -1
- package/src/{services/react-hmr-page-metadata-cache.js → hmr/page-metadata-cache.js} +8 -2
- 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 +23 -0
- package/src/{services/react-hydration-asset.service.d.ts → hydration/hydration-asset.d.ts} +33 -33
- package/src/{services/react-hydration-asset.service.js → hydration/hydration-asset.js} +63 -56
- package/src/hydration/hydration-scripts.d.ts +100 -0
- package/src/hydration/hydration-scripts.js +350 -0
- package/src/{utils → hydration}/hydration-scripts.test.browser.js +11 -5
- package/src/hydration/page-data-manifest.d.ts +58 -0
- package/src/hydration/page-data-manifest.js +53 -0
- package/src/hydration/page-data-reader.d.ts +31 -0
- package/src/hydration/page-data-reader.js +64 -0
- package/src/hydration/page-payload.d.ts +67 -0
- package/src/hydration/page-payload.js +104 -0
- package/src/hydration/serialize-page-data-script.d.ts +27 -0
- package/src/hydration/serialize-page-data-script.js +31 -0
- package/src/{services/react-mdx-config-dependency.service.d.ts → mdx/mdx-config-dependency.d.ts} +5 -5
- package/src/{services/react-mdx-config-dependency.service.js → mdx/mdx-config-dependency.js} +8 -10
- package/src/mdx/mdx-loader-plugin.d.ts +2 -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 +72 -0
- package/src/{react.plugin.d.ts → plugin/react.plugin.d.ts} +8 -3
- package/src/{react.plugin.js → plugin/react.plugin.js} +78 -54
- package/src/plugin/react.types.d.ts +149 -0
- package/src/render/component-ownership.d.ts +50 -0
- package/src/render/component-ownership.js +23 -0
- package/src/render/component-ssr.d.ts +113 -0
- package/src/render/component-ssr.js +202 -0
- package/src/render/errors.d.ts +16 -0
- package/src/render/errors.js +18 -0
- package/src/render/layout-compose.d.ts +66 -0
- package/src/render/layout-compose.js +106 -0
- package/src/render/react-renderer.d.ts +138 -0
- package/src/render/react-renderer.js +481 -0
- package/src/render/react-runtime.d.ts +13 -0
- package/src/render/react-runtime.js +21 -0
- package/src/render/unified-layout-composition.d.ts +53 -0
- package/src/render/unified-layout-composition.js +89 -0
- package/src/react-renderer.d.ts +0 -233
- package/src/react-renderer.js +0 -622
- package/src/react.types.d.ts +0 -88
- package/src/services/pages-index.d.ts +0 -64
- package/src/services/pages-index.js +0 -73
- 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/utils/hydration-scripts.d.ts +0 -92
- package/src/utils/hydration-scripts.js +0 -358
- 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 → app}/client-only.d.ts +0 -0
- /package/src/{utils → app}/client-only.js +0 -0
- /package/src/{utils → app}/dynamic.d.ts +0 -0
- /package/src/{utils → app}/dynamic.js +0 -0
- /package/src/{utils → app}/dynamic.test.browser.d.ts +0 -0
- /package/src/{utils → app}/dynamic.test.browser.js +0 -0
- /package/src/{utils/react-dom-runtime-interop-plugin.d.ts → bundling/runtime-interop-plugin.d.ts} +0 -0
- /package/src/{utils/react-dom-runtime-interop-plugin.js → bundling/runtime-interop-plugin.js} +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/{utils → client-graph}/declared-modules.js +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/{router-adapter.js → contracts/router-adapter.js} +0 -0
- /package/src/{utils → hmr}/hmr-scripts.d.ts +0 -0
- /package/src/{utils → hmr}/hmr-scripts.js +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/{react.constants.js → plugin/react.constants.js} +0 -0
- /package/src/{react.types.js → plugin/react.types.js} +0 -0
- /package/src/{eco-embed.d.ts → render/eco-embed.d.ts} +0 -0
- /package/src/{eco-embed.js → render/eco-embed.js} +0 -0
- /package/src/{utils → render}/html-boundary.d.ts +0 -0
- /package/src/{utils → render}/html-boundary.js +0 -0
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { dirname, extname, resolve } from "node:path";
|
|
1
|
+
import { extname } from "node:path";
|
|
3
2
|
import { cachedParseSync } from "@ecopages/core/cache";
|
|
4
|
-
import { ClientGraphBoundaryCache } from "./client-graph-boundary-cache.js";
|
|
5
3
|
import { analyzeReachability } from "./reachability-analyzer.js";
|
|
6
|
-
|
|
4
|
+
import {
|
|
5
|
+
isBareSpecifier,
|
|
6
|
+
isProjectAliasSpecifier,
|
|
7
|
+
isServerOnlySpecifier,
|
|
8
|
+
toModuleBaseSpecifier,
|
|
9
|
+
parseDeclaredModules,
|
|
10
|
+
mergeDeclaredModulesMap,
|
|
11
|
+
normalizeRequestedExportsKey,
|
|
12
|
+
resolveRequestedExportsKey,
|
|
13
|
+
mergeRequestedExportRules
|
|
14
|
+
} from "./specifier-classification.js";
|
|
7
15
|
const SERVER_ONLY_ECO_PAGE_OPTION_KEYS = /* @__PURE__ */ new Set([
|
|
8
16
|
"cache",
|
|
9
17
|
"middleware",
|
|
@@ -12,82 +20,6 @@ const SERVER_ONLY_ECO_PAGE_OPTION_KEYS = /* @__PURE__ */ new Set([
|
|
|
12
20
|
"staticProps",
|
|
13
21
|
"staticPaths"
|
|
14
22
|
]);
|
|
15
|
-
function isBareSpecifier(specifier) {
|
|
16
|
-
if (specifier.startsWith(".")) return false;
|
|
17
|
-
if (specifier.startsWith("/")) return false;
|
|
18
|
-
if (specifier.includes("://")) return false;
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
21
|
-
function isProjectAliasSpecifier(specifier) {
|
|
22
|
-
return specifier.startsWith("@/") || specifier.startsWith("~/") || specifier.startsWith("ecopages:");
|
|
23
|
-
}
|
|
24
|
-
function isServerOnlySpecifier(specifier) {
|
|
25
|
-
if (specifier.startsWith("node:")) return true;
|
|
26
|
-
return /(?:^|[/])[^/]+\.server(?:$|\.)/.test(specifier);
|
|
27
|
-
}
|
|
28
|
-
function toModuleBaseSpecifier(specifier) {
|
|
29
|
-
if (!isBareSpecifier(specifier) || specifier.startsWith("node:")) {
|
|
30
|
-
return specifier;
|
|
31
|
-
}
|
|
32
|
-
if (specifier.startsWith("@")) {
|
|
33
|
-
const [scope, name2] = specifier.split("/");
|
|
34
|
-
if (!scope || !name2) return specifier;
|
|
35
|
-
return `${scope}/${name2}`;
|
|
36
|
-
}
|
|
37
|
-
const [name] = specifier.split("/");
|
|
38
|
-
return name ?? specifier;
|
|
39
|
-
}
|
|
40
|
-
function parseDeclaredModules(moduleDeclarations) {
|
|
41
|
-
const map = /* @__PURE__ */ new Map();
|
|
42
|
-
for (const declaration of moduleDeclarations ?? []) {
|
|
43
|
-
const source = declaration.trim();
|
|
44
|
-
if (source.length === 0) continue;
|
|
45
|
-
const openBraceIndex = source.indexOf("{");
|
|
46
|
-
if (openBraceIndex < 0) {
|
|
47
|
-
map.set(toModuleBaseSpecifier(source), "*");
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
const closeBraceIndex = source.indexOf("}", openBraceIndex);
|
|
51
|
-
const rawPkg = source.slice(0, openBraceIndex).trim();
|
|
52
|
-
if (rawPkg.length === 0) continue;
|
|
53
|
-
const pkg = toModuleBaseSpecifier(rawPkg);
|
|
54
|
-
const namedImportsStr = closeBraceIndex > openBraceIndex ? source.slice(openBraceIndex + 1, closeBraceIndex) : source.slice(openBraceIndex + 1);
|
|
55
|
-
const namedImports = namedImportsStr.split(",").map((s) => s.trim()).filter(Boolean);
|
|
56
|
-
const existing = map.get(pkg);
|
|
57
|
-
if (existing === "*") continue;
|
|
58
|
-
if (!existing) {
|
|
59
|
-
if (namedImports.length === 0) {
|
|
60
|
-
map.set(pkg, "*");
|
|
61
|
-
} else {
|
|
62
|
-
map.set(pkg, new Set(namedImports));
|
|
63
|
-
}
|
|
64
|
-
} else {
|
|
65
|
-
for (const name of namedImports) {
|
|
66
|
-
existing.add(name);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return map;
|
|
71
|
-
}
|
|
72
|
-
function mergeDeclaredModulesMap(a, b) {
|
|
73
|
-
const result = new Map(a);
|
|
74
|
-
for (const [pkg, imports] of b.entries()) {
|
|
75
|
-
const existing = result.get(pkg);
|
|
76
|
-
if (existing === "*") continue;
|
|
77
|
-
if (imports === "*") {
|
|
78
|
-
result.set(pkg, "*");
|
|
79
|
-
continue;
|
|
80
|
-
}
|
|
81
|
-
if (!existing) {
|
|
82
|
-
result.set(pkg, imports);
|
|
83
|
-
} else {
|
|
84
|
-
for (const name of imports) {
|
|
85
|
-
existing.add(name);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return result;
|
|
90
|
-
}
|
|
91
23
|
function parserLanguageForFile(filename) {
|
|
92
24
|
const extension = extname(filename).toLowerCase();
|
|
93
25
|
if (extension === ".tsx") return "tsx";
|
|
@@ -151,65 +83,7 @@ function stripServerOnlyEcoPageOptions(source, program) {
|
|
|
151
83
|
}
|
|
152
84
|
return { transformed, modified: true };
|
|
153
85
|
}
|
|
154
|
-
function
|
|
155
|
-
let normalized = pathname.replace(/\\/g, "/");
|
|
156
|
-
normalized = normalized.replace(/\.(tsx?|jsx?)$/i, "");
|
|
157
|
-
if (normalized.endsWith("/index")) {
|
|
158
|
-
normalized = normalized.slice(0, -"/index".length);
|
|
159
|
-
}
|
|
160
|
-
return normalized;
|
|
161
|
-
}
|
|
162
|
-
function resolveRequestedExportsKey(importer, specifier) {
|
|
163
|
-
if (isBareSpecifier(specifier) || isProjectAliasSpecifier(specifier)) {
|
|
164
|
-
return void 0;
|
|
165
|
-
}
|
|
166
|
-
const resolved = specifier.startsWith("/") ? specifier : resolve(dirname(importer), specifier);
|
|
167
|
-
return normalizeRequestedExportsKey(resolved);
|
|
168
|
-
}
|
|
169
|
-
function mergeRequestedExportRules(registry, moduleKey, rules) {
|
|
170
|
-
const existing = registry.get(moduleKey);
|
|
171
|
-
if (existing === "*") return;
|
|
172
|
-
if (rules === "*") {
|
|
173
|
-
registry.set(moduleKey, "*");
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
if (!existing) {
|
|
177
|
-
registry.set(moduleKey, new Set(rules));
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
for (const rule of rules) {
|
|
181
|
-
existing.add(rule);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
function cloneRequestedExportRules(rules) {
|
|
185
|
-
return rules === "*" ? rules : new Set(rules);
|
|
186
|
-
}
|
|
187
|
-
function snapshotRegistry(registry) {
|
|
188
|
-
const out = /* @__PURE__ */ new Map();
|
|
189
|
-
for (const [key, rules] of registry) {
|
|
190
|
-
out.set(key, cloneRequestedExportRules(rules));
|
|
191
|
-
}
|
|
192
|
-
return out;
|
|
193
|
-
}
|
|
194
|
-
function diffRequestedExportRules(before, after) {
|
|
195
|
-
if (!before) {
|
|
196
|
-
return cloneRequestedExportRules(after);
|
|
197
|
-
}
|
|
198
|
-
if (before === "*") {
|
|
199
|
-
return void 0;
|
|
200
|
-
}
|
|
201
|
-
if (after === "*") {
|
|
202
|
-
return "*";
|
|
203
|
-
}
|
|
204
|
-
const addedRules = /* @__PURE__ */ new Set();
|
|
205
|
-
for (const rule of after) {
|
|
206
|
-
if (!before.has(rule)) {
|
|
207
|
-
addedRules.add(rule);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
return addedRules.size > 0 ? addedRules : void 0;
|
|
211
|
-
}
|
|
212
|
-
function transformModuleImports(source, filename, globallyAllowed, requestedExports) {
|
|
86
|
+
function transformModuleImports(source, filename, globallyAllowed, requestedExports, projectRoot) {
|
|
213
87
|
let result;
|
|
214
88
|
try {
|
|
215
89
|
result = cachedParseSync(filename, source, {
|
|
@@ -277,7 +151,7 @@ function transformModuleImports(source, filename, globallyAllowed, requestedExpo
|
|
|
277
151
|
}
|
|
278
152
|
return { allowed: false };
|
|
279
153
|
}
|
|
280
|
-
if (isProjectAliasSpecifier(specifier)) return { allowed: true, rules: explicitRules ?? "*" };
|
|
154
|
+
if (isProjectAliasSpecifier(specifier, projectRoot)) return { allowed: true, rules: explicitRules ?? "*" };
|
|
281
155
|
if (!isBareSpecifier(specifier)) return { allowed: true, rules: explicitRules ?? "*" };
|
|
282
156
|
return { allowed: true, rules: explicitRules ?? "*" };
|
|
283
157
|
}
|
|
@@ -456,97 +330,6 @@ function transformModuleImports(source, filename, globallyAllowed, requestedExpo
|
|
|
456
330
|
}
|
|
457
331
|
return { transformed, modified: true };
|
|
458
332
|
}
|
|
459
|
-
function createClientGraphBoundaryPlugin(options) {
|
|
460
|
-
return {
|
|
461
|
-
name: "ecopages-client-graph-boundary",
|
|
462
|
-
setup(build) {
|
|
463
|
-
const absWorkingDir = options?.absWorkingDir ?? process.cwd();
|
|
464
|
-
const cache = options?.cache;
|
|
465
|
-
const globallyDeclaredSources = parseDeclaredModules(options?.declaredModules);
|
|
466
|
-
const requestedExports = /* @__PURE__ */ new Map();
|
|
467
|
-
for (const alwaysAllow of options?.alwaysAllowSpecifiers ?? []) {
|
|
468
|
-
globallyDeclaredSources.set(toModuleBaseSpecifier(alwaysAllow), "*");
|
|
469
|
-
}
|
|
470
|
-
const allowListForCache = Array.from(globallyDeclaredSources.keys()).sort();
|
|
471
|
-
build.onLoad({ filter: SOURCE_FILE_FILTER }, (args) => {
|
|
472
|
-
let source;
|
|
473
|
-
try {
|
|
474
|
-
source = readFileSync(args.path, "utf-8");
|
|
475
|
-
} catch {
|
|
476
|
-
return void 0;
|
|
477
|
-
}
|
|
478
|
-
if (cache) {
|
|
479
|
-
const cached = cache.get(args.path, source, allowListForCache);
|
|
480
|
-
if (cached) {
|
|
481
|
-
for (const [moduleKey, rules] of cached.rulesAdded) {
|
|
482
|
-
mergeRequestedExportRules(requestedExports, moduleKey, rules);
|
|
483
|
-
}
|
|
484
|
-
if (!cached.modified) return void 0;
|
|
485
|
-
const ext2 = extname(args.path).slice(1);
|
|
486
|
-
return { contents: cached.transformed, loader: ext2, resolveDir: dirname(args.path) };
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
let transformed = source;
|
|
490
|
-
let modified = false;
|
|
491
|
-
if (source.includes("readFileSync")) {
|
|
492
|
-
const readFileTransformed = transformed.replace(
|
|
493
|
-
/\bfs\.readFileSync\s*\(\s*path\.resolve\s*\(\s*(['"`])([^'"`\n]+)\1\s*\)\s*,\s*['"`]utf-?8['"`]\s*\)/g,
|
|
494
|
-
(_match, _q, relPath) => {
|
|
495
|
-
modified = true;
|
|
496
|
-
try {
|
|
497
|
-
const sourceDir = dirname(args.path);
|
|
498
|
-
const srcDirIndex = args.path.lastIndexOf("/src/");
|
|
499
|
-
const inferredProjectRoot = srcDirIndex >= 0 ? args.path.slice(0, srcDirIndex) : void 0;
|
|
500
|
-
const candidates = [
|
|
501
|
-
resolve(absWorkingDir, relPath),
|
|
502
|
-
resolve(process.cwd(), relPath),
|
|
503
|
-
resolve(sourceDir, relPath),
|
|
504
|
-
...inferredProjectRoot ? [resolve(inferredProjectRoot, relPath)] : []
|
|
505
|
-
];
|
|
506
|
-
const absolutePath = candidates.find((candidate) => existsSync(candidate));
|
|
507
|
-
if (!absolutePath) return '""';
|
|
508
|
-
const content = readFileSync(absolutePath, "utf-8");
|
|
509
|
-
return JSON.stringify(content);
|
|
510
|
-
} catch {
|
|
511
|
-
return '""';
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
);
|
|
515
|
-
transformed = readFileTransformed;
|
|
516
|
-
}
|
|
517
|
-
const registryBefore = snapshotRegistry(requestedExports);
|
|
518
|
-
const { transformed: oxcTransformed, modified: importsModified } = transformModuleImports(
|
|
519
|
-
transformed,
|
|
520
|
-
args.path,
|
|
521
|
-
globallyDeclaredSources,
|
|
522
|
-
requestedExports
|
|
523
|
-
);
|
|
524
|
-
if (importsModified) {
|
|
525
|
-
modified = true;
|
|
526
|
-
transformed = oxcTransformed;
|
|
527
|
-
}
|
|
528
|
-
if (cache) {
|
|
529
|
-
const rulesAdded = /* @__PURE__ */ new Map();
|
|
530
|
-
for (const [key, afterRules] of requestedExports) {
|
|
531
|
-
const beforeRules = registryBefore.get(key);
|
|
532
|
-
const diff = diffRequestedExportRules(beforeRules, afterRules);
|
|
533
|
-
if (!diff) continue;
|
|
534
|
-
rulesAdded.set(key, diff);
|
|
535
|
-
}
|
|
536
|
-
const entry = {
|
|
537
|
-
transformed,
|
|
538
|
-
modified,
|
|
539
|
-
rulesAdded
|
|
540
|
-
};
|
|
541
|
-
cache.set(args.path, source, allowListForCache, entry);
|
|
542
|
-
}
|
|
543
|
-
if (!modified) return void 0;
|
|
544
|
-
const ext = extname(args.path).slice(1);
|
|
545
|
-
return { contents: transformed, loader: ext, resolveDir: dirname(args.path) };
|
|
546
|
-
});
|
|
547
|
-
}
|
|
548
|
-
};
|
|
549
|
-
}
|
|
550
333
|
export {
|
|
551
|
-
|
|
334
|
+
transformModuleImports
|
|
552
335
|
};
|
|
@@ -101,8 +101,3 @@ export declare class ClientGraphBoundaryCache {
|
|
|
101
101
|
hitRate: number;
|
|
102
102
|
};
|
|
103
103
|
}
|
|
104
|
-
/**
|
|
105
|
-
* Default shared cache. The React plugin owns one of these; tests can
|
|
106
|
-
* create their own.
|
|
107
|
-
*/
|
|
108
|
-
export declare const clientGraphBoundaryCache: ClientGraphBoundaryCache;
|
|
@@ -105,12 +105,10 @@ class ClientGraphBoundaryCache {
|
|
|
105
105
|
};
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
|
-
const clientGraphBoundaryCache = new ClientGraphBoundaryCache();
|
|
109
108
|
function hashAllowList(specifiers) {
|
|
110
109
|
const sorted = Array.from(specifiers).sort();
|
|
111
110
|
return rapidhash(sorted.join("\n"));
|
|
112
111
|
}
|
|
113
112
|
export {
|
|
114
|
-
ClientGraphBoundaryCache
|
|
115
|
-
clientGraphBoundaryCache
|
|
113
|
+
ClientGraphBoundaryCache
|
|
116
114
|
};
|
|
@@ -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,107 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, extname, resolve } from "node:path";
|
|
3
|
+
import { ClientGraphBoundaryCache } from "./boundary-cache.js";
|
|
4
|
+
import {
|
|
5
|
+
parseDeclaredModules,
|
|
6
|
+
toModuleBaseSpecifier,
|
|
7
|
+
mergeRequestedExportRules,
|
|
8
|
+
snapshotRegistry,
|
|
9
|
+
diffRequestedExportRules
|
|
10
|
+
} from "./specifier-classification.js";
|
|
11
|
+
import { transformModuleImports } from "./ast-transform.js";
|
|
12
|
+
const SOURCE_FILE_FILTER = /\.(tsx?|jsx?)$/;
|
|
13
|
+
function createClientGraphBoundaryPlugin(options) {
|
|
14
|
+
return {
|
|
15
|
+
name: "ecopages-client-graph-boundary",
|
|
16
|
+
setup(build) {
|
|
17
|
+
const absWorkingDir = options?.absWorkingDir ?? process.cwd();
|
|
18
|
+
const cache = options?.cache;
|
|
19
|
+
const globallyDeclaredSources = parseDeclaredModules(options?.declaredModules);
|
|
20
|
+
const requestedExports = /* @__PURE__ */ new Map();
|
|
21
|
+
for (const alwaysAllow of options?.alwaysAllowSpecifiers ?? []) {
|
|
22
|
+
globallyDeclaredSources.set(toModuleBaseSpecifier(alwaysAllow), "*");
|
|
23
|
+
}
|
|
24
|
+
const allowListForCache = Array.from(globallyDeclaredSources.keys()).sort();
|
|
25
|
+
build.onLoad({ filter: SOURCE_FILE_FILTER }, (args) => {
|
|
26
|
+
let source;
|
|
27
|
+
try {
|
|
28
|
+
source = readFileSync(args.path, "utf-8");
|
|
29
|
+
} catch {
|
|
30
|
+
return void 0;
|
|
31
|
+
}
|
|
32
|
+
if (cache) {
|
|
33
|
+
const cached = cache.get(args.path, source, allowListForCache);
|
|
34
|
+
if (cached) {
|
|
35
|
+
for (const [moduleKey, rules] of cached.rulesAdded) {
|
|
36
|
+
mergeRequestedExportRules(requestedExports, moduleKey, rules);
|
|
37
|
+
}
|
|
38
|
+
if (!cached.modified) return void 0;
|
|
39
|
+
const ext2 = extname(args.path).slice(1);
|
|
40
|
+
return { contents: cached.transformed, loader: ext2, resolveDir: dirname(args.path) };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
let transformed = source;
|
|
44
|
+
let modified = false;
|
|
45
|
+
if (source.includes("readFileSync")) {
|
|
46
|
+
const readFileTransformed = transformed.replace(
|
|
47
|
+
/\bfs\.readFileSync\s*\(\s*path\.resolve\s*\(\s*(['"`])([^'"`\n]+)\1\s*\)\s*,\s*['"`]utf-?8['"`]\s*\)/g,
|
|
48
|
+
(_match, _q, relPath) => {
|
|
49
|
+
modified = true;
|
|
50
|
+
try {
|
|
51
|
+
const sourceDir = dirname(args.path);
|
|
52
|
+
const srcDirIndex = args.path.lastIndexOf("/src/");
|
|
53
|
+
const inferredProjectRoot = srcDirIndex >= 0 ? args.path.slice(0, srcDirIndex) : void 0;
|
|
54
|
+
const candidates = [
|
|
55
|
+
resolve(absWorkingDir, relPath),
|
|
56
|
+
resolve(process.cwd(), relPath),
|
|
57
|
+
resolve(sourceDir, relPath),
|
|
58
|
+
...inferredProjectRoot ? [resolve(inferredProjectRoot, relPath)] : []
|
|
59
|
+
];
|
|
60
|
+
const absolutePath = candidates.find((candidate) => existsSync(candidate));
|
|
61
|
+
if (!absolutePath) return '""';
|
|
62
|
+
const content = readFileSync(absolutePath, "utf-8");
|
|
63
|
+
return JSON.stringify(content);
|
|
64
|
+
} catch {
|
|
65
|
+
return '""';
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
transformed = readFileTransformed;
|
|
70
|
+
}
|
|
71
|
+
const registryBefore = snapshotRegistry(requestedExports);
|
|
72
|
+
const { transformed: oxcTransformed, modified: importsModified } = transformModuleImports(
|
|
73
|
+
transformed,
|
|
74
|
+
args.path,
|
|
75
|
+
globallyDeclaredSources,
|
|
76
|
+
requestedExports,
|
|
77
|
+
options?.projectRoot
|
|
78
|
+
);
|
|
79
|
+
if (importsModified) {
|
|
80
|
+
modified = true;
|
|
81
|
+
transformed = oxcTransformed;
|
|
82
|
+
}
|
|
83
|
+
if (cache) {
|
|
84
|
+
const rulesAdded = /* @__PURE__ */ new Map();
|
|
85
|
+
for (const [key, afterRules] of requestedExports) {
|
|
86
|
+
const beforeRules = registryBefore.get(key);
|
|
87
|
+
const diff = diffRequestedExportRules(beforeRules, afterRules);
|
|
88
|
+
if (!diff) continue;
|
|
89
|
+
rulesAdded.set(key, diff);
|
|
90
|
+
}
|
|
91
|
+
const entry = {
|
|
92
|
+
transformed,
|
|
93
|
+
modified,
|
|
94
|
+
rulesAdded
|
|
95
|
+
};
|
|
96
|
+
cache.set(args.path, source, allowListForCache, entry);
|
|
97
|
+
}
|
|
98
|
+
if (!modified) return void 0;
|
|
99
|
+
const ext = extname(args.path).slice(1);
|
|
100
|
+
return { contents: transformed, loader: ext, resolveDir: dirname(args.path) };
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
export {
|
|
106
|
+
createClientGraphBoundaryPlugin
|
|
107
|
+
};
|
|
@@ -4,8 +4,10 @@ function walkConfigTree(config, visitor, visited = /* @__PURE__ */ new Set()) {
|
|
|
4
4
|
}
|
|
5
5
|
visited.add(config);
|
|
6
6
|
visitor(config);
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
for (const layout of config.layouts ?? []) {
|
|
8
|
+
if (layout?.config) {
|
|
9
|
+
walkConfigTree(layout.config, visitor, visited);
|
|
10
|
+
}
|
|
9
11
|
}
|
|
10
12
|
for (const component of config.dependencies?.components ?? []) {
|
|
11
13
|
walkConfigTree(component.config, visitor, visited);
|
|
@@ -6,7 +6,7 @@
|
|
|
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
|
*/
|
|
@@ -53,7 +53,7 @@ export type ReachabilityResult = {
|
|
|
53
53
|
type ExplicitlyRequestedExports = Set<string> | '*';
|
|
54
54
|
/**
|
|
55
55
|
* 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`).
|
|
56
|
+
* starting from client roots (`render`, `errorBoundary`, `loadingFallback` of `eco.page`, `eco.layout`, or `eco.component`).
|
|
57
57
|
*
|
|
58
58
|
* @param source - Raw source string of the module.
|
|
59
59
|
* @param filename - Absolute or relative path to the module file.
|
|
@@ -34,6 +34,9 @@ function analyzeReachability(source, filename, program, explicitlyRequestedExpor
|
|
|
34
34
|
const potentialClientRoots = [];
|
|
35
35
|
for (const statement of resolvedProgram.body) {
|
|
36
36
|
if (statement.type === "ImportDeclaration") {
|
|
37
|
+
if (statement.importKind === "type") {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
37
40
|
const specifier = statement.source.value;
|
|
38
41
|
const bindings = /* @__PURE__ */ new Map();
|
|
39
42
|
if (!statement.specifiers || statement.specifiers.length === 0) {
|
|
@@ -91,7 +94,7 @@ function analyzeReachability(source, filename, program, explicitlyRequestedExpor
|
|
|
91
94
|
if (node.type === "CallExpression" && node.callee.type === "MemberExpression") {
|
|
92
95
|
const obj = node.callee.object;
|
|
93
96
|
const prop = node.callee.property;
|
|
94
|
-
if (obj.type === "Identifier" && obj.name === "eco" && prop.type === "Identifier" && (prop.name === "page" || prop.name === "component")) {
|
|
97
|
+
if (obj.type === "Identifier" && obj.name === "eco" && prop.type === "Identifier" && (prop.name === "page" || prop.name === "component" || prop.name === "layout")) {
|
|
95
98
|
potentialClientRoots.push(node.callee);
|
|
96
99
|
const arg = node.arguments[0];
|
|
97
100
|
if (arg && arg.type === "ObjectExpression") {
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Specifier classification and requested-export registry helpers for the client graph boundary.
|
|
3
|
+
*/
|
|
4
|
+
import type { RequestedExportRules } from './boundary-cache.js';
|
|
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 as well as local module conventions such as
|
|
11
|
+
* `.server.ts` and extensionless imports that resolve to `.server.*` files.
|
|
12
|
+
*
|
|
13
|
+
* @param specifier - Raw import specifier from the module source.
|
|
14
|
+
* @returns True when the import must never become client-reachable.
|
|
15
|
+
*/
|
|
16
|
+
export declare function isServerOnlySpecifier(specifier: string): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Strips down a deep path module specifier to its foundational root package name.
|
|
19
|
+
*
|
|
20
|
+
* When checking against the "allowed modules" whitelist, a component might import something deeply
|
|
21
|
+
* nested like `lodash/fp/map` or `@myorg/ui/button`. However, the user configuration only whitelists
|
|
22
|
+
* the root `lodash` or `@myorg/ui` package. This normalizer ensures we are comparing apples to apples
|
|
23
|
+
* by extracting the base package scope before checking the authorization list.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* toModuleBaseSpecifier('@scope/package/deep/file') -> '@scope/package'
|
|
27
|
+
* toModuleBaseSpecifier('lodash/cloneDeep') -> 'lodash'
|
|
28
|
+
* toModuleBaseSpecifier('node:fs') -> 'node:fs'
|
|
29
|
+
*
|
|
30
|
+
* @param specifier - The raw import specifier from the code.
|
|
31
|
+
* @returns The root package name, preserving scoped npm organizations.
|
|
32
|
+
*/
|
|
33
|
+
export declare function toModuleBaseSpecifier(specifier: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Parses the grammar syntax of declared modules.
|
|
36
|
+
* Handles patterns like `@pkg/name` and `@pkg/name{namedImport,anotherImport}`
|
|
37
|
+
* returning a map of base packages to their explicitly allowed specifiers.
|
|
38
|
+
*
|
|
39
|
+
* @param moduleDeclarations - A list of module declaration strings.
|
|
40
|
+
* @returns A structured map of allowed packages and their named exports.
|
|
41
|
+
*/
|
|
42
|
+
export declare function parseDeclaredModules(moduleDeclarations: readonly string[] | undefined): Map<string, Set<string> | '*'>;
|
|
43
|
+
/**
|
|
44
|
+
* Merges two declared module maps, combining their allowable specific scopes.
|
|
45
|
+
*
|
|
46
|
+
* @param a - First map of declared modules.
|
|
47
|
+
* @param b - Second map of declared modules to merge into the first.
|
|
48
|
+
* @returns A unified map of both declarations.
|
|
49
|
+
*/
|
|
50
|
+
export declare function mergeDeclaredModulesMap(a: Map<string, Set<string> | '*'>, b: Map<string, Set<string> | '*'>): Map<string, Set<string> | '*'>;
|
|
51
|
+
export declare function normalizeRequestedExportsKey(pathname: string): string;
|
|
52
|
+
/**
|
|
53
|
+
* Resolves a local import specifier into a requested-export registry key.
|
|
54
|
+
*
|
|
55
|
+
* Bare package specifiers and project aliases are intentionally ignored because
|
|
56
|
+
* requested-export propagation is only used for cross-file local reachability.
|
|
57
|
+
*
|
|
58
|
+
* @param importer - Absolute path of the importing module.
|
|
59
|
+
* @param specifier - Raw import or re-export specifier.
|
|
60
|
+
* @returns Registry key for a local dependency, or `undefined` when not applicable.
|
|
61
|
+
*/
|
|
62
|
+
export declare function resolveRequestedExportsKey(importer: string, specifier: string): string | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Merges newly discovered requested-export rules into the local propagation registry.
|
|
65
|
+
*
|
|
66
|
+
* Once a module is promoted to `'*'`, it stays fully reachable for the remainder
|
|
67
|
+
* of the transform pass.
|
|
68
|
+
*
|
|
69
|
+
* @param registry - Cross-module requested-export registry.
|
|
70
|
+
* @param moduleKey - Normalized local module key.
|
|
71
|
+
* @param rules - Newly observed reachable export rules for the module.
|
|
72
|
+
*/
|
|
73
|
+
export declare function mergeRequestedExportRules(registry: Map<string, RequestedExportRules>, moduleKey: string, rules: Set<string> | '*'): void;
|
|
74
|
+
export declare function snapshotRegistry(registry: Map<string, RequestedExportRules>): Map<string, RequestedExportRules>;
|
|
75
|
+
export declare function diffRequestedExportRules(before: RequestedExportRules | undefined, after: RequestedExportRules): RequestedExportRules | undefined;
|