@ecopages/react 0.2.0-rc.6 → 0.2.0-rc.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -4
- package/package.json +5 -4
- package/src/hydration/browser/island-hydration.d.ts +82 -0
- package/src/hydration/browser/island-hydration.js +247 -0
- package/src/hydration/browser/page-hydration.d.ts +70 -0
- package/src/hydration/browser/page-hydration.js +254 -0
- package/src/hydration/hydration-asset.d.ts +20 -1
- package/src/hydration/hydration-asset.js +42 -24
- package/src/hydration/hydration-lifecycle.test.browser.js +408 -0
- package/src/hydration/hydration-script-compiler.d.ts +66 -0
- package/src/hydration/hydration-script-compiler.js +268 -0
- package/src/hydration/page-data-reader.d.ts +0 -14
- package/src/hydration/page-data-reader.js +1 -47
- package/src/render/component-ssr.d.ts +17 -0
- package/src/render/component-ssr.js +19 -1
- package/src/render/react-renderer.d.ts +2 -1
- package/src/render/react-renderer.js +5 -1
- package/src/render/unified-layout-composition.d.ts +2 -0
- package/src/render/unified-layout-composition.js +5 -1
- package/src/hydration/hydration-scripts.d.ts +0 -112
- package/src/hydration/hydration-scripts.js +0 -517
- package/src/hydration/hydration-scripts.test.browser.js +0 -199
- /package/src/hydration/{hydration-scripts.test.browser.d.ts → hydration-lifecycle.test.browser.d.ts} +0 -0
|
@@ -1,517 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Hydration script generators for React pages.
|
|
3
|
-
* These functions create the page entry modules that hydrate React routes.
|
|
4
|
-
* @module
|
|
5
|
-
*/
|
|
6
|
-
import { getDevPageDataReaderBootstrapSource } from './page-data-reader.js';
|
|
7
|
-
function getPageDataReaderSource(options) {
|
|
8
|
-
if (options.pageDataReaderImportPath) {
|
|
9
|
-
return `import { readPageDataDocument, getPageDataFromDocument as getPageData } from "${options.pageDataReaderImportPath}";`;
|
|
10
|
-
}
|
|
11
|
-
return getDevPageDataReaderBootstrapSource();
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Generates the page component import for the hydration entry.
|
|
15
|
-
*
|
|
16
|
-
* @remarks
|
|
17
|
-
* MDX pages need a namespace import so `config` can be copied onto the default
|
|
18
|
-
* export before layout normalization runs. Named `preload` is imported only
|
|
19
|
-
* when the Page actually exports it.
|
|
20
|
-
*/
|
|
21
|
-
function getImportStatement(importPath, isMdx, pageLayoutNormalizationImportPath, hasPagePreload) {
|
|
22
|
-
if (isMdx) {
|
|
23
|
-
const preloadLine = hasPagePreload ? '\nconst preload = MDXModule.preload;' : '';
|
|
24
|
-
return `import * as MDXModule from "${importPath}";
|
|
25
|
-
import { ensurePageConfigLayouts } from "${pageLayoutNormalizationImportPath}";
|
|
26
|
-
const Page = MDXModule.default;${preloadLine}
|
|
27
|
-
if (MDXModule.config) {
|
|
28
|
-
Page.config = MDXModule.config;
|
|
29
|
-
ensurePageConfigLayouts(Page.config);
|
|
30
|
-
}`;
|
|
31
|
-
}
|
|
32
|
-
if (hasPagePreload) {
|
|
33
|
-
return `import * as PageModule from "${importPath}";
|
|
34
|
-
const Page = PageModule.default;
|
|
35
|
-
const preload = PageModule.preload;`;
|
|
36
|
-
}
|
|
37
|
-
return `import Page from "${importPath}";`;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Assigns `NewPage` from a hot-reloaded module, including MDX `config` reattach.
|
|
41
|
-
*/
|
|
42
|
-
function getHmrImportStatement(isMdx, hasPagePreload) {
|
|
43
|
-
const preloadLine = hasPagePreload ? '\nconst nextPreload = newModule.preload;' : '';
|
|
44
|
-
return isMdx
|
|
45
|
-
? `const NewPage = newModule.default;${preloadLine}
|
|
46
|
-
if (newModule.config) {
|
|
47
|
-
NewPage.config = newModule.config;
|
|
48
|
-
ensurePageConfigLayouts(NewPage.config);
|
|
49
|
-
}`
|
|
50
|
-
: `const NewPage = newModule.default;${preloadLine}`;
|
|
51
|
-
}
|
|
52
|
-
function getPreloadAwaitStatement(identifier, propsIdentifier, hasPagePreload) {
|
|
53
|
-
return hasPagePreload ? `await ${identifier}?.(${propsIdentifier});` : '';
|
|
54
|
-
}
|
|
55
|
-
function getPreloadReexportStatement(hasPagePreload) {
|
|
56
|
-
return hasPagePreload ? 'export { preload };' : '';
|
|
57
|
-
}
|
|
58
|
-
function getComponentType(isMdx) {
|
|
59
|
-
return isMdx ? 'MDX' : 'React';
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Browser hook that unmounts the page React root and releases navigation ownership.
|
|
63
|
-
*
|
|
64
|
-
* @remarks
|
|
65
|
-
* Emitted into the page bootstrap so browser-router / react-router handoff can
|
|
66
|
-
* tear down React without knowing React root internals.
|
|
67
|
-
*/
|
|
68
|
-
function getPageRootCleanupScript() {
|
|
69
|
-
return `window.__ECO_PAGES__ = window.__ECO_PAGES__ || {};
|
|
70
|
-
window.__ECO_PAGES__.react = window.__ECO_PAGES__.react || {};
|
|
71
|
-
window.__ECO_PAGES__.react.cleanupPageRoot = () => {
|
|
72
|
-
const activeRoot = window.__ECO_PAGES__.react?.pageRoot || root;
|
|
73
|
-
if (!activeRoot) {
|
|
74
|
-
window.__ECO_PAGES__.react.pageRoot = null;
|
|
75
|
-
window.__ECO_PAGES__?.navigation?.releaseOwnership?.("react-router");
|
|
76
|
-
delete window.__ECO_PAGES__.page;
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
window.__ECO_PAGES__.react.pageRoot = null;
|
|
80
|
-
window.__ECO_PAGES__?.navigation?.releaseOwnership?.("react-router");
|
|
81
|
-
delete window.__ECO_PAGES__.page;
|
|
82
|
-
root = null;
|
|
83
|
-
activeRoot.unmount();
|
|
84
|
-
};`;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Registers react-router with the shared navigation coordinator when it does
|
|
88
|
-
* not already own SPA navigation.
|
|
89
|
-
*/
|
|
90
|
-
function getRouterBootstrapRegistrationScript() {
|
|
91
|
-
return `const currentOwnerState = window.__ECO_PAGES__?.navigation?.getOwnerState?.();
|
|
92
|
-
if (!(currentOwnerState?.owner === "react-router" && currentOwnerState.canHandleSpaNavigation)) {
|
|
93
|
-
window.__ECO_PAGES__?.navigation?.register({
|
|
94
|
-
owner: "react-router",
|
|
95
|
-
cleanupBeforeHandoff: async () => {
|
|
96
|
-
window.__ECO_PAGES__?.react?.cleanupPageRoot?.();
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
window.__ECO_PAGES__?.navigation?.claimOwnership?.("react-router");
|
|
100
|
-
}`;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Predicate used by router bootstraps to keep a long-lived hydrateRoot across
|
|
104
|
-
* client navigations when react-router already owns the document.
|
|
105
|
-
*/
|
|
106
|
-
function getReuseExistingRouterRootScript() {
|
|
107
|
-
return `const shouldReuseExistingRouterRoot = () => {
|
|
108
|
-
const ownerState = window.__ECO_PAGES__?.navigation?.getOwnerState?.();
|
|
109
|
-
return Boolean(
|
|
110
|
-
window.__ECO_PAGES__.react?.pageRoot &&
|
|
111
|
-
ownerState?.owner === "react-router" &&
|
|
112
|
-
ownerState.canHandleSpaNavigation
|
|
113
|
-
);
|
|
114
|
-
};`;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Registers `mount` under `window.__ECO_PAGES__.rerunScripts` for after-swap re-execution.
|
|
118
|
-
*/
|
|
119
|
-
function getRerunRegistrationScript(scriptId) {
|
|
120
|
-
return `window.__ECO_PAGES__ = window.__ECO_PAGES__ || {};
|
|
121
|
-
window.__ECO_PAGES__.rerunScripts = window.__ECO_PAGES__.rerunScripts || {};
|
|
122
|
-
window.__ECO_PAGES__.rerunScripts[${JSON.stringify(scriptId)}] = mount;`;
|
|
123
|
-
}
|
|
124
|
-
function getHydrationTimingScript() {
|
|
125
|
-
return `const __ecoRecordHydrationReady = () => {
|
|
126
|
-
if (window.__ECO_DEV_HYDRATION_STARTED__ == null) return;
|
|
127
|
-
window.__ECO_DEV_HYDRATION_MS__ = Math.round(performance.now() - window.__ECO_DEV_HYDRATION_STARTED__);
|
|
128
|
-
};`;
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* HMR handler for router pages: prefers coordinator reload when layouts change,
|
|
132
|
-
* otherwise re-renders the existing root.
|
|
133
|
-
*/
|
|
134
|
-
function getRouterHmrHandlerScript(options) {
|
|
135
|
-
const { importPath, isMdx, hasPagePreload } = options;
|
|
136
|
-
const preloadAwait = getPreloadAwaitStatement('nextPreload', 'nextProps', hasPagePreload);
|
|
137
|
-
return ` window.__ECO_PAGES__.hmrHandlers["${importPath}"] = async (newUrl) => {
|
|
138
|
-
try {
|
|
139
|
-
const newModule = await import(newUrl);
|
|
140
|
-
const nextProps = getPageData();
|
|
141
|
-
${getHmrImportStatement(isMdx, hasPagePreload)}
|
|
142
|
-
${preloadAwait}
|
|
143
|
-
const currentPageLayoutStack = (Component) =>
|
|
144
|
-
(Component.config?.layouts ?? []).map((layout) => layout?.config?.identity?.file ?? '').join('|');
|
|
145
|
-
const currentPageLayoutStackKey = currentPageLayoutStack(Page);
|
|
146
|
-
const nextPageLayoutStackKey = currentPageLayoutStack(NewPage);
|
|
147
|
-
|
|
148
|
-
if (window.__ECO_PAGES__?.navigation?.getOwnerState().owner === "react-router") {
|
|
149
|
-
await window.__ECO_PAGES__?.navigation?.reloadCurrentPage?.({
|
|
150
|
-
clearCache: currentPageLayoutStackKey !== nextPageLayoutStackKey,
|
|
151
|
-
moduleUrl: newUrl,
|
|
152
|
-
source: "react-router"
|
|
153
|
-
});
|
|
154
|
-
console.log("[ecopages] ${getComponentType(isMdx)} component updated via router");
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const nextPageData = readPageDataDocument();
|
|
159
|
-
window.__ECO_PAGES__.page = {
|
|
160
|
-
module: nextPageData.moduleUrl || pageModuleUrl,
|
|
161
|
-
props: nextProps
|
|
162
|
-
};
|
|
163
|
-
root.render(createTree(NewPage, nextProps));
|
|
164
|
-
console.log("[ecopages] ${getComponentType(isMdx)} component updated");
|
|
165
|
-
} catch (e) {
|
|
166
|
-
console.error("[ecopages] Failed to hot-reload ${getComponentType(isMdx)} component:", e);
|
|
167
|
-
}
|
|
168
|
-
};`;
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* HMR handler for non-router pages: hot-imports the module and re-renders the layout tree.
|
|
172
|
-
*/
|
|
173
|
-
function getNonRouterHmrHandlerScript(options) {
|
|
174
|
-
const { importPath, isMdx, hasPagePreload } = options;
|
|
175
|
-
const preloadAwait = getPreloadAwaitStatement('nextPreload', 'props', hasPagePreload);
|
|
176
|
-
return ` window.__ECO_PAGES__.hmrHandlers["${importPath}"] = async (newUrl) => {
|
|
177
|
-
try {
|
|
178
|
-
const newModule = await import(newUrl);
|
|
179
|
-
${getHmrImportStatement(isMdx, hasPagePreload)}
|
|
180
|
-
${preloadAwait}
|
|
181
|
-
root.render(createTree(NewPage, props));
|
|
182
|
-
console.log("[ecopages] ${getComponentType(isMdx)} component updated");
|
|
183
|
-
} catch (e) {
|
|
184
|
-
console.error("[ecopages] Failed to hot-reload ${getComponentType(isMdx)} component:", e);
|
|
185
|
-
}
|
|
186
|
-
};`;
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Creates a readable hydration script with router support.
|
|
190
|
-
*
|
|
191
|
-
* @remarks
|
|
192
|
-
* Router-managed pages keep a long-lived root across client navigations.
|
|
193
|
-
* When `hmrEnabled` is true, HMR handlers are included. Otherwise the same
|
|
194
|
-
* semantic source is emitted without HMR hooks; the asset layer bundles it and
|
|
195
|
-
* production builds minify it.
|
|
196
|
-
*/
|
|
197
|
-
function createScriptWithRouter(options) {
|
|
198
|
-
const { importPath, isMdx, router, reactImportPath, reactDomClientImportPath, routerImportPath, scriptId, hmrEnabled, } = options;
|
|
199
|
-
const hasPagePreload = options.hasPagePreload === true;
|
|
200
|
-
const pageModuleUrlExpression = options.pageModuleUrlExpression ?? 'import.meta.url';
|
|
201
|
-
const { components, getRouterProps } = router;
|
|
202
|
-
if (!routerImportPath) {
|
|
203
|
-
throw new Error('routerImportPath is required when router adapter is configured');
|
|
204
|
-
}
|
|
205
|
-
const pageLayoutNormalizationImportPath = options.pageLayoutNormalizationImportPath;
|
|
206
|
-
const hmrInit = hmrEnabled
|
|
207
|
-
? `window.__ECO_PAGES__.hmrHandlers = window.__ECO_PAGES__.hmrHandlers || {};
|
|
208
|
-
`
|
|
209
|
-
: '';
|
|
210
|
-
const hmrHandler = hmrEnabled ? getRouterHmrHandlerScript({ importPath, isMdx, hasPagePreload }) : '';
|
|
211
|
-
const preloadReexport = getPreloadReexportStatement(hasPagePreload);
|
|
212
|
-
const preloadAwait = getPreloadAwaitStatement('preload', 'props', hasPagePreload);
|
|
213
|
-
return `
|
|
214
|
-
import { hydrateRoot } from "${reactDomClientImportPath}";
|
|
215
|
-
import { createElement } from "${reactImportPath}";
|
|
216
|
-
import { ${components.router}, ${components.pageContent} } from "${routerImportPath}";
|
|
217
|
-
${getPageDataReaderSource(options)}
|
|
218
|
-
${getImportStatement(importPath, isMdx, pageLayoutNormalizationImportPath, hasPagePreload)}
|
|
219
|
-
const pageModuleUrl = ${pageModuleUrlExpression};
|
|
220
|
-
export default Page;
|
|
221
|
-
${preloadReexport}
|
|
222
|
-
export const config = Page.config;
|
|
223
|
-
const isActivePageEntry = Boolean(document.querySelector('script[data-eco-script-id="${scriptId}"]'));
|
|
224
|
-
|
|
225
|
-
if (isActivePageEntry) {
|
|
226
|
-
|
|
227
|
-
window.__ECO_PAGES__ = window.__ECO_PAGES__ || {};
|
|
228
|
-
${hmrInit}window.__ECO_PAGES__.react = window.__ECO_PAGES__.react || {};
|
|
229
|
-
window.__ECO_PAGES__.react.pageRoot = window.__ECO_PAGES__.react.pageRoot || null;
|
|
230
|
-
let root = window.__ECO_PAGES__.react.pageRoot;
|
|
231
|
-
${getPageRootCleanupScript()}
|
|
232
|
-
${getRouterBootstrapRegistrationScript()}
|
|
233
|
-
${getReuseExistingRouterRootScript()}
|
|
234
|
-
${getHydrationTimingScript()}
|
|
235
|
-
|
|
236
|
-
const initialPageData = readPageDataDocument();
|
|
237
|
-
const props = initialPageData.props;
|
|
238
|
-
|
|
239
|
-
window.__ECO_PAGES__.page = {
|
|
240
|
-
module: initialPageData.moduleUrl || pageModuleUrl,
|
|
241
|
-
props
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
const createTree = (Component, props) => {
|
|
245
|
-
const pageContent = createElement(${components.pageContent});
|
|
246
|
-
return createElement(${components.router}, ${getRouterProps('Component', 'props')}, pageContent);
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
const mount = async () => {
|
|
250
|
-
const pageData = readPageDataDocument();
|
|
251
|
-
const props = pageData.props;
|
|
252
|
-
window.__ECO_PAGES__.page = {
|
|
253
|
-
module: pageData.moduleUrl || pageModuleUrl,
|
|
254
|
-
props
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
if (shouldReuseExistingRouterRoot()) {
|
|
258
|
-
root = window.__ECO_PAGES__.react.pageRoot;
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
if (window.__ECO_PAGES__.react?.pageRoot) {
|
|
263
|
-
root = window.__ECO_PAGES__.react.pageRoot;
|
|
264
|
-
return;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
${preloadAwait}
|
|
268
|
-
|
|
269
|
-
window.__ECO_DEV_HYDRATION_STARTED__ = performance.now();
|
|
270
|
-
root = hydrateRoot(document.body, createTree(Page, props), {
|
|
271
|
-
onRecoverableError: (err) => console.warn("[ecopages] Hydration error:", err)
|
|
272
|
-
});
|
|
273
|
-
window.__ECO_PAGES__.react.pageRoot = root;
|
|
274
|
-
__ecoRecordHydrationReady();
|
|
275
|
-
${hmrHandler}
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
${getRerunRegistrationScript(scriptId)}
|
|
279
|
-
|
|
280
|
-
if (document.readyState === "loading") {
|
|
281
|
-
document.addEventListener("DOMContentLoaded", mount);
|
|
282
|
-
} else {
|
|
283
|
-
mount();
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
`.trim();
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Creates a readable hydration script without router.
|
|
290
|
-
*
|
|
291
|
-
* @remarks
|
|
292
|
-
* Reconstructs the page and layout tree via `composeLayoutPageTree` so hydration
|
|
293
|
-
* matches SSR. HMR handlers are included only when `hmrEnabled` is true.
|
|
294
|
-
*/
|
|
295
|
-
function createScriptWithoutRouter(options) {
|
|
296
|
-
const { importPath, isMdx, reactImportPath, reactDomClientImportPath, scriptId, hmrEnabled } = options;
|
|
297
|
-
const hasPagePreload = options.hasPagePreload === true;
|
|
298
|
-
const pageModuleUrlExpression = options.pageModuleUrlExpression ?? 'import.meta.url';
|
|
299
|
-
const layoutComposeImportPath = options.layoutComposeImportPath;
|
|
300
|
-
const pageLayoutNormalizationImportPath = options.pageLayoutNormalizationImportPath;
|
|
301
|
-
const preloadReexport = getPreloadReexportStatement(hasPagePreload);
|
|
302
|
-
const preloadAwait = getPreloadAwaitStatement('preload', 'props', hasPagePreload);
|
|
303
|
-
const hmrInit = hmrEnabled
|
|
304
|
-
? `window.__ECO_PAGES__.hmrHandlers = window.__ECO_PAGES__.hmrHandlers || {};
|
|
305
|
-
`
|
|
306
|
-
: '';
|
|
307
|
-
const hmrHandler = hmrEnabled ? getNonRouterHmrHandlerScript({ importPath, isMdx, hasPagePreload }) : '';
|
|
308
|
-
return `
|
|
309
|
-
import { hydrateRoot } from "${reactDomClientImportPath}";
|
|
310
|
-
import { createElement } from "${reactImportPath}";
|
|
311
|
-
import { composeLayoutPageTree } from "${layoutComposeImportPath}";
|
|
312
|
-
${getPageDataReaderSource(options)}
|
|
313
|
-
${getImportStatement(importPath, isMdx, pageLayoutNormalizationImportPath, hasPagePreload)}
|
|
314
|
-
const pageModuleUrl = ${pageModuleUrlExpression};
|
|
315
|
-
export default Page;
|
|
316
|
-
${preloadReexport}
|
|
317
|
-
export const config = Page.config;
|
|
318
|
-
const isActivePageEntry = Boolean(document.querySelector('script[data-eco-script-id="${scriptId}"]'));
|
|
319
|
-
|
|
320
|
-
if (isActivePageEntry) {
|
|
321
|
-
|
|
322
|
-
window.__ECO_PAGES__ = window.__ECO_PAGES__ || {};
|
|
323
|
-
${hmrInit}window.__ECO_PAGES__.react = window.__ECO_PAGES__.react || {};
|
|
324
|
-
window.__ECO_PAGES__.react.pageRoot = window.__ECO_PAGES__.react.pageRoot || null;
|
|
325
|
-
let root = window.__ECO_PAGES__.react.pageRoot;
|
|
326
|
-
${getPageRootCleanupScript()}
|
|
327
|
-
${getHydrationTimingScript()}
|
|
328
|
-
|
|
329
|
-
const initialPageData = readPageDataDocument();
|
|
330
|
-
const props = initialPageData.props;
|
|
331
|
-
|
|
332
|
-
window.__ECO_PAGES__.page = {
|
|
333
|
-
module: initialPageData.moduleUrl || pageModuleUrl,
|
|
334
|
-
props
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
const createTree = (Component, props) => composeLayoutPageTree(Component, props);
|
|
338
|
-
|
|
339
|
-
const mount = async () => {
|
|
340
|
-
const pageData = readPageDataDocument();
|
|
341
|
-
const props = pageData.props;
|
|
342
|
-
window.__ECO_PAGES__.page = {
|
|
343
|
-
module: pageData.moduleUrl || pageModuleUrl,
|
|
344
|
-
props
|
|
345
|
-
};
|
|
346
|
-
|
|
347
|
-
${preloadAwait}
|
|
348
|
-
|
|
349
|
-
if (window.__ECO_PAGES__.react?.pageRoot) {
|
|
350
|
-
root = window.__ECO_PAGES__.react.pageRoot;
|
|
351
|
-
root.render(createTree(Page, props));
|
|
352
|
-
} else {
|
|
353
|
-
window.__ECO_DEV_HYDRATION_STARTED__ = performance.now();
|
|
354
|
-
root = hydrateRoot(document.body, createTree(Page, props), {
|
|
355
|
-
onRecoverableError: (err) => console.warn("[ecopages] Hydration error:", err)
|
|
356
|
-
});
|
|
357
|
-
window.__ECO_PAGES__.react.pageRoot = root;
|
|
358
|
-
__ecoRecordHydrationReady();
|
|
359
|
-
}
|
|
360
|
-
${hmrHandler}
|
|
361
|
-
};
|
|
362
|
-
|
|
363
|
-
${getRerunRegistrationScript(scriptId)}
|
|
364
|
-
|
|
365
|
-
if (document.readyState === "loading") {
|
|
366
|
-
document.addEventListener("DOMContentLoaded", mount);
|
|
367
|
-
} else {
|
|
368
|
-
mount();
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
`.trim();
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* Creates a page hydration entry module.
|
|
375
|
-
*
|
|
376
|
-
* @remarks
|
|
377
|
-
* Always emits readable source. Production page assets set `bundle: true` so
|
|
378
|
-
* core's content-script processor minifies the result. Do not hand-minify here.
|
|
379
|
-
* If the Page exports `preload`, the generated entry imports, awaits, and
|
|
380
|
-
* re-exports it. Pages without that export keep a default import so the
|
|
381
|
-
* bundler does not warn about a missing named export.
|
|
382
|
-
*/
|
|
383
|
-
export function createHydrationScript(options) {
|
|
384
|
-
return options.router ? createScriptWithRouter(options) : createScriptWithoutRouter(options);
|
|
385
|
-
}
|
|
386
|
-
/**
|
|
387
|
-
* Registers an HMR handler that hot-imports an island module and re-renders mounted roots.
|
|
388
|
-
*/
|
|
389
|
-
function getIslandHmrHandlerScript(options) {
|
|
390
|
-
const { importPath, targetSelector } = options;
|
|
391
|
-
return ` window.__ECO_PAGES__ = window.__ECO_PAGES__ || {};
|
|
392
|
-
window.__ECO_PAGES__.hmrHandlers = window.__ECO_PAGES__.hmrHandlers || {};
|
|
393
|
-
window.__ECO_PAGES__.islandRoots = window.__ECO_PAGES__.islandRoots || {};
|
|
394
|
-
window.__ECO_PAGES__.hmrHandlers[${JSON.stringify(importPath)}] = async (newUrl) => {
|
|
395
|
-
try {
|
|
396
|
-
const nextModule = await import(newUrl);
|
|
397
|
-
const Component = resolveComponent(nextModule);
|
|
398
|
-
if (!Component) {
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
document.querySelectorAll(${JSON.stringify(targetSelector)}).forEach((target) => {
|
|
403
|
-
if (!(target instanceof HTMLElement)) {
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
const componentKey = target.getAttribute("data-eco-component-key") ?? "";
|
|
408
|
-
const props = JSON.parse(atob(target.getAttribute("data-eco-props") || "e30="));
|
|
409
|
-
let container = target;
|
|
410
|
-
|
|
411
|
-
if (target.tagName.toLowerCase() !== "eco-island") {
|
|
412
|
-
container = document.createElement("eco-island");
|
|
413
|
-
container.style.display = "block";
|
|
414
|
-
container.setAttribute("data-eco-component-key", componentKey);
|
|
415
|
-
container.setAttribute("data-eco-props", target.getAttribute("data-eco-props") || "e30=");
|
|
416
|
-
target.replaceWith(container);
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
let root = window.__ECO_PAGES__.islandRoots[componentKey];
|
|
420
|
-
if (!root) {
|
|
421
|
-
root = createRoot(container);
|
|
422
|
-
window.__ECO_PAGES__.islandRoots[componentKey] = root;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
root.render(createElement(Component, props));
|
|
426
|
-
});
|
|
427
|
-
console.log("[ecopages] Island component updated");
|
|
428
|
-
} catch (error) {
|
|
429
|
-
console.error("[ecopages] Failed to hot-reload island component:", error);
|
|
430
|
-
}
|
|
431
|
-
};`;
|
|
432
|
-
}
|
|
433
|
-
/**
|
|
434
|
-
* Creates the client bootstrap for component-level React islands.
|
|
435
|
-
*
|
|
436
|
-
* @remarks
|
|
437
|
-
* Island scripts use `bundle: false`, so compact output still needs a
|
|
438
|
-
* handwritten string.
|
|
439
|
-
*/
|
|
440
|
-
export function createIslandHydrationScript(options) {
|
|
441
|
-
const targetSelector = JSON.stringify(options.targetSelector);
|
|
442
|
-
const componentRef = JSON.stringify(options.componentRef ?? '');
|
|
443
|
-
const componentFile = JSON.stringify(options.componentFile ?? '');
|
|
444
|
-
const scriptId = options.scriptId;
|
|
445
|
-
const hmrHandler = options.hmrEnabled
|
|
446
|
-
? getIslandHmrHandlerScript({ importPath: options.importPath, targetSelector: options.targetSelector })
|
|
447
|
-
: '';
|
|
448
|
-
if (options.minify) {
|
|
449
|
-
return `import{createRoot as cr}from"${options.reactDomClientImportPath}";import{createElement as ce}from"${options.reactImportPath}";import*as M from"${options.importPath}";const r=${componentRef};const f=${componentFile};const mv=Object.values(M);const c=mv.find((e)=>{if(typeof e!=="function")return false;const ec=e.config?.identity;if(!ec)return false;if(r&&ec.id===r)return true;if(f&&ec.file===f)return true;return false;})??(typeof M.default==="function"?M.default:mv.find((e)=>typeof e==="function")??null);const m=()=>{const ts=document.querySelectorAll(${targetSelector});if(!c||ts.length===0)return;ts.forEach((t)=>{if(!(t instanceof HTMLElement))return;const p=JSON.parse(atob(t.getAttribute("data-eco-props")||"e30="));const ct=document.createElement("eco-island");ct.style.display="block";t.replaceWith(ct);cr(ct).render(ce(c,p))})};window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.rerunScripts=window.__ECO_PAGES__.rerunScripts||{};window.__ECO_PAGES__.rerunScripts[${JSON.stringify(scriptId)}]=m;document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m,{once:true}):m()`;
|
|
450
|
-
}
|
|
451
|
-
return `
|
|
452
|
-
import { createRoot } from "${options.reactDomClientImportPath}";
|
|
453
|
-
import { createElement } from "${options.reactImportPath}";
|
|
454
|
-
import * as ComponentModule from "${options.importPath}";
|
|
455
|
-
|
|
456
|
-
const resolveComponent = (module = ComponentModule) => {
|
|
457
|
-
const id = ${componentRef};
|
|
458
|
-
const file = ${componentFile};
|
|
459
|
-
const moduleValues = Object.values(module);
|
|
460
|
-
|
|
461
|
-
const matchByMetadata = moduleValues.find((entry) => {
|
|
462
|
-
if (typeof entry !== "function") return false;
|
|
463
|
-
const config = entry.config;
|
|
464
|
-
const eco = config?.identity;
|
|
465
|
-
if (!eco) return false;
|
|
466
|
-
if (id && eco.id === id) return true;
|
|
467
|
-
if (file && eco.file === file) return true;
|
|
468
|
-
return false;
|
|
469
|
-
});
|
|
470
|
-
|
|
471
|
-
if (matchByMetadata && typeof matchByMetadata === "function") {
|
|
472
|
-
return matchByMetadata;
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
const defaultExport = module.default;
|
|
476
|
-
if (typeof defaultExport === "function") {
|
|
477
|
-
return defaultExport;
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
const firstFunction = moduleValues.find((entry) => typeof entry === "function");
|
|
481
|
-
return typeof firstFunction === "function" ? firstFunction : null;
|
|
482
|
-
};
|
|
483
|
-
|
|
484
|
-
const mount = () => {
|
|
485
|
-
const targets = document.querySelectorAll(${targetSelector});
|
|
486
|
-
const Component = resolveComponent();
|
|
487
|
-
if (!Component || targets.length === 0) {
|
|
488
|
-
return;
|
|
489
|
-
}
|
|
490
|
-
targets.forEach((target) => {
|
|
491
|
-
if (!(target instanceof HTMLElement)) {
|
|
492
|
-
return;
|
|
493
|
-
}
|
|
494
|
-
const props = JSON.parse(atob(target.getAttribute("data-eco-props") || "e30="));
|
|
495
|
-
const container = document.createElement("eco-island");
|
|
496
|
-
container.style.display = "block";
|
|
497
|
-
container.setAttribute("data-eco-component-key", target.getAttribute("data-eco-component-key") ?? "");
|
|
498
|
-
container.setAttribute("data-eco-props", target.getAttribute("data-eco-props") || "e30=");
|
|
499
|
-
target.replaceWith(container);
|
|
500
|
-
const root = createRoot(container);
|
|
501
|
-
const componentKey = container.getAttribute("data-eco-component-key") ?? "";
|
|
502
|
-
window.__ECO_PAGES__ = window.__ECO_PAGES__ || {};
|
|
503
|
-
window.__ECO_PAGES__.islandRoots = window.__ECO_PAGES__.islandRoots || {};
|
|
504
|
-
window.__ECO_PAGES__.islandRoots[componentKey] = root;
|
|
505
|
-
root.render(createElement(Component, props));
|
|
506
|
-
});
|
|
507
|
-
};
|
|
508
|
-
|
|
509
|
-
${getRerunRegistrationScript(scriptId)}
|
|
510
|
-
${hmrHandler ? `${hmrHandler}\n` : ''}
|
|
511
|
-
if (document.readyState === "loading") {
|
|
512
|
-
document.addEventListener("DOMContentLoaded", mount, { once: true });
|
|
513
|
-
} else {
|
|
514
|
-
mount();
|
|
515
|
-
}
|
|
516
|
-
`.trim();
|
|
517
|
-
}
|