@ecopages/react 0.2.0-beta.30 → 0.2.0-beta.32
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 +5 -1
- package/package.json +4 -4
- package/src/app/client-only.js +13 -17
- package/src/app/dynamic.js +7 -10
- package/src/app/dynamic.test.browser.js +27 -29
- package/src/bundling/bootstrap-package-paths.js +5 -6
- package/src/bundling/bundle.js +115 -114
- package/src/bundling/discover-layout-runtime-modules.js +166 -155
- package/src/bundling/runtime-alias-map.d.ts +4 -1
- package/src/bundling/runtime-alias-map.js +107 -95
- package/src/bundling/runtime-bundle.js +250 -274
- package/src/bundling/runtime-interop-plugin.js +31 -35
- package/src/bundling/runtime-mode.js +10 -5
- package/src/bundling/runtime-modules.js +46 -35
- package/src/client-graph/ast-transform.js +415 -305
- package/src/client-graph/boundary-cache.js +146 -104
- package/src/client-graph/boundary-plugin.js +122 -104
- package/src/client-graph/component-config-traversal.js +70 -49
- package/src/client-graph/declared-modules.js +63 -41
- package/src/client-graph/reachability-analyzer.js +506 -313
- package/src/client-graph/specifier-classification.js +209 -141
- package/src/contracts/router-adapter.js +6 -0
- package/src/dev-transform/react-dev-transform-contributor.d.ts +17 -0
- package/src/dev-transform/react-dev-transform-contributor.js +22 -0
- package/src/hmr/hmr-scripts.js +22 -9
- package/src/hmr/hmr-strategy.d.ts +12 -181
- package/src/hmr/hmr-strategy.js +321 -650
- package/src/hmr/page-metadata-cache.js +43 -40
- package/src/hmr/react-hmr-dev-transform-plugins.d.ts +23 -0
- package/src/hmr/react-hmr-dev-transform-plugins.js +20 -0
- package/src/hydration/assert-no-bare-ecopages-imports.js +6 -6
- package/src/hydration/bootstrap-imports.js +30 -20
- package/src/hydration/hydration-asset.js +216 -213
- package/src/hydration/hydration-scripts.d.ts +4 -0
- package/src/hydration/hydration-scripts.js +199 -66
- package/src/hydration/hydration-scripts.test.browser.js +163 -168
- package/src/hydration/page-data-manifest.js +89 -45
- package/src/hydration/page-data-reader.js +45 -32
- package/src/hydration/page-payload.js +101 -99
- package/src/hydration/serialize-page-data-script.js +27 -28
- package/src/mdx/mdx-config-dependency.js +108 -112
- package/src/mdx/mdx-loader-plugin.js +7 -10
- package/src/page/page-module.js +72 -66
- package/src/plugin/react.constants.js +1 -4
- package/src/plugin/react.plugin.d.ts +1 -0
- package/src/plugin/react.plugin.js +227 -216
- package/src/plugin/react.types.js +1 -0
- package/src/render/component-ownership.js +42 -18
- package/src/render/component-ssr.js +168 -190
- package/src/render/eco-embed.js +8 -10
- package/src/render/errors.js +21 -16
- package/src/render/html-boundary.js +55 -51
- package/src/render/layout-compose.js +122 -94
- package/src/render/react-renderer.js +433 -468
- package/src/render/react-runtime.js +25 -20
- package/src/render/unified-layout-composition.js +74 -85
- package/src/runtime/use-sync-external-store-with-selector.js +52 -54
|
@@ -81,6 +81,10 @@ export type IslandHydrationScriptOptions = {
|
|
|
81
81
|
* here rather than delegated to the bundler.
|
|
82
82
|
*/
|
|
83
83
|
minify: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* When true, register an HMR handler that hot-imports the dev transform module.
|
|
86
|
+
*/
|
|
87
|
+
hmrEnabled?: boolean;
|
|
84
88
|
};
|
|
85
89
|
/**
|
|
86
90
|
* Creates a page hydration entry module.
|
|
@@ -1,31 +1,57 @@
|
|
|
1
|
-
|
|
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';
|
|
2
7
|
function getPageDataReaderSource(options) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
8
|
+
if (options.pageDataReaderImportPath) {
|
|
9
|
+
return `import { readPageDataDocument, getPageDataFromDocument as getPageData } from "${options.pageDataReaderImportPath}";`;
|
|
10
|
+
}
|
|
11
|
+
return getDevPageDataReaderBootstrapSource();
|
|
7
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.
|
|
19
|
+
*/
|
|
8
20
|
function getImportStatement(importPath, isMdx, pageLayoutNormalizationImportPath) {
|
|
9
|
-
|
|
21
|
+
return isMdx
|
|
22
|
+
? `import * as MDXModule from "${importPath}";
|
|
10
23
|
import { ensurePageConfigLayouts } from "${pageLayoutNormalizationImportPath}";
|
|
11
24
|
const Page = MDXModule.default;
|
|
12
25
|
if (MDXModule.config) {
|
|
13
26
|
Page.config = MDXModule.config;
|
|
14
27
|
ensurePageConfigLayouts(Page.config);
|
|
15
|
-
}`
|
|
28
|
+
}`
|
|
29
|
+
: `import Page from "${importPath}";`;
|
|
16
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Assigns `NewPage` from a hot-reloaded module, including MDX `config` reattach.
|
|
33
|
+
*/
|
|
17
34
|
function getHmrImportStatement(isMdx) {
|
|
18
|
-
|
|
35
|
+
return isMdx
|
|
36
|
+
? `const NewPage = newModule.default;
|
|
19
37
|
if (newModule.config) {
|
|
20
38
|
NewPage.config = newModule.config;
|
|
21
39
|
ensurePageConfigLayouts(NewPage.config);
|
|
22
|
-
}`
|
|
40
|
+
}`
|
|
41
|
+
: 'const NewPage = newModule.default;';
|
|
23
42
|
}
|
|
24
43
|
function getComponentType(isMdx) {
|
|
25
|
-
|
|
44
|
+
return isMdx ? 'MDX' : 'React';
|
|
26
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Browser hook that unmounts the page React root and releases navigation ownership.
|
|
48
|
+
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* Emitted into the page bootstrap so browser-router / react-router handoff can
|
|
51
|
+
* tear down React without knowing React root internals.
|
|
52
|
+
*/
|
|
27
53
|
function getPageRootCleanupScript() {
|
|
28
|
-
|
|
54
|
+
return `window.__ECO_PAGES__ = window.__ECO_PAGES__ || {};
|
|
29
55
|
window.__ECO_PAGES__.react = window.__ECO_PAGES__.react || {};
|
|
30
56
|
window.__ECO_PAGES__.react.cleanupPageRoot = () => {
|
|
31
57
|
const activeRoot = window.__ECO_PAGES__.react?.pageRoot || root;
|
|
@@ -42,8 +68,12 @@ window.__ECO_PAGES__.react.cleanupPageRoot = () => {
|
|
|
42
68
|
activeRoot.unmount();
|
|
43
69
|
};`;
|
|
44
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Registers react-router with the shared navigation coordinator when it does
|
|
73
|
+
* not already own SPA navigation.
|
|
74
|
+
*/
|
|
45
75
|
function getRouterBootstrapRegistrationScript() {
|
|
46
|
-
|
|
76
|
+
return `const currentOwnerState = window.__ECO_PAGES__?.navigation?.getOwnerState?.();
|
|
47
77
|
if (!(currentOwnerState?.owner === "react-router" && currentOwnerState.canHandleSpaNavigation)) {
|
|
48
78
|
window.__ECO_PAGES__?.navigation?.register({
|
|
49
79
|
owner: "react-router",
|
|
@@ -54,8 +84,12 @@ window.__ECO_PAGES__?.navigation?.register({
|
|
|
54
84
|
window.__ECO_PAGES__?.navigation?.claimOwnership?.("react-router");
|
|
55
85
|
}`;
|
|
56
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Predicate used by router bootstraps to keep a long-lived hydrateRoot across
|
|
89
|
+
* client navigations when react-router already owns the document.
|
|
90
|
+
*/
|
|
57
91
|
function getReuseExistingRouterRootScript() {
|
|
58
|
-
|
|
92
|
+
return `const shouldReuseExistingRouterRoot = () => {
|
|
59
93
|
const ownerState = window.__ECO_PAGES__?.navigation?.getOwnerState?.();
|
|
60
94
|
return Boolean(
|
|
61
95
|
window.__ECO_PAGES__.react?.pageRoot &&
|
|
@@ -64,14 +98,27 @@ function getReuseExistingRouterRootScript() {
|
|
|
64
98
|
);
|
|
65
99
|
};`;
|
|
66
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Registers `mount` under `window.__ECO_PAGES__.rerunScripts` for after-swap re-execution.
|
|
103
|
+
*/
|
|
67
104
|
function getRerunRegistrationScript(scriptId) {
|
|
68
|
-
|
|
105
|
+
return `window.__ECO_PAGES__ = window.__ECO_PAGES__ || {};
|
|
69
106
|
window.__ECO_PAGES__.rerunScripts = window.__ECO_PAGES__.rerunScripts || {};
|
|
70
107
|
window.__ECO_PAGES__.rerunScripts[${JSON.stringify(scriptId)}] = mount;`;
|
|
71
108
|
}
|
|
109
|
+
function getHydrationTimingScript() {
|
|
110
|
+
return `const __ecoRecordHydrationReady = () => {
|
|
111
|
+
if (window.__ECO_DEV_HYDRATION_STARTED__ == null) return;
|
|
112
|
+
window.__ECO_DEV_HYDRATION_MS__ = Math.round(performance.now() - window.__ECO_DEV_HYDRATION_STARTED__);
|
|
113
|
+
};`;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* HMR handler for router pages: prefers coordinator reload when layouts change,
|
|
117
|
+
* otherwise re-renders the existing root.
|
|
118
|
+
*/
|
|
72
119
|
function getRouterHmrHandlerScript(options) {
|
|
73
|
-
|
|
74
|
-
|
|
120
|
+
const { importPath, isMdx } = options;
|
|
121
|
+
return ` window.__ECO_PAGES__.hmrHandlers["${importPath}"] = async (newUrl) => {
|
|
75
122
|
try {
|
|
76
123
|
const newModule = await import(newUrl);
|
|
77
124
|
const nextProps = getPageData();
|
|
@@ -103,9 +150,12 @@ function getRouterHmrHandlerScript(options) {
|
|
|
103
150
|
}
|
|
104
151
|
};`;
|
|
105
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* HMR handler for non-router pages: hot-imports the module and re-renders the layout tree.
|
|
155
|
+
*/
|
|
106
156
|
function getNonRouterHmrHandlerScript(options) {
|
|
107
|
-
|
|
108
|
-
|
|
157
|
+
const { importPath, isMdx } = options;
|
|
158
|
+
return ` window.__ECO_PAGES__.hmrHandlers["${importPath}"] = async (newUrl) => {
|
|
109
159
|
try {
|
|
110
160
|
const newModule = await import(newUrl);
|
|
111
161
|
${getHmrImportStatement(isMdx)}
|
|
@@ -116,27 +166,29 @@ function getNonRouterHmrHandlerScript(options) {
|
|
|
116
166
|
}
|
|
117
167
|
};`;
|
|
118
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* Creates a readable hydration script with router support.
|
|
171
|
+
*
|
|
172
|
+
* @remarks
|
|
173
|
+
* Router-managed pages keep a long-lived root across client navigations.
|
|
174
|
+
* When `hmrEnabled` is true, HMR handlers are included. Otherwise the same
|
|
175
|
+
* semantic source is emitted without HMR hooks; the asset layer bundles it and
|
|
176
|
+
* production builds minify it.
|
|
177
|
+
*/
|
|
119
178
|
function createScriptWithRouter(options) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
throw new Error("routerImportPath is required when router adapter is configured");
|
|
134
|
-
}
|
|
135
|
-
const pageLayoutNormalizationImportPath = options.pageLayoutNormalizationImportPath;
|
|
136
|
-
const hmrInit = hmrEnabled ? `window.__ECO_PAGES__.hmrHandlers = window.__ECO_PAGES__.hmrHandlers || {};
|
|
137
|
-
` : "";
|
|
138
|
-
const hmrHandler = hmrEnabled ? getRouterHmrHandlerScript({ importPath, isMdx }) : "";
|
|
139
|
-
return `
|
|
179
|
+
const { importPath, isMdx, router, reactImportPath, reactDomClientImportPath, routerImportPath, scriptId, hmrEnabled, } = options;
|
|
180
|
+
const pageModuleUrlExpression = options.pageModuleUrlExpression ?? 'import.meta.url';
|
|
181
|
+
const { components, getRouterProps } = router;
|
|
182
|
+
if (!routerImportPath) {
|
|
183
|
+
throw new Error('routerImportPath is required when router adapter is configured');
|
|
184
|
+
}
|
|
185
|
+
const pageLayoutNormalizationImportPath = options.pageLayoutNormalizationImportPath;
|
|
186
|
+
const hmrInit = hmrEnabled
|
|
187
|
+
? `window.__ECO_PAGES__.hmrHandlers = window.__ECO_PAGES__.hmrHandlers || {};
|
|
188
|
+
`
|
|
189
|
+
: '';
|
|
190
|
+
const hmrHandler = hmrEnabled ? getRouterHmrHandlerScript({ importPath, isMdx }) : '';
|
|
191
|
+
return `
|
|
140
192
|
import { hydrateRoot } from "${reactDomClientImportPath}";
|
|
141
193
|
import { createElement } from "${reactImportPath}";
|
|
142
194
|
import { ${components.router}, ${components.pageContent} } from "${routerImportPath}";
|
|
@@ -156,6 +208,7 @@ let root = window.__ECO_PAGES__.react.pageRoot;
|
|
|
156
208
|
${getPageRootCleanupScript()}
|
|
157
209
|
${getRouterBootstrapRegistrationScript()}
|
|
158
210
|
${getReuseExistingRouterRootScript()}
|
|
211
|
+
${getHydrationTimingScript()}
|
|
159
212
|
|
|
160
213
|
const initialPageData = readPageDataDocument();
|
|
161
214
|
const props = initialPageData.props;
|
|
@@ -167,7 +220,7 @@ window.__ECO_PAGES__.page = {
|
|
|
167
220
|
|
|
168
221
|
const createTree = (Component, props) => {
|
|
169
222
|
const pageContent = createElement(${components.pageContent});
|
|
170
|
-
return createElement(${components.router}, ${getRouterProps(
|
|
223
|
+
return createElement(${components.router}, ${getRouterProps('Component', 'props')}, pageContent);
|
|
171
224
|
};
|
|
172
225
|
|
|
173
226
|
const mount = () => {
|
|
@@ -188,10 +241,12 @@ const mount = () => {
|
|
|
188
241
|
return;
|
|
189
242
|
}
|
|
190
243
|
|
|
244
|
+
window.__ECO_DEV_HYDRATION_STARTED__ = performance.now();
|
|
191
245
|
root = hydrateRoot(document.body, createTree(Page, props), {
|
|
192
246
|
onRecoverableError: (err) => console.warn("[ecopages] Hydration error:", err)
|
|
193
247
|
});
|
|
194
248
|
window.__ECO_PAGES__.react.pageRoot = root;
|
|
249
|
+
__ecoRecordHydrationReady();
|
|
195
250
|
${hmrHandler}
|
|
196
251
|
};
|
|
197
252
|
|
|
@@ -205,15 +260,24 @@ if (document.readyState === "loading") {
|
|
|
205
260
|
}
|
|
206
261
|
`.trim();
|
|
207
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Creates a readable hydration script without router.
|
|
265
|
+
*
|
|
266
|
+
* @remarks
|
|
267
|
+
* Reconstructs the page and layout tree via `composeLayoutPageTree` so hydration
|
|
268
|
+
* matches SSR. HMR handlers are included only when `hmrEnabled` is true.
|
|
269
|
+
*/
|
|
208
270
|
function createScriptWithoutRouter(options) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
`
|
|
215
|
-
|
|
216
|
-
|
|
271
|
+
const { importPath, isMdx, reactImportPath, reactDomClientImportPath, scriptId, hmrEnabled } = options;
|
|
272
|
+
const pageModuleUrlExpression = options.pageModuleUrlExpression ?? 'import.meta.url';
|
|
273
|
+
const layoutComposeImportPath = options.layoutComposeImportPath;
|
|
274
|
+
const pageLayoutNormalizationImportPath = options.pageLayoutNormalizationImportPath;
|
|
275
|
+
const hmrInit = hmrEnabled
|
|
276
|
+
? `window.__ECO_PAGES__.hmrHandlers = window.__ECO_PAGES__.hmrHandlers || {};
|
|
277
|
+
`
|
|
278
|
+
: '';
|
|
279
|
+
const hmrHandler = hmrEnabled ? getNonRouterHmrHandlerScript({ importPath, isMdx }) : '';
|
|
280
|
+
return `
|
|
217
281
|
import { hydrateRoot } from "${reactDomClientImportPath}";
|
|
218
282
|
import { createElement } from "${reactImportPath}";
|
|
219
283
|
import { composeLayoutPageTree } from "${layoutComposeImportPath}";
|
|
@@ -231,6 +295,7 @@ ${hmrInit}window.__ECO_PAGES__.react = window.__ECO_PAGES__.react || {};
|
|
|
231
295
|
window.__ECO_PAGES__.react.pageRoot = window.__ECO_PAGES__.react.pageRoot || null;
|
|
232
296
|
let root = window.__ECO_PAGES__.react.pageRoot;
|
|
233
297
|
${getPageRootCleanupScript()}
|
|
298
|
+
${getHydrationTimingScript()}
|
|
234
299
|
|
|
235
300
|
const initialPageData = readPageDataDocument();
|
|
236
301
|
const props = initialPageData.props;
|
|
@@ -254,10 +319,12 @@ const mount = () => {
|
|
|
254
319
|
root = window.__ECO_PAGES__.react.pageRoot;
|
|
255
320
|
root.render(createTree(Page, props));
|
|
256
321
|
} else {
|
|
322
|
+
window.__ECO_DEV_HYDRATION_STARTED__ = performance.now();
|
|
257
323
|
root = hydrateRoot(document.body, createTree(Page, props), {
|
|
258
324
|
onRecoverableError: (err) => console.warn("[ecopages] Hydration error:", err)
|
|
259
325
|
});
|
|
260
326
|
window.__ECO_PAGES__.react.pageRoot = root;
|
|
327
|
+
__ecoRecordHydrationReady();
|
|
261
328
|
}
|
|
262
329
|
${hmrHandler}
|
|
263
330
|
};
|
|
@@ -272,26 +339,90 @@ if (document.readyState === "loading") {
|
|
|
272
339
|
}
|
|
273
340
|
`.trim();
|
|
274
341
|
}
|
|
275
|
-
|
|
276
|
-
|
|
342
|
+
/**
|
|
343
|
+
* Creates a page hydration entry module.
|
|
344
|
+
*
|
|
345
|
+
* @remarks
|
|
346
|
+
* Always emits readable source. Production page assets set `bundle: true` so
|
|
347
|
+
* core's content-script processor minifies the result. Do not hand-minify here.
|
|
348
|
+
*/
|
|
349
|
+
export function createHydrationScript(options) {
|
|
350
|
+
return options.router ? createScriptWithRouter(options) : createScriptWithoutRouter(options);
|
|
277
351
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
}
|
|
286
|
-
|
|
352
|
+
/**
|
|
353
|
+
* Registers an HMR handler that hot-imports an island module and re-renders mounted roots.
|
|
354
|
+
*/
|
|
355
|
+
function getIslandHmrHandlerScript(options) {
|
|
356
|
+
const { importPath, targetSelector } = options;
|
|
357
|
+
return ` window.__ECO_PAGES__ = window.__ECO_PAGES__ || {};
|
|
358
|
+
window.__ECO_PAGES__.hmrHandlers = window.__ECO_PAGES__.hmrHandlers || {};
|
|
359
|
+
window.__ECO_PAGES__.islandRoots = window.__ECO_PAGES__.islandRoots || {};
|
|
360
|
+
window.__ECO_PAGES__.hmrHandlers[${JSON.stringify(importPath)}] = async (newUrl) => {
|
|
361
|
+
try {
|
|
362
|
+
const nextModule = await import(newUrl);
|
|
363
|
+
const Component = resolveComponent(nextModule);
|
|
364
|
+
if (!Component) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
document.querySelectorAll(${JSON.stringify(targetSelector)}).forEach((target) => {
|
|
369
|
+
if (!(target instanceof HTMLElement)) {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const componentKey = target.getAttribute("data-eco-component-key") ?? "";
|
|
374
|
+
const props = JSON.parse(atob(target.getAttribute("data-eco-props") || "e30="));
|
|
375
|
+
let container = target;
|
|
376
|
+
|
|
377
|
+
if (target.tagName.toLowerCase() !== "eco-island") {
|
|
378
|
+
container = document.createElement("eco-island");
|
|
379
|
+
container.style.display = "block";
|
|
380
|
+
container.setAttribute("data-eco-component-key", componentKey);
|
|
381
|
+
container.setAttribute("data-eco-props", target.getAttribute("data-eco-props") || "e30=");
|
|
382
|
+
target.replaceWith(container);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
let root = window.__ECO_PAGES__.islandRoots[componentKey];
|
|
386
|
+
if (!root) {
|
|
387
|
+
root = createRoot(container);
|
|
388
|
+
window.__ECO_PAGES__.islandRoots[componentKey] = root;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
root.render(createElement(Component, props));
|
|
392
|
+
});
|
|
393
|
+
console.log("[ecopages] Island component updated");
|
|
394
|
+
} catch (error) {
|
|
395
|
+
console.error("[ecopages] Failed to hot-reload island component:", error);
|
|
396
|
+
}
|
|
397
|
+
};`;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Creates the client bootstrap for component-level React islands.
|
|
401
|
+
*
|
|
402
|
+
* @remarks
|
|
403
|
+
* Island scripts use `bundle: false`, so compact output still needs a
|
|
404
|
+
* handwritten string.
|
|
405
|
+
*/
|
|
406
|
+
export function createIslandHydrationScript(options) {
|
|
407
|
+
const targetSelector = JSON.stringify(options.targetSelector);
|
|
408
|
+
const componentRef = JSON.stringify(options.componentRef ?? '');
|
|
409
|
+
const componentFile = JSON.stringify(options.componentFile ?? '');
|
|
410
|
+
const scriptId = options.scriptId;
|
|
411
|
+
const hmrHandler = options.hmrEnabled
|
|
412
|
+
? getIslandHmrHandlerScript({ importPath: options.importPath, targetSelector: options.targetSelector })
|
|
413
|
+
: '';
|
|
414
|
+
if (options.minify) {
|
|
415
|
+
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?.__eco;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()`;
|
|
416
|
+
}
|
|
417
|
+
return `
|
|
287
418
|
import { createRoot } from "${options.reactDomClientImportPath}";
|
|
288
419
|
import { createElement } from "${options.reactImportPath}";
|
|
289
420
|
import * as ComponentModule from "${options.importPath}";
|
|
290
421
|
|
|
291
|
-
const resolveComponent = () => {
|
|
422
|
+
const resolveComponent = (module = ComponentModule) => {
|
|
292
423
|
const id = ${componentRef};
|
|
293
424
|
const file = ${componentFile};
|
|
294
|
-
const moduleValues = Object.values(
|
|
425
|
+
const moduleValues = Object.values(module);
|
|
295
426
|
|
|
296
427
|
const matchByMetadata = moduleValues.find((entry) => {
|
|
297
428
|
if (typeof entry !== "function") return false;
|
|
@@ -307,7 +438,7 @@ const resolveComponent = () => {
|
|
|
307
438
|
return matchByMetadata;
|
|
308
439
|
}
|
|
309
440
|
|
|
310
|
-
const defaultExport =
|
|
441
|
+
const defaultExport = module.default;
|
|
311
442
|
if (typeof defaultExport === "function") {
|
|
312
443
|
return defaultExport;
|
|
313
444
|
}
|
|
@@ -329,14 +460,20 @@ const mount = () => {
|
|
|
329
460
|
const props = JSON.parse(atob(target.getAttribute("data-eco-props") || "e30="));
|
|
330
461
|
const container = document.createElement("eco-island");
|
|
331
462
|
container.style.display = "block";
|
|
463
|
+
container.setAttribute("data-eco-component-key", target.getAttribute("data-eco-component-key") ?? "");
|
|
464
|
+
container.setAttribute("data-eco-props", target.getAttribute("data-eco-props") || "e30=");
|
|
332
465
|
target.replaceWith(container);
|
|
333
466
|
const root = createRoot(container);
|
|
467
|
+
const componentKey = container.getAttribute("data-eco-component-key") ?? "";
|
|
468
|
+
window.__ECO_PAGES__ = window.__ECO_PAGES__ || {};
|
|
469
|
+
window.__ECO_PAGES__.islandRoots = window.__ECO_PAGES__.islandRoots || {};
|
|
470
|
+
window.__ECO_PAGES__.islandRoots[componentKey] = root;
|
|
334
471
|
root.render(createElement(Component, props));
|
|
335
472
|
});
|
|
336
473
|
};
|
|
337
474
|
|
|
338
475
|
${getRerunRegistrationScript(scriptId)}
|
|
339
|
-
|
|
476
|
+
${hmrHandler ? `${hmrHandler}\n` : ''}
|
|
340
477
|
if (document.readyState === "loading") {
|
|
341
478
|
document.addEventListener("DOMContentLoaded", mount, { once: true });
|
|
342
479
|
} else {
|
|
@@ -344,7 +481,3 @@ if (document.readyState === "loading") {
|
|
|
344
481
|
}
|
|
345
482
|
`.trim();
|
|
346
483
|
}
|
|
347
|
-
export {
|
|
348
|
-
createHydrationScript,
|
|
349
|
-
createIslandHydrationScript
|
|
350
|
-
};
|