@ecopages/react 0.2.0-beta.0 → 0.2.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -4
- package/src/react-hmr-strategy.d.ts +14 -0
- package/src/react-hmr-strategy.js +133 -107
- package/src/react-renderer.d.ts +3 -9
- package/src/react-renderer.js +30 -39
- package/src/react.plugin.js +6 -27
- package/src/services/react-bundle.service.d.ts +3 -2
- package/src/services/react-bundle.service.js +17 -8
- 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-runtime-bundle.service.d.ts +5 -1
- package/src/services/react-runtime-bundle.service.js +8 -2
- package/src/utils/react-mdx-loader-plugin.d.ts +2 -3
- package/src/utils/react-mdx-loader-plugin.js +5 -39
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.10",
|
|
4
4
|
"description": "React integration for Ecopages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ecopages",
|
|
@@ -65,19 +65,20 @@
|
|
|
65
65
|
},
|
|
66
66
|
"repository": {
|
|
67
67
|
"type": "git",
|
|
68
|
-
"url": "https://github.com/ecopages/ecopages.git",
|
|
68
|
+
"url": "git+https://github.com/ecopages/ecopages.git",
|
|
69
69
|
"directory": "packages/integrations/react"
|
|
70
70
|
},
|
|
71
71
|
"peerDependencies": {
|
|
72
|
-
"@ecopages/core": "0.2.0-beta.
|
|
72
|
+
"@ecopages/core": "0.2.0-beta.10",
|
|
73
73
|
"@types/react": "^19",
|
|
74
74
|
"@types/react-dom": "^19",
|
|
75
75
|
"react": "^19",
|
|
76
76
|
"react-dom": "^19"
|
|
77
77
|
},
|
|
78
78
|
"dependencies": {
|
|
79
|
-
"@ecopages/file-system": "0.2.0-beta.
|
|
79
|
+
"@ecopages/file-system": "0.2.0-beta.10",
|
|
80
80
|
"@ecopages/logger": "^0.2.3",
|
|
81
|
+
"@ecopages/mdx-core": "0.2.0-beta.10",
|
|
81
82
|
"@mdx-js/mdx": "^3.1.1",
|
|
82
83
|
"oxc-parser": "^0.124.0",
|
|
83
84
|
"oxc-transform": "^0.124.0",
|
|
@@ -171,6 +171,19 @@ export declare class ReactHmrStrategy extends HmrStrategy {
|
|
|
171
171
|
* @returns Action to broadcast update events (layout-update for layouts, update for components)
|
|
172
172
|
*/
|
|
173
173
|
process(_filePath: string): Promise<HmrAction>;
|
|
174
|
+
/**
|
|
175
|
+
* Clears stale HMR output once per outdir before any rebuild pass in `process()`.
|
|
176
|
+
*
|
|
177
|
+
* @remarks
|
|
178
|
+
* Grouped page builds share `getDistDir()`; single page and non-page targets use
|
|
179
|
+
* per-entrypoint temp directories. Clearing here avoids redundant work when both
|
|
180
|
+
* page and non-page targets land in the same outdir.
|
|
181
|
+
*/
|
|
182
|
+
private clearOutdirsForTargets;
|
|
183
|
+
private resolveDeclaredModulesForEntrypoint;
|
|
184
|
+
private collectDeclaredModulesForTargets;
|
|
185
|
+
private buildPluginsForDeclaredModules;
|
|
186
|
+
private recordBuildDependencyGraph;
|
|
174
187
|
/**
|
|
175
188
|
* Bundles a single React/MDX entrypoint with HMR support.
|
|
176
189
|
*
|
|
@@ -195,6 +208,7 @@ export declare class ReactHmrStrategy extends HmrStrategy {
|
|
|
195
208
|
* @returns Array of output URLs that were successfully built
|
|
196
209
|
*/
|
|
197
210
|
private bundleReactEntrypoints;
|
|
211
|
+
private bundleReactBuildTargets;
|
|
198
212
|
private resolveTempOutputPath;
|
|
199
213
|
/**
|
|
200
214
|
* Clears stale HMR output from a directory before a rebuild.
|
|
@@ -338,6 +338,7 @@ class ReactHmrStrategy extends HmrStrategy {
|
|
|
338
338
|
hasLayoutOwnedRequestedTarget = await this.hasLayoutOwnedDependencyTarget(_filePath, requestedTargets);
|
|
339
339
|
}
|
|
340
340
|
const requiresLayoutRefresh = isLayout || hasOwnedLayoutDependencyHit || hasLayoutOwnedRequestedTarget;
|
|
341
|
+
await this.clearOutdirsForTargets(pageTargets, nonPageTargets);
|
|
341
342
|
const updates = [];
|
|
342
343
|
const requestedOutputUrls = new Set(requestedTargets.map((target) => target.outputUrl));
|
|
343
344
|
if (pageTargets.length > 1) {
|
|
@@ -392,6 +393,67 @@ class ReactHmrStrategy extends HmrStrategy {
|
|
|
392
393
|
appLogger.debug(`No updates generated`);
|
|
393
394
|
return { type: "none" };
|
|
394
395
|
}
|
|
396
|
+
/**
|
|
397
|
+
* Clears stale HMR output once per outdir before any rebuild pass in `process()`.
|
|
398
|
+
*
|
|
399
|
+
* @remarks
|
|
400
|
+
* Grouped page builds share `getDistDir()`; single page and non-page targets use
|
|
401
|
+
* per-entrypoint temp directories. Clearing here avoids redundant work when both
|
|
402
|
+
* page and non-page targets land in the same outdir.
|
|
403
|
+
*/
|
|
404
|
+
async clearOutdirsForTargets(pageTargets, nonPageTargets) {
|
|
405
|
+
const outdirs = /* @__PURE__ */ new Set();
|
|
406
|
+
if (pageTargets.length > 1) {
|
|
407
|
+
outdirs.add(this.context.getDistDir());
|
|
408
|
+
} else {
|
|
409
|
+
for (const { entrypointPath } of pageTargets) {
|
|
410
|
+
outdirs.add(path.dirname(this.getEntrypointOutput(entrypointPath).outputPath));
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
for (const { entrypointPath } of nonPageTargets) {
|
|
414
|
+
outdirs.add(path.dirname(this.getEntrypointOutput(entrypointPath).outputPath));
|
|
415
|
+
}
|
|
416
|
+
await Promise.all([...outdirs].map((outdir) => this.clearHmrOutdir(outdir)));
|
|
417
|
+
}
|
|
418
|
+
async resolveDeclaredModulesForEntrypoint(entrypointPath) {
|
|
419
|
+
const cached = this.pageMetadataCache.getDeclaredModules(entrypointPath);
|
|
420
|
+
if (cached) {
|
|
421
|
+
return cached;
|
|
422
|
+
}
|
|
423
|
+
const declaredModules = entrypointPath.endsWith(".mdx") ? await collectPageDeclaredModules(entrypointPath) : collectPageDeclaredModulesFromModule(await this.importNodePageModule(entrypointPath));
|
|
424
|
+
this.pageMetadataCache.setDeclaredModules(entrypointPath, declaredModules);
|
|
425
|
+
return declaredModules;
|
|
426
|
+
}
|
|
427
|
+
async collectDeclaredModulesForTargets(targets) {
|
|
428
|
+
const declaredModules = /* @__PURE__ */ new Set();
|
|
429
|
+
let shouldEnableMdx = false;
|
|
430
|
+
for (const { entrypointPath } of targets) {
|
|
431
|
+
const entrypointDeclaredModules = await this.resolveDeclaredModulesForEntrypoint(entrypointPath);
|
|
432
|
+
for (const declaredModule of entrypointDeclaredModules) {
|
|
433
|
+
declaredModules.add(declaredModule);
|
|
434
|
+
}
|
|
435
|
+
if (entrypointPath.endsWith(".mdx")) {
|
|
436
|
+
shouldEnableMdx = true;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return { declaredModules: [...declaredModules], shouldEnableMdx };
|
|
440
|
+
}
|
|
441
|
+
buildPluginsForDeclaredModules(declaredModules, shouldEnableMdx) {
|
|
442
|
+
const plugins = this.getBuildPlugins(declaredModules);
|
|
443
|
+
if (shouldEnableMdx && this.mdxCompilerOptions) {
|
|
444
|
+
plugins.unshift(createReactMdxLoaderPlugin(this.mdxCompilerOptions));
|
|
445
|
+
}
|
|
446
|
+
return plugins;
|
|
447
|
+
}
|
|
448
|
+
recordBuildDependencyGraph(result) {
|
|
449
|
+
if (!result.dependencyGraph?.entrypoints) {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
const dependencyGraph = this.context.getEntrypointDependencyGraph();
|
|
453
|
+
for (const [entrypoint, deps] of Object.entries(result.dependencyGraph.entrypoints)) {
|
|
454
|
+
dependencyGraph.setEntrypointDependencies(entrypoint, deps);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
395
457
|
/**
|
|
396
458
|
* Bundles a single React/MDX entrypoint with HMR support.
|
|
397
459
|
*
|
|
@@ -405,61 +467,8 @@ class ReactHmrStrategy extends HmrStrategy {
|
|
|
405
467
|
* @returns True if bundling was successful
|
|
406
468
|
*/
|
|
407
469
|
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
|
-
}
|
|
470
|
+
const rebuiltOutputs = await this.bundleReactBuildTargets([{ entrypointPath, outputUrl }], { grouped: false });
|
|
471
|
+
return rebuiltOutputs.length > 0;
|
|
463
472
|
}
|
|
464
473
|
/**
|
|
465
474
|
* Bundles multiple React/MDX entrypoints in a single build pass.
|
|
@@ -472,73 +481,90 @@ class ReactHmrStrategy extends HmrStrategy {
|
|
|
472
481
|
* @returns Array of output URLs that were successfully built
|
|
473
482
|
*/
|
|
474
483
|
async bundleReactEntrypoints(entrypoints) {
|
|
484
|
+
return this.bundleReactBuildTargets(entrypoints, { grouped: true });
|
|
485
|
+
}
|
|
486
|
+
async bundleReactBuildTargets(targets, options) {
|
|
487
|
+
if (targets.length === 0) {
|
|
488
|
+
return [];
|
|
489
|
+
}
|
|
475
490
|
try {
|
|
476
|
-
const declaredModules
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
const
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
shouldEnableMdx = true;
|
|
491
|
+
const { declaredModules, shouldEnableMdx } = await this.collectDeclaredModulesForTargets(targets);
|
|
492
|
+
const plugins = this.buildPluginsForDeclaredModules(declaredModules, shouldEnableMdx);
|
|
493
|
+
if (options.grouped) {
|
|
494
|
+
const entryNameByPath = /* @__PURE__ */ new Map();
|
|
495
|
+
for (const { entrypointPath: entrypointPath2 } of targets) {
|
|
496
|
+
entryNameByPath.set(entrypointPath2, {
|
|
497
|
+
key: this.getRolldownEntryKey(entrypointPath2),
|
|
498
|
+
basename: this.getTempFileBasename(entrypointPath2)
|
|
499
|
+
});
|
|
486
500
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
basename: this.getTempFileBasename(entrypointPath)
|
|
501
|
+
const result2 = await this.context.getBrowserBundleService().bundle({
|
|
502
|
+
profile: "hmr-entrypoint",
|
|
503
|
+
entrypoints: Object.fromEntries(
|
|
504
|
+
targets.map(({ entrypointPath: entrypointPath2 }) => [entryNameByPath.get(entrypointPath2).key, entrypointPath2])
|
|
505
|
+
),
|
|
506
|
+
outdir: this.context.getDistDir(),
|
|
507
|
+
naming: "[name].[hash].tmp",
|
|
508
|
+
splitting: true,
|
|
509
|
+
plugins,
|
|
510
|
+
minify: false
|
|
498
511
|
});
|
|
512
|
+
if (!result2.success) {
|
|
513
|
+
appLogger.error(`Failed to build grouped React entrypoints:`, result2.logs);
|
|
514
|
+
return [];
|
|
515
|
+
}
|
|
516
|
+
this.recordBuildDependencyGraph(result2);
|
|
517
|
+
const updatedOutputs = [];
|
|
518
|
+
for (const { entrypointPath: entrypointPath2, outputUrl: outputUrl2 } of targets) {
|
|
519
|
+
const { outputPath: outputPath2 } = this.getEntrypointOutput(entrypointPath2);
|
|
520
|
+
const { basename: tempBasename, key: entryKey } = entryNameByPath.get(entrypointPath2);
|
|
521
|
+
const expectedSubdir = path.join(this.context.getDistDir(), path.dirname(entryKey));
|
|
522
|
+
const tempOutput = result2.outputs.find((output) => {
|
|
523
|
+
return path.dirname(output.path) === expectedSubdir && path.basename(output.path).startsWith(`${tempBasename}.`) && path.basename(output.path).includes(".tmp");
|
|
524
|
+
})?.path;
|
|
525
|
+
const resolvedTempOutput = tempOutput ? await this.resolveTempOutputPath(tempOutput) : await this.resolveTempOutputPath(path.join(expectedSubdir, `${tempBasename}.[hash].tmp.js`));
|
|
526
|
+
if (!resolvedTempOutput) {
|
|
527
|
+
appLogger.debug(`Missing grouped temp output for ${outputUrl2}`);
|
|
528
|
+
continue;
|
|
529
|
+
}
|
|
530
|
+
const processed2 = await this.processOutput(resolvedTempOutput, outputPath2, outputUrl2);
|
|
531
|
+
if (processed2) {
|
|
532
|
+
updatedOutputs.push(outputUrl2);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
return updatedOutputs;
|
|
499
536
|
}
|
|
537
|
+
const { entrypointPath, outputUrl } = targets[0];
|
|
538
|
+
const { outputPath } = this.getEntrypointOutput(entrypointPath);
|
|
539
|
+
const tempDir = path.dirname(outputPath);
|
|
500
540
|
const result = await this.context.getBrowserBundleService().bundle({
|
|
501
541
|
profile: "hmr-entrypoint",
|
|
502
|
-
entrypoints:
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
outdir: this.context.getDistDir(),
|
|
506
|
-
naming: "[name].[hash].tmp",
|
|
507
|
-
splitting: true,
|
|
542
|
+
entrypoints: [entrypointPath],
|
|
543
|
+
outdir: tempDir,
|
|
544
|
+
naming: `[name].[hash].tmp`,
|
|
508
545
|
plugins,
|
|
509
546
|
minify: false
|
|
510
547
|
});
|
|
511
548
|
if (!result.success) {
|
|
512
|
-
appLogger.error(`Failed to build
|
|
549
|
+
appLogger.error(`Failed to build ${entrypointPath}:`, result.logs);
|
|
513
550
|
return [];
|
|
514
551
|
}
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
552
|
+
this.recordBuildDependencyGraph(result);
|
|
553
|
+
const tempFile = result.outputs[0]?.path;
|
|
554
|
+
if (!tempFile) {
|
|
555
|
+
appLogger.error(`No output file generated for ${entrypointPath}`);
|
|
556
|
+
return [];
|
|
520
557
|
}
|
|
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
|
-
}
|
|
558
|
+
const resolvedTempFile = await this.resolveTempOutputPath(tempFile);
|
|
559
|
+
if (!resolvedTempFile) {
|
|
560
|
+
appLogger.debug(`Skipping stale temp output for ${outputUrl}: ${tempFile}`);
|
|
561
|
+
return [];
|
|
538
562
|
}
|
|
539
|
-
|
|
563
|
+
const processed = await this.processOutput(resolvedTempFile, outputPath, outputUrl);
|
|
564
|
+
return processed ? [outputUrl] : [];
|
|
540
565
|
} catch (error) {
|
|
541
|
-
|
|
566
|
+
const label = options.grouped ? "grouped React entrypoints" : targets[0]?.entrypointPath ?? "React entrypoint";
|
|
567
|
+
appLogger.error(`Error bundling ${label}:`, error);
|
|
542
568
|
return [];
|
|
543
569
|
}
|
|
544
570
|
}
|
package/src/react-renderer.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @module
|
|
4
4
|
*/
|
|
5
5
|
import type { ComponentRenderInput, ComponentRenderResult, EcoComponent, EcoPageFile, IntegrationRendererRenderOptions, RouteRendererBody } from '@ecopages/core';
|
|
6
|
-
import { IntegrationRenderer, type HtmlDocumentContribution, type HtmlDocumentContributionContext, type PageBrowserGraphContributionContext, type RenderToResponseContext
|
|
6
|
+
import { IntegrationRenderer, type HtmlDocumentContribution, type HtmlDocumentContributionContext, type PageBrowserGraphContributionContext, type RenderToResponseContext } from '@ecopages/core/route-renderer/integration-renderer';
|
|
7
7
|
import type { AssetDefinition, ProcessedAsset } from '@ecopages/core/services/asset-processing-service';
|
|
8
8
|
import type { ReactNode } from 'react';
|
|
9
9
|
import type { ReactRendererConfig } from './react.types.js';
|
|
@@ -149,13 +149,9 @@ export declare class ReactRenderer extends IntegrationRenderer<ReactNode> {
|
|
|
149
149
|
private renderQueuedChildrenToHtml;
|
|
150
150
|
/**
|
|
151
151
|
* Resolves queued renderer-owned foreign-subtree tokens produced during React component rendering.
|
|
152
|
-
*
|
|
153
|
-
* React components can enqueue nested foreign subtrees while the parent HTML is being
|
|
154
|
-
* rendered. This delegates to the shared renderer-owned queue resolver but keeps
|
|
155
|
-
* the React-specific child rendering behavior local so raw child HTML and React's
|
|
156
|
-
* fragment rendering semantics stay coordinated.
|
|
157
152
|
*/
|
|
158
|
-
private
|
|
153
|
+
private renderReactQueuedForeignSubtreeChildren;
|
|
154
|
+
private resolveReactQueuedForeignSubtreeHtml;
|
|
159
155
|
private buildHydrationProps;
|
|
160
156
|
/**
|
|
161
157
|
* Builds shared document html contributions for router-backed React pages rendered
|
|
@@ -203,8 +199,6 @@ export declare class ReactRenderer extends IntegrationRenderer<ReactNode> {
|
|
|
203
199
|
* @returns True if the file is an MDX file
|
|
204
200
|
*/
|
|
205
201
|
isMdxFile(filePath: string): boolean;
|
|
206
|
-
protected usesIntegrationPageImporter(file: string): boolean;
|
|
207
|
-
protected importIntegrationPageFile(file: string, options?: RouteModuleLoadOptions): Promise<EcoPageFile>;
|
|
208
202
|
protected normalizeImportedPageFile<TPageModule extends EcoPageFile>(file: string, pageModule: TPageModule): TPageModule;
|
|
209
203
|
protected collectPageBrowserGraphContribution(context: PageBrowserGraphContributionContext): Promise<{
|
|
210
204
|
dependencies?: AssetDefinition[];
|
package/src/react-renderer.js
CHANGED
|
@@ -2,7 +2,6 @@ import {
|
|
|
2
2
|
IntegrationRenderer
|
|
3
3
|
} from "@ecopages/core/route-renderer/integration-renderer";
|
|
4
4
|
import { RESOLVED_ASSETS_DIR } from "@ecopages/core/constants";
|
|
5
|
-
import { getAppBuildExecutor } from "@ecopages/core/build/build-adapter";
|
|
6
5
|
import { ECO_DOCUMENT_OWNER_ATTRIBUTE } from "@ecopages/core/router/navigation-coordinator";
|
|
7
6
|
import { createRequire } from "node:module";
|
|
8
7
|
import path from "node:path";
|
|
@@ -63,19 +62,17 @@ class ReactRenderer extends IntegrationRenderer {
|
|
|
63
62
|
this.explicitGraphEnabled = reactConfig?.explicitGraphEnabled ?? false;
|
|
64
63
|
this.bundleService = new ReactBundleService({
|
|
65
64
|
rootDir: this.appConfig.rootDir,
|
|
65
|
+
appConfig: this.appConfig,
|
|
66
|
+
hostIntegrationName: this.name,
|
|
66
67
|
routerAdapter: this.routerAdapter,
|
|
67
|
-
mdxCompilerOptions: this.mdxCompilerOptions
|
|
68
|
-
jsxImportSource: (this.appConfig.integrations ?? []).find((integration) => integration.name === this.name)?.jsxImportSource,
|
|
69
|
-
nonReactExtensions: (this.appConfig.integrations ?? []).filter((integration) => integration.name !== this.name).flatMap((integration) => integration.extensions)
|
|
68
|
+
mdxCompilerOptions: this.mdxCompilerOptions
|
|
70
69
|
});
|
|
71
70
|
this.pageModuleService = new ReactPageModuleService({
|
|
72
71
|
rootDir: this.appConfig.rootDir,
|
|
73
72
|
distDir: this.appConfig.absolutePaths.distDir,
|
|
74
73
|
workDir: this.appConfig.absolutePaths.workDir,
|
|
75
|
-
buildExecutor: getAppBuildExecutor(this.appConfig),
|
|
76
74
|
layoutsDir: this.appConfig.absolutePaths.layoutsDir,
|
|
77
75
|
componentsDir: this.appConfig.absolutePaths.componentsDir,
|
|
78
|
-
mdxCompilerOptions: this.mdxCompilerOptions,
|
|
79
76
|
mdxExtensions: this.mdxExtensions,
|
|
80
77
|
integrationName: this.name,
|
|
81
78
|
hasRouterAdapter: Boolean(this.routerAdapter)
|
|
@@ -267,34 +264,34 @@ class ReactRenderer extends IntegrationRenderer {
|
|
|
267
264
|
}
|
|
268
265
|
/**
|
|
269
266
|
* Resolves queued renderer-owned foreign-subtree tokens produced during React component rendering.
|
|
270
|
-
*
|
|
271
|
-
* React components can enqueue nested foreign subtrees while the parent HTML is being
|
|
272
|
-
* rendered. This delegates to the shared renderer-owned queue resolver but keeps
|
|
273
|
-
* the React-specific child rendering behavior local so raw child HTML and React's
|
|
274
|
-
* fragment rendering semantics stay coordinated.
|
|
275
267
|
*/
|
|
276
|
-
async
|
|
277
|
-
|
|
278
|
-
|
|
268
|
+
async renderReactQueuedForeignSubtreeChildren(children, currentRuntimeContext, queuedResolutionsByToken, resolveToken) {
|
|
269
|
+
const renderedHtml = await this.renderQueuedChildrenToHtml(
|
|
270
|
+
children,
|
|
271
|
+
currentRuntimeContext,
|
|
272
|
+
queuedResolutionsByToken,
|
|
273
|
+
resolveToken
|
|
274
|
+
);
|
|
275
|
+
if (renderedHtml === void 0) {
|
|
276
|
+
return { assets: [] };
|
|
277
|
+
}
|
|
278
|
+
return {
|
|
279
|
+
assets: [],
|
|
280
|
+
html: renderedHtml
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
resolveReactQueuedForeignSubtreeHtml(html, runtimeContext) {
|
|
284
|
+
return this.resolveQueuedForeignSubtreeHtml(
|
|
279
285
|
html,
|
|
280
286
|
runtimeContext,
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
queuedResolutionsByToken,
|
|
290
|
-
resolveToken
|
|
291
|
-
);
|
|
292
|
-
return {
|
|
293
|
-
assets: [],
|
|
294
|
-
html: renderedHtml
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
});
|
|
287
|
+
(children, currentRuntimeContext, queuedResolutionsByToken, resolveToken) => this.renderReactQueuedForeignSubtreeChildren(
|
|
288
|
+
children,
|
|
289
|
+
currentRuntimeContext,
|
|
290
|
+
queuedResolutionsByToken,
|
|
291
|
+
resolveToken
|
|
292
|
+
),
|
|
293
|
+
"React"
|
|
294
|
+
);
|
|
298
295
|
}
|
|
299
296
|
buildHydrationProps(props) {
|
|
300
297
|
if (!props || !Object.prototype.hasOwnProperty.call(props, "locals")) {
|
|
@@ -341,7 +338,7 @@ class ReactRenderer extends IntegrationRenderer {
|
|
|
341
338
|
const hasDependencies = Boolean(input.component.config?.dependencies);
|
|
342
339
|
const canResolveAssets = typeof this.assetProcessingService?.processDependencies === "function";
|
|
343
340
|
const assets = hasDependencies && canResolveAssets ? await this.processComponentDependencies([input.component]) : void 0;
|
|
344
|
-
const queuedForeignSubtreeResolution = await this.
|
|
341
|
+
const queuedForeignSubtreeResolution = await this.resolveReactQueuedForeignSubtreeHtml(html, runtimeContext);
|
|
345
342
|
const mergedAssets = this.htmlTransformer.dedupeProcessedAssets([
|
|
346
343
|
...assets ?? [],
|
|
347
344
|
...queuedForeignSubtreeResolution.assets
|
|
@@ -368,7 +365,7 @@ class ReactRenderer extends IntegrationRenderer {
|
|
|
368
365
|
};
|
|
369
366
|
const hasResolvedChildHtml = input.children !== void 0;
|
|
370
367
|
let html = this.renderComponentHtml(input, context, runtimeContext);
|
|
371
|
-
const queuedForeignSubtreeResolution = await this.
|
|
368
|
+
const queuedForeignSubtreeResolution = await this.resolveReactQueuedForeignSubtreeHtml(html, runtimeContext);
|
|
372
369
|
html = queuedForeignSubtreeResolution.html;
|
|
373
370
|
const canAttachAttributes = hasSingleRootElement(html);
|
|
374
371
|
const rootTag = this.getRootTagName(html);
|
|
@@ -440,12 +437,6 @@ class ReactRenderer extends IntegrationRenderer {
|
|
|
440
437
|
isMdxFile(filePath) {
|
|
441
438
|
return this.pageModuleService.isMdxFile(filePath);
|
|
442
439
|
}
|
|
443
|
-
usesIntegrationPageImporter(file) {
|
|
444
|
-
return this.pageModuleService.isMdxFile(file);
|
|
445
|
-
}
|
|
446
|
-
async importIntegrationPageFile(file, options) {
|
|
447
|
-
return await this.pageModuleService.importMdxPageFile(file, options);
|
|
448
|
-
}
|
|
449
440
|
normalizeImportedPageFile(file, pageModule) {
|
|
450
441
|
const reactModule = pageModule;
|
|
451
442
|
const { default: Page, getMetadata, config } = reactModule;
|
package/src/react.plugin.js
CHANGED
|
@@ -2,42 +2,18 @@ import {
|
|
|
2
2
|
IntegrationPlugin
|
|
3
3
|
} from "@ecopages/core/plugins/integration-plugin";
|
|
4
4
|
import { Logger } from "@ecopages/logger";
|
|
5
|
+
import path from "node:path";
|
|
5
6
|
import { REACT_PLUGIN_NAME } from "./react.constants.js";
|
|
6
7
|
import { ReactRenderer } from "./react-renderer.js";
|
|
7
8
|
import { ReactHmrStrategy } from "./react-hmr-strategy.js";
|
|
8
9
|
import { ReactRuntimeBundleService } from "./services/react-runtime-bundle.service.js";
|
|
9
10
|
import { ReactHmrPageMetadataCache } from "./services/react-hmr-page-metadata-cache.js";
|
|
10
11
|
import { createReactMdxLoaderPlugin } from "./utils/react-mdx-loader-plugin.js";
|
|
12
|
+
import { appendMdxExtensions, resolveMdxCompilerOptions } from "@ecopages/mdx-core";
|
|
11
13
|
import { ClientGraphBoundaryCache } from "./utils/client-graph-boundary-cache.js";
|
|
12
14
|
const appLogger = new Logger("[ReactPlugin]");
|
|
13
15
|
const PLUGIN_NAME = REACT_PLUGIN_NAME;
|
|
14
|
-
const
|
|
15
|
-
const merged = lists.flatMap((list) => list ? [...list] : []);
|
|
16
|
-
return merged.length > 0 ? merged : void 0;
|
|
17
|
-
};
|
|
18
|
-
const appendMdxExtensions = (target, mdxExtensions) => {
|
|
19
|
-
for (const extension of mdxExtensions) {
|
|
20
|
-
if (!target.includes(extension)) {
|
|
21
|
-
target.push(extension);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
const resolveReactMdxCompilerOptions = (mdxOptions) => {
|
|
26
|
-
const { compilerOptions, remarkPlugins, rehypePlugins, recmaPlugins } = mdxOptions;
|
|
27
|
-
const resolved = {
|
|
28
|
-
...compilerOptions,
|
|
29
|
-
jsxImportSource: "react",
|
|
30
|
-
jsxRuntime: "automatic",
|
|
31
|
-
development: process.env.NODE_ENV === "development"
|
|
32
|
-
};
|
|
33
|
-
const mergedRemark = mergePluginLists(compilerOptions?.remarkPlugins, remarkPlugins);
|
|
34
|
-
const mergedRehype = mergePluginLists(compilerOptions?.rehypePlugins, rehypePlugins);
|
|
35
|
-
const mergedRecma = mergePluginLists(compilerOptions?.recmaPlugins, recmaPlugins);
|
|
36
|
-
if (mergedRemark) resolved.remarkPlugins = mergedRemark;
|
|
37
|
-
if (mergedRehype) resolved.rehypePlugins = mergedRehype;
|
|
38
|
-
if (mergedRecma) resolved.recmaPlugins = mergedRecma;
|
|
39
|
-
return resolved;
|
|
40
|
-
};
|
|
16
|
+
const resolveReactMdxCompilerOptions = (mdxOptions) => resolveMdxCompilerOptions(mdxOptions, { jsxImportSource: "react" });
|
|
41
17
|
const resolveReactPluginOptions = (options) => {
|
|
42
18
|
const { extensions: userExtensions, router, mdx, explicitGraph, dependencies, ...baseConfig } = options ?? {};
|
|
43
19
|
const extensions = [...userExtensions ?? [".tsx"]];
|
|
@@ -129,6 +105,9 @@ class ReactPlugin extends IntegrationPlugin {
|
|
|
129
105
|
return;
|
|
130
106
|
}
|
|
131
107
|
this.runtimeBundleService.setRootDir(this.appConfig?.rootDir);
|
|
108
|
+
this.runtimeBundleService.setWorkDir(
|
|
109
|
+
this.appConfig?.absolutePaths?.workDir ?? (this.appConfig?.rootDir ? path.join(this.appConfig.rootDir, this.appConfig.workDir ?? ".eco") : void 0)
|
|
110
|
+
);
|
|
132
111
|
this.integrationDependencies.unshift(...this.runtimeBundleService.getDependencies());
|
|
133
112
|
this.runtimeDependenciesInitialized = true;
|
|
134
113
|
}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
* @module
|
|
8
8
|
*/
|
|
9
|
+
import type { EcoPagesAppConfig } from '@ecopages/core';
|
|
9
10
|
import type { ReactRouterAdapter } from '../router-adapter.js';
|
|
10
11
|
import type { CompileOptions } from '@mdx-js/mdx';
|
|
11
12
|
import { type ReactRuntimeImports } from './react-runtime-bundle.service.js';
|
|
@@ -14,10 +15,10 @@ import { type ReactRuntimeImports } from './react-runtime-bundle.service.js';
|
|
|
14
15
|
*/
|
|
15
16
|
export interface ReactBundleServiceConfig {
|
|
16
17
|
rootDir: string;
|
|
18
|
+
appConfig: EcoPagesAppConfig;
|
|
19
|
+
hostIntegrationName: string;
|
|
17
20
|
routerAdapter?: ReactRouterAdapter;
|
|
18
21
|
mdxCompilerOptions?: CompileOptions;
|
|
19
|
-
nonReactExtensions?: string[];
|
|
20
|
-
jsxImportSource?: string;
|
|
21
22
|
}
|
|
22
23
|
/**
|
|
23
24
|
* Optional flags that adjust how a React client entry is bundled.
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
getReactRuntimeExternalSpecifiers
|
|
5
5
|
} from "../utils/react-runtime-alias-map.js";
|
|
6
6
|
import { createBrowserRuntimePlugin } from "@ecopages/core/build/browser-runtime-plugin";
|
|
7
|
-
import {
|
|
7
|
+
import { getHostScopedJsxOwnershipPlugins } from "@ecopages/core/build/jsx-ownership-plugins";
|
|
8
8
|
import { ReactRuntimeBundleService } from "./react-runtime-bundle.service.js";
|
|
9
9
|
import { createReactMdxLoaderPlugin } from "../utils/react-mdx-loader-plugin.js";
|
|
10
10
|
class ReactBundleService {
|
|
@@ -61,11 +61,11 @@ class ReactBundleService {
|
|
|
61
61
|
declaredModules,
|
|
62
62
|
alwaysAllowSpecifiers: getReactClientGraphAllowSpecifiers([], this.config.routerAdapter)
|
|
63
63
|
});
|
|
64
|
-
const foreignJsxOverridePlugin =
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
const [foreignJsxOverridePlugin] = getHostScopedJsxOwnershipPlugins(
|
|
65
|
+
this.config.appConfig,
|
|
66
|
+
this.config.hostIntegrationName,
|
|
67
|
+
{ name: "react-renderer-foreign-jsx-override" }
|
|
68
|
+
);
|
|
69
69
|
const runtimeManifest = this.runtimeBundleService.getRuntimeManifest();
|
|
70
70
|
const runtimeRewritePlugin = createBrowserRuntimePlugin({
|
|
71
71
|
name: "react-renderer-runtime-import-rewrite",
|
|
@@ -74,9 +74,18 @@ class ReactBundleService {
|
|
|
74
74
|
const runtimePlugins = bundleOptions.includeRuntime ? [] : [runtimeRewritePlugin].filter((plugin) => plugin !== null);
|
|
75
75
|
if (isMdx && this.config.mdxCompilerOptions) {
|
|
76
76
|
const mdxPlugin = createReactMdxLoaderPlugin(this.config.mdxCompilerOptions);
|
|
77
|
-
options.plugins = [
|
|
77
|
+
options.plugins = [
|
|
78
|
+
...foreignJsxOverridePlugin ? [foreignJsxOverridePlugin] : [],
|
|
79
|
+
graphBoundaryPlugin,
|
|
80
|
+
...runtimePlugins,
|
|
81
|
+
mdxPlugin
|
|
82
|
+
];
|
|
78
83
|
} else {
|
|
79
|
-
options.plugins = [
|
|
84
|
+
options.plugins = [
|
|
85
|
+
...foreignJsxOverridePlugin ? [foreignJsxOverridePlugin] : [],
|
|
86
|
+
graphBoundaryPlugin,
|
|
87
|
+
...runtimePlugins
|
|
88
|
+
];
|
|
80
89
|
}
|
|
81
90
|
return options;
|
|
82
91
|
}
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Page module loading and configuration resolution service for React integration.
|
|
3
3
|
*
|
|
4
|
-
* Handles
|
|
5
|
-
*
|
|
4
|
+
* Handles component config metadata resolution and module hydration analysis.
|
|
5
|
+
* MDX page modules load through the core {@link PageModuleImportService} path
|
|
6
|
+
* using the React plugin's server build contributions.
|
|
6
7
|
*
|
|
7
8
|
* @module
|
|
8
9
|
*/
|
|
9
10
|
import type { EcoComponentConfig, EcoPageFile } from '@ecopages/core';
|
|
10
|
-
import { type BuildExecutor } from '@ecopages/core/build/build-adapter';
|
|
11
|
-
import type { CompileOptions } from '@mdx-js/mdx';
|
|
12
11
|
/**
|
|
13
12
|
* Configuration for the ReactPageModuleService.
|
|
14
13
|
*/
|
|
@@ -16,70 +15,32 @@ export interface ReactPageModuleServiceConfig {
|
|
|
16
15
|
rootDir: string;
|
|
17
16
|
distDir: string;
|
|
18
17
|
workDir: string;
|
|
19
|
-
buildExecutor: BuildExecutor;
|
|
20
18
|
layoutsDir?: string;
|
|
21
19
|
componentsDir?: string;
|
|
22
|
-
mdxCompilerOptions?: CompileOptions;
|
|
23
20
|
mdxExtensions: string[];
|
|
24
21
|
integrationName: string;
|
|
25
22
|
hasRouterAdapter: boolean;
|
|
26
23
|
}
|
|
27
24
|
/**
|
|
28
|
-
* Manages page module
|
|
29
|
-
* resolution, and hydration analysis for React pages.
|
|
25
|
+
* Manages page module metadata resolution and hydration analysis for React pages.
|
|
30
26
|
*/
|
|
31
27
|
export declare class ReactPageModuleService {
|
|
32
28
|
private readonly config;
|
|
33
29
|
constructor(config: ReactPageModuleServiceConfig);
|
|
34
30
|
/**
|
|
35
31
|
* Checks if the given file path corresponds to an MDX file based on configured extensions.
|
|
36
|
-
* @param filePath - The file path to check
|
|
37
|
-
* @returns True if the file is an MDX file
|
|
38
32
|
*/
|
|
39
33
|
isMdxFile(filePath: string): boolean;
|
|
40
|
-
/**
|
|
41
|
-
* Compiles and imports an MDX file as a page module.
|
|
42
|
-
*
|
|
43
|
-
* @param filePath - Absolute path to the MDX file
|
|
44
|
-
* @returns The imported module
|
|
45
|
-
*/
|
|
46
|
-
importMdxPageFile(filePath: string, options?: {
|
|
47
|
-
bypassCache?: boolean;
|
|
48
|
-
cacheScope?: string;
|
|
49
|
-
}): Promise<EcoPageFile<{
|
|
50
|
-
config?: EcoComponentConfig;
|
|
51
|
-
}>>;
|
|
52
34
|
/**
|
|
53
35
|
* Ensures that an EcoComponentConfig has proper `__eco` metadata attached.
|
|
54
|
-
* Resolves the file path from dependency declarations when not already set.
|
|
55
|
-
*
|
|
56
|
-
* @param config - The component config to augment
|
|
57
|
-
* @param pagePath - Fallback file path if dependency resolution fails
|
|
58
|
-
* @returns Config with `__eco` metadata populated
|
|
59
36
|
*/
|
|
60
37
|
ensureConfigFileMetadata(config: EcoComponentConfig, pagePath: string): EcoComponentConfig;
|
|
61
|
-
/**
|
|
62
|
-
* Recursively checks whether a component config tree declares any browser modules.
|
|
63
|
-
* Used to determine if a page needs hydration.
|
|
64
|
-
*/
|
|
65
38
|
hasModulesInConfig(config: EcoComponentConfig | undefined): boolean;
|
|
66
|
-
/**
|
|
67
|
-
* Determines whether a page needs client-side hydration.
|
|
68
|
-
*
|
|
69
|
-
* @param pageModule - The imported page module
|
|
70
|
-
* @returns True if the page should be hydrated
|
|
71
|
-
*/
|
|
72
39
|
shouldHydratePage(pageModule: EcoPageFile<{
|
|
73
40
|
config?: EcoComponentConfig;
|
|
74
41
|
}> & {
|
|
75
42
|
config?: EcoComponentConfig;
|
|
76
43
|
}): boolean;
|
|
77
|
-
/**
|
|
78
|
-
* Collects all explicitly declared browser module specifiers from a page module.
|
|
79
|
-
*
|
|
80
|
-
* @param pageModule - The imported page module
|
|
81
|
-
* @returns Deduplicated list of declared module specifiers
|
|
82
|
-
*/
|
|
83
44
|
collectPageDeclaredModules(pageModule: EcoPageFile<{
|
|
84
45
|
config?: EcoComponentConfig;
|
|
85
46
|
}> & {
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
-
import { pathToFileURL } from "node:url";
|
|
3
|
-
import { build } from "@ecopages/core/build/build-adapter";
|
|
4
|
-
import { normalizeNodeRuntimeBuildOutputFile } from "@ecopages/core/build/runtime-build-output-normalizer";
|
|
5
2
|
import { rapidhash } from "@ecopages/core/hash";
|
|
6
3
|
import { fileSystem } from "@ecopages/file-system";
|
|
7
4
|
import { someInConfigTree } from "../utils/component-config-traversal.js";
|
|
8
5
|
import { collectDeclaredModulesInConfig } from "../utils/declared-modules.js";
|
|
9
|
-
import { createReactMdxLoaderPlugin } from "../utils/react-mdx-loader-plugin.js";
|
|
10
6
|
class ReactPageModuleService {
|
|
11
7
|
config;
|
|
12
8
|
constructor(config) {
|
|
@@ -14,79 +10,12 @@ class ReactPageModuleService {
|
|
|
14
10
|
}
|
|
15
11
|
/**
|
|
16
12
|
* Checks if the given file path corresponds to an MDX file based on configured extensions.
|
|
17
|
-
* @param filePath - The file path to check
|
|
18
|
-
* @returns True if the file is an MDX file
|
|
19
13
|
*/
|
|
20
14
|
isMdxFile(filePath) {
|
|
21
15
|
return this.config.mdxExtensions.some((ext) => filePath.endsWith(ext));
|
|
22
16
|
}
|
|
23
|
-
/**
|
|
24
|
-
* Compiles and imports an MDX file as a page module.
|
|
25
|
-
*
|
|
26
|
-
* @param filePath - Absolute path to the MDX file
|
|
27
|
-
* @returns The imported module
|
|
28
|
-
*/
|
|
29
|
-
async importMdxPageFile(filePath, options) {
|
|
30
|
-
const mdxPlugin = createReactMdxLoaderPlugin(
|
|
31
|
-
this.config.mdxCompilerOptions ?? {
|
|
32
|
-
jsxImportSource: "react",
|
|
33
|
-
jsxRuntime: "automatic",
|
|
34
|
-
development: process?.env?.NODE_ENV === "development"
|
|
35
|
-
}
|
|
36
|
-
);
|
|
37
|
-
const outdir = path.join(this.config.workDir, ".server-modules-react-mdx");
|
|
38
|
-
const fileBaseName = path.basename(filePath, path.extname(filePath));
|
|
39
|
-
const fileHash = fileSystem.hash(filePath);
|
|
40
|
-
const cacheScopeSuffix = options?.cacheScope ? `-${sanitizeCacheScope(options.cacheScope)}` : "";
|
|
41
|
-
const cacheBuster = options?.bypassCache || process?.env?.NODE_ENV === "development" ? `-${Date.now()}` : "";
|
|
42
|
-
const outputFileName = `${fileBaseName}-${fileHash}${cacheScopeSuffix}${cacheBuster}.mjs`;
|
|
43
|
-
const outputNamingTemplate = `${fileBaseName}-${fileHash}${cacheScopeSuffix}${cacheBuster}.[ext]`;
|
|
44
|
-
const buildResult = await build(
|
|
45
|
-
{
|
|
46
|
-
entrypoints: [filePath],
|
|
47
|
-
root: this.config.rootDir,
|
|
48
|
-
outdir,
|
|
49
|
-
target: "es2022",
|
|
50
|
-
format: "esm",
|
|
51
|
-
sourcemap: "none",
|
|
52
|
-
splitting: false,
|
|
53
|
-
minify: false,
|
|
54
|
-
treeshaking: false,
|
|
55
|
-
externalPackages: true,
|
|
56
|
-
naming: outputNamingTemplate,
|
|
57
|
-
plugins: [mdxPlugin]
|
|
58
|
-
},
|
|
59
|
-
this.config.buildExecutor
|
|
60
|
-
);
|
|
61
|
-
if (!buildResult.success) {
|
|
62
|
-
const details = buildResult.logs.map((log) => log.message).join(" | ");
|
|
63
|
-
throw new Error(`Failed to compile MDX page module: ${details}`);
|
|
64
|
-
}
|
|
65
|
-
const preferredOutputPath = path.join(outdir, outputFileName);
|
|
66
|
-
const compiledOutput = buildResult.outputs.find((output) => output.path === preferredOutputPath)?.path ?? buildResult.outputs.find((output) => /\.(?:[cm]?js)$/u.test(output.path))?.path;
|
|
67
|
-
if (!compiledOutput) {
|
|
68
|
-
throw new Error(`No compiled MDX output generated for page: ${filePath}`);
|
|
69
|
-
}
|
|
70
|
-
normalizeNodeRuntimeBuildOutputFile(compiledOutput, this.config.rootDir);
|
|
71
|
-
const compiledOutputUrl = pathToFileURL(compiledOutput);
|
|
72
|
-
if (process?.env?.NODE_ENV === "development" || options?.cacheScope) {
|
|
73
|
-
compiledOutputUrl.searchParams.set(
|
|
74
|
-
"update",
|
|
75
|
-
[fileHash, options?.cacheScope ? sanitizeCacheScope(options.cacheScope) : void 0].filter((value) => value !== void 0).join("-")
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
return await import(
|
|
79
|
-
/* @vite-ignore */
|
|
80
|
-
compiledOutputUrl.href
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
17
|
/**
|
|
84
18
|
* Ensures that an EcoComponentConfig has proper `__eco` metadata attached.
|
|
85
|
-
* Resolves the file path from dependency declarations when not already set.
|
|
86
|
-
*
|
|
87
|
-
* @param config - The component config to augment
|
|
88
|
-
* @param pagePath - Fallback file path if dependency resolution fails
|
|
89
|
-
* @returns Config with `__eco` metadata populated
|
|
90
19
|
*/
|
|
91
20
|
ensureConfigFileMetadata(config, pagePath) {
|
|
92
21
|
if (config.__eco?.file) {
|
|
@@ -121,22 +50,12 @@ class ReactPageModuleService {
|
|
|
121
50
|
__eco: buildEcoMeta(pagePath)
|
|
122
51
|
};
|
|
123
52
|
}
|
|
124
|
-
/**
|
|
125
|
-
* Recursively checks whether a component config tree declares any browser modules.
|
|
126
|
-
* Used to determine if a page needs hydration.
|
|
127
|
-
*/
|
|
128
53
|
hasModulesInConfig(config) {
|
|
129
54
|
return someInConfigTree(
|
|
130
55
|
config,
|
|
131
56
|
(node) => node.dependencies?.modules?.some((entry) => entry.trim().length > 0) ?? false
|
|
132
57
|
);
|
|
133
58
|
}
|
|
134
|
-
/**
|
|
135
|
-
* Determines whether a page needs client-side hydration.
|
|
136
|
-
*
|
|
137
|
-
* @param pageModule - The imported page module
|
|
138
|
-
* @returns True if the page should be hydrated
|
|
139
|
-
*/
|
|
140
59
|
shouldHydratePage(pageModule) {
|
|
141
60
|
if (this.config.hasRouterAdapter) {
|
|
142
61
|
return true;
|
|
@@ -144,12 +63,6 @@ class ReactPageModuleService {
|
|
|
144
63
|
const pageConfig = pageModule.default?.config;
|
|
145
64
|
return this.hasModulesInConfig(pageConfig) || this.hasModulesInConfig(pageModule.config);
|
|
146
65
|
}
|
|
147
|
-
/**
|
|
148
|
-
* Collects all explicitly declared browser module specifiers from a page module.
|
|
149
|
-
*
|
|
150
|
-
* @param pageModule - The imported page module
|
|
151
|
-
* @returns Deduplicated list of declared module specifiers
|
|
152
|
-
*/
|
|
153
66
|
collectPageDeclaredModules(pageModule) {
|
|
154
67
|
const declarations = [
|
|
155
68
|
...collectDeclaredModulesInConfig(pageModule.default?.config),
|
|
@@ -158,9 +71,6 @@ class ReactPageModuleService {
|
|
|
158
71
|
return Array.from(new Set(declarations));
|
|
159
72
|
}
|
|
160
73
|
}
|
|
161
|
-
function sanitizeCacheScope(cacheScope) {
|
|
162
|
-
return cacheScope.replace(/[^a-zA-Z0-9_-]+/g, "-");
|
|
163
|
-
}
|
|
164
74
|
export {
|
|
165
75
|
ReactPageModuleService
|
|
166
76
|
};
|
|
@@ -22,12 +22,14 @@ export type ReactRuntimeImports = {
|
|
|
22
22
|
export interface ReactRuntimeBundleServiceConfig {
|
|
23
23
|
routerAdapter?: ReactRouterAdapter;
|
|
24
24
|
rootDir?: string;
|
|
25
|
+
workDir?: string;
|
|
25
26
|
}
|
|
26
27
|
type RuntimeMode = 'development' | 'production';
|
|
27
28
|
export declare class ReactRuntimeBundleService {
|
|
28
29
|
private readonly config;
|
|
29
30
|
constructor(config: ReactRuntimeBundleServiceConfig);
|
|
30
31
|
setRootDir(rootDir: string | undefined): void;
|
|
32
|
+
setWorkDir(workDir: string | undefined): void;
|
|
31
33
|
private get isDevelopment();
|
|
32
34
|
private getCurrentRuntimeMode;
|
|
33
35
|
private createRuntimeDefines;
|
|
@@ -39,7 +41,9 @@ export declare class ReactRuntimeBundleService {
|
|
|
39
41
|
getRuntimeImports(mode?: RuntimeMode): ReactRuntimeImports;
|
|
40
42
|
getRuntimeAliasMap(mode?: RuntimeMode): Record<string, string>;
|
|
41
43
|
getRuntimeManifest(mode?: RuntimeMode): BrowserRuntimeManifest;
|
|
42
|
-
getDependencies(
|
|
44
|
+
getDependencies(options?: {
|
|
45
|
+
modes?: RuntimeMode[];
|
|
46
|
+
}): AssetDefinition[];
|
|
43
47
|
createRuntimeAliasPlugin(mode?: RuntimeMode): EcoBuildPlugin;
|
|
44
48
|
}
|
|
45
49
|
export {};
|
|
@@ -18,6 +18,9 @@ class ReactRuntimeBundleService {
|
|
|
18
18
|
setRootDir(rootDir) {
|
|
19
19
|
this.config.rootDir = rootDir;
|
|
20
20
|
}
|
|
21
|
+
setWorkDir(workDir) {
|
|
22
|
+
this.config.workDir = workDir;
|
|
23
|
+
}
|
|
21
24
|
get isDevelopment() {
|
|
22
25
|
return process.env.NODE_ENV === "development";
|
|
23
26
|
}
|
|
@@ -83,9 +86,10 @@ class ReactRuntimeBundleService {
|
|
|
83
86
|
getRuntimeManifest(mode = this.getCurrentRuntimeMode()) {
|
|
84
87
|
return buildReactRuntimeManifest(this.getRuntimeImports(mode));
|
|
85
88
|
}
|
|
86
|
-
getDependencies() {
|
|
89
|
+
getDependencies(options) {
|
|
87
90
|
const dependencies = [];
|
|
88
|
-
|
|
91
|
+
const modes = options?.modes ?? [this.getCurrentRuntimeMode()];
|
|
92
|
+
for (const mode of modes) {
|
|
89
93
|
const reactVendorImportRewritePlugin = this.createReactVendorImportRewritePlugin(mode);
|
|
90
94
|
const reactDomRuntimeInteropPlugin = createReactDomRuntimeInteropPlugin({
|
|
91
95
|
reactSpecifier: buildBrowserRuntimeAssetUrl(this.getReactVendorFileName(mode))
|
|
@@ -117,6 +121,7 @@ class ReactRuntimeBundleService {
|
|
|
117
121
|
fileName: this.getReactVendorFileName(mode),
|
|
118
122
|
cacheDirName: `ecopages-react-runtime-${mode}`,
|
|
119
123
|
rootDir: this.config.rootDir,
|
|
124
|
+
workDir: this.config.workDir,
|
|
120
125
|
bundleOptions: {
|
|
121
126
|
define: this.createRuntimeDefines(mode),
|
|
122
127
|
excludeAppBuildPlugins: [DEFAULT_BROWSER_RUNTIME_PLUGIN_NAME]
|
|
@@ -128,6 +133,7 @@ class ReactRuntimeBundleService {
|
|
|
128
133
|
fileName: this.getReactDomVendorFileName(mode),
|
|
129
134
|
cacheDirName: `ecopages-react-runtime-${mode}`,
|
|
130
135
|
rootDir: this.config.rootDir,
|
|
136
|
+
workDir: this.config.workDir,
|
|
131
137
|
bundleOptions: {
|
|
132
138
|
define: this.createRuntimeDefines(mode),
|
|
133
139
|
excludeAppBuildPlugins: [DEFAULT_BROWSER_RUNTIME_PLUGIN_NAME],
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
export declare function createReactMdxLoaderPlugin(compilerOptions?: CompileOptions): EcoBuildPlugin;
|
|
1
|
+
import type { CompileOptions } from '@mdx-js/mdx';
|
|
2
|
+
export declare function createReactMdxLoaderPlugin(compilerOptions?: CompileOptions): import("@ecopages/core/build/build-types").EcoBuildPlugin;
|
|
@@ -1,44 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { compile } from "@mdx-js/mdx";
|
|
4
|
-
import sourceMap from "source-map";
|
|
5
|
-
import { VFile } from "vfile";
|
|
6
|
-
function resolveCompileFormat(filePath, compilerOptions) {
|
|
7
|
-
const configuredFormat = compilerOptions?.format;
|
|
8
|
-
if (configuredFormat && configuredFormat !== "detect") {
|
|
9
|
-
return configuredFormat;
|
|
10
|
-
}
|
|
11
|
-
return path.extname(filePath).toLowerCase() === ".md" ? "mdx" : configuredFormat;
|
|
12
|
-
}
|
|
1
|
+
import { createMdxLoaderPlugin as createMdxLoaderPluginCore } from "@ecopages/mdx-core";
|
|
13
2
|
function createReactMdxLoaderPlugin(compilerOptions) {
|
|
14
|
-
|
|
15
|
-
const mdExtensions = compilerOptions?.mdExtensions ?? [];
|
|
16
|
-
const allExtensions = [...mdxExtensions, ...mdExtensions];
|
|
17
|
-
const escapedExts = allExtensions.map((ext) => ext.replace(".", "\\."));
|
|
18
|
-
const filter = new RegExp(`(${escapedExts.join("|")})(\\?.*)?$`);
|
|
19
|
-
return {
|
|
3
|
+
return createMdxLoaderPluginCore({
|
|
20
4
|
name: "react-mdx-loader",
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const source = readFileSync(filePath, "utf-8");
|
|
25
|
-
const file = new VFile({ path: filePath, value: source });
|
|
26
|
-
const compiled = await compile(file, {
|
|
27
|
-
...compilerOptions,
|
|
28
|
-
format: resolveCompileFormat(filePath, compilerOptions),
|
|
29
|
-
SourceMapGenerator: sourceMap.SourceMapGenerator
|
|
30
|
-
});
|
|
31
|
-
const inlineSourceMap = compiled.map ? `
|
|
32
|
-
//# sourceMappingURL=data:application/json;base64,${Buffer.from(JSON.stringify(compiled.map)).toString("base64")}
|
|
33
|
-
` : "";
|
|
34
|
-
return {
|
|
35
|
-
contents: `${String(compiled.value)}${inlineSourceMap}`,
|
|
36
|
-
loader: compilerOptions?.jsx ? "jsx" : "js",
|
|
37
|
-
resolveDir: path.dirname(args.path)
|
|
38
|
-
};
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
};
|
|
5
|
+
compilerOptions,
|
|
6
|
+
defaultMdExtensions: []
|
|
7
|
+
});
|
|
42
8
|
}
|
|
43
9
|
export {
|
|
44
10
|
createReactMdxLoaderPlugin
|