@ecopages/react 0.2.0-beta.2 → 0.2.0-beta.21
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 +12 -0
- package/package.json +21 -4
- package/src/layout-compose.d.ts +58 -0
- package/src/layout-compose.js +98 -0
- package/src/react-hmr-strategy.d.ts +16 -0
- package/src/react-hmr-strategy.js +142 -113
- package/src/react-renderer.d.ts +34 -13
- package/src/react-renderer.js +187 -84
- package/src/react.plugin.js +6 -27
- package/src/serialize-page-data-script.d.ts +8 -0
- package/src/serialize-page-data-script.js +10 -0
- package/src/services/react-bundle.service.d.ts +3 -2
- package/src/services/react-bundle.service.js +17 -8
- package/src/services/react-mdx-config-dependency.service.js +5 -7
- package/src/services/react-page-module.service.d.ts +4 -43
- package/src/services/react-page-module.service.js +0 -90
- package/src/services/react-page-payload.service.js +2 -2
- package/src/services/react-runtime-bundle.service.d.ts +5 -1
- package/src/services/react-runtime-bundle.service.js +8 -2
- package/src/utils/client-graph-ast-transform.d.ts +18 -0
- package/src/utils/client-graph-ast-transform.js +335 -0
- package/src/utils/client-graph-boundary-plugin.d.ts +1 -12
- package/src/utils/client-graph-boundary-plugin.js +8 -454
- package/src/utils/client-graph-reachability.d.ts +75 -0
- package/src/utils/client-graph-reachability.js +148 -0
- package/src/utils/component-config-traversal.js +4 -2
- package/src/utils/hydration-scripts.d.ts +2 -0
- package/src/utils/hydration-scripts.js +27 -14
- package/src/utils/react-mdx-loader-plugin.d.ts +2 -3
- package/src/utils/react-mdx-loader-plugin.js +5 -39
package/README.md
CHANGED
|
@@ -206,3 +206,15 @@ The main regression coverage lives in:
|
|
|
206
206
|
- [../../react-router/test/hmr-reload.test.browser.ts](../../react-router/test/hmr-reload.test.browser.ts): verifies router-backed layout hydration receives `locals` with `persistLayouts` both enabled and disabled.
|
|
207
207
|
|
|
208
208
|
If you change the AST transform or hydration flow, update the corresponding tests in the same change.
|
|
209
|
+
|
|
210
|
+
### Nested layouts and unified SSR
|
|
211
|
+
|
|
212
|
+
Pages may declare `layout` as a single component or an **outer → inner** array on `eco.page()`. On the server, React-managed layout stacks compose through `composeDocumentShell` + `composeLayoutPageTree` so provider context reaches nested pages during SSR. On the client, `@ecopages/react-router` `PageContent` uses the same `composeLayoutPageTree` helper unless `persistLayouts` is enabled (then each tier is cached independently).
|
|
213
|
+
|
|
214
|
+
During SSR, `ReactRenderer` passes the app-resolved React runtime into `composeLayoutPageTree` via `options.react` so layout trees are built with the same module instance as `renderToString`. App code should import hooks and context from `react` normally; keep a single React version in the app dependency graph (avoid duplicate `react` copies in monorepos).
|
|
215
|
+
|
|
216
|
+
Layout prop factories receive `LayoutPropsContext` (`params`, `query`, `locals`). Route-scoped `locals` are passed to layout tiers via document-shell props during SSR; serialized `pageProps.locals` follow `Page.requires` and are intended for the page component.
|
|
217
|
+
|
|
218
|
+
- [src/layout-compose.ts](src/layout-compose.ts): shared client/SSR tree builder.
|
|
219
|
+
- [src/test/react-ssr-hydration-parity.test.tsx](src/test/react-ssr-hydration-parity.test.tsx): nested tier order parity between SSR and `composeLayoutPageTree`.
|
|
220
|
+
- [src/test/react-ssr-unified.test.tsx](src/test/react-ssr-unified.test.tsx): provider context through nested SSR layouts.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecopages/react",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.21",
|
|
4
4
|
"description": "React integration for Ecopages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ecopages",
|
|
@@ -39,6 +39,14 @@
|
|
|
39
39
|
"types": "./src/router-adapter.d.ts",
|
|
40
40
|
"default": "./src/router-adapter.js"
|
|
41
41
|
},
|
|
42
|
+
"./layout-compose": {
|
|
43
|
+
"types": "./src/layout-compose.d.ts",
|
|
44
|
+
"default": "./src/layout-compose.js"
|
|
45
|
+
},
|
|
46
|
+
"./serialize-page-data-script": {
|
|
47
|
+
"types": "./src/serialize-page-data-script.d.ts",
|
|
48
|
+
"default": "./src/serialize-page-data-script.js"
|
|
49
|
+
},
|
|
42
50
|
"./eco-embed.ts": {
|
|
43
51
|
"default": "./src/eco-embed.js",
|
|
44
52
|
"types": "./src/eco-embed.d.ts"
|
|
@@ -61,23 +69,32 @@
|
|
|
61
69
|
"./router-adapter.ts": {
|
|
62
70
|
"types": "./src/router-adapter.d.ts",
|
|
63
71
|
"default": "./src/router-adapter.js"
|
|
72
|
+
},
|
|
73
|
+
"./layout-compose.ts": {
|
|
74
|
+
"types": "./src/layout-compose.d.ts",
|
|
75
|
+
"default": "./src/layout-compose.js"
|
|
76
|
+
},
|
|
77
|
+
"./serialize-page-data-script.ts": {
|
|
78
|
+
"types": "./src/serialize-page-data-script.d.ts",
|
|
79
|
+
"default": "./src/serialize-page-data-script.js"
|
|
64
80
|
}
|
|
65
81
|
},
|
|
66
82
|
"repository": {
|
|
67
83
|
"type": "git",
|
|
68
|
-
"url": "https://github.com/ecopages/ecopages.git",
|
|
84
|
+
"url": "git+https://github.com/ecopages/ecopages.git",
|
|
69
85
|
"directory": "packages/integrations/react"
|
|
70
86
|
},
|
|
71
87
|
"peerDependencies": {
|
|
72
|
-
"@ecopages/core": "0.2.0-beta.
|
|
88
|
+
"@ecopages/core": "0.2.0-beta.21",
|
|
73
89
|
"@types/react": "^19",
|
|
74
90
|
"@types/react-dom": "^19",
|
|
75
91
|
"react": "^19",
|
|
76
92
|
"react-dom": "^19"
|
|
77
93
|
},
|
|
78
94
|
"dependencies": {
|
|
79
|
-
"@ecopages/file-system": "0.2.0-beta.
|
|
95
|
+
"@ecopages/file-system": "0.2.0-beta.21",
|
|
80
96
|
"@ecopages/logger": "^0.2.3",
|
|
97
|
+
"@ecopages/mdx": "0.2.0-beta.21",
|
|
81
98
|
"@mdx-js/mdx": "^3.1.1",
|
|
82
99
|
"oxc-parser": "^0.124.0",
|
|
83
100
|
"oxc-transform": "^0.124.0",
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
export type LayoutShellEntry = {
|
|
13
|
+
component: EcoComponent;
|
|
14
|
+
props?: Record<string, unknown>;
|
|
15
|
+
};
|
|
16
|
+
export type ComposablePage<P extends Record<string, unknown> = Record<string, unknown>> = FunctionComponent<P> & {
|
|
17
|
+
config?: Pick<EcoComponentConfig, 'layouts' | 'layoutEntries'>;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Reads request-scoped layout context from serialized page props.
|
|
21
|
+
*/
|
|
22
|
+
export declare function resolveLayoutContext(pageProps: Record<string, unknown>): LayoutComposeContext;
|
|
23
|
+
/**
|
|
24
|
+
* Builds layout prop factory context from document-shell layout entries.
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* Route-scoped `locals` come from shell layout props, not `pageProps.locals` (pageLocals).
|
|
28
|
+
*/
|
|
29
|
+
export declare function resolveLayoutContextFromShell(pageProps: Record<string, unknown>, shellLayouts: LayoutShellEntry[]): LayoutComposeContext;
|
|
30
|
+
/**
|
|
31
|
+
* Resolves props for one layout entry, merging shell locals with an optional factory.
|
|
32
|
+
*/
|
|
33
|
+
export declare function resolveLayoutEntryProps(entry: EcoPageLayoutEntry, context: LayoutComposeContext): Record<string, unknown>;
|
|
34
|
+
/**
|
|
35
|
+
* @remarks
|
|
36
|
+
* Eco page components are wider than React layout composition input; runtime checks narrow callables.
|
|
37
|
+
*/
|
|
38
|
+
export declare function assertComposablePage(Page: unknown): ComposablePage;
|
|
39
|
+
/**
|
|
40
|
+
* Builds the client or SSR React tree for a page and its outer→inner layout stack.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* Layout prop factories receive `LayoutPropsContext`. For SSR, pass `options.react` from the
|
|
44
|
+
* app-resolved runtime. Client code can omit it and use the bundled React import.
|
|
45
|
+
*/
|
|
46
|
+
export declare function composeLayoutPageTree<P extends Record<string, unknown>>(Page: ComposablePage<P>, pageProps: P, options?: LayoutComposeOptions): ReactElement;
|
|
47
|
+
/**
|
|
48
|
+
* Builds a layout tree from explicit document-shell layout entries.
|
|
49
|
+
*
|
|
50
|
+
* @remarks
|
|
51
|
+
* Use when the route renderer passes per-tier props (e.g. route `locals` on the layout tier).
|
|
52
|
+
* Shell `entry.props` are authoritative; page config normalization is not required.
|
|
53
|
+
*/
|
|
54
|
+
export declare function composeLayoutPageTreeFromShell<P extends Record<string, unknown>>(Page: ComposablePage<P>, pageProps: P, shellLayouts: LayoutShellEntry[], options?: LayoutComposeOptions): ReactElement;
|
|
55
|
+
/**
|
|
56
|
+
* Normalizes page config layout metadata to an outer→inner component list.
|
|
57
|
+
*/
|
|
58
|
+
export declare function normalizePageLayoutComponents(layouts?: EcoDeclaredComponent[]): EcoDeclaredComponent[];
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import * as bundledReact from "react";
|
|
2
|
+
function resolveReact(options) {
|
|
3
|
+
return options?.react ?? bundledReact;
|
|
4
|
+
}
|
|
5
|
+
function toReactComponent(component) {
|
|
6
|
+
if (typeof component !== "function") {
|
|
7
|
+
throw new TypeError("[ecopages] Expected a function component for layout composition.");
|
|
8
|
+
}
|
|
9
|
+
return component;
|
|
10
|
+
}
|
|
11
|
+
function resolveLayoutContext(pageProps) {
|
|
12
|
+
const context = {};
|
|
13
|
+
if (pageProps.params !== void 0) {
|
|
14
|
+
context.params = pageProps.params;
|
|
15
|
+
}
|
|
16
|
+
if (pageProps.query !== void 0) {
|
|
17
|
+
context.query = pageProps.query;
|
|
18
|
+
}
|
|
19
|
+
if (pageProps.locals !== void 0) {
|
|
20
|
+
context.locals = pageProps.locals;
|
|
21
|
+
}
|
|
22
|
+
return context;
|
|
23
|
+
}
|
|
24
|
+
function resolveLayoutContextFromShell(pageProps, shellLayouts) {
|
|
25
|
+
const context = {};
|
|
26
|
+
if (pageProps.params !== void 0) {
|
|
27
|
+
context.params = pageProps.params;
|
|
28
|
+
}
|
|
29
|
+
if (pageProps.query !== void 0) {
|
|
30
|
+
context.query = pageProps.query;
|
|
31
|
+
}
|
|
32
|
+
for (const entry of shellLayouts) {
|
|
33
|
+
if (entry.props !== void 0 && Object.prototype.hasOwnProperty.call(entry.props, "locals")) {
|
|
34
|
+
context.locals = entry.props.locals;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return context;
|
|
39
|
+
}
|
|
40
|
+
function resolveLayoutEntryProps(entry, context) {
|
|
41
|
+
const shellProps = context.locals ? { locals: context.locals } : {};
|
|
42
|
+
if (!entry.props) {
|
|
43
|
+
return shellProps;
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
...shellProps,
|
|
47
|
+
...entry.props(context)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function assertComposablePage(Page) {
|
|
51
|
+
if (typeof Page !== "function") {
|
|
52
|
+
throw new TypeError("[ecopages] Expected a function page component for layout composition.");
|
|
53
|
+
}
|
|
54
|
+
return Page;
|
|
55
|
+
}
|
|
56
|
+
function composeLayoutPageTree(Page, pageProps, options) {
|
|
57
|
+
const { createElement } = resolveReact(options);
|
|
58
|
+
const context = options?.context ?? resolveLayoutContext(pageProps);
|
|
59
|
+
const pageElement = createElement(Page, pageProps);
|
|
60
|
+
const layoutEntries = Page.config?.layoutEntries;
|
|
61
|
+
if (layoutEntries && layoutEntries.length > 0) {
|
|
62
|
+
return [...layoutEntries].reverse().reduce((children, entry) => {
|
|
63
|
+
const layoutProps = resolveLayoutEntryProps(entry, context);
|
|
64
|
+
return createElement(toReactComponent(entry.component), layoutProps, children);
|
|
65
|
+
}, pageElement);
|
|
66
|
+
}
|
|
67
|
+
const layouts = Page.config?.layouts;
|
|
68
|
+
if (layouts && layouts.length > 0) {
|
|
69
|
+
const layoutProps = context.locals ? { locals: context.locals } : {};
|
|
70
|
+
return [...layouts].reverse().reduce(
|
|
71
|
+
(children, Layout) => createElement(toReactComponent(Layout), layoutProps, children),
|
|
72
|
+
pageElement
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
return pageElement;
|
|
76
|
+
}
|
|
77
|
+
function composeLayoutPageTreeFromShell(Page, pageProps, shellLayouts, options) {
|
|
78
|
+
const { createElement } = resolveReact(options);
|
|
79
|
+
if (shellLayouts.length === 0) {
|
|
80
|
+
return composeLayoutPageTree(Page, pageProps, options);
|
|
81
|
+
}
|
|
82
|
+
const pageElement = createElement(Page, pageProps);
|
|
83
|
+
return [...shellLayouts].reverse().reduce((children, entry) => {
|
|
84
|
+
return createElement(toReactComponent(entry.component), entry.props ?? {}, children);
|
|
85
|
+
}, pageElement);
|
|
86
|
+
}
|
|
87
|
+
function normalizePageLayoutComponents(layouts) {
|
|
88
|
+
return layouts && layouts.length > 0 ? layouts : [];
|
|
89
|
+
}
|
|
90
|
+
export {
|
|
91
|
+
assertComposablePage,
|
|
92
|
+
composeLayoutPageTree,
|
|
93
|
+
composeLayoutPageTreeFromShell,
|
|
94
|
+
normalizePageLayoutComponents,
|
|
95
|
+
resolveLayoutContext,
|
|
96
|
+
resolveLayoutContextFromShell,
|
|
97
|
+
resolveLayoutEntryProps
|
|
98
|
+
};
|
|
@@ -127,6 +127,8 @@ export declare class ReactHmrStrategy extends HmrStrategy {
|
|
|
127
127
|
* @returns True if this file should trigger React HMR rebuilds
|
|
128
128
|
*/
|
|
129
129
|
matches(filePath: string): boolean;
|
|
130
|
+
canEmitEntrypoint(entrypointPath: string): boolean;
|
|
131
|
+
emitEntrypoint(entrypointPath: string, _outputPath: string): Promise<void>;
|
|
130
132
|
/**
|
|
131
133
|
* Checks if a file is a layout file.
|
|
132
134
|
*
|
|
@@ -171,6 +173,19 @@ export declare class ReactHmrStrategy extends HmrStrategy {
|
|
|
171
173
|
* @returns Action to broadcast update events (layout-update for layouts, update for components)
|
|
172
174
|
*/
|
|
173
175
|
process(_filePath: string): Promise<HmrAction>;
|
|
176
|
+
/**
|
|
177
|
+
* Clears stale HMR output once per outdir before any rebuild pass in `process()`.
|
|
178
|
+
*
|
|
179
|
+
* @remarks
|
|
180
|
+
* Grouped page builds share `getDistDir()`; single page and non-page targets use
|
|
181
|
+
* per-entrypoint temp directories. Clearing here avoids redundant work when both
|
|
182
|
+
* page and non-page targets land in the same outdir.
|
|
183
|
+
*/
|
|
184
|
+
private clearOutdirsForTargets;
|
|
185
|
+
private resolveDeclaredModulesForEntrypoint;
|
|
186
|
+
private collectDeclaredModulesForTargets;
|
|
187
|
+
private buildPluginsForDeclaredModules;
|
|
188
|
+
private recordBuildDependencyGraph;
|
|
174
189
|
/**
|
|
175
190
|
* Bundles a single React/MDX entrypoint with HMR support.
|
|
176
191
|
*
|
|
@@ -195,6 +210,7 @@ export declare class ReactHmrStrategy extends HmrStrategy {
|
|
|
195
210
|
* @returns Array of output URLs that were successfully built
|
|
196
211
|
*/
|
|
197
212
|
private bundleReactEntrypoints;
|
|
213
|
+
private bundleReactBuildTargets;
|
|
198
214
|
private resolveTempOutputPath;
|
|
199
215
|
/**
|
|
200
216
|
* Clears stale HMR output from a directory before a rebuild.
|
|
@@ -75,8 +75,7 @@ class ReactHmrStrategy extends HmrStrategy {
|
|
|
75
75
|
declaredModules,
|
|
76
76
|
cache: this.clientGraphBoundaryCache
|
|
77
77
|
}),
|
|
78
|
-
...runtimeRewritePlugin ? [runtimeRewritePlugin] : []
|
|
79
|
-
...this.context.getPlugins()
|
|
78
|
+
...runtimeRewritePlugin ? [runtimeRewritePlugin] : []
|
|
80
79
|
];
|
|
81
80
|
}
|
|
82
81
|
isReactEntrypoint(filePath) {
|
|
@@ -126,7 +125,7 @@ class ReactHmrStrategy extends HmrStrategy {
|
|
|
126
125
|
}
|
|
127
126
|
pageModuleRequiresLayoutRefresh(pageModule, filePath) {
|
|
128
127
|
return [pageModule.default?.config, pageModule.config].some((config) => {
|
|
129
|
-
return this.configContainsFile(
|
|
128
|
+
return (config?.layouts ?? []).some((layout) => this.configContainsFile(layout?.config, filePath));
|
|
130
129
|
});
|
|
131
130
|
}
|
|
132
131
|
async hasLayoutOwnedDependencyTarget(changedFilePath, requestedTargets) {
|
|
@@ -157,9 +156,6 @@ class ReactHmrStrategy extends HmrStrategy {
|
|
|
157
156
|
matches(filePath) {
|
|
158
157
|
const watchedFiles = this.context.getWatchedFiles();
|
|
159
158
|
appLogger.debug(`Checking ${filePath}. Watched: ${watchedFiles.size}`);
|
|
160
|
-
if (watchedFiles.size === 0) {
|
|
161
|
-
return false;
|
|
162
|
-
}
|
|
163
159
|
if (watchedFiles.has(filePath)) {
|
|
164
160
|
return this.ownsWatchedEntrypoint(filePath);
|
|
165
161
|
}
|
|
@@ -174,6 +170,13 @@ class ReactHmrStrategy extends HmrStrategy {
|
|
|
174
170
|
}
|
|
175
171
|
return this.isReactEntrypoint(filePath);
|
|
176
172
|
}
|
|
173
|
+
canEmitEntrypoint(entrypointPath) {
|
|
174
|
+
return this.isReactEntrypoint(entrypointPath) && this.ownsWatchedEntrypoint(entrypointPath);
|
|
175
|
+
}
|
|
176
|
+
async emitEntrypoint(entrypointPath, _outputPath) {
|
|
177
|
+
const { outputUrl } = this.getEntrypointOutput(entrypointPath);
|
|
178
|
+
await this.bundleReactEntrypoint(entrypointPath, outputUrl);
|
|
179
|
+
}
|
|
177
180
|
/**
|
|
178
181
|
* Checks if a file is a layout file.
|
|
179
182
|
*
|
|
@@ -338,6 +341,7 @@ class ReactHmrStrategy extends HmrStrategy {
|
|
|
338
341
|
hasLayoutOwnedRequestedTarget = await this.hasLayoutOwnedDependencyTarget(_filePath, requestedTargets);
|
|
339
342
|
}
|
|
340
343
|
const requiresLayoutRefresh = isLayout || hasOwnedLayoutDependencyHit || hasLayoutOwnedRequestedTarget;
|
|
344
|
+
await this.clearOutdirsForTargets(pageTargets, nonPageTargets);
|
|
341
345
|
const updates = [];
|
|
342
346
|
const requestedOutputUrls = new Set(requestedTargets.map((target) => target.outputUrl));
|
|
343
347
|
if (pageTargets.length > 1) {
|
|
@@ -392,6 +396,67 @@ class ReactHmrStrategy extends HmrStrategy {
|
|
|
392
396
|
appLogger.debug(`No updates generated`);
|
|
393
397
|
return { type: "none" };
|
|
394
398
|
}
|
|
399
|
+
/**
|
|
400
|
+
* Clears stale HMR output once per outdir before any rebuild pass in `process()`.
|
|
401
|
+
*
|
|
402
|
+
* @remarks
|
|
403
|
+
* Grouped page builds share `getDistDir()`; single page and non-page targets use
|
|
404
|
+
* per-entrypoint temp directories. Clearing here avoids redundant work when both
|
|
405
|
+
* page and non-page targets land in the same outdir.
|
|
406
|
+
*/
|
|
407
|
+
async clearOutdirsForTargets(pageTargets, nonPageTargets) {
|
|
408
|
+
const outdirs = /* @__PURE__ */ new Set();
|
|
409
|
+
if (pageTargets.length > 1) {
|
|
410
|
+
outdirs.add(this.context.getDistDir());
|
|
411
|
+
} else {
|
|
412
|
+
for (const { entrypointPath } of pageTargets) {
|
|
413
|
+
outdirs.add(path.dirname(this.getEntrypointOutput(entrypointPath).outputPath));
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
for (const { entrypointPath } of nonPageTargets) {
|
|
417
|
+
outdirs.add(path.dirname(this.getEntrypointOutput(entrypointPath).outputPath));
|
|
418
|
+
}
|
|
419
|
+
await Promise.all([...outdirs].map((outdir) => this.clearHmrOutdir(outdir)));
|
|
420
|
+
}
|
|
421
|
+
async resolveDeclaredModulesForEntrypoint(entrypointPath) {
|
|
422
|
+
const cached = this.pageMetadataCache.getDeclaredModules(entrypointPath);
|
|
423
|
+
if (cached) {
|
|
424
|
+
return cached;
|
|
425
|
+
}
|
|
426
|
+
const declaredModules = entrypointPath.endsWith(".mdx") ? await collectPageDeclaredModules(entrypointPath) : collectPageDeclaredModulesFromModule(await this.importNodePageModule(entrypointPath));
|
|
427
|
+
this.pageMetadataCache.setDeclaredModules(entrypointPath, declaredModules);
|
|
428
|
+
return declaredModules;
|
|
429
|
+
}
|
|
430
|
+
async collectDeclaredModulesForTargets(targets) {
|
|
431
|
+
const declaredModules = /* @__PURE__ */ new Set();
|
|
432
|
+
let shouldEnableMdx = false;
|
|
433
|
+
for (const { entrypointPath } of targets) {
|
|
434
|
+
const entrypointDeclaredModules = await this.resolveDeclaredModulesForEntrypoint(entrypointPath);
|
|
435
|
+
for (const declaredModule of entrypointDeclaredModules) {
|
|
436
|
+
declaredModules.add(declaredModule);
|
|
437
|
+
}
|
|
438
|
+
if (entrypointPath.endsWith(".mdx")) {
|
|
439
|
+
shouldEnableMdx = true;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return { declaredModules: [...declaredModules], shouldEnableMdx };
|
|
443
|
+
}
|
|
444
|
+
buildPluginsForDeclaredModules(declaredModules, shouldEnableMdx) {
|
|
445
|
+
const plugins = this.getBuildPlugins(declaredModules);
|
|
446
|
+
if (shouldEnableMdx && this.mdxCompilerOptions) {
|
|
447
|
+
plugins.unshift(createReactMdxLoaderPlugin(this.mdxCompilerOptions));
|
|
448
|
+
}
|
|
449
|
+
return plugins;
|
|
450
|
+
}
|
|
451
|
+
recordBuildDependencyGraph(result) {
|
|
452
|
+
if (!result.dependencyGraph?.entrypoints) {
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
const dependencyGraph = this.context.getEntrypointDependencyGraph();
|
|
456
|
+
for (const [entrypoint, deps] of Object.entries(result.dependencyGraph.entrypoints)) {
|
|
457
|
+
dependencyGraph.setEntrypointDependencies(entrypoint, deps);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
395
460
|
/**
|
|
396
461
|
* Bundles a single React/MDX entrypoint with HMR support.
|
|
397
462
|
*
|
|
@@ -405,61 +470,8 @@ class ReactHmrStrategy extends HmrStrategy {
|
|
|
405
470
|
* @returns True if bundling was successful
|
|
406
471
|
*/
|
|
407
472
|
async bundleReactEntrypoint(entrypointPath, outputUrl) {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
const { outputPath } = this.getEntrypointOutput(entrypointPath);
|
|
411
|
-
const tempDir = path.dirname(outputPath);
|
|
412
|
-
const cachedDeclared = this.pageMetadataCache.getDeclaredModules(entrypointPath);
|
|
413
|
-
let entrypointDeclaredModules;
|
|
414
|
-
let declaredModules;
|
|
415
|
-
if (cachedDeclared) {
|
|
416
|
-
entrypointDeclaredModules = cachedDeclared;
|
|
417
|
-
declaredModules = cachedDeclared;
|
|
418
|
-
} else {
|
|
419
|
-
entrypointDeclaredModules = isMdx ? await collectPageDeclaredModules(entrypointPath) : collectPageDeclaredModulesFromModule(await this.importNodePageModule(entrypointPath));
|
|
420
|
-
this.pageMetadataCache.setDeclaredModules(entrypointPath, entrypointDeclaredModules);
|
|
421
|
-
declaredModules = entrypointDeclaredModules;
|
|
422
|
-
}
|
|
423
|
-
const plugins = this.getBuildPlugins(declaredModules);
|
|
424
|
-
if (isMdx && this.mdxCompilerOptions) {
|
|
425
|
-
const mdxPlugin = createReactMdxLoaderPlugin(this.mdxCompilerOptions);
|
|
426
|
-
plugins.unshift(mdxPlugin);
|
|
427
|
-
}
|
|
428
|
-
await this.clearHmrOutdir(tempDir);
|
|
429
|
-
const result = await this.context.getBrowserBundleService().bundle({
|
|
430
|
-
profile: "hmr-entrypoint",
|
|
431
|
-
entrypoints: [entrypointPath],
|
|
432
|
-
outdir: tempDir,
|
|
433
|
-
naming: `[name].[hash].tmp`,
|
|
434
|
-
plugins,
|
|
435
|
-
minify: false
|
|
436
|
-
});
|
|
437
|
-
if (!result.success) {
|
|
438
|
-
appLogger.error(`Failed to build ${entrypointPath}:`, result.logs);
|
|
439
|
-
return false;
|
|
440
|
-
}
|
|
441
|
-
if (result.dependencyGraph?.entrypoints) {
|
|
442
|
-
const dependencyGraph = this.context.getEntrypointDependencyGraph();
|
|
443
|
-
for (const [entrypoint, deps] of Object.entries(result.dependencyGraph.entrypoints)) {
|
|
444
|
-
dependencyGraph.setEntrypointDependencies(entrypoint, deps);
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
const tempFile = result.outputs[0]?.path;
|
|
448
|
-
if (!tempFile) {
|
|
449
|
-
appLogger.error(`No output file generated for ${entrypointPath}`);
|
|
450
|
-
return false;
|
|
451
|
-
}
|
|
452
|
-
const resolvedTempFile = await this.resolveTempOutputPath(tempFile);
|
|
453
|
-
if (!resolvedTempFile) {
|
|
454
|
-
appLogger.debug(`Skipping stale temp output for ${outputUrl}: ${tempFile}`);
|
|
455
|
-
return false;
|
|
456
|
-
}
|
|
457
|
-
const processed = await this.processOutput(resolvedTempFile, outputPath, outputUrl);
|
|
458
|
-
return processed;
|
|
459
|
-
} catch (error) {
|
|
460
|
-
appLogger.error(`Error bundling ${entrypointPath}:`, error);
|
|
461
|
-
return false;
|
|
462
|
-
}
|
|
473
|
+
const rebuiltOutputs = await this.bundleReactBuildTargets([{ entrypointPath, outputUrl }], { grouped: false });
|
|
474
|
+
return rebuiltOutputs.length > 0;
|
|
463
475
|
}
|
|
464
476
|
/**
|
|
465
477
|
* Bundles multiple React/MDX entrypoints in a single build pass.
|
|
@@ -472,73 +484,90 @@ class ReactHmrStrategy extends HmrStrategy {
|
|
|
472
484
|
* @returns Array of output URLs that were successfully built
|
|
473
485
|
*/
|
|
474
486
|
async bundleReactEntrypoints(entrypoints) {
|
|
487
|
+
return this.bundleReactBuildTargets(entrypoints, { grouped: true });
|
|
488
|
+
}
|
|
489
|
+
async bundleReactBuildTargets(targets, options) {
|
|
490
|
+
if (targets.length === 0) {
|
|
491
|
+
return [];
|
|
492
|
+
}
|
|
475
493
|
try {
|
|
476
|
-
const declaredModules
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
const
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
shouldEnableMdx = true;
|
|
494
|
+
const { declaredModules, shouldEnableMdx } = await this.collectDeclaredModulesForTargets(targets);
|
|
495
|
+
const plugins = this.buildPluginsForDeclaredModules(declaredModules, shouldEnableMdx);
|
|
496
|
+
if (options.grouped) {
|
|
497
|
+
const entryNameByPath = /* @__PURE__ */ new Map();
|
|
498
|
+
for (const { entrypointPath: entrypointPath2 } of targets) {
|
|
499
|
+
entryNameByPath.set(entrypointPath2, {
|
|
500
|
+
key: this.getRolldownEntryKey(entrypointPath2),
|
|
501
|
+
basename: this.getTempFileBasename(entrypointPath2)
|
|
502
|
+
});
|
|
486
503
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
basename: this.getTempFileBasename(entrypointPath)
|
|
504
|
+
const result2 = await this.context.getBrowserBundleService().bundle({
|
|
505
|
+
profile: "hmr-entrypoint",
|
|
506
|
+
entrypoints: Object.fromEntries(
|
|
507
|
+
targets.map(({ entrypointPath: entrypointPath2 }) => [entryNameByPath.get(entrypointPath2).key, entrypointPath2])
|
|
508
|
+
),
|
|
509
|
+
outdir: this.context.getDistDir(),
|
|
510
|
+
naming: "[name].[hash].tmp",
|
|
511
|
+
splitting: true,
|
|
512
|
+
plugins,
|
|
513
|
+
minify: false
|
|
498
514
|
});
|
|
515
|
+
if (!result2.success) {
|
|
516
|
+
appLogger.error(`Failed to build grouped React entrypoints:`, result2.logs);
|
|
517
|
+
return [];
|
|
518
|
+
}
|
|
519
|
+
this.recordBuildDependencyGraph(result2);
|
|
520
|
+
const updatedOutputs = [];
|
|
521
|
+
for (const { entrypointPath: entrypointPath2, outputUrl: outputUrl2 } of targets) {
|
|
522
|
+
const { outputPath: outputPath2 } = this.getEntrypointOutput(entrypointPath2);
|
|
523
|
+
const { basename: tempBasename, key: entryKey } = entryNameByPath.get(entrypointPath2);
|
|
524
|
+
const expectedSubdir = path.join(this.context.getDistDir(), path.dirname(entryKey));
|
|
525
|
+
const tempOutput = result2.outputs.find((output) => {
|
|
526
|
+
return path.dirname(output.path) === expectedSubdir && path.basename(output.path).startsWith(`${tempBasename}.`) && path.basename(output.path).includes(".tmp");
|
|
527
|
+
})?.path;
|
|
528
|
+
const resolvedTempOutput = tempOutput ? await this.resolveTempOutputPath(tempOutput) : await this.resolveTempOutputPath(path.join(expectedSubdir, `${tempBasename}.[hash].tmp.js`));
|
|
529
|
+
if (!resolvedTempOutput) {
|
|
530
|
+
appLogger.debug(`Missing grouped temp output for ${outputUrl2}`);
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
const processed2 = await this.processOutput(resolvedTempOutput, outputPath2, outputUrl2);
|
|
534
|
+
if (processed2) {
|
|
535
|
+
updatedOutputs.push(outputUrl2);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return updatedOutputs;
|
|
499
539
|
}
|
|
540
|
+
const { entrypointPath, outputUrl } = targets[0];
|
|
541
|
+
const { outputPath } = this.getEntrypointOutput(entrypointPath);
|
|
542
|
+
const tempDir = path.dirname(outputPath);
|
|
500
543
|
const result = await this.context.getBrowserBundleService().bundle({
|
|
501
544
|
profile: "hmr-entrypoint",
|
|
502
|
-
entrypoints:
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
outdir: this.context.getDistDir(),
|
|
506
|
-
naming: "[name].[hash].tmp",
|
|
507
|
-
splitting: true,
|
|
545
|
+
entrypoints: [entrypointPath],
|
|
546
|
+
outdir: tempDir,
|
|
547
|
+
naming: `[name].[hash].tmp`,
|
|
508
548
|
plugins,
|
|
509
549
|
minify: false
|
|
510
550
|
});
|
|
511
551
|
if (!result.success) {
|
|
512
|
-
appLogger.error(`Failed to build
|
|
552
|
+
appLogger.error(`Failed to build ${entrypointPath}:`, result.logs);
|
|
513
553
|
return [];
|
|
514
554
|
}
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
555
|
+
this.recordBuildDependencyGraph(result);
|
|
556
|
+
const tempFile = result.outputs[0]?.path;
|
|
557
|
+
if (!tempFile) {
|
|
558
|
+
appLogger.error(`No output file generated for ${entrypointPath}`);
|
|
559
|
+
return [];
|
|
520
560
|
}
|
|
521
|
-
const
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
const expectedSubdir = path.join(this.context.getDistDir(), path.dirname(entryKey));
|
|
526
|
-
const tempOutput = result.outputs.find((output) => {
|
|
527
|
-
return path.dirname(output.path) === expectedSubdir && path.basename(output.path).startsWith(`${tempBasename}.`) && path.basename(output.path).includes(".tmp");
|
|
528
|
-
})?.path;
|
|
529
|
-
const resolvedTempOutput = tempOutput ? await this.resolveTempOutputPath(tempOutput) : await this.resolveTempOutputPath(path.join(expectedSubdir, `${tempBasename}.[hash].tmp.js`));
|
|
530
|
-
if (!resolvedTempOutput) {
|
|
531
|
-
appLogger.debug(`Missing grouped temp output for ${outputUrl}`);
|
|
532
|
-
continue;
|
|
533
|
-
}
|
|
534
|
-
const processed = await this.processOutput(resolvedTempOutput, outputPath, outputUrl);
|
|
535
|
-
if (processed) {
|
|
536
|
-
updatedOutputs.push(outputUrl);
|
|
537
|
-
}
|
|
561
|
+
const resolvedTempFile = await this.resolveTempOutputPath(tempFile);
|
|
562
|
+
if (!resolvedTempFile) {
|
|
563
|
+
appLogger.debug(`Skipping stale temp output for ${outputUrl}: ${tempFile}`);
|
|
564
|
+
return [];
|
|
538
565
|
}
|
|
539
|
-
|
|
566
|
+
const processed = await this.processOutput(resolvedTempFile, outputPath, outputUrl);
|
|
567
|
+
return processed ? [outputUrl] : [];
|
|
540
568
|
} catch (error) {
|
|
541
|
-
|
|
569
|
+
const label = options.grouped ? "grouped React entrypoints" : targets[0]?.entrypointPath ?? "React entrypoint";
|
|
570
|
+
appLogger.error(`Error bundling ${label}:`, error);
|
|
542
571
|
return [];
|
|
543
572
|
}
|
|
544
573
|
}
|