@nx/react 20.8.0-canary.20250402-2d210b8 → 20.8.0-canary.20250404-6e50a01
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 +6 -6
- package/plugins/nx-react-webpack-plugin/lib/apply-react-config.js +3 -1
- package/src/generators/application/lib/add-e2e.js +4 -0
- package/src/generators/application/lib/create-application-files.d.ts +61 -0
- package/src/generators/application/lib/create-application-files.js +49 -44
- package/src/generators/host/files/rspack-module-federation/rspack.config.js__tmpl__ +37 -13
- package/src/generators/host/files/rspack-module-federation/rspack.config.prod.js__tmpl__ +60 -32
- package/src/generators/host/files/rspack-module-federation-ssr/module-federation.server.config.js__tmpl__ +6 -0
- package/src/generators/host/files/rspack-module-federation-ssr/rspack.config.js__tmpl__ +66 -0
- package/src/generators/host/files/rspack-module-federation-ssr/server.ts__tmpl__ +1 -1
- package/src/generators/host/files/rspack-module-federation-ssr/src/main.server.tsx__tmpl__ +49 -0
- package/src/generators/host/files/rspack-module-federation-ssr-ts/module-federation.server.config.ts__tmpl__ +6 -0
- package/src/generators/host/files/rspack-module-federation-ssr-ts/rspack.config.ts__tmpl__ +66 -0
- package/src/generators/host/files/rspack-module-federation-ssr-ts/server.ts__tmpl__ +1 -1
- package/src/generators/host/files/rspack-module-federation-ssr-ts/src/main.server.tsx__tmpl__ +49 -0
- package/src/generators/host/files/rspack-module-federation-ts/rspack.config.prod.ts__tmpl__ +37 -9
- package/src/generators/host/files/rspack-module-federation-ts/rspack.config.ts__tmpl__ +37 -14
- package/src/generators/host/host.js +15 -15
- package/src/generators/host/lib/add-module-federation-files.js +28 -12
- package/src/generators/host/lib/normalize-host-name.d.ts +2 -0
- package/src/generators/host/lib/normalize-host-name.js +12 -0
- package/src/generators/host/lib/setup-ssr-for-host.d.ts +3 -3
- package/src/generators/host/lib/setup-ssr-for-host.js +46 -22
- package/src/generators/remote/files/rspack-module-federation/rspack.config.js__tmpl__ +40 -13
- package/src/generators/remote/files/rspack-module-federation-ssr/module-federation.server.config.js__tmpl__ +6 -0
- package/src/generators/remote/files/rspack-module-federation-ssr/rspack.config.js__tmpl__ +69 -0
- package/src/generators/remote/files/rspack-module-federation-ssr/server.ts__tmpl__ +2 -2
- package/src/generators/remote/files/rspack-module-federation-ssr/src/main.server.tsx__tmpl__ +45 -0
- package/src/generators/remote/files/rspack-module-federation-ssr-ts/module-federation.server.config.ts__tmpl__ +6 -0
- package/src/generators/remote/files/rspack-module-federation-ssr-ts/rspack.config.ts__tmpl__ +69 -0
- package/src/generators/remote/files/rspack-module-federation-ssr-ts/server.ts__tmpl__ +2 -2
- package/src/generators/remote/files/rspack-module-federation-ssr-ts/src/main.server.tsx__tmpl__ +45 -0
- package/src/generators/remote/files/rspack-module-federation-ts/rspack.config.ts__tmpl__ +40 -13
- package/src/generators/remote/lib/setup-ssr-for-remote.d.ts +1 -1
- package/src/generators/remote/lib/setup-ssr-for-remote.js +37 -15
- package/src/generators/remote/remote.js +27 -21
- package/src/rules/update-module-federation-project.d.ts +2 -1
- package/src/rules/update-module-federation-project.js +28 -47
- package/src/generators/host/files/rspack-module-federation-ssr/rspack.server.config.js__tmpl__ +0 -16
- package/src/generators/host/files/rspack-module-federation-ssr-ts/rspack.server.config.ts__tmpl__ +0 -16
- package/src/generators/remote/files/rspack-module-federation-ssr/rspack.server.config.js__tmpl__ +0 -16
- package/src/generators/remote/files/rspack-module-federation-ssr-ts/rspack.server.config.ts__tmpl__ +0 -16
@@ -0,0 +1,49 @@
|
|
1
|
+
import type { Request, Response } from 'express';
|
2
|
+
import * as fs from 'fs';
|
3
|
+
import * as ReactDOMServer from 'react-dom/server';
|
4
|
+
import isbot from 'isbot'
|
5
|
+
|
6
|
+
import App from './app/app';
|
7
|
+
|
8
|
+
import { StaticRouter } from 'react-router-dom/server';
|
9
|
+
|
10
|
+
|
11
|
+
let indexHtml: null | string = null;
|
12
|
+
|
13
|
+
export function handleRequest(indexPath: string) {
|
14
|
+
return function render(req: Request, res: Response) {
|
15
|
+
let didError = false;
|
16
|
+
|
17
|
+
if (!indexHtml) {
|
18
|
+
indexHtml = fs.readFileSync(indexPath).toString();
|
19
|
+
}
|
20
|
+
|
21
|
+
const [htmlStart, htmlEnd] = indexHtml.split(`<div id="root"></div>`);
|
22
|
+
|
23
|
+
// For bots (e.g. search engines), the content will not be streamed but render all at once.
|
24
|
+
// For users, content should be streamed to the user as they are ready.
|
25
|
+
const callbackName = isbot(req.headers['user-agent']) ? 'onAllReady' : 'onShellReady';
|
26
|
+
|
27
|
+
const stream = ReactDOMServer.renderToPipeableStream(
|
28
|
+
<StaticRouter location={req.originalUrl}><App /></StaticRouter>,
|
29
|
+
{
|
30
|
+
[callbackName]() {
|
31
|
+
res.statusCode = didError ? 500 : 200;
|
32
|
+
res.setHeader('Content-type', 'text/html; charset=utf-8');
|
33
|
+
res.write(`${htmlStart}<div id="root">`);
|
34
|
+
stream.pipe(res);
|
35
|
+
res.write(`</div>${htmlEnd}`);
|
36
|
+
},
|
37
|
+
onShellError(error) {
|
38
|
+
console.error(error);
|
39
|
+
res.statusCode = 500;
|
40
|
+
res.send('<!doctype html><h1>Server Error</h1>');
|
41
|
+
},
|
42
|
+
onError(error) {
|
43
|
+
didError = true;
|
44
|
+
console.error(error);
|
45
|
+
}
|
46
|
+
}
|
47
|
+
);
|
48
|
+
}
|
49
|
+
}
|
@@ -1,6 +1,8 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
1
|
+
import { NxAppRspackPlugin } from '@nx/rspack/app-plugin';
|
2
|
+
import { NxReactRspackPlugin } from '@nx/rspack/react-plugin';
|
3
|
+
import { NxModuleFederationPlugin, NxModuleFederationDevServerPlugin } from '@nx/module-federation/rspack';
|
3
4
|
import { ModuleFederationConfig } from '@nx/module-federation';
|
5
|
+
import { join } from 'path';
|
4
6
|
|
5
7
|
import baseConfig from './module-federation.config';
|
6
8
|
|
@@ -30,10 +32,36 @@ const prodConfig: ModuleFederationConfig = {
|
|
30
32
|
],
|
31
33
|
};
|
32
34
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
35
|
+
export default {
|
36
|
+
output: {
|
37
|
+
path: join(__dirname, '<%= rspackPluginOptions.outputPath %>'),
|
38
|
+
publicPath: 'auto'
|
39
|
+
},
|
40
|
+
devServer: {
|
41
|
+
port: <%= devServerPort %>,
|
42
|
+
historyApiFallback: {
|
43
|
+
index: '/index.html',
|
44
|
+
disableDotRule: true,
|
45
|
+
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
|
46
|
+
},
|
47
|
+
},
|
48
|
+
plugins: [
|
49
|
+
new NxAppRspackPlugin({
|
50
|
+
tsConfig: '<%= rspackPluginOptions.tsConfig %>',
|
51
|
+
main: '<%= rspackPluginOptions.main %>',
|
52
|
+
index: '<%= rspackPluginOptions.index %>',
|
53
|
+
baseHref: '<%= rspackPluginOptions.baseHref %>',
|
54
|
+
assets: <%- JSON.stringify(rspackPluginOptions.assets) %>,
|
55
|
+
styles: <%- JSON.stringify(rspackPluginOptions.styles) %>,
|
56
|
+
outputHashing: process.env['NODE_ENV'] === 'production' ? 'all' : 'none',
|
57
|
+
optimization: process.env['NODE_ENV'] === 'production',
|
58
|
+
}),
|
59
|
+
new NxReactRspackPlugin({
|
60
|
+
// Uncomment this line if you don't want to use SVGR
|
61
|
+
// See: https://react-svgr.com/
|
62
|
+
// svgr: false
|
63
|
+
}),
|
64
|
+
new NxModuleFederationPlugin({ config: prodConfig }, { dts: false }),
|
65
|
+
new NxModuleFederationDevServerPlugin({ config: prodConfig }),
|
66
|
+
],
|
67
|
+
};
|
@@ -1,17 +1,40 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
3
|
-
import {
|
1
|
+
import { NxAppRspackPlugin } from '@nx/rspack/app-plugin';
|
2
|
+
import { NxReactRspackPlugin } from '@nx/rspack/react-plugin';
|
3
|
+
import { NxModuleFederationPlugin, NxModuleFederationDevServerPlugin } from '@nx/module-federation/rspack';
|
4
|
+
import { join } from 'path';
|
4
5
|
|
5
|
-
import
|
6
|
+
import config from './module-federation.config';
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
export default {
|
9
|
+
output: {
|
10
|
+
path: join(__dirname, '<%= rspackPluginOptions.outputPath %>'),
|
11
|
+
publicPath: 'auto'
|
12
|
+
},
|
13
|
+
devServer: {
|
14
|
+
port: <%= devServerPort %>,
|
15
|
+
historyApiFallback: {
|
16
|
+
index: '/index.html',
|
17
|
+
disableDotRule: true,
|
18
|
+
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
|
19
|
+
},
|
20
|
+
},
|
21
|
+
plugins: [
|
22
|
+
new NxAppRspackPlugin({
|
23
|
+
tsConfig: '<%= rspackPluginOptions.tsConfig %>',
|
24
|
+
main: '<%= rspackPluginOptions.main %>',
|
25
|
+
index: '<%= rspackPluginOptions.index %>',
|
26
|
+
baseHref: '<%= rspackPluginOptions.baseHref %>',
|
27
|
+
assets: <%- JSON.stringify(rspackPluginOptions.assets) %>,
|
28
|
+
styles: <%- JSON.stringify(rspackPluginOptions.styles) %>,
|
29
|
+
outputHashing: process.env['NODE_ENV'] === 'production' ? 'all' : 'none',
|
30
|
+
optimization: process.env['NODE_ENV'] === 'production',
|
31
|
+
}),
|
32
|
+
new NxReactRspackPlugin({
|
33
|
+
// Uncomment this line if you don't want to use SVGR
|
34
|
+
// See: https://react-svgr.com/
|
35
|
+
// svgr: false
|
36
|
+
}),
|
37
|
+
new NxModuleFederationPlugin({ config }, { dts: false }),
|
38
|
+
new NxModuleFederationDevServerPlugin({ config }),
|
39
|
+
],
|
9
40
|
};
|
10
|
-
|
11
|
-
// Nx plugins for rspack to build config object from Nx options and context.
|
12
|
-
/**
|
13
|
-
* DTS Plugin is disabled in Nx Workspaces as Nx already provides Typing support for Module Federation
|
14
|
-
* The DTS Plugin can be enabled by setting dts: true
|
15
|
-
* Learn more about the DTS Plugin here: https://module-federation.io/configure/dts.html
|
16
|
-
*/
|
17
|
-
export default composePlugins(withNx(), withReact(), withModuleFederation(config, { dts: false }));
|
@@ -16,11 +16,14 @@ const js_1 = require("@nx/js");
|
|
16
16
|
const versions_1 = require("../../utils/versions");
|
17
17
|
const project_name_and_root_utils_1 = require("@nx/devkit/src/generators/project-name-and-root-utils");
|
18
18
|
const update_module_federation_tsconfig_1 = require("./lib/update-module-federation-tsconfig");
|
19
|
+
const normalize_host_name_1 = require("./lib/normalize-host-name");
|
19
20
|
async function hostGenerator(host, schema) {
|
20
21
|
const tasks = [];
|
22
|
+
const name = await (0, normalize_host_name_1.normalizeHostName)(host, schema.directory, schema.name);
|
21
23
|
const options = {
|
22
24
|
...(await (0, normalize_options_1.normalizeOptions)(host, {
|
23
25
|
...schema,
|
26
|
+
name,
|
24
27
|
useProjectJson: true,
|
25
28
|
})),
|
26
29
|
js: schema.js ?? false,
|
@@ -28,8 +31,8 @@ async function hostGenerator(host, schema) {
|
|
28
31
|
? false
|
29
32
|
: schema.typescriptConfiguration ?? true,
|
30
33
|
dynamic: schema.dynamic ?? false,
|
31
|
-
// TODO(colum): remove when MF works with Crystal
|
32
|
-
addPlugin: false,
|
34
|
+
// TODO(colum): remove when Webpack MF works with Crystal
|
35
|
+
addPlugin: !schema.bundler || schema.bundler === 'rspack' ? true : false,
|
33
36
|
bundler: schema.bundler ?? 'rspack',
|
34
37
|
};
|
35
38
|
// Check to see if remotes are provided and also check if --dynamic is provided
|
@@ -81,25 +84,22 @@ async function hostGenerator(host, schema) {
|
|
81
84
|
}
|
82
85
|
}
|
83
86
|
(0, add_module_federation_files_1.addModuleFederationFiles)(host, options, remotesWithPorts);
|
84
|
-
(0, update_module_federation_project_1.updateModuleFederationProject)(host, options);
|
87
|
+
(0, update_module_federation_project_1.updateModuleFederationProject)(host, options, true);
|
85
88
|
(0, update_module_federation_e2e_project_1.updateModuleFederationE2eProject)(host, options);
|
86
89
|
(0, update_module_federation_tsconfig_1.updateModuleFederationTsconfig)(host, options);
|
87
90
|
if (options.ssr) {
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
91
|
+
if (options.bundler !== 'rspack') {
|
92
|
+
const setupSsrTask = await (0, setup_ssr_1.default)(host, {
|
93
|
+
project: options.projectName,
|
94
|
+
serverPort: options.devServerPort,
|
95
|
+
skipFormat: true,
|
96
|
+
});
|
97
|
+
tasks.push(setupSsrTask);
|
98
|
+
}
|
94
99
|
const setupSsrForHostTask = await (0, setup_ssr_for_host_1.setupSsrForHost)(host, options, options.projectName, remotesWithPorts);
|
95
100
|
tasks.push(setupSsrForHostTask);
|
96
101
|
const projectConfig = (0, devkit_1.readProjectConfiguration)(host, options.projectName);
|
97
|
-
if (options.bundler
|
98
|
-
projectConfig.targets.server.executor = '@nx/rspack:rspack';
|
99
|
-
projectConfig.targets.server.options.rspackConfig = (0, devkit_1.joinPathFragments)(projectConfig.root, `rspack.server.config.${options.typescriptConfiguration ? 'ts' : 'js'}`);
|
100
|
-
delete projectConfig.targets.server.options.webpackConfig;
|
101
|
-
}
|
102
|
-
else {
|
102
|
+
if (options.bundler !== 'rspack') {
|
103
103
|
projectConfig.targets.server.options.webpackConfig = (0, devkit_1.joinPathFragments)(projectConfig.root, `webpack.server.config.${options.typescriptConfiguration ? 'ts' : 'js'}`);
|
104
104
|
}
|
105
105
|
(0, devkit_1.updateProjectConfiguration)(host, options.projectName, projectConfig);
|
@@ -3,19 +3,35 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.addModuleFederationFiles = addModuleFederationFiles;
|
4
4
|
const devkit_1 = require("@nx/devkit");
|
5
5
|
const maybe_js_1 = require("../../../utils/maybe-js");
|
6
|
+
const create_application_files_1 = require("../../application/lib/create-application-files");
|
6
7
|
function addModuleFederationFiles(host, options, defaultRemoteManifest) {
|
7
|
-
const templateVariables =
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
8
|
+
const templateVariables = options.bundler === 'rspack'
|
9
|
+
? {
|
10
|
+
...(0, create_application_files_1.getDefaultTemplateVariables)(host, options),
|
11
|
+
rspackPluginOptions: {
|
12
|
+
...(0, create_application_files_1.createNxRspackPluginOptions)(options, (0, devkit_1.offsetFromRoot)(options.appProjectRoot), false),
|
13
|
+
mainServer: `./server.ts`,
|
14
|
+
},
|
15
|
+
static: !options?.dynamic,
|
16
|
+
remotes: defaultRemoteManifest.map(({ name, port }) => {
|
17
|
+
return {
|
18
|
+
...(0, devkit_1.names)(name),
|
19
|
+
port,
|
20
|
+
};
|
21
|
+
}),
|
22
|
+
}
|
23
|
+
: {
|
24
|
+
...(0, devkit_1.names)(options.projectName),
|
25
|
+
...options,
|
26
|
+
static: !options?.dynamic,
|
27
|
+
tmpl: '',
|
28
|
+
remotes: defaultRemoteManifest.map(({ name, port }) => {
|
29
|
+
return {
|
30
|
+
...(0, devkit_1.names)(name),
|
31
|
+
port,
|
32
|
+
};
|
33
|
+
}),
|
34
|
+
};
|
19
35
|
const projectConfig = (0, devkit_1.readProjectConfiguration)(host, options.projectName);
|
20
36
|
const pathToMFManifest = (0, devkit_1.joinPathFragments)(projectConfig.sourceRoot, 'assets/module-federation.manifest.json');
|
21
37
|
// Module federation requires bootstrap code to be dynamically imported.
|
@@ -0,0 +1,12 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.normalizeHostName = normalizeHostName;
|
4
|
+
const project_name_and_root_utils_1 = require("@nx/devkit/src/generators/project-name-and-root-utils");
|
5
|
+
async function normalizeHostName(tree, directory, name) {
|
6
|
+
const { projectName } = await (0, project_name_and_root_utils_1.determineProjectNameAndRootOptions)(tree, {
|
7
|
+
name,
|
8
|
+
directory,
|
9
|
+
projectType: 'application',
|
10
|
+
});
|
11
|
+
return projectName;
|
12
|
+
}
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import
|
2
|
-
import type {
|
3
|
-
export declare function setupSsrForHost(tree: Tree, options:
|
1
|
+
import { GeneratorCallback, Tree } from '@nx/devkit';
|
2
|
+
import type { NormalizedSchema } from '../schema';
|
3
|
+
export declare function setupSsrForHost(tree: Tree, options: NormalizedSchema, appName: string, defaultRemoteManifest: {
|
4
4
|
name: string;
|
5
5
|
port: number;
|
6
6
|
}[]): Promise<GeneratorCallback>;
|
@@ -2,35 +2,59 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.setupSsrForHost = setupSsrForHost;
|
4
4
|
const devkit_1 = require("@nx/devkit");
|
5
|
+
const devkit_2 = require("@nx/devkit");
|
5
6
|
const versions_1 = require("../../../utils/versions");
|
7
|
+
const create_application_files_1 = require("../../application/lib/create-application-files");
|
6
8
|
async function setupSsrForHost(tree, options, appName, defaultRemoteManifest) {
|
7
9
|
const tasks = [];
|
8
|
-
let project = (0,
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
let project = (0, devkit_2.readProjectConfiguration)(tree, appName);
|
11
|
+
if (options.bundler !== 'rspack') {
|
12
|
+
project.targets.serve.executor =
|
13
|
+
'@nx/react:module-federation-ssr-dev-server';
|
14
|
+
(0, devkit_2.updateProjectConfiguration)(tree, appName, project);
|
15
|
+
}
|
14
16
|
const pathToModuleFederationSsrFiles = options.typescriptConfiguration
|
15
17
|
? `${options.bundler === 'rspack' ? 'rspack-' : 'webpack-'}module-federation-ssr-ts`
|
16
18
|
: `${options.bundler === 'rspack' ? 'rspack-' : 'webpack-'}module-federation-ssr`;
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
19
|
+
const templateVariables = options.bundler === 'rspack'
|
20
|
+
? {
|
21
|
+
...(0, create_application_files_1.getDefaultTemplateVariables)(tree, options),
|
22
|
+
rspackPluginOptions: {
|
23
|
+
...(0, create_application_files_1.createNxRspackPluginOptions)(options, (0, devkit_1.offsetFromRoot)(options.appProjectRoot), false),
|
24
|
+
mainServer: `./server.ts`,
|
25
|
+
},
|
26
|
+
port: Number(options?.devServerPort) || 4200,
|
27
|
+
appName,
|
28
|
+
static: !options?.dynamic,
|
29
|
+
remotes: defaultRemoteManifest.map(({ name, port }) => {
|
30
|
+
return {
|
31
|
+
...(0, devkit_2.names)(name),
|
32
|
+
port,
|
33
|
+
};
|
34
|
+
}),
|
35
|
+
}
|
36
|
+
: {
|
37
|
+
...options,
|
38
|
+
static: !options?.dynamic,
|
39
|
+
port: Number(options?.devServerPort) || 4200,
|
40
|
+
appName,
|
41
|
+
tmpl: '',
|
42
|
+
browserBuildOutputPath: project.targets.build?.options?.outputPath,
|
43
|
+
remotes: defaultRemoteManifest.map(({ name, port }) => {
|
44
|
+
return {
|
45
|
+
...(0, devkit_2.names)(name),
|
46
|
+
port,
|
47
|
+
};
|
48
|
+
}),
|
49
|
+
};
|
50
|
+
(0, devkit_2.generateFiles)(tree, (0, devkit_2.joinPathFragments)(__dirname, `../files/${pathToModuleFederationSsrFiles}`), project.root, templateVariables);
|
51
|
+
const installTask = (0, devkit_2.addDependenciesToPackageJson)(tree, {
|
32
52
|
'@module-federation/node': versions_1.moduleFederationNodeVersion,
|
53
|
+
cors: versions_1.corsVersion,
|
54
|
+
isbot: versions_1.isbotVersion,
|
55
|
+
express: versions_1.expressVersion,
|
56
|
+
'@types/express': versions_1.typesExpressVersion,
|
33
57
|
}, {});
|
34
58
|
tasks.push(installTask);
|
35
|
-
return (0,
|
59
|
+
return (0, devkit_2.runTasksInSerial)(...tasks);
|
36
60
|
}
|
@@ -1,16 +1,43 @@
|
|
1
|
-
const {
|
2
|
-
const {
|
1
|
+
const { NxAppRspackPlugin } = require('@nx/rspack/app-plugin');
|
2
|
+
const { NxReactRspackPlugin } = require('@nx/rspack/react-plugin');
|
3
|
+
const { NxModuleFederationPlugin, NxModuleFederationDevServerPlugin } = require('@nx/module-federation/rspack');
|
4
|
+
const { join } = require('path');
|
3
5
|
|
4
|
-
const
|
6
|
+
const config = require('./module-federation.config');
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
+
module.exports = {
|
9
|
+
output: {
|
10
|
+
path: join(__dirname, '<%= rspackPluginOptions.outputPath %>'),
|
11
|
+
publicPath: 'auto'
|
12
|
+
},
|
13
|
+
devServer: {
|
14
|
+
port: <%= devServerPort %>,
|
15
|
+
headers: {
|
16
|
+
"Access-Control-Allow-Origin": "*"
|
17
|
+
},
|
18
|
+
historyApiFallback: {
|
19
|
+
index: '/index.html',
|
20
|
+
disableDotRule: true,
|
21
|
+
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
|
22
|
+
},
|
23
|
+
},
|
24
|
+
plugins: [
|
25
|
+
new NxAppRspackPlugin({
|
26
|
+
tsConfig: '<%= rspackPluginOptions.tsConfig %>',
|
27
|
+
main: '<%= rspackPluginOptions.main %>',
|
28
|
+
index: '<%= rspackPluginOptions.index %>',
|
29
|
+
baseHref: '<%= rspackPluginOptions.baseHref %>',
|
30
|
+
assets: <%- JSON.stringify(rspackPluginOptions.assets) %>,
|
31
|
+
styles: <%- JSON.stringify(rspackPluginOptions.styles) %>,
|
32
|
+
outputHashing: process.env['NODE_ENV'] === 'production' ? 'all' : 'none',
|
33
|
+
optimization: process.env['NODE_ENV'] === 'production',
|
34
|
+
}),
|
35
|
+
new NxReactRspackPlugin({
|
36
|
+
// Uncomment this line if you don't want to use SVGR
|
37
|
+
// See: https://react-svgr.com/
|
38
|
+
// svgr: false
|
39
|
+
}),
|
40
|
+
new NxModuleFederationPlugin({ config }, { dts: false }),
|
41
|
+
new NxModuleFederationDevServerPlugin({ config }),
|
42
|
+
],
|
8
43
|
};
|
9
|
-
|
10
|
-
// Nx plugins for rspack to build config object from Nx options and context.
|
11
|
-
/**
|
12
|
-
* DTS Plugin is disabled in Nx Workspaces as Nx already provides Typing support Module Federation
|
13
|
-
* The DTS Plugin can be enabled by setting dts: true
|
14
|
-
* Learn more about the DTS Plugin here: https://module-federation.io/configure/dts.html
|
15
|
-
*/
|
16
|
-
module.exports = composePlugins(withNx(), withReact(), withModuleFederation(config, { dts: false }));
|
@@ -0,0 +1,69 @@
|
|
1
|
+
const { NxAppRspackPlugin } = require('@nx/rspack/app-plugin');
|
2
|
+
const { NxReactRspackPlugin } = require('@nx/rspack/react-plugin');
|
3
|
+
const { NxModuleFederationPlugin, NxModuleFederationSSRDevServerPlugin } = require('@nx/module-federation/rspack');
|
4
|
+
const { join } = require('path');
|
5
|
+
|
6
|
+
const browserMfConfig = require('./module-federation.config');
|
7
|
+
const serverMfConfig = require('./module-federation.server.config');
|
8
|
+
|
9
|
+
const browserRspackConfig = {
|
10
|
+
name: 'browser',
|
11
|
+
output: {
|
12
|
+
path: join(__dirname, '<%= rspackPluginOptions.outputPath %>', 'browser'),
|
13
|
+
publicPath: 'auto'
|
14
|
+
},
|
15
|
+
devServer: {
|
16
|
+
port: <%= devServerPort %>,
|
17
|
+
headers: {
|
18
|
+
"Access-Control-Allow-Origin": "*"
|
19
|
+
},
|
20
|
+
historyApiFallback: {
|
21
|
+
index: '/index.html',
|
22
|
+
disableDotRule: true,
|
23
|
+
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
|
24
|
+
},
|
25
|
+
devMiddleware: {
|
26
|
+
writeToDisk: (file: string) => !file.includes('.hot-update.'),
|
27
|
+
},
|
28
|
+
},
|
29
|
+
plugins: [
|
30
|
+
new NxAppRspackPlugin({
|
31
|
+
tsConfig: '<%= rspackPluginOptions.tsConfig %>',
|
32
|
+
main: '<%= rspackPluginOptions.main %>',
|
33
|
+
index: '<%= rspackPluginOptions.index %>',
|
34
|
+
baseHref: '<%= rspackPluginOptions.baseHref %>',
|
35
|
+
assets: <%- JSON.stringify(rspackPluginOptions.assets) %>,
|
36
|
+
styles: <%- JSON.stringify(rspackPluginOptions.styles) %>,
|
37
|
+
outputHashing: process.env['NODE_ENV'] === 'production' ? 'all' : 'none',
|
38
|
+
optimization: process.env['NODE_ENV'] === 'production',
|
39
|
+
}),
|
40
|
+
new NxReactRspackPlugin({
|
41
|
+
// Uncomment this line if you don't want to use SVGR
|
42
|
+
// See: https://react-svgr.com/
|
43
|
+
// svgr: false
|
44
|
+
}),
|
45
|
+
new NxModuleFederationPlugin({ config: browserMfConfig }, { dts: false }),
|
46
|
+
],
|
47
|
+
};
|
48
|
+
|
49
|
+
const serverRspackConfig = {
|
50
|
+
name: 'server',
|
51
|
+
target: 'async-node',
|
52
|
+
output: {
|
53
|
+
path: join(__dirname, '<%= rspackPluginOptions.outputPath %>', 'server'),
|
54
|
+
filename: 'server.js'
|
55
|
+
},
|
56
|
+
plugins: [
|
57
|
+
new NxAppRspackPlugin({
|
58
|
+
outputPath: join(__dirname, '<%= rspackPluginOptions.outputPath %>', 'server'),
|
59
|
+
outputFileName: 'server.js',
|
60
|
+
tsConfig: '<%= rspackPluginOptions.tsConfig %>',
|
61
|
+
main: '<%= rspackPluginOptions.mainServer %>',
|
62
|
+
baseHref: '<%= rspackPluginOptions.baseHref %>',
|
63
|
+
}),
|
64
|
+
new NxModuleFederationPlugin({ config: serverMfConfig, isServer: true }, { dts: false }),
|
65
|
+
new NxModuleFederationSSRDevServerPlugin({ config: serverMfConfig }),
|
66
|
+
],
|
67
|
+
};
|
68
|
+
|
69
|
+
module.exports = [browserRspackConfig, serverRspackConfig];
|
@@ -7,8 +7,8 @@ import { handleRequest } from './src/main.server';
|
|
7
7
|
const port = process.env['PORT'] || <%= port %>;
|
8
8
|
const app = express();
|
9
9
|
|
10
|
-
const browserDist = path.join(process.cwd(), '<%=
|
11
|
-
const serverDist = path.join(process.cwd(), '<%=
|
10
|
+
const browserDist = path.join(process.cwd(), '<%= rspackPluginOptions.outputPath %>', 'browser');
|
11
|
+
const serverDist = path.join(process.cwd(), '<%= rspackPluginOptions.outputPath %>', 'server');
|
12
12
|
const indexPath = path.join(browserDist, 'index.html');
|
13
13
|
|
14
14
|
app.use(cors());
|
@@ -0,0 +1,45 @@
|
|
1
|
+
import type { Request, Response } from 'express';
|
2
|
+
import * as fs from 'fs';
|
3
|
+
import * as ReactDOMServer from 'react-dom/server';
|
4
|
+
import isbot from 'isbot';
|
5
|
+
|
6
|
+
import App from './app/app';
|
7
|
+
|
8
|
+
let indexHtml: null | string = null;
|
9
|
+
|
10
|
+
export function handleRequest(indexPath: string) {
|
11
|
+
return function render(req: Request, res: Response) {
|
12
|
+
let didError = false;
|
13
|
+
|
14
|
+
if (!indexHtml) {
|
15
|
+
indexHtml = fs.readFileSync(indexPath).toString();
|
16
|
+
}
|
17
|
+
|
18
|
+
const [htmlStart, htmlEnd] = indexHtml.split(`<div id="root"></div>`);
|
19
|
+
|
20
|
+
// For bots (e.g. search engines), the content will not be streamed but render all at once.
|
21
|
+
// For users, content should be streamed to the user as they are ready.
|
22
|
+
const callbackName = isbot(req.headers['user-agent'])
|
23
|
+
? 'onAllReady'
|
24
|
+
: 'onShellReady';
|
25
|
+
|
26
|
+
const stream = ReactDOMServer.renderToPipeableStream(<App />, {
|
27
|
+
[callbackName]() {
|
28
|
+
res.statusCode = didError ? 500 : 200;
|
29
|
+
res.setHeader('Content-type', 'text/html; charset=utf-8');
|
30
|
+
res.write(`${htmlStart}<div id="root">`);
|
31
|
+
stream.pipe(res);
|
32
|
+
res.write(`</div>${htmlEnd}`);
|
33
|
+
},
|
34
|
+
onShellError(error) {
|
35
|
+
console.error(error);
|
36
|
+
res.statusCode = 500;
|
37
|
+
res.send('<!doctype html><h1>Server Error</h1>');
|
38
|
+
},
|
39
|
+
onError(error) {
|
40
|
+
didError = true;
|
41
|
+
console.error(error);
|
42
|
+
},
|
43
|
+
});
|
44
|
+
};
|
45
|
+
}
|