@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.
Files changed (57) hide show
  1. package/README.md +5 -1
  2. package/package.json +4 -4
  3. package/src/app/client-only.js +13 -17
  4. package/src/app/dynamic.js +7 -10
  5. package/src/app/dynamic.test.browser.js +27 -29
  6. package/src/bundling/bootstrap-package-paths.js +5 -6
  7. package/src/bundling/bundle.js +115 -114
  8. package/src/bundling/discover-layout-runtime-modules.js +166 -155
  9. package/src/bundling/runtime-alias-map.d.ts +4 -1
  10. package/src/bundling/runtime-alias-map.js +107 -95
  11. package/src/bundling/runtime-bundle.js +250 -274
  12. package/src/bundling/runtime-interop-plugin.js +31 -35
  13. package/src/bundling/runtime-mode.js +10 -5
  14. package/src/bundling/runtime-modules.js +46 -35
  15. package/src/client-graph/ast-transform.js +415 -305
  16. package/src/client-graph/boundary-cache.js +146 -104
  17. package/src/client-graph/boundary-plugin.js +122 -104
  18. package/src/client-graph/component-config-traversal.js +70 -49
  19. package/src/client-graph/declared-modules.js +63 -41
  20. package/src/client-graph/reachability-analyzer.js +506 -313
  21. package/src/client-graph/specifier-classification.js +209 -141
  22. package/src/contracts/router-adapter.js +6 -0
  23. package/src/dev-transform/react-dev-transform-contributor.d.ts +17 -0
  24. package/src/dev-transform/react-dev-transform-contributor.js +22 -0
  25. package/src/hmr/hmr-scripts.js +22 -9
  26. package/src/hmr/hmr-strategy.d.ts +12 -181
  27. package/src/hmr/hmr-strategy.js +321 -650
  28. package/src/hmr/page-metadata-cache.js +43 -40
  29. package/src/hmr/react-hmr-dev-transform-plugins.d.ts +23 -0
  30. package/src/hmr/react-hmr-dev-transform-plugins.js +20 -0
  31. package/src/hydration/assert-no-bare-ecopages-imports.js +6 -6
  32. package/src/hydration/bootstrap-imports.js +30 -20
  33. package/src/hydration/hydration-asset.js +216 -213
  34. package/src/hydration/hydration-scripts.d.ts +4 -0
  35. package/src/hydration/hydration-scripts.js +199 -66
  36. package/src/hydration/hydration-scripts.test.browser.js +163 -168
  37. package/src/hydration/page-data-manifest.js +89 -45
  38. package/src/hydration/page-data-reader.js +45 -32
  39. package/src/hydration/page-payload.js +101 -99
  40. package/src/hydration/serialize-page-data-script.js +27 -28
  41. package/src/mdx/mdx-config-dependency.js +108 -112
  42. package/src/mdx/mdx-loader-plugin.js +7 -10
  43. package/src/page/page-module.js +72 -66
  44. package/src/plugin/react.constants.js +1 -4
  45. package/src/plugin/react.plugin.d.ts +1 -0
  46. package/src/plugin/react.plugin.js +227 -216
  47. package/src/plugin/react.types.js +1 -0
  48. package/src/render/component-ownership.js +42 -18
  49. package/src/render/component-ssr.js +168 -190
  50. package/src/render/eco-embed.js +8 -10
  51. package/src/render/errors.js +21 -16
  52. package/src/render/html-boundary.js +55 -51
  53. package/src/render/layout-compose.js +122 -94
  54. package/src/render/react-renderer.js +433 -468
  55. package/src/render/react-runtime.js +25 -20
  56. package/src/render/unified-layout-composition.js +74 -85
  57. package/src/runtime/use-sync-external-store-with-selector.js +52 -54
