@ecopages/react 0.2.0-rc.1 → 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 +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 -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,409 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
2
|
+
import { createElement, useEffect } from 'react';
|
|
3
|
+
import { hydrateRoot } from 'react-dom/client';
|
|
4
|
+
import { mountIslands, updateIslands } from './browser/island-hydration.js';
|
|
5
|
+
import { startPageHydration } from './browser/page-hydration.js';
|
|
6
|
+
function createPageOptions(testWindow, router, pageRoot) {
|
|
7
|
+
const runtime = testWindow.__ECO_REACT_HYDRATION_TEST__;
|
|
8
|
+
const Page = () => null;
|
|
9
|
+
const reactRuntime = {
|
|
10
|
+
hydrateRoot: (target, tree, options) => {
|
|
11
|
+
runtime.hydrateCalls.push({
|
|
12
|
+
containerTag: target.tagName,
|
|
13
|
+
hasRecoverableErrorHandler: typeof options?.onRecoverableError === 'function',
|
|
14
|
+
tree,
|
|
15
|
+
});
|
|
16
|
+
return (pageRoot ?? {
|
|
17
|
+
render: (nextTree) => runtime.renderCalls.push(nextTree),
|
|
18
|
+
unmount: () => {
|
|
19
|
+
runtime.unmountCount += 1;
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
},
|
|
23
|
+
createElement: (...args) => ({ args }),
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
scriptId: 'ecopages-react-page',
|
|
27
|
+
pageModuleUrl: '/assets/page.js',
|
|
28
|
+
Page,
|
|
29
|
+
pageDataReader: {
|
|
30
|
+
readPageDataDocument: () => ({ props: { title: 'Hello React', locals: { theme: 'dark' } } }),
|
|
31
|
+
getPageDataFromDocument: () => ({ title: 'Hello React' }),
|
|
32
|
+
},
|
|
33
|
+
runtime: reactRuntime,
|
|
34
|
+
createTree: (Component, props) => ({ Component, props }),
|
|
35
|
+
hasRouter: router,
|
|
36
|
+
isMdx: false,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
describe('React page lifecycle', () => {
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
document.body.innerHTML = '';
|
|
42
|
+
delete window.__ECO_PAGES__;
|
|
43
|
+
delete window.__ECO_REACT_HYDRATION_TEST__;
|
|
44
|
+
});
|
|
45
|
+
it('registers router ownership and cleanup', async () => {
|
|
46
|
+
const testWindow = window;
|
|
47
|
+
const testState = {
|
|
48
|
+
hydrateCalls: [],
|
|
49
|
+
renderCalls: [],
|
|
50
|
+
claimedOwners: [],
|
|
51
|
+
releasedOwners: [],
|
|
52
|
+
registrations: [],
|
|
53
|
+
unmountCount: 0,
|
|
54
|
+
};
|
|
55
|
+
testWindow.__ECO_REACT_HYDRATION_TEST__ = testState;
|
|
56
|
+
testWindow.__ECO_PAGES__ = {
|
|
57
|
+
navigation: {
|
|
58
|
+
getOwnerState: () => ({ owner: 'html', canHandleSpaNavigation: false }),
|
|
59
|
+
register: (registration) => testState.registrations.push(registration),
|
|
60
|
+
claimOwnership: (owner) => testState.claimedOwners.push(owner),
|
|
61
|
+
releaseOwnership: (owner) => testState.releasedOwners.push(owner),
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
document.body.innerHTML = '<script data-eco-script-id="ecopages-react-page"></script>';
|
|
65
|
+
startPageHydration(createPageOptions(testWindow, true));
|
|
66
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
67
|
+
expect(testState.hydrateCalls).toHaveLength(1);
|
|
68
|
+
expect(testState.hydrateCalls[0].containerTag).toBe('BODY');
|
|
69
|
+
expect(testState.hydrateCalls[0].hasRecoverableErrorHandler).toBe(true);
|
|
70
|
+
expect(testState.claimedOwners).toEqual(['react-router']);
|
|
71
|
+
expect(testState.registrations).toHaveLength(1);
|
|
72
|
+
expect(typeof testWindow.__ECO_PAGES__?.react?.cleanupPageRoot).toBe('function');
|
|
73
|
+
await testWindow.__ECO_PAGES__?.react?.cleanupPageRoot?.();
|
|
74
|
+
expect(testState.unmountCount).toBe(1);
|
|
75
|
+
expect(testState.releasedOwners).toEqual(['react-router']);
|
|
76
|
+
expect(testWindow.__ECO_PAGES__?.page).toBeUndefined();
|
|
77
|
+
expect(testWindow.__ECO_PAGES__?.react?.pageRoot).toBeNull();
|
|
78
|
+
});
|
|
79
|
+
it('reuses an existing router-owned page root on rerun', async () => {
|
|
80
|
+
const testWindow = window;
|
|
81
|
+
testWindow.__ECO_REACT_HYDRATION_TEST__ = {
|
|
82
|
+
hydrateCalls: [],
|
|
83
|
+
renderCalls: [],
|
|
84
|
+
claimedOwners: [],
|
|
85
|
+
releasedOwners: [],
|
|
86
|
+
registrations: [],
|
|
87
|
+
unmountCount: 0,
|
|
88
|
+
};
|
|
89
|
+
const existingRoot = {
|
|
90
|
+
render: (tree) => testWindow.__ECO_REACT_HYDRATION_TEST__?.renderCalls.push(tree),
|
|
91
|
+
unmount: () => {
|
|
92
|
+
testWindow.__ECO_REACT_HYDRATION_TEST__.unmountCount += 1;
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
testWindow.__ECO_PAGES__ = {
|
|
96
|
+
navigation: {
|
|
97
|
+
getOwnerState: () => ({ owner: 'react-router', canHandleSpaNavigation: true }),
|
|
98
|
+
},
|
|
99
|
+
react: { pageRoot: existingRoot },
|
|
100
|
+
};
|
|
101
|
+
document.body.innerHTML = '<script data-eco-script-id="ecopages-react-page"></script>';
|
|
102
|
+
startPageHydration(createPageOptions(testWindow, true, existingRoot));
|
|
103
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
104
|
+
expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.hydrateCalls).toHaveLength(0);
|
|
105
|
+
expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.renderCalls).toHaveLength(0);
|
|
106
|
+
expect(testWindow.__ECO_PAGES__?.react?.pageRoot).toBe(existingRoot);
|
|
107
|
+
});
|
|
108
|
+
it('coalesces concurrent startup and cancels a preload after cleanup', async () => {
|
|
109
|
+
const testWindow = window;
|
|
110
|
+
testWindow.__ECO_REACT_HYDRATION_TEST__ = {
|
|
111
|
+
hydrateCalls: [],
|
|
112
|
+
renderCalls: [],
|
|
113
|
+
claimedOwners: [],
|
|
114
|
+
releasedOwners: [],
|
|
115
|
+
registrations: [],
|
|
116
|
+
unmountCount: 0,
|
|
117
|
+
};
|
|
118
|
+
document.body.innerHTML = '<script data-eco-script-id="ecopages-react-page"></script>';
|
|
119
|
+
let resolvePreload;
|
|
120
|
+
const preload = new Promise((resolve) => {
|
|
121
|
+
resolvePreload = resolve;
|
|
122
|
+
});
|
|
123
|
+
const options = createPageOptions(testWindow, false);
|
|
124
|
+
options.preload = () => preload;
|
|
125
|
+
options.hmr = { importPath: '/assets/page.js' };
|
|
126
|
+
startPageHydration(options);
|
|
127
|
+
expect(typeof testWindow.__ECO_PAGES__?.hmrHandlers?.['/assets/page.js']).toBe('function');
|
|
128
|
+
const rerun = testWindow.__ECO_PAGES__?.rerunScripts?.['ecopages-react-page'];
|
|
129
|
+
rerun?.();
|
|
130
|
+
await Promise.resolve();
|
|
131
|
+
expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.hydrateCalls).toHaveLength(0);
|
|
132
|
+
await testWindow.__ECO_PAGES__?.react?.cleanupPageRoot?.();
|
|
133
|
+
resolvePreload();
|
|
134
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
135
|
+
expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.hydrateCalls).toHaveLength(0);
|
|
136
|
+
});
|
|
137
|
+
it('ignores an HMR update that settles after navigation cleanup', async () => {
|
|
138
|
+
const testWindow = window;
|
|
139
|
+
testWindow.__ECO_REACT_HYDRATION_TEST__ = {
|
|
140
|
+
hydrateCalls: [],
|
|
141
|
+
renderCalls: [],
|
|
142
|
+
claimedOwners: [],
|
|
143
|
+
releasedOwners: [],
|
|
144
|
+
registrations: [],
|
|
145
|
+
unmountCount: 0,
|
|
146
|
+
};
|
|
147
|
+
document.body.innerHTML = '<script data-eco-script-id="ecopages-react-page"></script>';
|
|
148
|
+
const options = createPageOptions(testWindow, false);
|
|
149
|
+
options.hmr = { importPath: '/assets/page.js' };
|
|
150
|
+
startPageHydration(options);
|
|
151
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
152
|
+
let resolveUpdate;
|
|
153
|
+
globalThis.__ECO_TEST_HMR_RELEASE__ = new Promise((resolve) => {
|
|
154
|
+
resolveUpdate = resolve;
|
|
155
|
+
});
|
|
156
|
+
const source = [
|
|
157
|
+
'export default function UpdatedPage() {}',
|
|
158
|
+
'export async function preload() {',
|
|
159
|
+
' await globalThis.__ECO_TEST_HMR_RELEASE__',
|
|
160
|
+
'}',
|
|
161
|
+
].join('\n');
|
|
162
|
+
const hmrHandler = testWindow.__ECO_PAGES__?.hmrHandlers?.['/assets/page.js'];
|
|
163
|
+
expect(hmrHandler).toBeDefined();
|
|
164
|
+
const update = hmrHandler?.(`data:text/javascript;charset=utf-8,${encodeURIComponent(source)}`);
|
|
165
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
166
|
+
await testWindow.__ECO_PAGES__?.react?.cleanupPageRoot?.();
|
|
167
|
+
const replacementRoot = {
|
|
168
|
+
render: (tree) => testWindow.__ECO_REACT_HYDRATION_TEST__?.renderCalls.push(tree),
|
|
169
|
+
unmount: () => undefined,
|
|
170
|
+
};
|
|
171
|
+
testWindow.__ECO_PAGES__.react.pageRoot = replacementRoot;
|
|
172
|
+
resolveUpdate();
|
|
173
|
+
await update;
|
|
174
|
+
await hmrHandler?.('data:text/javascript,export default function RemovedPage() {}');
|
|
175
|
+
expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.renderCalls).toHaveLength(0);
|
|
176
|
+
expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.unmountCount).toBe(1);
|
|
177
|
+
delete globalThis.__ECO_TEST_HMR_RELEASE__;
|
|
178
|
+
});
|
|
179
|
+
it('restores HMR handler with updated generation on page reactivation after cleanup', async () => {
|
|
180
|
+
const testWindow = window;
|
|
181
|
+
testWindow.__ECO_REACT_HYDRATION_TEST__ = {
|
|
182
|
+
hydrateCalls: [],
|
|
183
|
+
renderCalls: [],
|
|
184
|
+
claimedOwners: [],
|
|
185
|
+
releasedOwners: [],
|
|
186
|
+
registrations: [],
|
|
187
|
+
unmountCount: 0,
|
|
188
|
+
};
|
|
189
|
+
document.body.innerHTML = '<script data-eco-script-id="ecopages-react-page"></script>';
|
|
190
|
+
const options = createPageOptions(testWindow, false);
|
|
191
|
+
options.hmr = { importPath: '/assets/page.js' };
|
|
192
|
+
startPageHydration(options);
|
|
193
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
194
|
+
expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.hydrateCalls).toHaveLength(1);
|
|
195
|
+
const initialHmr = testWindow.__ECO_PAGES__?.hmrHandlers?.['/assets/page.js'];
|
|
196
|
+
expect(initialHmr).toBeDefined();
|
|
197
|
+
await testWindow.__ECO_PAGES__?.react?.cleanupPageRoot?.();
|
|
198
|
+
expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.unmountCount).toBe(1);
|
|
199
|
+
await initialHmr?.('data:text/javascript,export default function IgnoredPage() {}');
|
|
200
|
+
expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.renderCalls).toHaveLength(0);
|
|
201
|
+
await testWindow.__ECO_PAGES__.rerunScripts['ecopages-react-page']();
|
|
202
|
+
expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.hydrateCalls).toHaveLength(2);
|
|
203
|
+
const restoredHmr = testWindow.__ECO_PAGES__?.hmrHandlers?.['/assets/page.js'];
|
|
204
|
+
expect(restoredHmr).toBeDefined();
|
|
205
|
+
await restoredHmr?.('data:text/javascript,export default function UpdatedPage() {}');
|
|
206
|
+
expect(testWindow.__ECO_REACT_HYDRATION_TEST__?.renderCalls).toHaveLength(1);
|
|
207
|
+
});
|
|
208
|
+
it('publishes a replacement only after its preload completes', async () => {
|
|
209
|
+
const testWindow = window;
|
|
210
|
+
testWindow.__ECO_REACT_HYDRATION_TEST__ = {
|
|
211
|
+
hydrateCalls: [],
|
|
212
|
+
renderCalls: [],
|
|
213
|
+
claimedOwners: [],
|
|
214
|
+
releasedOwners: [],
|
|
215
|
+
registrations: [],
|
|
216
|
+
unmountCount: 0,
|
|
217
|
+
};
|
|
218
|
+
document.body.innerHTML = '<script data-eco-script-id="ecopages-react-page"></script>';
|
|
219
|
+
let releaseInitial;
|
|
220
|
+
let releaseUpdate;
|
|
221
|
+
let signalUpdate;
|
|
222
|
+
const initialPreload = new Promise((resolve) => {
|
|
223
|
+
releaseInitial = resolve;
|
|
224
|
+
});
|
|
225
|
+
const updateStarted = new Promise((resolve) => {
|
|
226
|
+
signalUpdate = resolve;
|
|
227
|
+
});
|
|
228
|
+
const updatePreload = new Promise((resolve) => {
|
|
229
|
+
releaseUpdate = resolve;
|
|
230
|
+
});
|
|
231
|
+
const testGlobals = globalThis;
|
|
232
|
+
testGlobals.__ECO_TEST_PENDING_PRELOAD__ = () => {
|
|
233
|
+
signalUpdate();
|
|
234
|
+
return updatePreload;
|
|
235
|
+
};
|
|
236
|
+
const options = createPageOptions(testWindow, false);
|
|
237
|
+
const initialPage = options.Page;
|
|
238
|
+
options.preload = () => initialPreload;
|
|
239
|
+
options.hmr = { importPath: '/assets/page.js' };
|
|
240
|
+
try {
|
|
241
|
+
startPageHydration(options);
|
|
242
|
+
const handler = testWindow.__ECO_PAGES__.hmrHandlers['/assets/page.js'];
|
|
243
|
+
const source = 'export default function ReadyPage() {} export const preload = () => globalThis.__ECO_TEST_PENDING_PRELOAD__();';
|
|
244
|
+
const update = handler(`data:text/javascript,${encodeURIComponent(source)}`);
|
|
245
|
+
await updateStarted;
|
|
246
|
+
expect(options.Page).toBe(initialPage);
|
|
247
|
+
releaseInitial();
|
|
248
|
+
await testWindow.__ECO_PAGES__.rerunScripts['ecopages-react-page']();
|
|
249
|
+
expect(testWindow.__ECO_REACT_HYDRATION_TEST__.hydrateCalls[0]?.tree).toMatchObject({
|
|
250
|
+
Component: initialPage,
|
|
251
|
+
});
|
|
252
|
+
expect(testWindow.__ECO_REACT_HYDRATION_TEST__.renderCalls).toHaveLength(0);
|
|
253
|
+
releaseUpdate();
|
|
254
|
+
await update;
|
|
255
|
+
expect(options.Page).not.toBe(initialPage);
|
|
256
|
+
expect(testWindow.__ECO_REACT_HYDRATION_TEST__.renderCalls).toEqual([
|
|
257
|
+
{ Component: options.Page, props: { title: 'Hello React' } },
|
|
258
|
+
]);
|
|
259
|
+
}
|
|
260
|
+
finally {
|
|
261
|
+
releaseInitial();
|
|
262
|
+
releaseUpdate();
|
|
263
|
+
delete testGlobals.__ECO_TEST_PENDING_PRELOAD__;
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
describe('React Island Host lifecycle', () => {
|
|
268
|
+
afterEach(() => {
|
|
269
|
+
document.body.innerHTML = '';
|
|
270
|
+
});
|
|
271
|
+
it('hydrates in place, ignores repeated startup, and releases removed hosts', async () => {
|
|
272
|
+
const host = document.createElement('eco-island');
|
|
273
|
+
host.setAttribute('data-eco-island', '');
|
|
274
|
+
host.setAttribute('data-eco-island-integration', 'react');
|
|
275
|
+
host.setAttribute('data-eco-component-id', 'instance-1');
|
|
276
|
+
host.setAttribute('data-eco-component-key', 'component-1');
|
|
277
|
+
host.setAttribute('data-eco-props', btoa(JSON.stringify({ value: 'server' })));
|
|
278
|
+
const child = document.createElement('input');
|
|
279
|
+
child.value = 'typed before hydration';
|
|
280
|
+
host.appendChild(child);
|
|
281
|
+
document.body.appendChild(host);
|
|
282
|
+
const calls = { hydrate: 0, render: 0, unmount: 0, recoverableErrors: 0 };
|
|
283
|
+
const hydratedComponents = [];
|
|
284
|
+
const runtime = { islandRoots: {}, islandComponents: {} };
|
|
285
|
+
const reactRuntime = {
|
|
286
|
+
hydrateRoot: (target, tree, options) => {
|
|
287
|
+
calls.hydrate += 1;
|
|
288
|
+
if (options?.onRecoverableError)
|
|
289
|
+
calls.recoverableErrors += 1;
|
|
290
|
+
hydratedComponents.push(tree.props.Component);
|
|
291
|
+
const root = {
|
|
292
|
+
render: (nextTree) => {
|
|
293
|
+
calls.render += 1;
|
|
294
|
+
hydratedComponents.push(nextTree.props.Component);
|
|
295
|
+
},
|
|
296
|
+
unmount: () => {
|
|
297
|
+
calls.unmount += 1;
|
|
298
|
+
},
|
|
299
|
+
};
|
|
300
|
+
tree.type(tree.props);
|
|
301
|
+
void target;
|
|
302
|
+
return root;
|
|
303
|
+
},
|
|
304
|
+
createElement: (type, props) => ({ type, props }),
|
|
305
|
+
useEffect: (effect) => queueMicrotask(effect),
|
|
306
|
+
};
|
|
307
|
+
const Component = () => null;
|
|
308
|
+
const options = {
|
|
309
|
+
targetSelector: '[data-eco-component-key="component-1"]',
|
|
310
|
+
componentModule: { default: Component },
|
|
311
|
+
scriptId: 'island-script',
|
|
312
|
+
runtime: reactRuntime,
|
|
313
|
+
runtimeState: runtime,
|
|
314
|
+
};
|
|
315
|
+
mountIslands(options);
|
|
316
|
+
mountIslands(options);
|
|
317
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
318
|
+
expect(calls.hydrate).toBe(1);
|
|
319
|
+
expect(host.firstElementChild).toBe(child);
|
|
320
|
+
expect(host.getAttribute('data-eco-hydrated')).toBe('true');
|
|
321
|
+
expect(calls.recoverableErrors).toBe(1);
|
|
322
|
+
const NextComponent = () => null;
|
|
323
|
+
updateIslands(options, { default: NextComponent });
|
|
324
|
+
expect(calls.render).toBe(1);
|
|
325
|
+
expect(hydratedComponents[hydratedComponents.length - 1]).toBe(NextComponent);
|
|
326
|
+
const secondHost = host.cloneNode(false);
|
|
327
|
+
document.body.appendChild(secondHost);
|
|
328
|
+
mountIslands(options);
|
|
329
|
+
expect(calls.hydrate).toBe(2);
|
|
330
|
+
expect(hydratedComponents[hydratedComponents.length - 1]).toBe(NextComponent);
|
|
331
|
+
host.remove();
|
|
332
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
333
|
+
expect(calls.unmount).toBe(1);
|
|
334
|
+
runtime.observer?.disconnect();
|
|
335
|
+
});
|
|
336
|
+
it('preserves a focused uncontrolled input with the real React hydrator', async () => {
|
|
337
|
+
const host = document.createElement('eco-island');
|
|
338
|
+
host.setAttribute('data-eco-island', '');
|
|
339
|
+
host.setAttribute('data-eco-component-id', 'real-instance');
|
|
340
|
+
host.setAttribute('data-eco-component-key', 'real-component');
|
|
341
|
+
host.setAttribute('data-eco-props', btoa(JSON.stringify({ value: 'server value' })));
|
|
342
|
+
const input = document.createElement('input');
|
|
343
|
+
input.defaultValue = 'server value';
|
|
344
|
+
input.value = 'typed before hydration';
|
|
345
|
+
host.appendChild(input);
|
|
346
|
+
document.body.appendChild(host);
|
|
347
|
+
input.focus();
|
|
348
|
+
const Component = (props) => createElement('input', { defaultValue: String(props.value ?? '') });
|
|
349
|
+
const reactRuntime = {
|
|
350
|
+
hydrateRoot: (target, tree, options) => {
|
|
351
|
+
const root = hydrateRoot(target, tree, options);
|
|
352
|
+
return {
|
|
353
|
+
render: (nextTree) => root.render(nextTree),
|
|
354
|
+
unmount: () => root.unmount(),
|
|
355
|
+
};
|
|
356
|
+
},
|
|
357
|
+
createElement: (type, props) => createElement(type, props),
|
|
358
|
+
useEffect: (effect) => {
|
|
359
|
+
useEffect(effect);
|
|
360
|
+
},
|
|
361
|
+
};
|
|
362
|
+
const runtime = { islandRoots: {}, islandComponents: {} };
|
|
363
|
+
mountIslands({
|
|
364
|
+
targetSelector: '[data-eco-component-key="real-component"]',
|
|
365
|
+
componentModule: { default: Component },
|
|
366
|
+
scriptId: 'real-island-script',
|
|
367
|
+
runtime: reactRuntime,
|
|
368
|
+
runtimeState: runtime,
|
|
369
|
+
});
|
|
370
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
371
|
+
expect(host.firstElementChild).toBe(input);
|
|
372
|
+
expect(input.value).toBe('typed before hydration');
|
|
373
|
+
expect(document.activeElement).toBe(input);
|
|
374
|
+
expect(host.getAttribute('data-eco-hydrated')).toBe('true');
|
|
375
|
+
runtime.observer?.disconnect();
|
|
376
|
+
});
|
|
377
|
+
it('resolves the component matching componentRef when a module exports multiple components from the same file', async () => {
|
|
378
|
+
const host = document.createElement('eco-island');
|
|
379
|
+
host.setAttribute('data-eco-island', '');
|
|
380
|
+
host.setAttribute('data-eco-component-id', 'multi-instance');
|
|
381
|
+
host.setAttribute('data-eco-component-key', 'multi-component');
|
|
382
|
+
document.body.appendChild(host);
|
|
383
|
+
const Helper = () => null;
|
|
384
|
+
Helper.config = { identity: { id: 'Helper', file: '/app/Counter.tsx' } };
|
|
385
|
+
const Target = () => null;
|
|
386
|
+
Target.config = { identity: { id: 'Target', file: '/app/Counter.tsx' } };
|
|
387
|
+
let hydratedComponent;
|
|
388
|
+
const reactRuntime = {
|
|
389
|
+
hydrateRoot: (_target, tree) => {
|
|
390
|
+
hydratedComponent = tree.props.Component;
|
|
391
|
+
return { render: () => { }, unmount: () => { } };
|
|
392
|
+
},
|
|
393
|
+
createElement: (type, props) => ({ type, props }),
|
|
394
|
+
useEffect: () => { },
|
|
395
|
+
};
|
|
396
|
+
const runtime = { islandRoots: {}, islandComponents: {} };
|
|
397
|
+
mountIslands({
|
|
398
|
+
targetSelector: '[data-eco-component-key="multi-component"]',
|
|
399
|
+
componentModule: { Helper, Target },
|
|
400
|
+
componentRef: 'Target',
|
|
401
|
+
componentFile: '/app/Counter.tsx',
|
|
402
|
+
scriptId: 'multi-island-script',
|
|
403
|
+
runtime: reactRuntime,
|
|
404
|
+
runtimeState: runtime,
|
|
405
|
+
});
|
|
406
|
+
expect(hydratedComponent).toBe(Target);
|
|
407
|
+
runtime.observer?.disconnect();
|
|
408
|
+
});
|
|
409
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
function assertRouterConfiguration(options) {
|
|
2
|
+
if (options.routerComponents && !options.routerImportPath) {
|
|
3
|
+
throw new Error('routerImportPath is required when router components are configured');
|
|
4
|
+
}
|
|
5
|
+
if (options.routerComponents && !options.routerPropsExpression) {
|
|
6
|
+
throw new Error('routerPropsExpression is required when router components are configured');
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function buildPageImport(options) {
|
|
10
|
+
if (options.isMdx || options.hasPagePreload) {
|
|
11
|
+
return `import * as PageModule from ${JSON.stringify(options.importPath)};`;
|
|
12
|
+
}
|
|
13
|
+
return `import Page from ${JSON.stringify(options.importPath)};`;
|
|
14
|
+
}
|
|
15
|
+
function buildInitialPageBinding(options) {
|
|
16
|
+
if (options.isMdx || options.hasPagePreload) {
|
|
17
|
+
return `const initialPage = resolvePageModule(PageModule, ${options.isMdx}, ${options.isMdx ? 'ensurePageConfigLayouts' : 'undefined'});`;
|
|
18
|
+
}
|
|
19
|
+
return `const initialPage = resolvePageModule({ default: Page }, false);`;
|
|
20
|
+
}
|
|
21
|
+
function buildRouterImports(options) {
|
|
22
|
+
if (!options.routerComponents)
|
|
23
|
+
return '';
|
|
24
|
+
return `import { ${options.routerComponents.router}, ${options.routerComponents.pageContent} } from ${JSON.stringify(options.routerImportPath)};`;
|
|
25
|
+
}
|
|
26
|
+
function buildRouterTree(options) {
|
|
27
|
+
if (!options.routerComponents) {
|
|
28
|
+
return 'const createTree = (Page, props) => composeLayoutPageTree(Page, props);';
|
|
29
|
+
}
|
|
30
|
+
return `const createTree = (Page, props) => {
|
|
31
|
+
const pageContent = createElement(${options.routerComponents.pageContent});
|
|
32
|
+
return createElement(${options.routerComponents.router}, ${options.routerPropsExpression}, pageContent);
|
|
33
|
+
};`;
|
|
34
|
+
}
|
|
35
|
+
function buildNormalizationImport(options) {
|
|
36
|
+
if (!options.isMdx)
|
|
37
|
+
return '';
|
|
38
|
+
return `import { ensurePageConfigLayouts } from ${JSON.stringify(options.pageLayoutNormalizationImportPath)};`;
|
|
39
|
+
}
|
|
40
|
+
function buildHmrOptions(options) {
|
|
41
|
+
if (!options.hmrEnabled)
|
|
42
|
+
return '';
|
|
43
|
+
const layoutStack = options.routerComponents
|
|
44
|
+
? 'getLayoutStack: (Page) => (Page.config?.layouts ?? []).map((layout) => layout?.config?.identity?.file ?? "").join("|"),'
|
|
45
|
+
: '';
|
|
46
|
+
return `hmr: {
|
|
47
|
+
importPath: ${JSON.stringify(options.importPath)},
|
|
48
|
+
${layoutStack}
|
|
49
|
+
},`;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Creates the Page entry contract consumed by the browser lifecycle.
|
|
53
|
+
*/
|
|
54
|
+
export function createPageEntrySource(options) {
|
|
55
|
+
assertRouterConfiguration(options);
|
|
56
|
+
const pageBootstrapId = '\0ecopages-react-page-bootstrap';
|
|
57
|
+
const pageDataReaderId = '\0ecopages-react-page-data-reader';
|
|
58
|
+
return `
|
|
59
|
+
import { hydrateRoot } from ${JSON.stringify(options.reactDomClientImportPath)};
|
|
60
|
+
import { createElement } from ${JSON.stringify(options.reactImportPath)};
|
|
61
|
+
import { resolvePageModule, startPageHydration } from ${JSON.stringify(pageBootstrapId)};
|
|
62
|
+
import { readPageDataDocument, getPageDataFromDocument } from ${JSON.stringify(pageDataReaderId)};
|
|
63
|
+
${options.routerComponents ? '' : `import { composeLayoutPageTree } from ${JSON.stringify(options.layoutComposeImportPath)};`}
|
|
64
|
+
${buildRouterImports(options)}
|
|
65
|
+
${buildNormalizationImport(options)}
|
|
66
|
+
${buildPageImport(options)}
|
|
67
|
+
const pageModuleUrl = ${options.pageModuleUrlExpression};
|
|
68
|
+
${buildInitialPageBinding(options)}
|
|
69
|
+
${buildRouterTree(options)}
|
|
70
|
+
export default initialPage.Page;
|
|
71
|
+
${options.hasPagePreload ? 'export const preload = initialPage.preload;' : ''}
|
|
72
|
+
export const config = initialPage.Page.config;
|
|
73
|
+
startPageHydration({
|
|
74
|
+
scriptId: ${JSON.stringify(options.scriptId)},
|
|
75
|
+
pageModuleUrl,
|
|
76
|
+
Page: initialPage.Page,
|
|
77
|
+
pageDataReader: { readPageDataDocument, getPageDataFromDocument },
|
|
78
|
+
runtime: { hydrateRoot, createElement },
|
|
79
|
+
createTree,
|
|
80
|
+
hasRouter: ${Boolean(options.routerComponents)},
|
|
81
|
+
isMdx: ${options.isMdx},
|
|
82
|
+
${options.isMdx ? 'normalizePageConfig: ensurePageConfigLayouts,' : ''}
|
|
83
|
+
${options.hasPagePreload ? 'preload: initialPage.preload,' : ''}
|
|
84
|
+
${buildHmrOptions(options)}
|
|
85
|
+
});
|
|
86
|
+
`.trim();
|
|
87
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** Complete inputs that affect an Island Host script's generated source. */
|
|
2
|
+
export type CompiledIslandHydrationOptions = {
|
|
3
|
+
importPath: string;
|
|
4
|
+
scriptId: string;
|
|
5
|
+
reactImportPath: string;
|
|
6
|
+
reactDomClientImportPath: string;
|
|
7
|
+
targetSelector: string;
|
|
8
|
+
componentRef?: string;
|
|
9
|
+
componentFile?: string;
|
|
10
|
+
minify: boolean;
|
|
11
|
+
hmrEnabled?: boolean;
|
|
12
|
+
};
|
|
13
|
+
export type { CompiledPageHydrationOptions } from './compiled-page-hydration-options.js';
|
|
14
|
+
import type { CompiledPageHydrationOptions } from './compiled-page-hydration-options.js';
|
|
15
|
+
/**
|
|
16
|
+
* Compiles the editable React Island Host lifecycle into a browser ESM string.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* Compilation is cached by the complete entry configuration. Rejected promises
|
|
20
|
+
* are removed so a transient filesystem or bundler error can recover.
|
|
21
|
+
*/
|
|
22
|
+
export declare class IslandHydrationScriptCompiler {
|
|
23
|
+
private readonly compilationCache;
|
|
24
|
+
/**
|
|
25
|
+
* Compiles and caches one readable or minified Island Host entry.
|
|
26
|
+
*
|
|
27
|
+
* @param options - Complete entry imports, selector, identity, and output mode.
|
|
28
|
+
* @returns A browser ESM source string.
|
|
29
|
+
*/
|
|
30
|
+
compile(options: CompiledIslandHydrationOptions): Promise<string>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Compiles editable Page lifecycle modules into browser ESM strings.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* The cache belongs to the service instance, allowing development restarts to
|
|
37
|
+
* discard framework-source changes without sharing stale output globally.
|
|
38
|
+
*/
|
|
39
|
+
export declare class PageHydrationScriptCompiler {
|
|
40
|
+
private readonly compilationCache;
|
|
41
|
+
/**
|
|
42
|
+
* Compiles and caches one readable or minified Page entry.
|
|
43
|
+
*
|
|
44
|
+
* @param options - Complete page imports, router contract, and output mode.
|
|
45
|
+
* @returns A browser ESM source string.
|
|
46
|
+
*/
|
|
47
|
+
compile(options: CompiledPageHydrationOptions): Promise<string>;
|
|
48
|
+
}
|