@ecopages/react 0.2.1 → 0.3.0
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 +178 -35
- package/package.json +80 -32
- package/src/app/client-only.js +10 -0
- package/src/app/dynamic.js +9 -0
- package/src/app/dynamic.test.browser.d.ts +1 -0
- package/src/app/dynamic.test.browser.js +38 -0
- package/src/bundling/bootstrap-package-paths.d.ts +5 -0
- package/src/bundling/bootstrap-package-paths.js +5 -0
- package/src/bundling/bundle.d.ts +78 -0
- package/src/bundling/bundle.js +130 -0
- package/src/bundling/runtime-alias-map.d.ts +12 -0
- package/src/bundling/runtime-alias-map.js +110 -0
- package/src/bundling/runtime-bundle.d.ts +70 -0
- package/src/bundling/runtime-bundle.js +233 -0
- package/src/{utils/react-dom-runtime-interop-plugin.d.ts → bundling/runtime-interop-plugin.d.ts} +1 -1
- package/src/bundling/runtime-interop-plugin.js +32 -0
- package/src/bundling/runtime-mode.d.ts +1 -0
- package/src/bundling/runtime-mode.js +10 -0
- package/src/bundling/runtime-modules.d.ts +21 -0
- package/src/bundling/runtime-modules.js +28 -0
- package/src/client-graph/ast-transform-eco-page.d.ts +10 -0
- package/src/client-graph/ast-transform-eco-page.js +71 -0
- package/src/client-graph/ast-transform-import-edits.d.ts +13 -0
- package/src/client-graph/ast-transform-import-edits.js +75 -0
- package/src/client-graph/ast-transform-walk-imports.d.ts +20 -0
- package/src/client-graph/ast-transform-walk-imports.js +105 -0
- package/src/client-graph/ast-transform.d.ts +12 -0
- package/src/client-graph/ast-transform.js +91 -0
- package/src/client-graph/ast-walk.d.ts +10 -0
- package/src/client-graph/ast-walk.js +26 -0
- package/src/client-graph/boundary-plugin-read-file-inline.d.ts +7 -0
- package/src/client-graph/boundary-plugin-read-file-inline.js +43 -0
- package/src/client-graph/boundary-plugin-transform.d.ts +16 -0
- package/src/client-graph/boundary-plugin-transform.js +31 -0
- package/src/client-graph/boundary-plugin.d.ts +23 -0
- package/src/client-graph/boundary-plugin.js +177 -0
- package/src/client-graph/component-config-traversal.d.ts +36 -0
- package/src/client-graph/component-config-traversal.js +77 -0
- package/src/{utils → client-graph}/declared-modules.d.ts +1 -1
- package/src/client-graph/declared-modules.js +69 -0
- package/src/client-graph/module-classification.d.ts +3 -0
- package/src/client-graph/module-classification.js +43 -0
- package/src/client-graph/reachability-analyzer.d.ts +26 -0
- package/src/client-graph/reachability-analyzer.js +55 -0
- package/src/client-graph/reachability-client-roots.d.ts +8 -0
- package/src/client-graph/reachability-client-roots.js +59 -0
- package/src/client-graph/reachability-export-names.d.ts +5 -0
- package/src/client-graph/reachability-export-names.js +34 -0
- package/src/client-graph/reachability-fallback-roots.d.ts +3 -0
- package/src/client-graph/reachability-fallback-roots.js +71 -0
- package/src/client-graph/reachability-preload.d.ts +10 -0
- package/src/client-graph/reachability-preload.js +30 -0
- package/src/client-graph/reachability-top-level-index.d.ts +14 -0
- package/src/client-graph/reachability-top-level-index.js +93 -0
- package/src/client-graph/reachability-traverse.d.ts +7 -0
- package/src/client-graph/reachability-traverse.js +201 -0
- package/src/client-graph/specifier-classification.d.ts +74 -0
- package/src/client-graph/specifier-classification.js +191 -0
- package/src/content/collection-component-cache.d.ts +25 -0
- package/src/content/collection-component-cache.js +62 -0
- package/src/{router-adapter.d.ts → contracts/router-adapter.d.ts} +7 -14
- package/src/contracts/router-adapter.js +6 -0
- package/src/dev-transform/react-dev-transform-contributor.d.ts +17 -0
- package/src/dev-transform/react-dev-transform-contributor.js +22 -0
- package/src/hmr/hmr-strategy-process.d.ts +34 -0
- package/src/hmr/hmr-strategy-process.js +83 -0
- package/src/hmr/hmr-strategy.d.ts +67 -0
- package/src/hmr/hmr-strategy.js +306 -0
- package/src/{services/react-hmr-page-metadata-cache.d.ts → hmr/page-metadata-cache.d.ts} +6 -2
- package/src/hmr/page-metadata-cache.js +44 -0
- package/src/hmr/react-hmr-dev-transform-plugins.d.ts +22 -0
- package/src/hmr/react-hmr-dev-transform-plugins.js +19 -0
- package/src/hydration/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/browser/island-hydration.d.ts +82 -0
- package/src/hydration/browser/island-hydration.js +251 -0
- package/src/hydration/browser/page-hydration-hmr.d.ts +11 -0
- package/src/hydration/browser/page-hydration-hmr.js +56 -0
- package/src/hydration/browser/page-hydration-module.d.ts +8 -0
- package/src/hydration/browser/page-hydration-module.js +17 -0
- package/src/hydration/browser/page-hydration.d.ts +54 -0
- package/src/hydration/browser/page-hydration.js +177 -0
- package/src/hydration/compiled-page-hydration-options.d.ts +20 -0
- package/src/hydration/compiled-page-hydration-options.js +1 -0
- package/src/hydration/hydration-asset.d.ts +127 -0
- package/src/hydration/hydration-asset.js +379 -0
- package/src/hydration/hydration-lifecycle.test.browser.d.ts +1 -0
- package/src/hydration/hydration-lifecycle.test.browser.js +461 -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-manifest.d.ts +58 -0
- package/src/hydration/page-data-manifest.js +97 -0
- package/src/hydration/page-data-reader.d.ts +17 -0
- package/src/hydration/page-data-reader.js +31 -0
- package/src/hydration/page-payload.d.ts +68 -0
- package/src/hydration/page-payload.js +106 -0
- package/src/hydration/serialize-page-data-script.d.ts +27 -0
- package/src/hydration/serialize-page-data-script.js +30 -0
- package/src/mdx/mdx-config-dependency.d.ts +37 -0
- package/src/mdx/mdx-config-dependency.js +107 -0
- package/src/mdx/mdx-loader-plugin.d.ts +8 -0
- package/src/mdx/mdx-loader-plugin.js +11 -0
- package/src/page/page-module.d.ts +46 -0
- package/src/page/page-module.js +74 -0
- package/src/plugin/react.constants.d.ts +1 -0
- package/src/plugin/react.constants.js +1 -0
- package/src/plugin/react.plugin.d.ts +71 -0
- package/src/plugin/react.plugin.js +210 -0
- package/src/plugin/react.types.d.ts +106 -0
- package/src/plugin/react.types.js +1 -0
- package/src/render/component-ownership.d.ts +49 -0
- package/src/render/component-ownership.js +47 -0
- package/src/render/component-ssr.d.ts +59 -0
- package/src/render/component-ssr.js +76 -0
- package/src/render/eco-embed.d.ts +11 -0
- package/src/render/eco-embed.js +9 -0
- package/src/render/errors.d.ts +16 -0
- package/src/render/errors.js +23 -0
- package/src/render/html-boundary.js +59 -0
- package/src/render/layout-compose.d.ts +66 -0
- package/src/render/layout-compose.js +134 -0
- package/src/render/react-renderer.d.ts +114 -0
- package/src/render/react-renderer.js +490 -0
- package/src/render/react-runtime.d.ts +13 -0
- package/src/render/react-runtime.js +26 -0
- package/src/render/unified-layout-composition.d.ts +22 -0
- package/src/render/unified-layout-composition.js +31 -0
- package/src/runtime/use-sync-external-store-with-selector.d.ts +3 -0
- package/src/runtime/use-sync-external-store-with-selector.js +54 -0
- package/CHANGELOG.md +0 -29
- package/src/react-hmr-strategy.d.ts +0 -148
- package/src/react-hmr-strategy.js +0 -313
- package/src/react-renderer.d.ts +0 -213
- package/src/react-renderer.js +0 -654
- package/src/react.plugin.d.ts +0 -147
- package/src/react.plugin.js +0 -146
- package/src/router-adapter.js +0 -0
- package/src/services/react-bundle.service.d.ts +0 -47
- package/src/services/react-bundle.service.js +0 -93
- package/src/services/react-hmr-page-metadata-cache.js +0 -35
- package/src/services/react-hydration-asset.service.d.ts +0 -76
- package/src/services/react-hydration-asset.service.js +0 -210
- package/src/services/react-page-module.service.d.ts +0 -83
- package/src/services/react-page-module.service.js +0 -162
- package/src/services/react-runtime-bundle.service.d.ts +0 -40
- package/src/services/react-runtime-bundle.service.js +0 -130
- package/src/utils/client-graph-boundary-plugin.d.ts +0 -43
- package/src/utils/client-graph-boundary-plugin.js +0 -494
- package/src/utils/client-only.js +0 -19
- package/src/utils/declared-modules.js +0 -59
- package/src/utils/dynamic.js +0 -12
- package/src/utils/hmr-scripts.d.ts +0 -18
- package/src/utils/hmr-scripts.js +0 -31
- package/src/utils/html-boundary.js +0 -55
- package/src/utils/hydration-scripts.d.ts +0 -86
- package/src/utils/hydration-scripts.js +0 -285
- package/src/utils/reachability-analyzer.d.ts +0 -66
- package/src/utils/reachability-analyzer.js +0 -339
- package/src/utils/react-dom-runtime-interop-plugin.js +0 -29
- package/src/utils/react-mdx-loader-plugin.d.ts +0 -3
- package/src/utils/react-mdx-loader-plugin.js +0 -45
- package/src/utils/react-runtime-specifier-map.d.ts +0 -6
- package/src/utils/react-runtime-specifier-map.js +0 -37
- package/src/utils/use-sync-external-store-shim-plugin.d.ts +0 -5
- package/src/utils/use-sync-external-store-shim-plugin.js +0 -41
- /package/src/{utils → app}/client-only.d.ts +0 -0
- /package/src/{utils → app}/dynamic.d.ts +0 -0
- /package/src/{declarations.d.ts → contracts/declarations.d.ts} +0 -0
- /package/src/{utils → render}/html-boundary.d.ts +0 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Specifier classification and requested-export registry helpers for the client graph boundary.
|
|
3
|
+
*/
|
|
4
|
+
import { loadTsconfigPathPrefixes, matchesTsconfigPathPrefix } from '@ecopages/core/build';
|
|
5
|
+
import { toPackageRootSpecifier } from '@ecopages/core/build';
|
|
6
|
+
import { isContentServerVirtualModule } from '@ecopages/core/build';
|
|
7
|
+
import { isServerOnlyModuleSpecifier } from '@ecopages/core/build/contracts';
|
|
8
|
+
import { dirname, resolve } from 'node:path';
|
|
9
|
+
export function isBareSpecifier(specifier) {
|
|
10
|
+
if (specifier.startsWith('.'))
|
|
11
|
+
return false;
|
|
12
|
+
if (specifier.startsWith('/'))
|
|
13
|
+
return false;
|
|
14
|
+
if (specifier.includes('://'))
|
|
15
|
+
return false;
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
export function isProjectAliasSpecifier(specifier, projectRoot) {
|
|
19
|
+
if (specifier.startsWith('ecopages:')) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
if (projectRoot) {
|
|
23
|
+
return matchesTsconfigPathPrefix(specifier, loadTsconfigPathPrefixes(projectRoot));
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Determines whether a specifier should be treated as server-only.
|
|
29
|
+
*
|
|
30
|
+
* This covers Node built-ins, content collection `/server` virtual modules,
|
|
31
|
+
* and local module conventions such as `.server.ts` and extensionless imports
|
|
32
|
+
* that resolve to `.server.*` files.
|
|
33
|
+
*
|
|
34
|
+
* @param specifier - Raw import specifier from the module source.
|
|
35
|
+
* @returns True when the import must never become client-reachable.
|
|
36
|
+
*/
|
|
37
|
+
export function isServerOnlySpecifier(specifier) {
|
|
38
|
+
if (specifier.startsWith('node:'))
|
|
39
|
+
return true;
|
|
40
|
+
if (isContentServerVirtualModule(specifier))
|
|
41
|
+
return true;
|
|
42
|
+
return isServerOnlyModuleSpecifier(specifier);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Strips down a deep path module specifier to its foundational root package name.
|
|
46
|
+
*
|
|
47
|
+
* When checking against the "allowed modules" whitelist, a component might import something deeply
|
|
48
|
+
* nested like `lodash/fp/map` or `@myorg/ui/button`. However, the user configuration only whitelists
|
|
49
|
+
* the root `lodash` or `@myorg/ui` package. This normalizer ensures we are comparing apples to apples
|
|
50
|
+
* by extracting the base package scope before checking the authorization list.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* toModuleBaseSpecifier('@scope/package/deep/file') -> '@scope/package'
|
|
54
|
+
* toModuleBaseSpecifier('lodash/cloneDeep') -> 'lodash'
|
|
55
|
+
* toModuleBaseSpecifier('node:fs') -> 'node:fs'
|
|
56
|
+
*
|
|
57
|
+
* @param specifier - The raw import specifier from the code.
|
|
58
|
+
* @returns The root package name, preserving scoped npm organizations.
|
|
59
|
+
*/
|
|
60
|
+
export function toModuleBaseSpecifier(specifier) {
|
|
61
|
+
return toPackageRootSpecifier(specifier);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Parses the grammar syntax of declared modules.
|
|
65
|
+
* Handles patterns like `@pkg/name` and `@pkg/name{namedImport,anotherImport}`
|
|
66
|
+
* returning a map of base packages to their explicitly allowed specifiers.
|
|
67
|
+
*
|
|
68
|
+
* @param moduleDeclarations - A list of module declaration strings.
|
|
69
|
+
* @returns A structured map of allowed packages and their named exports.
|
|
70
|
+
*/
|
|
71
|
+
export function parseDeclaredModules(moduleDeclarations) {
|
|
72
|
+
const map = new Map();
|
|
73
|
+
for (const declaration of moduleDeclarations ?? []) {
|
|
74
|
+
const source = declaration.trim();
|
|
75
|
+
if (source.length === 0)
|
|
76
|
+
continue;
|
|
77
|
+
const openBraceIndex = source.indexOf('{');
|
|
78
|
+
if (openBraceIndex < 0) {
|
|
79
|
+
map.set(toModuleBaseSpecifier(source), '*');
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const closeBraceIndex = source.indexOf('}', openBraceIndex);
|
|
83
|
+
const rawPkg = source.slice(0, openBraceIndex).trim();
|
|
84
|
+
if (rawPkg.length === 0)
|
|
85
|
+
continue;
|
|
86
|
+
const pkg = toModuleBaseSpecifier(rawPkg);
|
|
87
|
+
const namedImportsStr = closeBraceIndex > openBraceIndex
|
|
88
|
+
? source.slice(openBraceIndex + 1, closeBraceIndex)
|
|
89
|
+
: source.slice(openBraceIndex + 1);
|
|
90
|
+
const namedImports = namedImportsStr
|
|
91
|
+
.split(',')
|
|
92
|
+
.map((s) => s.trim())
|
|
93
|
+
.filter(Boolean);
|
|
94
|
+
const existing = map.get(pkg);
|
|
95
|
+
if (existing === '*')
|
|
96
|
+
continue;
|
|
97
|
+
if (!existing) {
|
|
98
|
+
if (namedImports.length === 0) {
|
|
99
|
+
map.set(pkg, '*');
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
map.set(pkg, new Set(namedImports));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
for (const name of namedImports) {
|
|
107
|
+
existing.add(name);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return map;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Merges two declared module maps, combining their allowable specific scopes.
|
|
115
|
+
*
|
|
116
|
+
* @param a - First map of declared modules.
|
|
117
|
+
* @param b - Second map of declared modules to merge into the first.
|
|
118
|
+
* @returns A unified map of both declarations.
|
|
119
|
+
*/
|
|
120
|
+
export function mergeDeclaredModulesMap(a, b) {
|
|
121
|
+
const result = new Map(a);
|
|
122
|
+
for (const [pkg, imports] of b.entries()) {
|
|
123
|
+
const existing = result.get(pkg);
|
|
124
|
+
if (existing === '*')
|
|
125
|
+
continue;
|
|
126
|
+
if (imports === '*') {
|
|
127
|
+
result.set(pkg, '*');
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (!existing) {
|
|
131
|
+
result.set(pkg, imports);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
for (const name of imports) {
|
|
135
|
+
existing.add(name);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
export function normalizeRequestedExportsKey(pathname) {
|
|
142
|
+
let normalized = pathname.replace(/\\/g, '/');
|
|
143
|
+
normalized = normalized.replace(/\.(tsx?|jsx?)$/i, '');
|
|
144
|
+
if (normalized.endsWith('/index')) {
|
|
145
|
+
normalized = normalized.slice(0, -'/index'.length);
|
|
146
|
+
}
|
|
147
|
+
return normalized;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Resolves a local import specifier into a requested-export registry key.
|
|
151
|
+
*
|
|
152
|
+
* Bare package specifiers and project aliases are intentionally ignored because
|
|
153
|
+
* requested-export propagation is only used for cross-file local reachability.
|
|
154
|
+
*
|
|
155
|
+
* @param importer - Absolute path of the importing module.
|
|
156
|
+
* @param specifier - Raw import or re-export specifier.
|
|
157
|
+
* @returns Registry key for a local dependency, or `undefined` when not applicable.
|
|
158
|
+
*/
|
|
159
|
+
export function resolveRequestedExportsKey(importer, specifier) {
|
|
160
|
+
if (isBareSpecifier(specifier) || isProjectAliasSpecifier(specifier)) {
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
const resolved = specifier.startsWith('/') ? specifier : resolve(dirname(importer), specifier);
|
|
164
|
+
return normalizeRequestedExportsKey(resolved);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Merges newly discovered requested-export rules into the local propagation registry.
|
|
168
|
+
*
|
|
169
|
+
* Once a module is promoted to `'*'`, it stays fully reachable for the remainder
|
|
170
|
+
* of the transform pass.
|
|
171
|
+
*
|
|
172
|
+
* @param registry - Cross-module requested-export registry.
|
|
173
|
+
* @param moduleKey - Normalized local module key.
|
|
174
|
+
* @param rules - Newly observed reachable export rules for the module.
|
|
175
|
+
*/
|
|
176
|
+
export function mergeRequestedExportRules(registry, moduleKey, rules) {
|
|
177
|
+
const existing = registry.get(moduleKey);
|
|
178
|
+
if (existing === '*')
|
|
179
|
+
return;
|
|
180
|
+
if (rules === '*') {
|
|
181
|
+
registry.set(moduleKey, '*');
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
if (!existing) {
|
|
185
|
+
registry.set(moduleKey, new Set(rules));
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
for (const rule of rules) {
|
|
189
|
+
existing.add(rule);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { EcoComponent } from '@ecopages/core';
|
|
2
|
+
export type CollectionComponentCache = {
|
|
3
|
+
preload(slug: string): Promise<void>;
|
|
4
|
+
prime(slug: string, component: Promise<EcoComponent<Record<string, unknown>>>): Promise<void>;
|
|
5
|
+
get(slug: string): EcoComponent<Record<string, unknown>>;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Creates the synchronous component lookup required by a Page that preloads
|
|
9
|
+
* collection MDX before SSR, hydration, and client navigation.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* `preload()` loads the browser-compiled component and must not run in
|
|
13
|
+
* `staticProps`. A hydratable collection Page primes the cache in `staticProps`
|
|
14
|
+
* with its server component, then exposes a named `preload` export for
|
|
15
|
+
* hydration and client navigation to load the browser component.
|
|
16
|
+
*
|
|
17
|
+
* Completed loads are deliberately refreshed by the next preload. Native
|
|
18
|
+
* module caching keeps that inexpensive while allowing HMR to replace a
|
|
19
|
+
* component before the Page re-renders.
|
|
20
|
+
*
|
|
21
|
+
* `prime()` always stores the provided server component, even when a
|
|
22
|
+
* `preload()` is already in flight. Completions from a superseded load are
|
|
23
|
+
* ignored so they cannot replace the primed entry.
|
|
24
|
+
*/
|
|
25
|
+
export declare function createCollectionComponentCache(loadComponent: (slug: string) => Promise<EcoComponent<Record<string, unknown>>>): CollectionComponentCache;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates the synchronous component lookup required by a Page that preloads
|
|
3
|
+
* collection MDX before SSR, hydration, and client navigation.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* `preload()` loads the browser-compiled component and must not run in
|
|
7
|
+
* `staticProps`. A hydratable collection Page primes the cache in `staticProps`
|
|
8
|
+
* with its server component, then exposes a named `preload` export for
|
|
9
|
+
* hydration and client navigation to load the browser component.
|
|
10
|
+
*
|
|
11
|
+
* Completed loads are deliberately refreshed by the next preload. Native
|
|
12
|
+
* module caching keeps that inexpensive while allowing HMR to replace a
|
|
13
|
+
* component before the Page re-renders.
|
|
14
|
+
*
|
|
15
|
+
* `prime()` always stores the provided server component, even when a
|
|
16
|
+
* `preload()` is already in flight. Completions from a superseded load are
|
|
17
|
+
* ignored so they cannot replace the primed entry.
|
|
18
|
+
*/
|
|
19
|
+
export function createCollectionComponentCache(loadComponent) {
|
|
20
|
+
const records = new Map();
|
|
21
|
+
const store = async (slug, component) => {
|
|
22
|
+
const pending = {};
|
|
23
|
+
const promise = component.then((resolvedComponent) => {
|
|
24
|
+
if (records.get(slug) === pending.record) {
|
|
25
|
+
records.set(slug, { status: 'success', component: resolvedComponent });
|
|
26
|
+
}
|
|
27
|
+
}, (error) => {
|
|
28
|
+
if (records.get(slug) === pending.record) {
|
|
29
|
+
records.set(slug, { status: 'error', error });
|
|
30
|
+
}
|
|
31
|
+
throw error;
|
|
32
|
+
});
|
|
33
|
+
pending.record = { status: 'pending', promise };
|
|
34
|
+
records.set(slug, pending.record);
|
|
35
|
+
return await promise;
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
async preload(slug) {
|
|
39
|
+
const existing = records.get(slug);
|
|
40
|
+
if (existing?.status === 'pending') {
|
|
41
|
+
return await existing.promise;
|
|
42
|
+
}
|
|
43
|
+
return await store(slug, loadComponent(slug));
|
|
44
|
+
},
|
|
45
|
+
async prime(slug, component) {
|
|
46
|
+
return await store(slug, component);
|
|
47
|
+
},
|
|
48
|
+
get(slug) {
|
|
49
|
+
const record = records.get(slug);
|
|
50
|
+
if (!record) {
|
|
51
|
+
throw new Error(`[ecopages] Collection content '${slug}' was rendered before it was preloaded.`);
|
|
52
|
+
}
|
|
53
|
+
if (record.status === 'success') {
|
|
54
|
+
return record.component;
|
|
55
|
+
}
|
|
56
|
+
if (record.status === 'error') {
|
|
57
|
+
throw record.error;
|
|
58
|
+
}
|
|
59
|
+
throw new Error(`[ecopages] Collection content '${slug}' is still loading during render.`);
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -12,11 +12,10 @@
|
|
|
12
12
|
* const myRouter: ReactRouterAdapter = {
|
|
13
13
|
* name: 'my-router',
|
|
14
14
|
* bundle: {
|
|
15
|
-
* importPath: '@my/router/browser
|
|
15
|
+
* importPath: '@my/router/browser',
|
|
16
16
|
* outputName: 'my-router',
|
|
17
17
|
* externals: ['react', 'react-dom'],
|
|
18
18
|
* },
|
|
19
|
-
* importMapKey: '@my/router',
|
|
20
19
|
* components: {
|
|
21
20
|
* router: 'MyRouter',
|
|
22
21
|
* pageContent: 'PageOutlet',
|
|
@@ -36,39 +35,33 @@ export interface ReactRouterAdapter {
|
|
|
36
35
|
bundle: {
|
|
37
36
|
/**
|
|
38
37
|
* Node module import path for the browser-compatible entry.
|
|
39
|
-
* @example '@ecopages/react-router/browser
|
|
38
|
+
* @example '@ecopages/react-router/browser'
|
|
40
39
|
*/
|
|
41
40
|
importPath: string;
|
|
42
41
|
/**
|
|
43
42
|
* Output filename (without extension).
|
|
44
|
-
* @example '
|
|
43
|
+
* @example 'my-router'
|
|
45
44
|
*/
|
|
46
45
|
outputName: string;
|
|
47
46
|
/**
|
|
48
47
|
* Packages to externalize when bundling.
|
|
49
|
-
* These should
|
|
50
|
-
* @example ['react', 'react-dom'
|
|
48
|
+
* These should stay as bare runtime dependencies for the router bundle.
|
|
49
|
+
* @example ['react', 'react-dom']
|
|
51
50
|
*/
|
|
52
51
|
externals: string[];
|
|
53
52
|
};
|
|
54
|
-
/**
|
|
55
|
-
* Bare specifier for the runtime mapping entry.
|
|
56
|
-
* This is what the hydration script will import from.
|
|
57
|
-
* @example '@ecopages/react-router'
|
|
58
|
-
*/
|
|
59
|
-
importMapKey: string;
|
|
60
53
|
/**
|
|
61
54
|
* Component names to import from the router package.
|
|
62
55
|
*/
|
|
63
56
|
components: {
|
|
64
57
|
/**
|
|
65
58
|
* The router component that wraps the layout.
|
|
66
|
-
* @example '
|
|
59
|
+
* @example 'MyRouter'
|
|
67
60
|
*/
|
|
68
61
|
router: string;
|
|
69
62
|
/**
|
|
70
63
|
* The component that renders the current page content.
|
|
71
|
-
* @example '
|
|
64
|
+
* @example 'PageOutlet'
|
|
72
65
|
*/
|
|
73
66
|
pageContent: string;
|
|
74
67
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { EcoBuildPlugin } from '@ecopages/core/integration';
|
|
2
|
+
import type { DevTransformBundleContributor } from '@ecopages/core/integration';
|
|
3
|
+
import type { ReactHmrStrategy } from '../hmr/hmr-strategy.js';
|
|
4
|
+
export type ReactDevTransformContributorOptions = {
|
|
5
|
+
strategy: ReactHmrStrategy;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Supplies React module plugins for the core dev transform bundler.
|
|
9
|
+
*/
|
|
10
|
+
export declare class ReactDevTransformContributor implements DevTransformBundleContributor {
|
|
11
|
+
private readonly strategy;
|
|
12
|
+
constructor(options: ReactDevTransformContributorOptions);
|
|
13
|
+
ownsModule(sourcePath: string): boolean;
|
|
14
|
+
getModulePlugins(sourcePath: string): Promise<readonly EcoBuildPlugin[]>;
|
|
15
|
+
getRuntimeSpecifierMap(): ReadonlyMap<string, string>;
|
|
16
|
+
getVendorBundlePlugins(): Promise<readonly EcoBuildPlugin[]>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getBrowserRuntimeSpecifierMap } from '@ecopages/core/build';
|
|
2
|
+
/**
|
|
3
|
+
* Supplies React module plugins for the core dev transform bundler.
|
|
4
|
+
*/
|
|
5
|
+
export class ReactDevTransformContributor {
|
|
6
|
+
strategy;
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.strategy = options.strategy;
|
|
9
|
+
}
|
|
10
|
+
ownsModule(sourcePath) {
|
|
11
|
+
return this.strategy.ownsDevTransformEntrypoint(sourcePath);
|
|
12
|
+
}
|
|
13
|
+
async getModulePlugins(sourcePath) {
|
|
14
|
+
return this.strategy.createDevTransformPlugins(sourcePath);
|
|
15
|
+
}
|
|
16
|
+
getRuntimeSpecifierMap() {
|
|
17
|
+
return getBrowserRuntimeSpecifierMap(this.strategy.getRuntimeManifest());
|
|
18
|
+
}
|
|
19
|
+
async getVendorBundlePlugins() {
|
|
20
|
+
return this.strategy.getVendorBundlePlugins();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { HmrAction } from '@ecopages/core/integration';
|
|
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
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React HMR Strategy
|
|
3
|
+
*
|
|
4
|
+
* Handles hot module replacement for React components.
|
|
5
|
+
* Triggers module invalidation on changes to ensure fresh component re-renders.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { HmrStrategy, type HmrAction } from '@ecopages/core/integration';
|
|
10
|
+
import type { EcoBuildPlugin } from '@ecopages/core/integration';
|
|
11
|
+
import type { BrowserRuntimeManifest } from '@ecopages/core/build';
|
|
12
|
+
import type { DefaultHmrContext } from '@ecopages/core/integration';
|
|
13
|
+
import type { HmrPageMetadataCache } from './page-metadata-cache.js';
|
|
14
|
+
export interface ReactHmrStrategyOptions {
|
|
15
|
+
context: DefaultHmrContext;
|
|
16
|
+
projectRoot?: string;
|
|
17
|
+
pageMetadataCache: HmrPageMetadataCache;
|
|
18
|
+
runtimeManifest: BrowserRuntimeManifest;
|
|
19
|
+
getMdxLoaderPlugin?: () => EcoBuildPlugin;
|
|
20
|
+
ownedTemplateExtensions?: string[];
|
|
21
|
+
allTemplateExtensions?: string[];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Strategy for handling React component HMR updates.
|
|
25
|
+
*/
|
|
26
|
+
export declare class ReactHmrStrategy extends HmrStrategy {
|
|
27
|
+
readonly type: 100;
|
|
28
|
+
private readonly getMdxLoaderPlugin?;
|
|
29
|
+
private readonly projectRoot;
|
|
30
|
+
private readonly ownedTemplateExtensions;
|
|
31
|
+
private readonly allTemplateExtensions;
|
|
32
|
+
private readonly context;
|
|
33
|
+
private readonly pageMetadataCache;
|
|
34
|
+
private readonly runtimeManifest;
|
|
35
|
+
private importNodePageModule;
|
|
36
|
+
private getDevTransformPluginOptions;
|
|
37
|
+
constructor(options: ReactHmrStrategyOptions);
|
|
38
|
+
private getBuildPlugins;
|
|
39
|
+
private isReactEntrypoint;
|
|
40
|
+
/**
|
|
41
|
+
* @remarks
|
|
42
|
+
* React integration owns plain `.tsx` route templates. Compound extensions in
|
|
43
|
+
* pages/layouts are integration-specific route templates and should not be
|
|
44
|
+
* claimed by React HMR strategy.
|
|
45
|
+
*/
|
|
46
|
+
private isRouteTemplate;
|
|
47
|
+
private resolveTemplateExtension;
|
|
48
|
+
private isDevTransformOutputUrl;
|
|
49
|
+
private queueDevTransformOutputUpdates;
|
|
50
|
+
private ownsWatchedEntrypoint;
|
|
51
|
+
private configContainsFile;
|
|
52
|
+
private pageModuleRequiresLayoutRefresh;
|
|
53
|
+
private hasLayoutOwnedDependencyTarget;
|
|
54
|
+
matches(filePath: string): boolean;
|
|
55
|
+
ownsDevTransformEntrypoint(sourcePath: string): boolean;
|
|
56
|
+
getRuntimeManifest(): BrowserRuntimeManifest;
|
|
57
|
+
getVendorBundlePlugins(): Promise<readonly EcoBuildPlugin[]>;
|
|
58
|
+
createDevTransformPlugins(sourcePath: string): Promise<EcoBuildPlugin[]>;
|
|
59
|
+
private isLayoutFile;
|
|
60
|
+
private isPageEntrypoint;
|
|
61
|
+
private resolveEntrypointOutputUrl;
|
|
62
|
+
private collectReactPageBuildTargets;
|
|
63
|
+
private resolveBuildTargets;
|
|
64
|
+
private partitionBuildTargets;
|
|
65
|
+
private createProcessContext;
|
|
66
|
+
process(_filePath: string): Promise<HmrAction>;
|
|
67
|
+
}
|