@nx/react 20.7.2 → 20.8.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/package.json +6 -6
  2. package/src/generators/application/lib/add-e2e.js +4 -0
  3. package/src/generators/application/lib/create-application-files.d.ts +61 -0
  4. package/src/generators/application/lib/create-application-files.js +49 -44
  5. package/src/generators/component-test/component-test.js +1 -1
  6. package/src/generators/cypress-component-configuration/lib/add-files.d.ts +1 -1
  7. package/src/generators/cypress-component-configuration/lib/add-files.js +7 -4
  8. package/src/generators/host/files/rspack-module-federation/rspack.config.js__tmpl__ +37 -13
  9. package/src/generators/host/files/rspack-module-federation/rspack.config.prod.js__tmpl__ +60 -32
  10. package/src/generators/host/files/rspack-module-federation-ssr/module-federation.server.config.js__tmpl__ +6 -0
  11. package/src/generators/host/files/rspack-module-federation-ssr/rspack.config.js__tmpl__ +66 -0
  12. package/src/generators/host/files/rspack-module-federation-ssr/server.ts__tmpl__ +1 -1
  13. package/src/generators/host/files/rspack-module-federation-ssr/src/main.server.tsx__tmpl__ +49 -0
  14. package/src/generators/host/files/rspack-module-federation-ssr-ts/module-federation.server.config.ts__tmpl__ +6 -0
  15. package/src/generators/host/files/rspack-module-federation-ssr-ts/rspack.config.ts__tmpl__ +66 -0
  16. package/src/generators/host/files/rspack-module-federation-ssr-ts/server.ts__tmpl__ +1 -1
  17. package/src/generators/host/files/rspack-module-federation-ssr-ts/src/main.server.tsx__tmpl__ +49 -0
  18. package/src/generators/host/files/rspack-module-federation-ts/rspack.config.prod.ts__tmpl__ +37 -9
  19. package/src/generators/host/files/rspack-module-federation-ts/rspack.config.ts__tmpl__ +37 -14
  20. package/src/generators/host/host.js +15 -15
  21. package/src/generators/host/lib/add-module-federation-files.js +28 -12
  22. package/src/generators/host/lib/normalize-host-name.d.ts +2 -0
  23. package/src/generators/host/lib/normalize-host-name.js +12 -0
  24. package/src/generators/host/lib/setup-ssr-for-host.d.ts +3 -3
  25. package/src/generators/host/lib/setup-ssr-for-host.js +46 -22
  26. package/src/generators/library/library.js +14 -12
  27. package/src/generators/remote/files/rspack-module-federation/rspack.config.js__tmpl__ +40 -13
  28. package/src/generators/remote/files/rspack-module-federation-ssr/module-federation.server.config.js__tmpl__ +6 -0
  29. package/src/generators/remote/files/rspack-module-federation-ssr/rspack.config.js__tmpl__ +69 -0
  30. package/src/generators/remote/files/rspack-module-federation-ssr/server.ts__tmpl__ +2 -2
  31. package/src/generators/remote/files/rspack-module-federation-ssr/src/main.server.tsx__tmpl__ +45 -0
  32. package/src/generators/remote/files/rspack-module-federation-ssr-ts/module-federation.server.config.ts__tmpl__ +6 -0
  33. package/src/generators/remote/files/rspack-module-federation-ssr-ts/rspack.config.ts__tmpl__ +69 -0
  34. package/src/generators/remote/files/rspack-module-federation-ssr-ts/server.ts__tmpl__ +2 -2
  35. package/src/generators/remote/files/rspack-module-federation-ssr-ts/src/main.server.tsx__tmpl__ +45 -0
  36. package/src/generators/remote/files/rspack-module-federation-ts/rspack.config.ts__tmpl__ +40 -13
  37. package/src/generators/remote/lib/setup-ssr-for-remote.d.ts +1 -1
  38. package/src/generators/remote/lib/setup-ssr-for-remote.js +37 -15
  39. package/src/generators/remote/remote.js +27 -21
  40. package/src/rules/update-module-federation-project.d.ts +2 -1
  41. package/src/rules/update-module-federation-project.js +28 -47
  42. package/src/generators/host/files/rspack-module-federation-ssr/rspack.server.config.js__tmpl__ +0 -16
  43. package/src/generators/host/files/rspack-module-federation-ssr-ts/rspack.server.config.ts__tmpl__ +0 -16
  44. package/src/generators/remote/files/rspack-module-federation-ssr/rspack.server.config.js__tmpl__ +0 -16
  45. 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 { composePlugins, withNx, withReact } from '@nx/rspack';
