@ecopages/react 0.2.0-beta.8 → 0.2.0-rc.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/README.md +179 -7
- package/package.json +54 -29
- package/src/app/client-only.js +15 -0
- package/src/app/dynamic.js +9 -0
- package/src/app/dynamic.test.browser.js +31 -0
- package/src/bundling/bootstrap-package-paths.d.ts +5 -0
- package/src/bundling/bootstrap-package-paths.js +5 -0
- package/src/{services/react-bundle.service.d.ts → bundling/bundle.d.ts} +23 -8
- package/src/bundling/bundle.js +120 -0
- package/src/bundling/discover-layout-runtime-modules.d.ts +32 -0
- package/src/bundling/discover-layout-runtime-modules.js +185 -0
- package/src/bundling/runtime-alias-map.d.ts +13 -0
- package/src/bundling/runtime-alias-map.js +113 -0
- package/src/{services/react-runtime-bundle.service.d.ts → bundling/runtime-bundle.d.ts} +25 -4
- package/src/bundling/runtime-bundle.js +258 -0
- package/src/bundling/runtime-interop-plugin.js +34 -0
- package/src/bundling/runtime-mode.d.ts +9 -0
- package/src/bundling/runtime-mode.js +11 -0
- package/src/bundling/runtime-modules.d.ts +29 -0
- package/src/bundling/runtime-modules.js +49 -0
- package/src/client-graph/ast-transform.d.ts +18 -0
- package/src/client-graph/ast-transform.js +426 -0
- package/src/{utils/client-graph-boundary-cache.d.ts → client-graph/boundary-cache.d.ts} +23 -13
- package/src/client-graph/boundary-cache.js +182 -0
- package/src/{utils/client-graph-boundary-plugin.d.ts → client-graph/boundary-plugin.d.ts} +4 -13
- package/src/client-graph/boundary-plugin.js +149 -0
- package/src/client-graph/component-config-traversal.js +77 -0
- package/src/client-graph/declared-modules.js +69 -0
- package/src/client-graph/module-classification.d.ts +3 -0
- package/src/client-graph/module-classification.js +43 -0
- package/src/{utils → client-graph}/reachability-analyzer.d.ts +4 -12
- package/src/client-graph/reachability-analyzer.js +515 -0
- package/src/client-graph/specifier-classification.d.ts +75 -0
- package/src/client-graph/specifier-classification.js +214 -0
- 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 +44 -0
- package/src/hmr/hmr-strategy.d.ts +74 -0
- package/src/hmr/hmr-strategy.js +351 -0
- package/src/{services/react-hmr-page-metadata-cache.d.ts → hmr/page-metadata-cache.d.ts} +5 -1
- package/src/hmr/page-metadata-cache.js +44 -0
- 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.d.ts +4 -0
- package/src/hydration/assert-no-bare-ecopages-imports.js +7 -0
- package/src/hydration/bootstrap-imports.d.ts +25 -0
- package/src/hydration/bootstrap-imports.js +33 -0
- package/src/hydration/hydration-asset.d.ts +84 -0
- package/src/hydration/hydration-asset.js +227 -0
- package/src/hydration/hydration-scripts.d.ts +104 -0
- package/src/hydration/hydration-scripts.js +483 -0
- package/src/hydration/hydration-scripts.test.browser.js +199 -0
- package/src/hydration/page-data-manifest.d.ts +58 -0
- package/src/hydration/page-data-manifest.js +97 -0
- package/src/hydration/page-data-reader.d.ts +31 -0
- package/src/hydration/page-data-reader.js +77 -0
- package/src/hydration/page-payload.d.ts +67 -0
- package/src/hydration/page-payload.js +106 -0
- package/src/hydration/serialize-page-data-script.d.ts +27 -0
- package/src/hydration/serialize-page-data-script.js +30 -0
- package/src/{services/react-mdx-config-dependency.service.d.ts → mdx/mdx-config-dependency.d.ts} +6 -6
- package/src/mdx/mdx-config-dependency.js +110 -0
- package/src/mdx/mdx-loader-plugin.d.ts +2 -0
- package/src/mdx/mdx-loader-plugin.js +8 -0
- package/src/page/page-module.d.ts +46 -0
- package/src/page/page-module.js +74 -0
- package/src/plugin/react.constants.js +1 -0
- package/src/{react.plugin.d.ts → plugin/react.plugin.d.ts} +9 -3
- package/src/plugin/react.plugin.js +241 -0
- package/src/plugin/react.types.d.ts +156 -0
- package/src/plugin/react.types.js +1 -0
- package/src/render/component-ownership.d.ts +49 -0
- package/src/render/component-ownership.js +46 -0
- package/src/render/component-ssr.d.ts +113 -0
- package/src/render/component-ssr.js +180 -0
- package/src/render/eco-embed.js +9 -0
- package/src/render/errors.d.ts +16 -0
- package/src/render/errors.js +23 -0
- package/src/render/html-boundary.js +59 -0
- package/src/render/layout-compose.d.ts +66 -0
- package/src/render/layout-compose.js +134 -0
- package/src/render/react-renderer.d.ts +138 -0
- package/src/render/react-renderer.js +448 -0
- package/src/render/react-runtime.d.ts +13 -0
- package/src/render/react-runtime.js +26 -0
- package/src/render/unified-layout-composition.d.ts +53 -0
- package/src/render/unified-layout-composition.js +78 -0
- package/src/runtime/use-sync-external-store-with-selector.js +52 -54
- package/src/eco-embed.js +0 -11
- package/src/react-hmr-strategy.d.ts +0 -230
- package/src/react-hmr-strategy.js +0 -657
- package/src/react-renderer.d.ts +0 -233
- package/src/react-renderer.js +0 -622
- package/src/react.constants.js +0 -4
- package/src/react.plugin.js +0 -209
- package/src/react.types.d.ts +0 -88
- package/src/react.types.js +0 -0
- package/src/router-adapter.js +0 -0
- package/src/services/pages-index.d.ts +0 -64
- package/src/services/pages-index.js +0 -73
- package/src/services/react-bundle.service.js +0 -86
- package/src/services/react-hmr-page-metadata-cache.js +0 -35
- package/src/services/react-hydration-asset.service.d.ts +0 -84
- package/src/services/react-hydration-asset.service.js +0 -217
- package/src/services/react-mdx-config-dependency.service.js +0 -122
- package/src/services/react-page-module.service.d.ts +0 -88
- package/src/services/react-page-module.service.js +0 -166
- package/src/services/react-page-payload.service.d.ts +0 -46
- package/src/services/react-page-payload.service.js +0 -67
- package/src/services/react-runtime-bundle.service.js +0 -192
- package/src/utils/client-graph-boundary-cache.js +0 -116
- package/src/utils/client-graph-boundary-plugin.js +0 -552
- package/src/utils/client-only.js +0 -19
- package/src/utils/component-config-traversal.js +0 -54
- package/src/utils/declared-modules.js +0 -47
- package/src/utils/dynamic.js +0 -12
- package/src/utils/dynamic.test.browser.js +0 -33
- package/src/utils/hmr-scripts.js +0 -31
- package/src/utils/html-boundary.js +0 -55
- package/src/utils/hydration-scripts.d.ts +0 -92
- package/src/utils/hydration-scripts.js +0 -358
- package/src/utils/hydration-scripts.test.browser.js +0 -198
- package/src/utils/reachability-analyzer.js +0 -339
- package/src/utils/react-dom-runtime-interop-plugin.js +0 -38
- package/src/utils/react-mdx-loader-plugin.d.ts +0 -3
- package/src/utils/react-mdx-loader-plugin.js +0 -45
- package/src/utils/react-runtime-alias-map.d.ts +0 -8
- package/src/utils/react-runtime-alias-map.js +0 -90
- /package/src/{utils → app}/client-only.d.ts +0 -0
- /package/src/{utils → app}/dynamic.d.ts +0 -0
- /package/src/{utils → app}/dynamic.test.browser.d.ts +0 -0
- /package/src/{utils/react-dom-runtime-interop-plugin.d.ts → bundling/runtime-interop-plugin.d.ts} +0 -0
- /package/src/{utils → client-graph}/component-config-traversal.d.ts +0 -0
- /package/src/{utils → client-graph}/declared-modules.d.ts +0 -0
- /package/src/{declarations.d.ts → contracts/declarations.d.ts} +0 -0
- /package/src/{router-adapter.d.ts → contracts/router-adapter.d.ts} +0 -0
- /package/src/{utils → hmr}/hmr-scripts.d.ts +0 -0
- /package/src/{utils → hydration}/hydration-scripts.test.browser.d.ts +0 -0
- /package/src/{react.constants.d.ts → plugin/react.constants.d.ts} +0 -0
- /package/src/{eco-embed.d.ts → render/eco-embed.d.ts} +0 -0
- /package/src/{utils → render}/html-boundary.d.ts +0 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React component SSR helpers: renderToString, foreign-subtree HTML, island attributes.
|
|
3
|
+
*/
|
|
4
|
+
import type { ComponentRenderInput, ComponentRenderResult, EcoComponent, EcoPagesElement } from '@ecopages/core';
|
|
5
|
+
import type { ProcessedAsset } from '@ecopages/core/services/asset-processing-service';
|
|
6
|
+
import type { IntegrationRenderer } from '@ecopages/core/route-renderer/orchestration/integration-renderer';
|
|
7
|
+
import type { ReactNode } from 'react';
|
|
8
|
+
import { type HydrationAssetService } from '../hydration/hydration-asset.js';
|
|
9
|
+
import { type SerializableProps } from './component-ownership.js';
|
|
10
|
+
import type { ReactRuntimeModules } from './react-runtime.js';
|
|
11
|
+
export type ReactComponentRenderContext = {
|
|
12
|
+
componentInstanceId?: string;
|
|
13
|
+
};
|
|
14
|
+
export type ReactForeignSubtreeResolutionContext = {
|
|
15
|
+
rendererCache: Map<string, IntegrationRenderer<any>>;
|
|
16
|
+
componentInstanceScope?: string;
|
|
17
|
+
nextForeignSubtreeId: number;
|
|
18
|
+
queuedResolutions: Array<{
|
|
19
|
+
token: string;
|
|
20
|
+
component: EcoComponent;
|
|
21
|
+
props: Record<string, unknown>;
|
|
22
|
+
componentInstanceId: string;
|
|
23
|
+
}>;
|
|
24
|
+
rawChildrenToken?: string;
|
|
25
|
+
rawChildrenHtml?: string;
|
|
26
|
+
};
|
|
27
|
+
export declare function buildHydrationProps(props: SerializableProps | undefined): SerializableProps;
|
|
28
|
+
/**
|
|
29
|
+
* Renders a non-React layout or HTML template and enforces that mixed shells
|
|
30
|
+
* return serialized HTML.
|
|
31
|
+
*/
|
|
32
|
+
export declare function renderNonReactShellComponent<P extends SerializableProps>(Component: (props: P) => EcoPagesElement | Promise<EcoPagesElement>, props: P, label: 'Layout' | 'HtmlTemplate' | 'Component'): Promise<string>;
|
|
33
|
+
/**
|
|
34
|
+
* Renders one React component while preserving already-resolved child HTML.
|
|
35
|
+
*
|
|
36
|
+
* @remarks
|
|
37
|
+
* When nested foreign-subtree resolution has already produced child HTML, the child
|
|
38
|
+
* payload must remain raw SSR output rather than a React string child, otherwise
|
|
39
|
+
* React would escape it. This helper renders a unique token through React and swaps
|
|
40
|
+
* that token back to the resolved HTML afterward.
|
|
41
|
+
*/
|
|
42
|
+
export declare function renderComponentHtml(options: {
|
|
43
|
+
input: ComponentRenderInput;
|
|
44
|
+
context: ReactComponentRenderContext;
|
|
45
|
+
runtimeContext?: ReactForeignSubtreeResolutionContext;
|
|
46
|
+
runtime: ReactRuntimeModules;
|
|
47
|
+
normalizeUnresolvedMarkerArtifactHtml: (html: string) => string;
|
|
48
|
+
}): string;
|
|
49
|
+
export declare function toReactNode(children: unknown, integrationName: string): ReactNode;
|
|
50
|
+
/**
|
|
51
|
+
* Restores raw child HTML placeholders stored on the foreign-subtree runtime context.
|
|
52
|
+
*
|
|
53
|
+
* @remarks
|
|
54
|
+
* Queued foreign-subtree resolution may render children through a fragment path before
|
|
55
|
+
* nested integration tokens are resolved. React must never see resolved child HTML as a
|
|
56
|
+
* normal string child or it would escape it.
|
|
57
|
+
*/
|
|
58
|
+
export declare function restoreRuntimeChildHtml(html: string, runtimeContext: ReactForeignSubtreeResolutionContext | undefined): string;
|
|
59
|
+
export declare function renderQueuedChildrenToHtml(options: {
|
|
60
|
+
children: unknown;
|
|
61
|
+
runtimeContext: ReactForeignSubtreeResolutionContext;
|
|
62
|
+
queuedResolutionsByToken: Map<string, ReactForeignSubtreeResolutionContext['queuedResolutions'][number]>;
|
|
63
|
+
resolveToken: (token: string) => Promise<string>;
|
|
64
|
+
runtime: ReactRuntimeModules;
|
|
65
|
+
integrationName: string;
|
|
66
|
+
normalizeUnresolvedMarkerArtifactHtml: (html: string) => string;
|
|
67
|
+
resolveQueuedTokens: (html: string, queuedResolutionsByToken: Map<string, ReactForeignSubtreeResolutionContext['queuedResolutions'][number]>, resolveToken: (token: string) => Promise<string>) => Promise<string>;
|
|
68
|
+
}): Promise<string | undefined>;
|
|
69
|
+
export declare function renderReactQueuedForeignSubtreeChildren(options: {
|
|
70
|
+
children: unknown;
|
|
71
|
+
currentRuntimeContext: ReactForeignSubtreeResolutionContext;
|
|
72
|
+
queuedResolutionsByToken: Map<string, ReactForeignSubtreeResolutionContext['queuedResolutions'][number]>;
|
|
73
|
+
resolveToken: (token: string) => Promise<string>;
|
|
74
|
+
runtime: ReactRuntimeModules;
|
|
75
|
+
integrationName: string;
|
|
76
|
+
normalizeUnresolvedMarkerArtifactHtml: (html: string) => string;
|
|
77
|
+
resolveQueuedTokens: (html: string, queuedResolutionsByToken: Map<string, ReactForeignSubtreeResolutionContext['queuedResolutions'][number]>, resolveToken: (token: string) => Promise<string>) => Promise<string>;
|
|
78
|
+
}): Promise<{
|
|
79
|
+
assets: ProcessedAsset[];
|
|
80
|
+
html?: string;
|
|
81
|
+
}>;
|
|
82
|
+
export declare function renderForeignComponentWithSerializedHtml(options: {
|
|
83
|
+
input: ComponentRenderInput;
|
|
84
|
+
runtimeContext: ReactForeignSubtreeResolutionContext | undefined;
|
|
85
|
+
integrationName: string;
|
|
86
|
+
canResolveAssets: boolean;
|
|
87
|
+
processComponentDependencies: (components: EcoComponent[]) => Promise<ProcessedAsset[]>;
|
|
88
|
+
dedupeProcessedAssets: (assets: ProcessedAsset[]) => ProcessedAsset[];
|
|
89
|
+
getRootTagName: (html: string) => string | undefined;
|
|
90
|
+
resolveQueuedForeignSubtreeHtml: (html: string, runtimeContext: ReactForeignSubtreeResolutionContext | undefined) => Promise<{
|
|
91
|
+
assets: ProcessedAsset[];
|
|
92
|
+
html: string;
|
|
93
|
+
}>;
|
|
94
|
+
}): Promise<ComponentRenderResult>;
|
|
95
|
+
export declare function renderReactManagedComponent(options: {
|
|
96
|
+
input: ComponentRenderInput;
|
|
97
|
+
runtimeContext: ReactForeignSubtreeResolutionContext | undefined;
|
|
98
|
+
runtime: ReactRuntimeModules;
|
|
99
|
+
integrationName: string;
|
|
100
|
+
normalizeUnresolvedMarkerArtifactHtml: (html: string) => string;
|
|
101
|
+
resolveQueuedForeignSubtreeHtml: (html: string, runtimeContext: ReactForeignSubtreeResolutionContext | undefined) => Promise<{
|
|
102
|
+
assets: ProcessedAsset[];
|
|
103
|
+
html: string;
|
|
104
|
+
}>;
|
|
105
|
+
getRootTagName: (html: string) => string | undefined;
|
|
106
|
+
dedupeProcessedAssets: (assets: ProcessedAsset[]) => ProcessedAsset[];
|
|
107
|
+
hydrationAssetService: HydrationAssetService;
|
|
108
|
+
canBuildIslandAssets: boolean;
|
|
109
|
+
}): Promise<ComponentRenderResult>;
|
|
110
|
+
export declare function createForeignSubtreeRuntimeContext(options: {
|
|
111
|
+
rendererCache: Map<string, IntegrationRenderer<any>>;
|
|
112
|
+
componentInstanceScope?: string;
|
|
113
|
+
}): ReactForeignSubtreeResolutionContext;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React component SSR helpers: renderToString, foreign-subtree HTML, island attributes.
|
|
3
|
+
*/
|
|
4
|
+
import { buildIslandHostAttributes, getComponentIdentity } from '@ecopages/core';
|
|
5
|
+
import { isValidElement } from 'react';
|
|
6
|
+
import { hasSingleRootElement } from './html-boundary.js';
|
|
7
|
+
import { getIslandComponentKey } from '../hydration/hydration-asset.js';
|
|
8
|
+
import { asNonReactShellComponent, asReactComponent } from './component-ownership.js';
|
|
9
|
+
import { ReactRenderError } from './errors.js';
|
|
10
|
+
export function buildHydrationProps(props) {
|
|
11
|
+
if (!props || !Object.prototype.hasOwnProperty.call(props, 'locals')) {
|
|
12
|
+
return props ?? {};
|
|
13
|
+
}
|
|
14
|
+
const { locals: _locals, ...hydrationProps } = props;
|
|
15
|
+
return hydrationProps;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Renders a non-React layout or HTML template and enforces that mixed shells
|
|
19
|
+
* return serialized HTML.
|
|
20
|
+
*/
|
|
21
|
+
export async function renderNonReactShellComponent(Component, props, label) {
|
|
22
|
+
const output = await Component(props);
|
|
23
|
+
if (typeof output === 'string') {
|
|
24
|
+
return output;
|
|
25
|
+
}
|
|
26
|
+
throw new ReactRenderError(`${label} must return a string when used as a mixed shell for React pages.`);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Renders one React component while preserving already-resolved child HTML.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* When nested foreign-subtree resolution has already produced child HTML, the child
|
|
33
|
+
* payload must remain raw SSR output rather than a React string child, otherwise
|
|
34
|
+
* React would escape it. This helper renders a unique token through React and swaps
|
|
35
|
+
* that token back to the resolved HTML afterward.
|
|
36
|
+
*/
|
|
37
|
+
export function renderComponentHtml(options) {
|
|
38
|
+
const { input, context, runtimeContext, runtime, normalizeUnresolvedMarkerArtifactHtml } = options;
|
|
39
|
+
const { react, reactDomServer } = runtime;
|
|
40
|
+
if (input.children === undefined) {
|
|
41
|
+
return normalizeUnresolvedMarkerArtifactHtml(reactDomServer.renderToString(react.createElement(asReactComponent(input.component), input.props)));
|
|
42
|
+
}
|
|
43
|
+
if (isValidElement(input.children)) {
|
|
44
|
+
return normalizeUnresolvedMarkerArtifactHtml(reactDomServer.renderToString(react.createElement(asReactComponent(input.component), input.props, input.children)));
|
|
45
|
+
}
|
|
46
|
+
const resolvedChildHtml = typeof input.children === 'string' ? input.children : String(input.children ?? '');
|
|
47
|
+
const rawChildrenToken = `__ECO_RAW_HTML_CHILD_${context.componentInstanceId ?? 'component'}__`;
|
|
48
|
+
if (runtimeContext) {
|
|
49
|
+
runtimeContext.rawChildrenToken = rawChildrenToken;
|
|
50
|
+
runtimeContext.rawChildrenHtml = resolvedChildHtml;
|
|
51
|
+
}
|
|
52
|
+
const html = reactDomServer.renderToString(react.createElement(asReactComponent(input.component), input.props, rawChildrenToken));
|
|
53
|
+
return normalizeUnresolvedMarkerArtifactHtml(html.split(rawChildrenToken).join(resolvedChildHtml));
|
|
54
|
+
}
|
|
55
|
+
export function toReactNode(children, integrationName) {
|
|
56
|
+
if (children === null ||
|
|
57
|
+
typeof children === 'string' ||
|
|
58
|
+
typeof children === 'number' ||
|
|
59
|
+
typeof children === 'boolean' ||
|
|
60
|
+
isValidElement(children) ||
|
|
61
|
+
Array.isArray(children)) {
|
|
62
|
+
return children;
|
|
63
|
+
}
|
|
64
|
+
throw new TypeError(`[ecopages] ${integrationName} renderer expected a React node child.`);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Restores raw child HTML placeholders stored on the foreign-subtree runtime context.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* Queued foreign-subtree resolution may render children through a fragment path before
|
|
71
|
+
* nested integration tokens are resolved. React must never see resolved child HTML as a
|
|
72
|
+
* normal string child or it would escape it.
|
|
73
|
+
*/
|
|
74
|
+
export function restoreRuntimeChildHtml(html, runtimeContext) {
|
|
75
|
+
if (!runtimeContext?.rawChildrenToken || runtimeContext.rawChildrenHtml === undefined) {
|
|
76
|
+
return html;
|
|
77
|
+
}
|
|
78
|
+
return html.split(runtimeContext.rawChildrenToken).join(runtimeContext.rawChildrenHtml);
|
|
79
|
+
}
|
|
80
|
+
export async function renderQueuedChildrenToHtml(options) {
|
|
81
|
+
const { children, runtimeContext, queuedResolutionsByToken, resolveToken, runtime, integrationName, normalizeUnresolvedMarkerArtifactHtml, resolveQueuedTokens, } = options;
|
|
82
|
+
if (children === undefined) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
const { react, reactDomServer } = runtime;
|
|
86
|
+
let html = normalizeUnresolvedMarkerArtifactHtml(reactDomServer.renderToString(react.createElement(react.Fragment, null, toReactNode(children, integrationName))));
|
|
87
|
+
html = restoreRuntimeChildHtml(html, runtimeContext);
|
|
88
|
+
return await resolveQueuedTokens(html, queuedResolutionsByToken, resolveToken);
|
|
89
|
+
}
|
|
90
|
+
export async function renderReactQueuedForeignSubtreeChildren(options) {
|
|
91
|
+
const { currentRuntimeContext, ...rest } = options;
|
|
92
|
+
const renderedHtml = await renderQueuedChildrenToHtml({
|
|
93
|
+
...rest,
|
|
94
|
+
runtimeContext: currentRuntimeContext,
|
|
95
|
+
});
|
|
96
|
+
if (renderedHtml === undefined) {
|
|
97
|
+
return { assets: [] };
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
assets: [],
|
|
101
|
+
html: renderedHtml,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
export async function renderForeignComponentWithSerializedHtml(options) {
|
|
105
|
+
const { input, runtimeContext, integrationName, canResolveAssets, processComponentDependencies, dedupeProcessedAssets, getRootTagName, resolveQueuedForeignSubtreeHtml, } = options;
|
|
106
|
+
let props = input.props;
|
|
107
|
+
if (input.children !== undefined) {
|
|
108
|
+
props = {
|
|
109
|
+
...input.props,
|
|
110
|
+
children: typeof input.children === 'string' ? input.children : String(input.children ?? ''),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const html = await renderNonReactShellComponent(asNonReactShellComponent(input.component), props, 'Component');
|
|
114
|
+
const hasDependencies = Boolean(input.component.config?.dependencies);
|
|
115
|
+
const assets = hasDependencies && canResolveAssets ? await processComponentDependencies([input.component]) : undefined;
|
|
116
|
+
const queuedForeignSubtreeResolution = await resolveQueuedForeignSubtreeHtml(html, runtimeContext);
|
|
117
|
+
const mergedAssets = dedupeProcessedAssets([...(assets ?? []), ...queuedForeignSubtreeResolution.assets]);
|
|
118
|
+
return {
|
|
119
|
+
html: queuedForeignSubtreeResolution.html,
|
|
120
|
+
canAttachAttributes: true,
|
|
121
|
+
rootTag: getRootTagName(queuedForeignSubtreeResolution.html),
|
|
122
|
+
integrationName,
|
|
123
|
+
assets: mergedAssets.length > 0 ? mergedAssets : undefined,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
export async function renderReactManagedComponent(options) {
|
|
127
|
+
const { input, runtimeContext, runtime, integrationName, normalizeUnresolvedMarkerArtifactHtml, resolveQueuedForeignSubtreeHtml, getRootTagName, dedupeProcessedAssets, hydrationAssetService, canBuildIslandAssets, } = options;
|
|
128
|
+
const componentConfig = input.component.config;
|
|
129
|
+
const context = {
|
|
130
|
+
componentInstanceId: input.integrationContext?.componentInstanceId,
|
|
131
|
+
};
|
|
132
|
+
const hasResolvedChildHtml = input.children !== undefined;
|
|
133
|
+
let html = renderComponentHtml({
|
|
134
|
+
input,
|
|
135
|
+
context,
|
|
136
|
+
runtimeContext,
|
|
137
|
+
runtime,
|
|
138
|
+
normalizeUnresolvedMarkerArtifactHtml,
|
|
139
|
+
});
|
|
140
|
+
const queuedForeignSubtreeResolution = await resolveQueuedForeignSubtreeHtml(html, runtimeContext);
|
|
141
|
+
html = queuedForeignSubtreeResolution.html;
|
|
142
|
+
const canAttachAttributes = hasSingleRootElement(html);
|
|
143
|
+
const rootTag = getRootTagName(html);
|
|
144
|
+
const componentFile = getComponentIdentity(componentConfig)?.file;
|
|
145
|
+
let rootAttributes;
|
|
146
|
+
let assets;
|
|
147
|
+
if (canAttachAttributes &&
|
|
148
|
+
componentFile &&
|
|
149
|
+
context.componentInstanceId &&
|
|
150
|
+
canBuildIslandAssets &&
|
|
151
|
+
!hasResolvedChildHtml) {
|
|
152
|
+
const componentInstanceId = context.componentInstanceId;
|
|
153
|
+
assets = await hydrationAssetService.buildComponentRenderAssets(componentFile, componentConfig);
|
|
154
|
+
rootAttributes = buildIslandHostAttributes({
|
|
155
|
+
integrationName,
|
|
156
|
+
componentInstanceId,
|
|
157
|
+
componentKey: getIslandComponentKey(componentFile, componentConfig),
|
|
158
|
+
props: buildHydrationProps(input.props),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
const mergedAssets = dedupeProcessedAssets([...(assets ?? []), ...queuedForeignSubtreeResolution.assets]);
|
|
162
|
+
return {
|
|
163
|
+
html,
|
|
164
|
+
canAttachAttributes,
|
|
165
|
+
rootTag,
|
|
166
|
+
integrationName,
|
|
167
|
+
rootAttributes,
|
|
168
|
+
assets: mergedAssets.length > 0 ? mergedAssets : undefined,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
export function createForeignSubtreeRuntimeContext(options) {
|
|
172
|
+
return {
|
|
173
|
+
rendererCache: options.rendererCache,
|
|
174
|
+
componentInstanceScope: options.componentInstanceScope,
|
|
175
|
+
nextForeignSubtreeId: 0,
|
|
176
|
+
queuedResolutions: [],
|
|
177
|
+
rawChildrenToken: undefined,
|
|
178
|
+
rawChildrenHtml: undefined,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** @jsxImportSource react */
|
|
2
|
+
import { eco } from '@ecopages/core';
|
|
3
|
+
/**
|
|
4
|
+
* Renders a foreign or same-integration eco component from a React JSX file
|
|
5
|
+
* without forcing inline mixed-JSX authoring.
|
|
6
|
+
*/
|
|
7
|
+
export function EcoEmbed({ component, props, children, }) {
|
|
8
|
+
return eco.embed(component, props, children);
|
|
9
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Errors thrown by the React integration renderer and bundling paths.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when an error occurs while rendering a React component.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ReactRenderError extends Error {
|
|
8
|
+
constructor(message: string);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Error thrown when an error occurs while bundling a React component.
|
|
12
|
+
*/
|
|
13
|
+
export declare class BundleError extends Error {
|
|
14
|
+
readonly logs: string[];
|
|
15
|
+
constructor(message: string, logs: string[]);
|
|
16
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Errors thrown by the React integration renderer and bundling paths.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when an error occurs while rendering a React component.
|
|
6
|
+
*/
|
|
7
|
+
export class ReactRenderError extends Error {
|
|
8
|
+
constructor(message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'ReactRenderError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Error thrown when an error occurs while bundling a React component.
|
|
15
|
+
*/
|
|
16
|
+
export class BundleError extends Error {
|
|
17
|
+
logs;
|
|
18
|
+
constructor(message, logs) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = 'BundleError';
|
|
21
|
+
this.logs = logs;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const VOID_TAGS = new Set([
|
|
2
|
+
'area',
|
|
3
|
+
'base',
|
|
4
|
+
'br',
|
|
5
|
+
'col',
|
|
6
|
+
'embed',
|
|
7
|
+
'hr',
|
|
8
|
+
'img',
|
|
9
|
+
'input',
|
|
10
|
+
'link',
|
|
11
|
+
'meta',
|
|
12
|
+
'param',
|
|
13
|
+
'source',
|
|
14
|
+
'track',
|
|
15
|
+
'wbr',
|
|
16
|
+
]);
|
|
17
|
+
/**
|
|
18
|
+
* Returns true when HTML contains exactly one root element node.
|
|
19
|
+
*
|
|
20
|
+
* Used by component-level React rendering to decide whether root attributes can
|
|
21
|
+
* be attached safely without introducing synthetic wrapper nodes.
|
|
22
|
+
*/
|
|
23
|
+
export function hasSingleRootElement(html) {
|
|
24
|
+
const firstTagMatch = html.match(/^\s*<([a-zA-Z][a-zA-Z0-9:-]*)\b[^>]*>/);
|
|
25
|
+
if (!firstTagMatch) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
const firstTag = firstTagMatch[1].toLowerCase();
|
|
29
|
+
const firstTagText = firstTagMatch[0];
|
|
30
|
+
const firstTagStart = firstTagMatch.index ?? 0;
|
|
31
|
+
const firstTagEnd = firstTagStart + firstTagText.length;
|
|
32
|
+
const isSelfClosing = /\/\s*>$/.test(firstTagText);
|
|
33
|
+
if (isSelfClosing || VOID_TAGS.has(firstTag)) {
|
|
34
|
+
return html.slice(firstTagEnd).trim().length === 0;
|
|
35
|
+
}
|
|
36
|
+
const tokenRegex = /<\/?([a-zA-Z][a-zA-Z0-9:-]*)\b[^>]*>/g;
|
|
37
|
+
tokenRegex.lastIndex = firstTagEnd;
|
|
38
|
+
let depth = 1;
|
|
39
|
+
for (let token = tokenRegex.exec(html); token; token = tokenRegex.exec(html)) {
|
|
40
|
+
const tagText = token[0];
|
|
41
|
+
const tagName = token[1].toLowerCase();
|
|
42
|
+
const isClosing = tagText.startsWith('</');
|
|
43
|
+
const tokenSelfClosing = /\/\s*>$/.test(tagText);
|
|
44
|
+
if (VOID_TAGS.has(tagName) || tokenSelfClosing) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (isClosing) {
|
|
48
|
+
depth--;
|
|
49
|
+
if (depth === 0) {
|
|
50
|
+
const afterRoot = html.slice(token.index + token[0].length).trim();
|
|
51
|
+
return afterRoot.length === 0;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
depth++;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import * as bundledReact from 'react';
|
|
2
|
+
import type { FunctionComponent, ReactElement } from 'react';
|
|
3
|
+
import type { EcoComponent, EcoDeclaredComponent, EcoPageLayoutEntry, LayoutPropsContext } from '@ecopages/core';
|
|
4
|
+
import type { EcoComponentConfig } from '@ecopages/core';
|
|
5
|
+
export type LayoutComposeContext = LayoutPropsContext;
|
|
6
|
+
export type ReactRuntime = typeof bundledReact;
|
|
7
|
+
export type LayoutComposeOptions = {
|
|
8
|
+
context?: LayoutComposeContext;
|
|
9
|
+
/** @remarks Pass the app-resolved React runtime during SSR so composition matches `renderToString`. */
|
|
10
|
+
react?: ReactRuntime;
|
|
11
|
+
/**
|
|
12
|
+
* Optional resolver that substitutes cached layout instances and React keys
|
|
13
|
+
* for SPA layout persistence.
|
|
14
|
+
*/
|
|
15
|
+
resolvePersistedTier?: (layout: EcoComponent, index: number) => {
|
|
16
|
+
layout: EcoComponent;
|
|
17
|
+
key?: string;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export type LayoutShellEntry = {
|
|
21
|
+
component: EcoComponent;
|
|
22
|
+
props?: Record<string, unknown>;
|
|
23
|
+
};
|
|
24
|
+
export type ComposablePage<P extends Record<string, unknown> = Record<string, unknown>> = FunctionComponent<P> & {
|
|
25
|
+
config?: Pick<EcoComponentConfig, 'layouts' | 'layoutEntries'>;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Reads request-scoped layout context from serialized page props.
|
|
29
|
+
*/
|
|
30
|
+
export declare function resolveLayoutContext(pageProps: Record<string, unknown>): LayoutComposeContext;
|
|
31
|
+
/**
|
|
32
|
+
* Builds layout prop factory context from document-shell layout entries.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* Route-scoped `locals` come from shell layout props, not `pageProps.locals` (pageLocals).
|
|
36
|
+
*/
|
|
37
|
+
export declare function resolveLayoutContextFromShell(pageProps: Record<string, unknown>, shellLayouts: LayoutShellEntry[]): LayoutComposeContext;
|
|
38
|
+
/**
|
|
39
|
+
* Resolves props for one layout entry, merging shell locals with an optional factory.
|
|
40
|
+
*/
|
|
41
|
+
export declare function resolveLayoutEntryProps(entry: EcoPageLayoutEntry, context: LayoutComposeContext): Record<string, unknown>;
|
|
42
|
+
/**
|
|
43
|
+
* @remarks
|
|
44
|
+
* Eco page components are wider than React layout composition input; runtime checks narrow callables.
|
|
45
|
+
*/
|
|
46
|
+
export declare function assertComposablePage(Page: unknown): ComposablePage;
|
|
47
|
+
/**
|
|
48
|
+
* Builds the client or SSR React tree for a page and its outer→inner layout stack.
|
|
49
|
+
*
|
|
50
|
+
* @remarks
|
|
51
|
+
* Layout prop factories receive `LayoutPropsContext`. For SSR, pass `options.react` from the
|
|
52
|
+
* app-resolved runtime. Client code can omit it and use the bundled React import.
|
|
53
|
+
*/
|
|
54
|
+
export declare function composeLayoutPageTree<P extends Record<string, unknown>>(Page: ComposablePage<P>, pageProps: P, options?: LayoutComposeOptions): ReactElement;
|
|
55
|
+
/**
|
|
56
|
+
* Builds a layout tree from explicit document-shell layout entries.
|
|
57
|
+
*
|
|
58
|
+
* @remarks
|
|
59
|
+
* Use when the route renderer passes per-tier props (e.g. route `locals` on the layout tier).
|
|
60
|
+
* Shell `entry.props` are authoritative; page config normalization is not required.
|
|
61
|
+
*/
|
|
62
|
+
export declare function composeLayoutPageTreeFromShell<P extends Record<string, unknown>>(Page: ComposablePage<P>, pageProps: P, shellLayouts: LayoutShellEntry[], options?: LayoutComposeOptions): ReactElement;
|
|
63
|
+
/**
|
|
64
|
+
* Normalizes page config layout metadata to an outer→inner component list.
|
|
65
|
+
*/
|
|
66
|
+
export declare function normalizePageLayoutComponents(layouts?: EcoDeclaredComponent[]): EcoDeclaredComponent[];
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import * as bundledReact from 'react';
|
|
2
|
+
function resolveReact(options) {
|
|
3
|
+
return options?.react ?? bundledReact;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* @remarks
|
|
7
|
+
* Eco components are wider than React's `createElement` input; runtime checks narrow callables.
|
|
8
|
+
*/
|
|
9
|
+
function toReactComponent(component) {
|
|
10
|
+
if (typeof component !== 'function') {
|
|
11
|
+
throw new TypeError('[ecopages] Expected a function component for layout composition.');
|
|
12
|
+
}
|
|
13
|
+
return component;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Reads request-scoped layout context from serialized page props.
|
|
17
|
+
*/
|
|
18
|
+
export function resolveLayoutContext(pageProps) {
|
|
19
|
+
const context = {};
|
|
20
|
+
if (pageProps.params !== undefined) {
|
|
21
|
+
context.params = pageProps.params;
|
|
22
|
+
}
|
|
23
|
+
if (pageProps.query !== undefined) {
|
|
24
|
+
context.query = pageProps.query;
|
|
25
|
+
}
|
|
26
|
+
if (pageProps.locals !== undefined) {
|
|
27
|
+
context.locals = pageProps.locals;
|
|
28
|
+
}
|
|
29
|
+
return context;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Builds layout prop factory context from document-shell layout entries.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* Route-scoped `locals` come from shell layout props, not `pageProps.locals` (pageLocals).
|
|
36
|
+
*/
|
|
37
|
+
export function resolveLayoutContextFromShell(pageProps, shellLayouts) {
|
|
38
|
+
const context = {};
|
|
39
|
+
if (pageProps.params !== undefined) {
|
|
40
|
+
context.params = pageProps.params;
|
|
41
|
+
}
|
|
42
|
+
if (pageProps.query !== undefined) {
|
|
43
|
+
context.query = pageProps.query;
|
|
44
|
+
}
|
|
45
|
+
for (const entry of shellLayouts) {
|
|
46
|
+
if (entry.props !== undefined && Object.prototype.hasOwnProperty.call(entry.props, 'locals')) {
|
|
47
|
+
context.locals = entry.props.locals;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return context;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Resolves props for one layout entry, merging shell locals with an optional factory.
|
|
55
|
+
*/
|
|
56
|
+
export function resolveLayoutEntryProps(entry, context) {
|
|
57
|
+
const shellProps = context.locals ? { locals: context.locals } : {};
|
|
58
|
+
if (!entry.props) {
|
|
59
|
+
return shellProps;
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
...shellProps,
|
|
63
|
+
...entry.props(context),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* @remarks
|
|
68
|
+
* Eco page components are wider than React layout composition input; runtime checks narrow callables.
|
|
69
|
+
*/
|
|
70
|
+
export function assertComposablePage(Page) {
|
|
71
|
+
if (typeof Page !== 'function') {
|
|
72
|
+
throw new TypeError('[ecopages] Expected a function page component for layout composition.');
|
|
73
|
+
}
|
|
74
|
+
return Page;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Builds the client or SSR React tree for a page and its outer→inner layout stack.
|
|
78
|
+
*
|
|
79
|
+
* @remarks
|
|
80
|
+
* Layout prop factories receive `LayoutPropsContext`. For SSR, pass `options.react` from the
|
|
81
|
+
* app-resolved runtime. Client code can omit it and use the bundled React import.
|
|
82
|
+
*/
|
|
83
|
+
export function composeLayoutPageTree(Page, pageProps, options) {
|
|
84
|
+
const { createElement } = resolveReact(options);
|
|
85
|
+
const context = options?.context ?? resolveLayoutContext(pageProps);
|
|
86
|
+
const pageElement = createElement(Page, pageProps);
|
|
87
|
+
const layoutEntries = Page.config?.layoutEntries;
|
|
88
|
+
const resolvePersistedTier = options?.resolvePersistedTier;
|
|
89
|
+
if (layoutEntries && layoutEntries.length > 0) {
|
|
90
|
+
return [...layoutEntries].reverse().reduce((children, entry, reverseIndex) => {
|
|
91
|
+
const index = layoutEntries.length - 1 - reverseIndex;
|
|
92
|
+
const layoutProps = resolveLayoutEntryProps(entry, context);
|
|
93
|
+
const tier = resolvePersistedTier?.(entry.component, index);
|
|
94
|
+
const Layout = toReactComponent(tier?.layout ?? entry.component);
|
|
95
|
+
const elementProps = tier?.key ? { key: tier.key, ...layoutProps } : layoutProps;
|
|
96
|
+
return createElement(Layout, elementProps, children);
|
|
97
|
+
}, pageElement);
|
|
98
|
+
}
|
|
99
|
+
const layouts = Page.config?.layouts;
|
|
100
|
+
if (layouts && layouts.length > 0) {
|
|
101
|
+
const layoutProps = context.locals ? { locals: context.locals } : {};
|
|
102
|
+
return [...layouts].reverse().reduce((children, Layout, reverseIndex) => {
|
|
103
|
+
const index = layouts.length - 1 - reverseIndex;
|
|
104
|
+
const tier = resolvePersistedTier?.(Layout, index);
|
|
105
|
+
const ResolvedLayout = toReactComponent(tier?.layout ?? Layout);
|
|
106
|
+
const elementProps = tier?.key ? { key: tier.key, ...layoutProps } : layoutProps;
|
|
107
|
+
return createElement(ResolvedLayout, elementProps, children);
|
|
108
|
+
}, pageElement);
|
|
109
|
+
}
|
|
110
|
+
return pageElement;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Builds a layout tree from explicit document-shell layout entries.
|
|
114
|
+
*
|
|
115
|
+
* @remarks
|
|
116
|
+
* Use when the route renderer passes per-tier props (e.g. route `locals` on the layout tier).
|
|
117
|
+
* Shell `entry.props` are authoritative; page config normalization is not required.
|
|
118
|
+
*/
|
|
119
|
+
export function composeLayoutPageTreeFromShell(Page, pageProps, shellLayouts, options) {
|
|
120
|
+
const { createElement } = resolveReact(options);
|
|
121
|
+
if (shellLayouts.length === 0) {
|
|
122
|
+
return composeLayoutPageTree(Page, pageProps, options);
|
|
123
|
+
}
|
|
124
|
+
const pageElement = createElement(Page, pageProps);
|
|
125
|
+
return [...shellLayouts].reverse().reduce((children, entry) => {
|
|
126
|
+
return createElement(toReactComponent(entry.component), entry.props ?? {}, children);
|
|
127
|
+
}, pageElement);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Normalizes page config layout metadata to an outer→inner component list.
|
|
131
|
+
*/
|
|
132
|
+
export function normalizePageLayoutComponents(layouts) {
|
|
133
|
+
return layouts && layouts.length > 0 ? layouts : [];
|
|
134
|
+
}
|