@ecopages/react 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecopages/react",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "React integration for Ecopages",
5
5
  "keywords": [
6
6
  "ecopages",
@@ -104,16 +104,16 @@
104
104
  "directory": "packages/integrations/react"
105
105
  },
106
106
  "peerDependencies": {
107
- "@ecopages/core": "0.3.1",
107
+ "@ecopages/core": "0.4.0",
108
108
  "@types/react": "^19",
109
109
  "@types/react-dom": "^19",
110
110
  "react": "^19",
111
111
  "react-dom": "^19"
112
112
  },
113
113
  "dependencies": {
114
- "@ecopages/file-system": "0.3.1",
114
+ "@ecopages/file-system": "0.4.0",
115
115
  "@ecopages/logger": "^0.2.3",
116
- "@ecopages/mdx": "0.3.1",
116
+ "@ecopages/mdx": "0.4.0",
117
117
  "@mdx-js/mdx": "^3.1.1",
118
118
  "oxc-parser": "^0.141.0",
119
119
  "rolldown": "^1.1.0"
@@ -23,6 +23,8 @@ export type IslandHydrationRuntime = {
23
23
  islandRoots: Record<string, IslandRoot>;
24
24
  islandComponents: Record<string, IslandComponent>;
25
25
  observer?: MutationObserver;
26
+ /** Island entries by HMR module path, then by script id. One file can declare several islands. */
27
+ hmrEntries?: Map<string, Map<string, IslandHydrationOptions>>;
26
28
  };
27
29
  /** Configuration for one compiled React Island Host entry. */
28
30
  export type IslandHydrationOptions = {
@@ -47,10 +49,11 @@ export declare function getIslandHydrationRuntime(): IslandHydrationRuntime;
47
49
  * Registers the development HMR callback for one component module.
48
50
  *
49
51
  * @remarks
50
- * The callback imports the replacement module and delegates to
51
- * {@link updateIslands}, which updates every live host matching this entry.
52
- * The dynamic import function is supplied by the generated entry so HMR keeps
53
- * the browser's normal module-resolution behavior.
52
+ * The callback imports the replacement module once and delegates to
53
+ * {@link updateIslands} for every island entry registered under the same module
54
+ * path, since every island of one file shares that path. The dynamic import
55
+ * function is supplied by the generated entry so HMR keeps the browser's normal
56
+ * module-resolution behavior.
54
57
  *
55
58
  * @param options - Entry configuration whose hosts should receive updates.
56
59
  * @param importModule - Browser dynamic-import function for the replacement module.
@@ -75,6 +78,11 @@ export declare function mountIslands(options: IslandHydrationOptions): void;
75
78
  * Updates arriving before the initial commit are stored on each host and
76
79
  * rendered by {@link IslandLifecycle} after it marks that host hydrated.
77
80
  *
81
+ * An entry with a component id throws when the new module exports nothing with
82
+ * that id, for example after the declaration was renamed. Falling back to another
83
+ * export could render a sibling or a helper; the thrown error makes the HMR
84
+ * client reload the page instead.
85
+ *
78
86
  * @param options - The live entry configuration and runtime state.
79
87
  * @param newModule - Newly imported component module.
80
88
  */
@@ -32,10 +32,11 @@ export function getIslandHydrationRuntime() {
32
32
  * Registers the development HMR callback for one component module.
33
33
  *
34
34
  * @remarks
35
- * The callback imports the replacement module and delegates to
36
- * {@link updateIslands}, which updates every live host matching this entry.
37
- * The dynamic import function is supplied by the generated entry so HMR keeps
38
- * the browser's normal module-resolution behavior.
35
+ * The callback imports the replacement module once and delegates to
36
+ * {@link updateIslands} for every island entry registered under the same module
37
+ * path, since every island of one file shares that path. The dynamic import
38
+ * function is supplied by the generated entry so HMR keeps the browser's normal
39
+ * module-resolution behavior.
39
40
  *
40
41
  * @param options - Entry configuration whose hosts should receive updates.
41
42
  * @param importModule - Browser dynamic-import function for the replacement module.
@@ -44,13 +45,23 @@ export function getIslandHydrationRuntime() {
44
45
  export function registerIslandHmr(options, importModule, importPath) {
45
46
  if (!importPath)
46
47
  return;
48
+ options.runtimeState.hmrEntries ??= new Map();
49
+ const entries = options.runtimeState.hmrEntries.get(importPath) ?? new Map();
50
+ options.runtimeState.hmrEntries.set(importPath, entries);
51
+ entries.set(options.scriptId, options);
47
52
  const pageWindow = window;
48
53
  const pageRuntime = (pageWindow.__ECO_PAGES__ ??= {});
49
54
  pageRuntime.hmrHandlers ??= {};
50
55
  pageRuntime.hmrHandlers[importPath] = async (url) => {
51
- updateIslands(options, await importModule(url));
56
+ const newModule = await importModule(url);
57
+ for (const entry of entries.values())
58
+ updateIslands(entry, newModule);
52
59
  };
53
60
  }
61
+ function findComponentById(module, componentRef) {
62
+ return Object.values(module).find((entry) => typeof entry === 'function' &&
63
+ entry.config?.identity?.id === componentRef);
64
+ }
54
65
  /**
55
66
  * Selects the component exported by a browser entry for the current Island Host.
56
67
  *
@@ -61,15 +72,12 @@ export function registerIslandHmr(options, importModule, importPath) {
61
72
  * entries without framework identity metadata.
62
73
  */
63
74
  function resolveComponent(module, componentRef) {
64
- const values = Object.values(module).filter((entry) => typeof entry === 'function');
65
- if (componentRef) {
66
- const matchById = values.find((entry) => entry.config?.identity?.id === componentRef);
67
- if (matchById)
68
- return matchById;
69
- }
75
+ const matchById = componentRef ? findComponentById(module, componentRef) : undefined;
76
+ if (matchById)
77
+ return matchById;
70
78
  if (typeof module.default === 'function')
71
79
  return module.default;
72
- return values[0] ?? null;
80
+ return Object.values(module).find((entry) => typeof entry === 'function') ?? null;
73
81
  }
74
82
  function getComponentId(target) {
75
83
  return target.getAttribute('data-eco-component-id');
@@ -215,13 +223,22 @@ export function mountIslands(options) {
215
223
  * Updates arriving before the initial commit are stored on each host and
216
224
  * rendered by {@link IslandLifecycle} after it marks that host hydrated.
217
225
  *
226
+ * An entry with a component id throws when the new module exports nothing with
227
+ * that id, for example after the declaration was renamed. Falling back to another
228
+ * export could render a sibling or a helper; the thrown error makes the HMR
229
+ * client reload the page instead.
230
+ *
218
231
  * @param options - The live entry configuration and runtime state.
219
232
  * @param newModule - Newly imported component module.
220
233
  */
221
234
  export function updateIslands(options, newModule) {
222
- const component = resolveComponent(newModule, options.componentRef ?? '');
223
- if (!component)
224
- return;
235
+ const componentRef = options.componentRef;
236
+ const component = componentRef ? findComponentById(newModule, componentRef) : resolveComponent(newModule, '');
237
+ if (!component) {
238
+ if (!componentRef)
239
+ return;
240
+ throw new Error(`[ecopages] The updated module no longer exports island component "${componentRef}".`);
241
+ }
225
242
  options.componentModule = newModule;
226
243
  const scriptId = options.scriptId;
227
244
  options.runtimeState.islandComponents[scriptId] = component;