2
- import { withModuleFederation } from '@nx/module-federation/rspack';
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
- // Nx plugins for rspack to build config object from Nx options and context.
34
- /**
35
- * DTS Plugin is disabled in Nx Workspaces as Nx already provides Typing support for Module Federation
36
- * The DTS Plugin can be enabled by setting dts: true
37
- * Learn more about the DTS Plugin here: https://module-federation.io/configure/dts.html
38
- */
39
- export default composePlugins(withNx(), withReact(), withModuleFederation(prodConfig, { dts: false }));
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 {composePlugins, withNx, withReact} from '@nx/rspack';
2
- import { withModuleFederation } from '@nx/module-federation/rspack';
3
- import { ModuleFederationConfig } from '@nx/module-federation';
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 baseConfig from './module-federation.config';
6
+ import config from './module-federation.config';
6
7
 
7
- const config: ModuleFederationConfig = {
8
- ...baseConfig,
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
- const setupSsrTask = await (0, setup_ssr_1.default)(host, {
89
- project: options.projectName,
90
- serverPort: options.devServerPort,
91
- skipFormat: true,
92
- });
93
- tasks.push(setupSsrTask);
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 === 'rspack') {
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
- ...(0, devkit_1.names)(options.projectName),
9
- ...options,
10
- static: !options?.dynamic,
11
- tmpl: '',
12
- remotes: defaultRemoteManifest.map(({ name, port }) => {
13
- return {
14
- ...(0, devkit_1.names)(name),
15
- port,
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,2 @@
1
+ import { Tree } from '@nx/devkit';
2
+ export declare function normalizeHostName(tree: Tree, directory: string, name?: string): Promise<string>;
@@ -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 type { GeneratorCallback, Tree } from '@nx/devkit';
2
- import type { Schema } from '../schema';
3
- export declare function setupSsrForHost(tree: Tree, options: Schema, appName: string, defaultRemoteManifest: {
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, devkit_1.readProjectConfiguration)(tree, appName);
9
- project.targets.serve.executor =
10
- options.bundler === 'rspack'
11
- ? '@nx/rspack:module-federation-ssr-dev-server'
12
- : '@nx/react:module-federation-ssr-dev-server';
13
- (0, devkit_1.updateProjectConfiguration)(tree, appName, project);
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
- (0, devkit_1.generateFiles)(tree, (0, devkit_1.joinPathFragments)(__dirname, `../files/${pathToModuleFederationSsrFiles}`), project.root, {
18
- ...options,
19
- static: !options?.dynamic,
20
- port: Number(options?.devServerPort) || 4200,
21
- remotes: defaultRemoteManifest.map(({ name, port }) => {
22
- return {
23
- ...(0, devkit_1.names)(name),
24
- port,
25
- };
26
- }),
27
- appName,
28
- tmpl: '',
29
- browserBuildOutputPath: project.targets.build.options.outputPath,
30
- });
31
- const installTask = (0, devkit_1.addDependenciesToPackageJson)(tree, {
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, devkit_1.runTasksInSerial)(...tasks);
59
+ return (0, devkit_2.runTasksInSerial)(...tasks);
36
60
  }
@@ -2,28 +2,29 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.libraryGenerator = libraryGenerator;
4
4
  exports.libraryGeneratorInternal = libraryGeneratorInternal;
5
- const path_1 = require("path");
6
5
  const devkit_1 = require("@nx/devkit");
7
6
  const artifact_name_and_directory_utils_1 = require("@nx/devkit/src/generators/artifact-name-and-directory-utils");
8
7
  const log_show_project_command_1 = require("@nx/devkit/src/utils/log-show-project-command");
9
8
  const js_1 = require("@nx/js");
10
- const versions_1 = require("../../utils/versions");
9
+ const path_1 = require("path");
10
+ const add_release_config_1 = require("@nx/js/src/generators/library/utils/add-release-config");
11
+ const sort_fields_1 = require("@nx/js/src/utils/package-json/sort-fields");
12
+ const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup");
13
+ const use_legacy_versioning_1 = require("nx/src/command-line/release/config/use-legacy-versioning");
14
+ const create_ts_config_1 = require("../../utils/create-ts-config");
15
+ const jest_utils_1 = require("../../utils/jest-utils");
11
16
  const maybe_js_1 = require("../../utils/maybe-js");
17
+ const versions_1 = require("../../utils/versions");
12
18
  const component_1 = require("../component/component");
13
19
  const init_1 = require("../init/init");
14
- const jest_utils_1 = require("../../utils/jest-utils");
15
- const normalize_options_1 = require("./lib/normalize-options");
16
- const add_rollup_build_target_1 = require("./lib/add-rollup-build-target");
17
20
  const add_linting_1 = require("./lib/add-linting");
18
- const update_app_routes_1 = require("./lib/update-app-routes");
21
+ const add_rollup_build_target_1 = require("./lib/add-rollup-build-target");
19
22
  const create_files_1 = require("./lib/create-files");
20
- const create_ts_config_1 = require("../../utils/create-ts-config");
23
+ const determine_entry_fields_1 = require("./lib/determine-entry-fields");
21
24
  const install_common_dependencies_1 = require("./lib/install-common-dependencies");
25
+ const normalize_options_1 = require("./lib/normalize-options");
22
26
  const set_defaults_1 = require("./lib/set-defaults");
23
- const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup");
24
- const determine_entry_fields_1 = require("./lib/determine-entry-fields");
25
- const sort_fields_1 = require("@nx/js/src/utils/package-json/sort-fields");
26
- const add_release_config_1 = require("@nx/js/src/generators/library/utils/add-release-config");
27
+ const update_app_routes_1 = require("./lib/update-app-routes");
27
28
  async function libraryGenerator(host, schema) {
28
29
  return await libraryGeneratorInternal(host, {
29
30
  addPlugin: false,
@@ -196,7 +197,8 @@ async function libraryGeneratorInternal(host, schema) {
196
197
  await (0, add_release_config_1.addReleaseConfigForTsSolution)(host, options.name, projectConfiguration);
197
198
  }
198
199
  else {
199
- await (0, add_release_config_1.addReleaseConfigForNonTsSolution)(host, options.name, projectConfiguration);
200
+ const nxJson = (0, devkit_1.readNxJson)(host);
201
+ await (0, add_release_config_1.addReleaseConfigForNonTsSolution)((0, use_legacy_versioning_1.shouldUseLegacyVersioning)(nxJson.release), host, options.name, projectConfiguration);
200
202
  }
201
203
  (0, devkit_1.updateProjectConfiguration)(host, options.name, projectConfiguration);
202
204
  tasks.push(await (0, add_release_config_1.releaseTasks)(host));
@@ -1,16 +1,43 @@
1
- const { composePlugins, withNx, withReact } = require('@nx/rspack');
2
- const { withModuleFederation } = require('@nx/module-federation/rspack');
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 baseConfig = require('./module-federation.config');
6
+ const config = require('./module-federation.config');
5
7
 
6
- const config = {
7
- ...baseConfig,
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 }));
@@ -6,4 +6,10 @@ module.exports = {
6
6
  exposes: {
7
7
  './Module': './src/remote-entry.<%= js ? 'js' : 'ts' %>',
8
8
  },
9
+ shared: (libraryName, libraryConfig) => {
10
+ return {
11
+ ...libraryConfig,
12
+ eager: true
13
+ }
14
+ },
9
15
  };
@@ -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(), '<%= browserBuildOutputPath %>');
11
- const serverDist = path.join(process.cwd(), '<%= serverBuildOutputPath %>');
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
+ }
@@ -5,6 +5,12 @@ const config: ModuleFederationConfig = {
5
5
  exposes: {
6
6
  './Module': './src/remote-entry.<%= js ? 'js' : 'ts' %>',
7
7
  },
8
+ shared: (libraryName, libraryConfig) => {
9
+ return {
10
+ ...libraryConfig,
11
+ eager: true
12
+ }
13
+ },
8
14
  };
9
15
 
10
16
  /**