@ecopages/react 0.2.0-rc.0 → 0.2.0-rc.10
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 +68 -14
- package/package.json +17 -5
- package/src/app/client-only.js +3 -8
- package/src/bundling/bundle.js +4 -1
- package/src/bundling/runtime-alias-map.d.ts +0 -1
- package/src/bundling/runtime-alias-map.js +1 -4
- package/src/bundling/runtime-bundle.d.ts +9 -2
- package/src/bundling/runtime-bundle.js +27 -16
- package/src/bundling/runtime-modules.d.ts +21 -0
- package/src/bundling/runtime-modules.js +25 -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 +19 -0
- package/src/client-graph/ast-transform-walk-imports.js +103 -0
- package/src/client-graph/ast-transform.d.ts +1 -7
- package/src/client-graph/ast-transform.js +42 -378
- package/src/client-graph/ast-walk.d.ts +10 -0
- package/src/client-graph/ast-walk.js +26 -0
- package/src/client-graph/boundary-plugin-read-file-inline.d.ts +8 -0
- package/src/client-graph/boundary-plugin-read-file-inline.js +45 -0
- package/src/client-graph/boundary-plugin-transform.d.ts +18 -0
- package/src/client-graph/boundary-plugin-transform.js +61 -0
- package/src/client-graph/boundary-plugin.d.ts +0 -20
- package/src/client-graph/boundary-plugin.js +16 -98
- package/src/client-graph/reachability-analyzer.d.ts +4 -36
- package/src/client-graph/reachability-analyzer.js +23 -483
- 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 +3 -2
- package/src/client-graph/specifier-classification.js +6 -2
- package/src/content/collection-component-cache.d.ts +25 -0
- package/src/content/collection-component-cache.js +62 -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 +3 -0
- package/src/hmr/hmr-strategy.js +47 -86
- package/src/hmr/react-hmr-dev-transform-plugins.d.ts +1 -0
- package/src/hmr/react-hmr-dev-transform-plugins.js +4 -1
- package/src/hydration/browser/island-hydration.d.ts +82 -0
- package/src/hydration/browser/island-hydration.js +247 -0
- package/src/hydration/browser/page-hydration-hmr.d.ts +11 -0
- package/src/hydration/browser/page-hydration-hmr.js +56 -0
- package/src/hydration/browser/page-hydration-module.d.ts +8 -0
- package/src/hydration/browser/page-hydration-module.js +17 -0
- package/src/hydration/browser/page-hydration.d.ts +54 -0
- package/src/hydration/browser/page-hydration.js +177 -0
- package/src/hydration/compiled-page-hydration-options.d.ts +20 -0
- package/src/hydration/hydration-asset.d.ts +21 -1
- package/src/hydration/hydration-asset.js +54 -24
- package/src/hydration/hydration-lifecycle.test.browser.d.ts +1 -0
- package/src/hydration/hydration-lifecycle.test.browser.js +409 -0
- package/src/hydration/hydration-page-entry-source.d.ts +5 -0
- package/src/hydration/hydration-page-entry-source.js +87 -0
- package/src/hydration/hydration-script-compiler.d.ts +48 -0
- package/src/hydration/hydration-script-compiler.js +213 -0
- package/src/hydration/page-data-reader.d.ts +0 -14
- package/src/hydration/page-data-reader.js +1 -47
- package/src/mdx/mdx-loader-plugin.d.ts +5 -1
- package/src/mdx/mdx-loader-plugin.js +4 -2
- package/src/plugin/react.plugin.js +8 -1
- package/src/plugin/react.types.d.ts +11 -2
- package/src/render/component-ssr.d.ts +17 -0
- package/src/render/component-ssr.js +19 -1
- package/src/render/react-renderer.d.ts +4 -2
- package/src/render/react-renderer.js +13 -7
- package/src/render/unified-layout-composition.d.ts +2 -0
- package/src/render/unified-layout-composition.js +5 -1
- package/src/hydration/hydration-scripts.d.ts +0 -104
- package/src/hydration/hydration-scripts.js +0 -483
- package/src/hydration/hydration-scripts.test.browser.js +0 -199
- /package/src/hydration/{hydration-scripts.test.browser.d.ts → compiled-page-hydration-options.js} +0 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { resolvePageModule } from './page-hydration-module.js';
|
|
2
|
+
function getRuntime() {
|
|
3
|
+
return (window.__ECO_PAGES__ ??= {});
|
|
4
|
+
}
|
|
5
|
+
function isCurrentMountGeneration(generation) {
|
|
6
|
+
return (getRuntime().react?.mountGeneration ?? 0) === generation;
|
|
7
|
+
}
|
|
8
|
+
async function applyRouterHmrUpdate(options, newUrl, currentPage, nextPage, _nextProps) {
|
|
9
|
+
if (!options.hasRouter || getRuntime().navigation?.getOwnerState?.().owner !== 'react-router') {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
const currentLayoutStack = options.hmr.getLayoutStack?.(currentPage) ?? '';
|
|
13
|
+
const nextLayoutStack = options.hmr.getLayoutStack?.(nextPage.Page) ?? '';
|
|
14
|
+
await getRuntime().navigation?.reloadCurrentPage?.({
|
|
15
|
+
clearCache: currentLayoutStack !== nextLayoutStack,
|
|
16
|
+
moduleUrl: newUrl,
|
|
17
|
+
source: 'react-router',
|
|
18
|
+
});
|
|
19
|
+
console.log(`[ecopages] ${options.isMdx ? 'MDX' : 'React'} component updated via router`);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
function applyDirectHmrRender(options, nextPage, nextProps, readPageData, setPageState, generation) {
|
|
23
|
+
const activeRoot = getRuntime().react?.pageRoot ?? null;
|
|
24
|
+
if (!activeRoot || !isCurrentMountGeneration(generation))
|
|
25
|
+
return;
|
|
26
|
+
setPageState(options, readPageData(options));
|
|
27
|
+
activeRoot.render(options.createTree(nextPage.Page, nextProps));
|
|
28
|
+
console.log(`[ecopages] ${options.isMdx ? 'MDX' : 'React'} component updated`);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Imports and applies one HMR page update.
|
|
32
|
+
*/
|
|
33
|
+
export async function handlePageHmr(options, newUrl, generation, readPageData, setPageState, getPageDataFromDocument) {
|
|
34
|
+
if (!options.hmr || !isCurrentMountGeneration(generation))
|
|
35
|
+
return;
|
|
36
|
+
try {
|
|
37
|
+
const newModule = (await import(/* @vite-ignore */ newUrl));
|
|
38
|
+
if (!isCurrentMountGeneration(generation))
|
|
39
|
+
return;
|
|
40
|
+
const nextPage = resolvePageModule(newModule, options.isMdx, options.normalizePageConfig);
|
|
41
|
+
const nextProps = getPageDataFromDocument(options);
|
|
42
|
+
await nextPage.preload?.(nextProps);
|
|
43
|
+
if (!isCurrentMountGeneration(generation))
|
|
44
|
+
return;
|
|
45
|
+
const currentPage = options.Page;
|
|
46
|
+
options.Page = nextPage.Page;
|
|
47
|
+
options.preload = nextPage.preload;
|
|
48
|
+
if (await applyRouterHmrUpdate(options, newUrl, currentPage, nextPage, nextProps)) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
applyDirectHmrRender(options, nextPage, nextProps, readPageData, setPageState, generation);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.error(`[ecopages] Failed to hot-reload ${options.isMdx ? 'MDX' : 'React'} component:`, error);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type PageModule = {
|
|
2
|
+
Page: unknown;
|
|
3
|
+
preload?: (props: Record<string, unknown>) => void | Promise<void>;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Normalizes a loaded Page module into the shape used by hydration and HMR.
|
|
7
|
+
*/
|
|
8
|
+
export declare function resolvePageModule(module: Record<string, unknown>, isMdx: boolean, normalizePageConfig?: (config: unknown) => void): PageModule;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function isPagePreload(value) {
|
|
2
|
+
return typeof value === 'function';
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Normalizes a loaded Page module into the shape used by hydration and HMR.
|
|
6
|
+
*/
|
|
7
|
+
export function resolvePageModule(module, isMdx, normalizePageConfig) {
|
|
8
|
+
const Page = module.default ?? module;
|
|
9
|
+
if (isMdx && module.config && Page && typeof Page === 'function') {
|
|
10
|
+
Object.assign(Page, { config: module.config });
|
|
11
|
+
normalizePageConfig?.(module.config);
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
Page,
|
|
15
|
+
preload: isPagePreload(module.preload) ? module.preload : undefined,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-side React page lifecycle.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* The entry module supplies the page tree and module resolution details. This
|
|
6
|
+
* module owns document state, navigation ownership, cleanup, and HMR so those
|
|
7
|
+
* behaviors stay identical for readable and compiled bootstraps.
|
|
8
|
+
*/
|
|
9
|
+
export type PageRoot = {
|
|
10
|
+
render: (tree: unknown) => void;
|
|
11
|
+
unmount: () => void;
|
|
12
|
+
};
|
|
13
|
+
export type PageReactRuntime = {
|
|
14
|
+
hydrateRoot: (target: HTMLElement, tree: unknown, options?: {
|
|
15
|
+
onRecoverableError?: (error: unknown) => void;
|
|
16
|
+
}) => PageRoot;
|
|
17
|
+
createElement: (...args: unknown[]) => unknown;
|
|
18
|
+
};
|
|
19
|
+
export type PageData = {
|
|
20
|
+
moduleUrl?: string;
|
|
21
|
+
props: Record<string, unknown>;
|
|
22
|
+
};
|
|
23
|
+
export type { PageModule } from './page-hydration-module.js';
|
|
24
|
+
export { resolvePageModule } from './page-hydration-module.js';
|
|
25
|
+
/** Configuration passed from a generated Page hydration entry. */
|
|
26
|
+
export type PageHydrationOptions = {
|
|
27
|
+
scriptId: string;
|
|
28
|
+
pageModuleUrl: string;
|
|
29
|
+
Page: unknown;
|
|
30
|
+
pageDataReader: {
|
|
31
|
+
readPageDataDocument: () => PageData;
|
|
32
|
+
getPageDataFromDocument: () => Record<string, unknown>;
|
|
33
|
+
};
|
|
34
|
+
runtime: PageReactRuntime;
|
|
35
|
+
createTree: (Page: unknown, props: Record<string, unknown>) => unknown;
|
|
36
|
+
hasRouter: boolean;
|
|
37
|
+
isMdx: boolean;
|
|
38
|
+
normalizePageConfig?: (config: unknown) => void;
|
|
39
|
+
hmr?: {
|
|
40
|
+
importPath: string;
|
|
41
|
+
getLayoutStack?: (Page: unknown) => string;
|
|
42
|
+
};
|
|
43
|
+
preload?: (props: Record<string, unknown>) => void | Promise<void>;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Registers and starts the Page lifecycle when its persistent script is present.
|
|
47
|
+
*
|
|
48
|
+
* @remarks
|
|
49
|
+
* Restores lifecycle registration, router ownership, and HMR generation binding
|
|
50
|
+
* whenever the page is activated or rerun after navigation.
|
|
51
|
+
*
|
|
52
|
+
* @param options - Generated entry configuration for the current Page.
|
|
53
|
+
*/
|
|
54
|
+
export declare function startPageHydration(options: PageHydrationOptions): void;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-side React page lifecycle.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* The entry module supplies the page tree and module resolution details. This
|
|
6
|
+
* module owns document state, navigation ownership, cleanup, and HMR so those
|
|
7
|
+
* behaviors stay identical for readable and compiled bootstraps.
|
|
8
|
+
*/
|
|
9
|
+
export { resolvePageModule } from './page-hydration-module.js';
|
|
10
|
+
import { handlePageHmr } from './page-hydration-hmr.js';
|
|
11
|
+
/** Returns and lazily initializes the shared page runtime on `window`. */
|
|
12
|
+
function getRuntime() {
|
|
13
|
+
const pageWindow = window;
|
|
14
|
+
const runtime = (pageWindow.__ECO_PAGES__ ??= {});
|
|
15
|
+
runtime.react ??= {};
|
|
16
|
+
return runtime;
|
|
17
|
+
}
|
|
18
|
+
/** Records the development hydration duration when the dev marker is present. */
|
|
19
|
+
function recordHydrationTiming() {
|
|
20
|
+
const pageWindow = window;
|
|
21
|
+
if (pageWindow.__ECO_DEV_HYDRATION_STARTED__ == null)
|
|
22
|
+
return;
|
|
23
|
+
pageWindow.__ECO_DEV_HYDRATION_MS__ = Math.round(performance.now() - pageWindow.__ECO_DEV_HYDRATION_STARTED__);
|
|
24
|
+
}
|
|
25
|
+
/** Reads the current page-data document through the shared reader contract. */
|
|
26
|
+
function readPageData(options) {
|
|
27
|
+
return options.pageDataReader.readPageDataDocument();
|
|
28
|
+
}
|
|
29
|
+
/** Publishes the current module URL and props for router/navigation consumers. */
|
|
30
|
+
function setPageState(options, pageData) {
|
|
31
|
+
const runtime = getRuntime();
|
|
32
|
+
runtime.page = {
|
|
33
|
+
module: pageData.moduleUrl || options.pageModuleUrl,
|
|
34
|
+
props: pageData.props,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Invalidates pending startup, releases the page root, and hands navigation
|
|
39
|
+
* ownership back to the document runtime.
|
|
40
|
+
*
|
|
41
|
+
* @remarks
|
|
42
|
+
* Incrementing the generation prevents an already-running preload from
|
|
43
|
+
* hydrating after cleanup. The pending promise is cleared so a later page can
|
|
44
|
+
* start independently while the old preload settles.
|
|
45
|
+
*/
|
|
46
|
+
function cleanupPageRoot(root) {
|
|
47
|
+
const runtime = getRuntime();
|
|
48
|
+
if (runtime.react) {
|
|
49
|
+
runtime.react.mountGeneration = (runtime.react.mountGeneration ?? 0) + 1;
|
|
50
|
+
delete runtime.react.mountPromise;
|
|
51
|
+
}
|
|
52
|
+
const activeRoot = runtime.react?.pageRoot || root;
|
|
53
|
+
runtime.react.pageRoot = null;
|
|
54
|
+
runtime.navigation?.releaseOwnership?.('react-router');
|
|
55
|
+
delete runtime.page;
|
|
56
|
+
activeRoot?.unmount();
|
|
57
|
+
}
|
|
58
|
+
/** Registers React Router as the page navigation owner when this page uses it. */
|
|
59
|
+
function registerRouterOwnership(options) {
|
|
60
|
+
if (!options.hasRouter)
|
|
61
|
+
return;
|
|
62
|
+
const runtime = getRuntime();
|
|
63
|
+
const ownerState = runtime.navigation?.getOwnerState?.();
|
|
64
|
+
if (ownerState?.owner === 'react-router' && ownerState.canHandleSpaNavigation)
|
|
65
|
+
return;
|
|
66
|
+
runtime.navigation?.register?.({
|
|
67
|
+
owner: 'react-router',
|
|
68
|
+
cleanupBeforeHandoff: async () => cleanupPageRoot(runtime.react?.pageRoot ?? null),
|
|
69
|
+
});
|
|
70
|
+
runtime.navigation?.claimOwnership?.('react-router');
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Checks whether asynchronous work still belongs to the current page instance.
|
|
74
|
+
*
|
|
75
|
+
* @remarks
|
|
76
|
+
* Cleanup increments this token before unmounting. Any import or preload that
|
|
77
|
+
* settles afterward must leave the document alone, even if a new page has
|
|
78
|
+
* already mounted a root in the same global runtime slot.
|
|
79
|
+
*/
|
|
80
|
+
function isCurrentMountGeneration(generation) {
|
|
81
|
+
return (getRuntime().react?.mountGeneration ?? 0) === generation;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Registers HMR before initial preload and binds it to this page generation.
|
|
85
|
+
*
|
|
86
|
+
* @remarks
|
|
87
|
+
* The global handler index can outlive the page. Capturing the generation here
|
|
88
|
+
* makes retained callbacks inert after cleanup, including calls that begin only
|
|
89
|
+
* after another page has mounted. Reactivation renews this binding for the
|
|
90
|
+
* new generation.
|
|
91
|
+
*/
|
|
92
|
+
function registerHmr(options) {
|
|
93
|
+
if (!options.hmr)
|
|
94
|
+
return;
|
|
95
|
+
const runtime = getRuntime();
|
|
96
|
+
const generation = runtime.react?.mountGeneration ?? 0;
|
|
97
|
+
runtime.hmrHandlers ??= {};
|
|
98
|
+
runtime.hmrHandlers[options.hmr.importPath] = (newUrl) => handlePageHmr(options, newUrl, generation, readPageData, setPageState, (opts) => opts.pageDataReader.getPageDataFromDocument());
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Coalesces concurrent page starts into one promise.
|
|
102
|
+
*
|
|
103
|
+
* @remarks
|
|
104
|
+
* Rerun scripts and DOM-ready callbacks can arrive together. Sharing the
|
|
105
|
+
* pending promise prevents two calls to `hydrateRoot` for document.body.
|
|
106
|
+
*/
|
|
107
|
+
async function mount(options) {
|
|
108
|
+
const runtime = getRuntime();
|
|
109
|
+
const reactRuntime = runtime.react;
|
|
110
|
+
if (reactRuntime.mountPromise)
|
|
111
|
+
return reactRuntime.mountPromise;
|
|
112
|
+
const generation = reactRuntime.mountGeneration ?? 0;
|
|
113
|
+
const pendingMount = mountPage(options, generation);
|
|
114
|
+
reactRuntime.mountPromise = pendingMount;
|
|
115
|
+
pendingMount.then(() => {
|
|
116
|
+
if (reactRuntime.mountPromise === pendingMount)
|
|
117
|
+
delete reactRuntime.mountPromise;
|
|
118
|
+
}, () => {
|
|
119
|
+
if (reactRuntime.mountPromise === pendingMount)
|
|
120
|
+
delete reactRuntime.mountPromise;
|
|
121
|
+
});
|
|
122
|
+
return pendingMount;
|
|
123
|
+
}
|
|
124
|
+
/** Performs one page start after the caller has reserved the current generation. */
|
|
125
|
+
async function mountPage(options, generation) {
|
|
126
|
+
const runtime = getRuntime();
|
|
127
|
+
const pageData = readPageData(options);
|
|
128
|
+
setPageState(options, pageData);
|
|
129
|
+
if (runtime.react?.pageRoot) {
|
|
130
|
+
if (!options.hasRouter) {
|
|
131
|
+
runtime.react.pageRoot.render(options.createTree(options.Page, pageData.props));
|
|
132
|
+
}
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
await options.preload?.(pageData.props);
|
|
136
|
+
if (!isCurrentMountGeneration(generation))
|
|
137
|
+
return;
|
|
138
|
+
if (runtime.react?.pageRoot)
|
|
139
|
+
return;
|
|
140
|
+
window.__ECO_DEV_HYDRATION_STARTED__ = performance.now();
|
|
141
|
+
const root = options.runtime.hydrateRoot(document.body, options.createTree(options.Page, pageData.props), {
|
|
142
|
+
onRecoverableError: (error) => console.warn('[ecopages] Hydration error:', error),
|
|
143
|
+
});
|
|
144
|
+
runtime.react.pageRoot = root;
|
|
145
|
+
recordHydrationTiming();
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Registers and starts the Page lifecycle when its persistent script is present.
|
|
149
|
+
*
|
|
150
|
+
* @remarks
|
|
151
|
+
* Restores lifecycle registration, router ownership, and HMR generation binding
|
|
152
|
+
* whenever the page is activated or rerun after navigation.
|
|
153
|
+
*
|
|
154
|
+
* @param options - Generated entry configuration for the current Page.
|
|
155
|
+
*/
|
|
156
|
+
export function startPageHydration(options) {
|
|
157
|
+
if (!document.querySelector(`script[data-eco-script-id="${options.scriptId}"]`))
|
|
158
|
+
return;
|
|
159
|
+
const runtime = getRuntime();
|
|
160
|
+
runtime.react.pageRoot ??= null;
|
|
161
|
+
const initialPageData = readPageData(options);
|
|
162
|
+
setPageState(options, initialPageData);
|
|
163
|
+
const activatePage = () => {
|
|
164
|
+
runtime.react.cleanupPageRoot = () => cleanupPageRoot(runtime.react?.pageRoot ?? null);
|
|
165
|
+
registerRouterOwnership(options);
|
|
166
|
+
registerHmr(options);
|
|
167
|
+
return mount(options);
|
|
168
|
+
};
|
|
169
|
+
runtime.rerunScripts ??= {};
|
|
170
|
+
runtime.rerunScripts[options.scriptId] = activatePage;
|
|
171
|
+
if (document.readyState === 'loading') {
|
|
172
|
+
document.addEventListener('DOMContentLoaded', activatePage, { once: true });
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
void activatePage();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Complete inputs that affect a Page hydration script's generated source. */
|
|
2
|
+
export type CompiledPageHydrationOptions = {
|
|
3
|
+
importPath: string;
|
|
4
|
+
pageModuleUrlExpression: string;
|
|
5
|
+
scriptId: string;
|
|
6
|
+
reactImportPath: string;
|
|
7
|
+
reactDomClientImportPath: string;
|
|
8
|
+
routerImportPath?: string;
|
|
9
|
+
layoutComposeImportPath: string;
|
|
10
|
+
pageLayoutNormalizationImportPath: string;
|
|
11
|
+
routerComponents?: {
|
|
12
|
+
router: string;
|
|
13
|
+
pageContent: string;
|
|
14
|
+
};
|
|
15
|
+
routerPropsExpression?: string;
|
|
16
|
+
hmrEnabled: boolean;
|
|
17
|
+
isMdx: boolean;
|
|
18
|
+
hasPagePreload: boolean;
|
|
19
|
+
minify: boolean;
|
|
20
|
+
};
|
|
@@ -31,13 +31,28 @@ type PageDependencyOptions = {
|
|
|
31
31
|
hmrEnabled: boolean;
|
|
32
32
|
useBrowserRuntimeImports: boolean;
|
|
33
33
|
isMdx: boolean;
|
|
34
|
+
hasPagePreload?: boolean;
|
|
34
35
|
};
|
|
36
|
+
/**
|
|
37
|
+
* Derives the stable component key used by asset grouping and host selectors.
|
|
38
|
+
*
|
|
39
|
+
* @remarks
|
|
40
|
+
* The key identifies the component entry. Individual SSR instances receive a
|
|
41
|
+
* separate component ID, so repeated uses share browser assets without sharing
|
|
42
|
+
* a React root.
|
|
43
|
+
*
|
|
44
|
+
* @param componentFile - Absolute source path of the React component.
|
|
45
|
+
* @param config - Optional component identity used to distinguish entries.
|
|
46
|
+
* @returns Stable key used in selectors and asset names.
|
|
47
|
+
*/
|
|
35
48
|
export declare function getIslandComponentKey(componentFile: string, config?: EcoComponentConfig): string;
|
|
36
49
|
/**
|
|
37
50
|
* Manages the creation of client-side hydration assets for React pages and component islands.
|
|
38
51
|
*/
|
|
39
52
|
export declare class HydrationAssetService {
|
|
40
53
|
private readonly config;
|
|
54
|
+
private readonly islandScriptCompiler;
|
|
55
|
+
private readonly pageScriptCompiler;
|
|
41
56
|
private static readonly ROUTER_PAGE_GROUPED_BUNDLE_ID;
|
|
42
57
|
constructor(config: HydrationAssetServiceConfig);
|
|
43
58
|
private getIslandBundleName;
|
|
@@ -54,8 +69,13 @@ export declare class HydrationAssetService {
|
|
|
54
69
|
resolveAssetImportPath(pagePath: string, assetName: string): Promise<string>;
|
|
55
70
|
/**
|
|
56
71
|
* Creates the page-owned route entry asset for hydration and client navigation.
|
|
72
|
+
*
|
|
73
|
+
* @remarks
|
|
74
|
+
* The entry is compiled asynchronously from the editable browser lifecycle.
|
|
75
|
+
* Production router pages may be grouped into the Page Browser Graph, while
|
|
76
|
+
* HMR entries remain separate ESM scripts with browser-resolvable imports.
|
|
57
77
|
*/
|
|
58
|
-
createPageDependencies(options: PageDependencyOptions): AssetDefinition[]
|
|
78
|
+
createPageDependencies(options: PageDependencyOptions): Promise<AssetDefinition[]>;
|
|
59
79
|
/**
|
|
60
80
|
* Builds client-side assets for a React component island.
|
|
61
81
|
*
|
|
@@ -11,9 +11,23 @@ import { rapidhash } from '@ecopages/core/hash';
|
|
|
11
11
|
import { RESOLVED_ASSETS_DIR } from '@ecopages/core/constants';
|
|
12
12
|
import { isReactProductionRuntime } from '../bundling/runtime-mode.js';
|
|
13
13
|
import { AssetFactory, } from '@ecopages/core/services/asset-processing-service';
|
|
14
|
-
import {
|
|
14
|
+
import { IslandHydrationScriptCompiler, PageHydrationScriptCompiler } from './hydration-script-compiler.js';
|
|
15
15
|
import { resolveHydrationBootstrapImports } from './bootstrap-imports.js';
|
|
16
16
|
import { collectDeclaredModulesInConfig } from '../client-graph/declared-modules.js';
|
|
17
|
+
import { hasPagePreloadExport } from '../client-graph/reachability-analyzer.js';
|
|
18
|
+
import { fileSystem } from '@ecopages/file-system';
|
|
19
|
+
/**
|
|
20
|
+
* Derives the stable component key used by asset grouping and host selectors.
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* The key identifies the component entry. Individual SSR instances receive a
|
|
24
|
+
* separate component ID, so repeated uses share browser assets without sharing
|
|
25
|
+
* a React root.
|
|
26
|
+
*
|
|
27
|
+
* @param componentFile - Absolute source path of the React component.
|
|
28
|
+
* @param config - Optional component identity used to distinguish entries.
|
|
29
|
+
* @returns Stable key used in selectors and asset names.
|
|
30
|
+
*/
|
|
17
31
|
export function getIslandComponentKey(componentFile, config) {
|
|
18
32
|
return rapidhash(`${componentFile}:${config?.identity?.id ?? ''}`).toString();
|
|
19
33
|
}
|
|
@@ -22,6 +36,8 @@ export function getIslandComponentKey(componentFile, config) {
|
|
|
22
36
|
*/
|
|
23
37
|
export class HydrationAssetService {
|
|
24
38
|
config;
|
|
39
|
+
islandScriptCompiler = new IslandHydrationScriptCompiler();
|
|
40
|
+
pageScriptCompiler = new PageHydrationScriptCompiler();
|
|
25
41
|
static ROUTER_PAGE_GROUPED_BUNDLE_ID = 'ecopages-react-router-pages';
|
|
26
42
|
constructor(config) {
|
|
27
43
|
this.config = config;
|
|
@@ -59,15 +75,15 @@ export class HydrationAssetService {
|
|
|
59
75
|
}
|
|
60
76
|
/**
|
|
61
77
|
* Creates the page-owned route entry asset for hydration and client navigation.
|
|
78
|
+
*
|
|
79
|
+
* @remarks
|
|
80
|
+
* The entry is compiled asynchronously from the editable browser lifecycle.
|
|
81
|
+
* Production router pages may be grouped into the Page Browser Graph, while
|
|
82
|
+
* HMR entries remain separate ESM scripts with browser-resolvable imports.
|
|
62
83
|
*/
|
|
63
|
-
createPageDependencies(options) {
|
|
64
|
-
const { pagePath, componentName, importPath, pageModuleUrlExpression, bundleOptions, hmrEnabled, useBrowserRuntimeImports, isMdx, } = options;
|
|
84
|
+
async createPageDependencies(options) {
|
|
85
|
+
const { pagePath, componentName, importPath, pageModuleUrlExpression, bundleOptions, hmrEnabled, useBrowserRuntimeImports, isMdx, hasPagePreload, } = options;
|
|
65
86
|
const runtimeImports = this.config.bundleService.getRuntimeImports();
|
|
66
|
-
/**
|
|
67
|
-
* @remarks
|
|
68
|
-
* Production router pages share a grouped bundle. Development HMR keeps each
|
|
69
|
-
* page bootstrap independent and unbundled; shared helpers resolve via vendor URLs.
|
|
70
|
-
*/
|
|
71
87
|
const groupedBundle = !hmrEnabled && this.config.routerAdapter
|
|
72
88
|
? {
|
|
73
89
|
id: HydrationAssetService.ROUTER_PAGE_GROUPED_BUNDLE_ID,
|
|
@@ -79,22 +95,26 @@ export class HydrationAssetService {
|
|
|
79
95
|
runtimeImports,
|
|
80
96
|
routerAdapterImportPath: this.config.routerAdapter?.bundle.importPath,
|
|
81
97
|
});
|
|
98
|
+
const pageHydrationScript = await this.pageScriptCompiler.compile({
|
|
99
|
+
importPath: hmrEnabled ? importPath : pagePath,
|
|
100
|
+
pageModuleUrlExpression,
|
|
101
|
+
scriptId: componentName,
|
|
102
|
+
reactImportPath: bootstrapImports.reactImportPath,
|
|
103
|
+
reactDomClientImportPath: bootstrapImports.reactDomClientImportPath,
|
|
104
|
+
routerImportPath: bootstrapImports.routerImportPath,
|
|
105
|
+
layoutComposeImportPath: bootstrapImports.layoutComposeImportPath,
|
|
106
|
+
pageLayoutNormalizationImportPath: bootstrapImports.pageLayoutNormalizationImportPath,
|
|
107
|
+
routerComponents: this.config.routerAdapter?.components,
|
|
108
|
+
routerPropsExpression: this.config.routerAdapter?.getRouterProps('Page', 'props'),
|
|
109
|
+
hmrEnabled,
|
|
110
|
+
isMdx,
|
|
111
|
+
hasPagePreload: hasPagePreload === true,
|
|
112
|
+
minify: !hmrEnabled && isReactProductionRuntime(),
|
|
113
|
+
});
|
|
82
114
|
return [
|
|
83
115
|
AssetFactory.createContentScript({
|
|
84
116
|
position: 'head',
|
|
85
|
-
content:
|
|
86
|
-
importPath: hmrEnabled ? importPath : pagePath,
|
|
87
|
-
pageModuleUrlExpression,
|
|
88
|
-
reactImportPath: bootstrapImports.reactImportPath,
|
|
89
|
-
reactDomClientImportPath: bootstrapImports.reactDomClientImportPath,
|
|
90
|
-
routerImportPath: bootstrapImports.routerImportPath,
|
|
91
|
-
layoutComposeImportPath: bootstrapImports.layoutComposeImportPath,
|
|
92
|
-
pageLayoutNormalizationImportPath: bootstrapImports.pageLayoutNormalizationImportPath,
|
|
93
|
-
hmrEnabled,
|
|
94
|
-
isMdx,
|
|
95
|
-
router: this.config.routerAdapter,
|
|
96
|
-
scriptId: componentName,
|
|
97
|
-
}),
|
|
117
|
+
content: pageHydrationScript,
|
|
98
118
|
name: componentName,
|
|
99
119
|
packageRole: 'page-script',
|
|
100
120
|
/**
|
|
@@ -137,7 +157,7 @@ export class HydrationAssetService {
|
|
|
137
157
|
}
|
|
138
158
|
const importPath = await this.resolveAssetImportPath(componentFile, componentName);
|
|
139
159
|
const runtimeImports = this.config.bundleService.getRuntimeImports();
|
|
140
|
-
const
|
|
160
|
+
const islandHydrationOptions = {
|
|
141
161
|
importPath,
|
|
142
162
|
scriptId: hydrationName,
|
|
143
163
|
reactImportPath: runtimeImports.react,
|
|
@@ -147,7 +167,8 @@ export class HydrationAssetService {
|
|
|
147
167
|
componentFile,
|
|
148
168
|
minify: !hmrEnabled,
|
|
149
169
|
hmrEnabled,
|
|
150
|
-
}
|
|
170
|
+
};
|
|
171
|
+
const islandHydrationScript = await this.islandScriptCompiler.compile(islandHydrationOptions);
|
|
151
172
|
const hydrationScript = AssetFactory.createContentScript({
|
|
152
173
|
position: 'head',
|
|
153
174
|
content: islandHydrationScript,
|
|
@@ -211,8 +232,16 @@ export class HydrationAssetService {
|
|
|
211
232
|
}
|
|
212
233
|
const importPath = await this.resolveAssetImportPath(pagePath, componentName);
|
|
213
234
|
const pageModuleUrlExpression = hmrEnabled ? JSON.stringify(importPath) : 'import.meta.url';
|
|
235
|
+
let hasPagePreload = false;
|
|
236
|
+
try {
|
|
237
|
+
const source = await fileSystem.readFile(pagePath);
|
|
238
|
+
hasPagePreload = hasPagePreloadExport(source, pagePath);
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
hasPagePreload = false;
|
|
242
|
+
}
|
|
214
243
|
const bundleOptions = await this.config.bundleService.createBundleOptions(componentName, isMdx, declaredModules, { includeRuntime: !useBrowserRuntimeImports, splitting: usesRouterRuntime });
|
|
215
|
-
const dependencies = this.createPageDependencies({
|
|
244
|
+
const dependencies = await this.createPageDependencies({
|
|
216
245
|
pagePath,
|
|
217
246
|
componentName,
|
|
218
247
|
importPath,
|
|
@@ -221,6 +250,7 @@ export class HydrationAssetService {
|
|
|
221
250
|
hmrEnabled,
|
|
222
251
|
useBrowserRuntimeImports,
|
|
223
252
|
isMdx,
|
|
253
|
+
hasPagePreload,
|
|
224
254
|
});
|
|
225
255
|
return dependencies;
|
|
226
256
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|