@@ -1,21 +1,26 @@
1
- import { createRequire } from "node:module";
2
- import path from "node:path";
3
- function resolveReactRuntimeModules(rootDir) {
4
- const appPackageJsonPath = path.resolve(rootDir || process.cwd(), "package.json");
5
- try {
6
- const requireFromApp = createRequire(appPackageJsonPath);
7
- return {
8
- react: requireFromApp("react"),
9
- reactDomServer: requireFromApp("react-dom/server")
10
- };
11
- } catch {
12
- const requireFromIntegration = createRequire(import.meta.url);
13
- return {
14
- react: requireFromIntegration("react"),
15
- reactDomServer: requireFromIntegration("react-dom/server")
16
- };
17
- }
1
+ /**
2
+ * Resolves the React and react-dom/server modules used for SSR.
3
+ *
4
+ * @remarks
5
+ * Prefers the app's own React install so SSR identity matches the app bundle.
6
+ * Falls back to the integration's React when the app package cannot resolve them.
7
+ */
8
+ import { createRequire } from 'node:module';
9
+ import path from 'node:path';
10
+ export function resolveReactRuntimeModules(rootDir) {
11
+ const appPackageJsonPath = path.resolve(rootDir || process.cwd(), 'package.json');
12
+ try {
13
+ const requireFromApp = createRequire(appPackageJsonPath);
14
+ return {
15
+ react: requireFromApp('react'),
16
+ reactDomServer: requireFromApp('react-dom/server'),
17
+ };
18
+ }
19
+ catch {
20
+ const requireFromIntegration = createRequire(import.meta.url);
21
+ return {
22
+ react: requireFromIntegration('react'),
23
+ reactDomServer: requireFromIntegration('react-dom/server'),
24
+ };
25
+ }
18
26
  }
19
- export {
20
- resolveReactRuntimeModules
21
- };
@@ -1,89 +1,78 @@
1
- import { hasSingleRootElement } from "./html-boundary.js";
2
- import { isReactManagedComponent } from "./component-ownership.js";
3
- import {
4
- assertComposablePage,
5
- composeLayoutPageTree,
6
- composeLayoutPageTreeFromShell,
7
- resolveLayoutContextFromShell
8
- } from "./layout-compose.js";
9
- function composeReactLayoutPageTree(Page, pageProps, shellEntries, options) {
10
- const hasNormalizedLayouts = Boolean(Page.config?.layoutEntries?.length) || Boolean(Page.config?.layouts?.length);
11
- if (hasNormalizedLayouts) {
12
- return composeLayoutPageTree(Page, pageProps, options);
13
- }
14
- return composeLayoutPageTreeFromShell(Page, pageProps, shellEntries, options);
15
- }
16
- function shouldUseUnifiedReactLayoutComposition(options) {
17
- const { page, shellLayouts, reactIntegrationName } = options;
18
- if (!isReactManagedComponent(page, reactIntegrationName)) {
19
- return false;
20
- }
21
- const composablePage = assertComposablePage(page);
22
- const configLayouts = composablePage.config?.layouts ?? composablePage.config?.layoutEntries?.map((entry) => entry.component) ?? [];
23
- const layoutComponents = shellLayouts.length > 0 ? shellLayouts.map((layout) => layout.component) : configLayouts;
24
- if (layoutComponents.length === 0) {
25
- return false;
26
- }
27
- return layoutComponents.every((component) => isReactManagedComponent(component, reactIntegrationName));
1
+ /**
2
+ * Unified React layout + page composition for SSR document shells.
3
+ *
4
+ * @remarks
5
+ * When every shell layout is React-managed, compose one element tree via
6
+ * {@link composeLayoutPageTree} / {@link composeLayoutPageTreeFromShell} instead of
7
+ * rendering each tier separately. This keeps SSR DOM shape aligned with client
8
+ * hydration through `@ecopages/react/layout-compose`.
9
+ */
10
+ import { hasSingleRootElement } from './html-boundary.js';
11
+ import { isReactManagedComponent } from './component-ownership.js';
12
+ import { assertComposablePage, composeLayoutPageTree, composeLayoutPageTreeFromShell, resolveLayoutContextFromShell, } from './layout-compose.js';
13
+ export function composeReactLayoutPageTree(Page, pageProps, shellEntries, options) {
14
+ const hasNormalizedLayouts = Boolean(Page.config?.layoutEntries?.length) || Boolean(Page.config?.layouts?.length);
15
+ if (hasNormalizedLayouts) {
16
+ return composeLayoutPageTree(Page, pageProps, options);
17
+ }
18
+ return composeLayoutPageTreeFromShell(Page, pageProps, shellEntries, options);
28
19
  }
29
- function resolveComposeChildren(options) {
30
- const { page, shellLayouts, reactIntegrationName, composeChildren } = options;
31
- if (!shouldUseUnifiedReactLayoutComposition({
32
- page: page.component,
33
- shellLayouts,
34
- reactIntegrationName
35
- })) {
36
- return void 0;
37
- }
38
- return (context) => composeChildren(page, context);
20
+ export function shouldUseUnifiedReactLayoutComposition(options) {
21
+ const { page, shellLayouts, reactIntegrationName } = options;
22
+ if (!isReactManagedComponent(page, reactIntegrationName)) {
23
+ return false;
24
+ }
25
+ const composablePage = assertComposablePage(page);
26
+ const configLayouts = composablePage.config?.layouts ?? composablePage.config?.layoutEntries?.map((entry) => entry.component) ?? [];
27
+ const layoutComponents = shellLayouts.length > 0 ? shellLayouts.map((layout) => layout.component) : configLayouts;
28
+ if (layoutComponents.length === 0) {
29
+ return false;
30
+ }
31
+ return layoutComponents.every((component) => isReactManagedComponent(component, reactIntegrationName));
39
32
  }
40
- async function composeReactLayoutPageChildren(options) {
41
- const {
42
- page,
43
- context,
44
- runtime,
45
- integrationName,
46
- normalizeUnresolvedMarkerArtifactHtml,
47
- getRootTagName,
48
- getQueuedForeignSubtreeResolutionContext,
49
- resolveQueuedForeignSubtreeHtml
50
- } = options;
51
- const pageComponent = assertComposablePage(page.component);
52
- const shellEntries = context.layouts.map((layout) => ({
53
- component: layout.component,
54
- props: layout.props
55
- }));
56
- const { react, reactDomServer } = runtime;
57
- const tree = composeReactLayoutPageTree(pageComponent, page.props, shellEntries, {
58
- context: resolveLayoutContextFromShell(page.props, shellEntries),
59
- react
60
- });
61
- const runtimeInput = {
62
- component: page.component,
63
- props: page.props,
64
- integrationContext: {
65
- rendererCache: context.rendererCache
33
+ export function resolveComposeChildren(options) {
34
+ const { page, shellLayouts, reactIntegrationName, composeChildren } = options;
35
+ if (!shouldUseUnifiedReactLayoutComposition({
36
+ page: page.component,
37
+ shellLayouts,
38
+ reactIntegrationName,
39
+ })) {
40
+ return undefined;
66
41
  }
67
- };
68
- const runtimeContext = getQueuedForeignSubtreeResolutionContext(runtimeInput);
69
- const html = normalizeUnresolvedMarkerArtifactHtml(reactDomServer.renderToString(tree));
70
- const resolved = await resolveQueuedForeignSubtreeHtml(html, runtimeContext);
71
- const primaryRender = {
72
- html: resolved.html,
73
- canAttachAttributes: hasSingleRootElement(resolved.html),
74
- rootTag: getRootTagName(resolved.html),
75
- integrationName,
76
- assets: resolved.assets.length > 0 ? resolved.assets : void 0
77
- };
78
- return {
79
- children: resolved.html,
80
- layoutRenders: [],
81
- primaryRender
82
- };
42
+ return (context) => composeChildren(page, context);
43
+ }
44
+ export async function composeReactLayoutPageChildren(options) {
45
+ const { page, context, runtime, integrationName, normalizeUnresolvedMarkerArtifactHtml, getRootTagName, getQueuedForeignSubtreeResolutionContext, resolveQueuedForeignSubtreeHtml, } = options;
46
+ const pageComponent = assertComposablePage(page.component);
47
+ const shellEntries = context.layouts.map((layout) => ({
48
+ component: layout.component,
49
+ props: layout.props,
50
+ }));
51
+ const { react, reactDomServer } = runtime;
52
+ const tree = composeReactLayoutPageTree(pageComponent, page.props, shellEntries, {
53
+ context: resolveLayoutContextFromShell(page.props, shellEntries),
54
+ react,
55
+ });
56
+ const runtimeInput = {
57
+ component: page.component,
58
+ props: page.props,
59
+ integrationContext: {
60
+ rendererCache: context.rendererCache,
61
+ },
62
+ };
63
+ const runtimeContext = getQueuedForeignSubtreeResolutionContext(runtimeInput);
64
+ const html = normalizeUnresolvedMarkerArtifactHtml(reactDomServer.renderToString(tree));
65
+ const resolved = await resolveQueuedForeignSubtreeHtml(html, runtimeContext);
66
+ const primaryRender = {
67
+ html: resolved.html,
68
+ canAttachAttributes: hasSingleRootElement(resolved.html),
69
+ rootTag: getRootTagName(resolved.html),
70
+ integrationName,
71
+ assets: resolved.assets.length > 0 ? resolved.assets : undefined,
72
+ };
73
+ return {
74
+ children: resolved.html,
75
+ layoutRenders: [],
76
+ primaryRender,
77
+ };
83
78
  }
84
- export {
85
- composeReactLayoutPageChildren,
86
- composeReactLayoutPageTree,
87
- resolveComposeChildren,
88
- shouldUseUnifiedReactLayoutComposition
89
- };
@@ -1,56 +1,54 @@
1
- import { useDebugValue, useEffect, useMemo, useRef, useSyncExternalStore } from "react";
1
+ /* eslint-disable react-hooks/exhaustive-deps -- shim mirrors upstream ref-based hook semantics */
2
+ import { useDebugValue, useEffect, useMemo, useRef, useSyncExternalStore } from 'react';
2
3
  const objectIs = Object.is;
3
- function useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnapshot, selector, isEqual) {
4
- const instRef = useRef({ hasValue: false, value: void 0 });
5
- const memoizedSelectionRef = useMemo(() => {
6
- let hasMemo = false;
7
- let memoizedSnapshot;
8
- let memoizedSelection;
9
- const maybeGetServerSnapshot = getServerSnapshot === void 0 ? null : getServerSnapshot;
10
- const memoizedSelector = (nextSnapshot) => {
11
- if (!hasMemo) {
12
- hasMemo = true;
13
- memoizedSnapshot = nextSnapshot;
14
- const nextSelection2 = selector(nextSnapshot);
15
- const inst = instRef.current;
16
- if (isEqual !== void 0 && inst.hasValue) {
17
- const currentSelection2 = inst.value;
18
- if (isEqual(currentSelection2, nextSelection2)) {
19
- memoizedSelection = currentSelection2;
20
- return currentSelection2;
21
- }
22
- }
23
- memoizedSelection = nextSelection2;
24
- return nextSelection2;
25
- }
26
- const currentSelection = memoizedSelection;
27
- if (objectIs(memoizedSnapshot, nextSnapshot)) {
28
- return currentSelection;
29
- }
30
- const nextSelection = selector(nextSnapshot);
31
- if (isEqual !== void 0 && isEqual(currentSelection, nextSelection)) {
32
- memoizedSnapshot = nextSnapshot;
33
- return currentSelection;
34
- }
35
- memoizedSnapshot = nextSnapshot;
36
- memoizedSelection = nextSelection;
37
- return nextSelection;
38
- };
39
- return [
40
- () => memoizedSelector(getSnapshot()),
41
- maybeGetServerSnapshot === null ? void 0 : () => memoizedSelector(maybeGetServerSnapshot())
42
- ];
43
- }, [getSnapshot, getServerSnapshot, selector, isEqual]);
44
- const value = useSyncExternalStore(subscribe, memoizedSelectionRef[0], memoizedSelectionRef[1]);
45
- useEffect(() => {
46
- instRef.current = {
47
- hasValue: true,
48
- value
49
- };
50
- }, [value]);
51
- useDebugValue(value);
52
- return value;
4
+ export function useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnapshot, selector, isEqual) {
5
+ const instRef = useRef({ hasValue: false, value: undefined });
6
+ const memoizedSelectionRef = useMemo(() => {
7
+ let hasMemo = false;
8
+ let memoizedSnapshot;
9
+ let memoizedSelection;
10
+ const maybeGetServerSnapshot = getServerSnapshot === undefined ? null : getServerSnapshot;
11
+ const memoizedSelector = (nextSnapshot) => {
12
+ if (!hasMemo) {
13
+ hasMemo = true;
14
+ memoizedSnapshot = nextSnapshot;
15
+ const nextSelection = selector(nextSnapshot);
16
+ const inst = instRef.current;
17
+ if (isEqual !== undefined && inst.hasValue) {
18
+ const currentSelection = inst.value;
19
+ if (isEqual(currentSelection, nextSelection)) {
20
+ memoizedSelection = currentSelection;
21
+ return currentSelection;
22
+ }
23
+ }
24
+ memoizedSelection = nextSelection;
25
+ return nextSelection;
26
+ }
27
+ const currentSelection = memoizedSelection;
28
+ if (objectIs(memoizedSnapshot, nextSnapshot)) {
29
+ return currentSelection;
30
+ }
31
+ const nextSelection = selector(nextSnapshot);
32
+ if (isEqual !== undefined && isEqual(currentSelection, nextSelection)) {
33
+ memoizedSnapshot = nextSnapshot;
34
+ return currentSelection;
35
+ }
36
+ memoizedSnapshot = nextSnapshot;
37
+ memoizedSelection = nextSelection;
38
+ return nextSelection;
39
+ };
40
+ return [
41
+ () => memoizedSelector(getSnapshot()),
42
+ maybeGetServerSnapshot === null ? undefined : () => memoizedSelector(maybeGetServerSnapshot()),
43
+ ];
44
+ }, [getSnapshot, getServerSnapshot, selector, isEqual]);
45
+ const value = useSyncExternalStore(subscribe, memoizedSelectionRef[0], memoizedSelectionRef[1]);
46
+ useEffect(() => {
47
+ instRef.current = {
48
+ hasValue: true,
49
+ value,
50
+ };
51
+ }, [value]);
52
+ useDebugValue(value);
53
+ return value;
53
54
  }
54
- export {
55
- useSyncExternalStoreWithSelector
56
- };