@nx/react 21.0.0-beta.0 → 21.0.0-beta.2

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 (100) hide show
  1. package/package.json +6 -6
  2. package/plugins/nx-react-webpack-plugin/lib/apply-react-config.js +3 -1
  3. package/router-plugin.d.ts +1 -0
  4. package/router-plugin.js +5 -0
  5. package/src/generators/application/application.js +52 -15
  6. package/src/generators/application/files/react-router-ssr/common/app/app-nav.tsx__tmpl__ +14 -0
  7. package/src/generators/application/files/react-router-ssr/common/app/entry.client.tsx__tmpl__ +18 -0
  8. package/src/generators/application/files/react-router-ssr/common/app/entry.server.tsx__tmpl__ +74 -0
  9. package/src/generators/application/files/react-router-ssr/common/app/root.tsx__tmpl__ +51 -0
  10. package/src/generators/application/files/react-router-ssr/common/app/routes/about.tsx__tmpl__ +7 -0
  11. package/src/generators/application/files/react-router-ssr/common/app/routes.tsx__tmpl__ +6 -0
  12. package/src/generators/application/files/react-router-ssr/common/public/favicon.ico +0 -0
  13. package/src/generators/application/files/react-router-ssr/common/react-router.config.ts__tmpl__ +5 -0
  14. package/src/generators/application/files/react-router-ssr/common/tests/routes/_index.spec.tsx__tmpl__ +16 -0
  15. package/src/generators/application/files/react-router-ssr/common/tsconfig.app.json__tmpl__ +23 -0
  16. package/src/generators/application/files/react-router-ssr/common/tsconfig.json__tmpl__ +27 -0
  17. package/src/generators/application/files/react-router-ssr/non-root/.gitignore__tmpl__ +5 -0
  18. package/src/generators/application/files/react-router-ssr/non-root/package.json__tmpl__ +24 -0
  19. package/src/generators/application/files/react-router-ssr/nx-welcome/claimed/app/nx-welcome.tsx__tmpl__ +866 -0
  20. package/src/generators/application/files/react-router-ssr/nx-welcome/not-configured/app/nx-welcome.tsx__tmpl__ +866 -0
  21. package/src/generators/application/files/react-router-ssr/nx-welcome/unclaimed/app/nx-welcome.tsx__tmpl__ +864 -0
  22. package/src/generators/application/files/react-router-ssr/ts-solution/package.json__tmpl__ +24 -0
  23. package/src/generators/application/files/react-router-ssr/ts-solution/tsconfig.app.json__tmpl__ +39 -0
  24. package/src/generators/application/lib/add-e2e.js +33 -24
  25. package/src/generators/application/lib/add-jest.js +26 -2
  26. package/src/generators/application/lib/add-linting.d.ts +1 -0
  27. package/src/generators/application/lib/add-linting.js +38 -0
  28. package/src/generators/application/lib/add-project.js +31 -15
  29. package/src/generators/application/lib/add-routing.js +1 -1
  30. package/src/generators/application/lib/bundlers/add-rspack.d.ts +0 -1
  31. package/src/generators/application/lib/bundlers/add-rspack.js +0 -18
  32. package/src/generators/application/lib/bundlers/add-vite.js +15 -6
  33. package/src/generators/application/lib/create-application-files.d.ts +61 -0
  34. package/src/generators/application/lib/create-application-files.js +81 -39
  35. package/src/generators/application/lib/get-app-tests.js +3 -3
  36. package/src/generators/application/lib/install-common-dependencies.js +13 -2
  37. package/src/generators/application/lib/normalize-options.d.ts +0 -2
  38. package/src/generators/application/lib/normalize-options.js +14 -20
  39. package/src/generators/application/schema.d.ts +5 -1
  40. package/src/generators/application/schema.json +10 -1
  41. package/src/generators/component-test/component-test.js +1 -1
  42. package/src/generators/cypress-component-configuration/lib/add-files.d.ts +1 -1
  43. package/src/generators/cypress-component-configuration/lib/add-files.js +7 -4
  44. package/src/generators/host/files/rspack-module-federation/rspack.config.js__tmpl__ +37 -13
  45. package/src/generators/host/files/rspack-module-federation/rspack.config.prod.js__tmpl__ +60 -32
  46. package/src/generators/host/files/rspack-module-federation-ssr/module-federation.server.config.js__tmpl__ +6 -0
  47. package/src/generators/host/files/rspack-module-federation-ssr/rspack.config.js__tmpl__ +66 -0
  48. package/src/generators/host/files/rspack-module-federation-ssr/server.ts__tmpl__ +1 -1
  49. package/src/generators/host/files/rspack-module-federation-ssr/src/main.server.tsx__tmpl__ +49 -0
  50. package/src/generators/host/files/rspack-module-federation-ssr-ts/module-federation.server.config.ts__tmpl__ +6 -0
  51. package/src/generators/host/files/rspack-module-federation-ssr-ts/rspack.config.ts__tmpl__ +66 -0
  52. package/src/generators/host/files/rspack-module-federation-ssr-ts/server.ts__tmpl__ +1 -1
  53. package/src/generators/host/files/rspack-module-federation-ssr-ts/src/main.server.tsx__tmpl__ +49 -0
  54. package/src/generators/host/files/rspack-module-federation-ts/rspack.config.prod.ts__tmpl__ +37 -9
  55. package/src/generators/host/files/rspack-module-federation-ts/rspack.config.ts__tmpl__ +37 -14
  56. package/src/generators/host/host.js +21 -18
  57. package/src/generators/host/lib/add-module-federation-files.js +28 -12
  58. package/src/generators/host/lib/normalize-host-name.d.ts +2 -0
  59. package/src/generators/host/lib/normalize-host-name.js +12 -0
  60. package/src/generators/host/lib/setup-ssr-for-host.d.ts +3 -3
  61. package/src/generators/host/lib/setup-ssr-for-host.js +46 -22
  62. package/src/generators/init/init.js +23 -0
  63. package/src/generators/init/schema.d.ts +2 -0
  64. package/src/generators/library/lib/add-rollup-build-target.d.ts +3 -1
  65. package/src/generators/library/lib/add-rollup-build-target.js +6 -7
  66. package/src/generators/library/lib/create-files.js +2 -1
  67. package/src/generators/library/lib/normalize-options.js +8 -5
  68. package/src/generators/library/library.js +44 -30
  69. package/src/generators/library/schema.d.ts +2 -0
  70. package/src/generators/library/schema.json +4 -0
  71. package/src/generators/remote/files/rspack-module-federation/rspack.config.js__tmpl__ +40 -13
  72. package/src/generators/remote/files/rspack-module-federation-ssr/module-federation.server.config.js__tmpl__ +6 -0
  73. package/src/generators/remote/files/rspack-module-federation-ssr/rspack.config.js__tmpl__ +69 -0
  74. package/src/generators/remote/files/rspack-module-federation-ssr/server.ts__tmpl__ +2 -2
  75. package/src/generators/remote/files/rspack-module-federation-ssr/src/main.server.tsx__tmpl__ +45 -0
  76. package/src/generators/remote/files/rspack-module-federation-ssr-ts/module-federation.server.config.ts__tmpl__ +6 -0
  77. package/src/generators/remote/files/rspack-module-federation-ssr-ts/rspack.config.ts__tmpl__ +69 -0
  78. package/src/generators/remote/files/rspack-module-federation-ssr-ts/server.ts__tmpl__ +2 -2
  79. package/src/generators/remote/files/rspack-module-federation-ssr-ts/src/main.server.tsx__tmpl__ +45 -0
  80. package/src/generators/remote/files/rspack-module-federation-ts/rspack.config.ts__tmpl__ +40 -13
  81. package/src/generators/remote/lib/setup-ssr-for-remote.d.ts +1 -1
  82. package/src/generators/remote/lib/setup-ssr-for-remote.js +37 -15
  83. package/src/generators/remote/remote.js +46 -30
  84. package/src/plugins/router-plugin.d.ts +10 -0
  85. package/src/plugins/router-plugin.js +219 -0
  86. package/src/rules/update-module-federation-project.d.ts +2 -1
  87. package/src/rules/update-module-federation-project.js +28 -47
  88. package/src/utils/assertion.d.ts +2 -0
  89. package/src/utils/assertion.js +6 -0
  90. package/src/utils/ast-utils.d.ts +1 -1
  91. package/src/utils/ast-utils.js +2 -2
  92. package/src/utils/ct-utils.d.ts +1 -1
  93. package/src/utils/versions.d.ts +6 -4
  94. package/src/utils/versions.js +9 -6
  95. package/src/generators/host/files/rspack-module-federation-ssr/rspack.server.config.js__tmpl__ +0 -16
  96. package/src/generators/host/files/rspack-module-federation-ssr-ts/rspack.server.config.ts__tmpl__ +0 -16
  97. package/src/generators/remote/files/rspack-module-federation-ssr/rspack.server.config.js__tmpl__ +0 -16
  98. package/src/generators/remote/files/rspack-module-federation-ssr-ts/rspack.server.config.ts__tmpl__ +0 -16
  99. package/src/utils/format-file.d.ts +0 -1
  100. package/src/utils/format-file.js +0 -11
@@ -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,17 +16,23 @@ 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
- ...(await (0, normalize_options_1.normalizeOptions)(host, schema)),
24
+ ...(await (0, normalize_options_1.normalizeOptions)(host, {
25
+ ...schema,
26
+ name,
27
+ useProjectJson: true,
28
+ })),
23
29
  js: schema.js ?? false,
24
30
  typescriptConfiguration: schema.js
25
31
  ? false
26
32
  : schema.typescriptConfiguration ?? true,
27
33
  dynamic: schema.dynamic ?? false,
28
- // TODO(colum): remove when MF works with Crystal
29
- addPlugin: false,
34
+ // TODO(colum): remove when Webpack MF works with Crystal
35
+ addPlugin: !schema.bundler || schema.bundler === 'rspack' ? true : false,
30
36
  bundler: schema.bundler ?? 'rspack',
31
37
  };
32
38
  // Check to see if remotes are provided and also check if --dynamic is provided
@@ -39,7 +45,7 @@ async function hostGenerator(host, schema) {
39
45
  }
40
46
  });
41
47
  }
42
- await (0, project_name_and_root_utils_1.ensureProjectName)(host, options, 'application');
48
+ await (0, project_name_and_root_utils_1.ensureRootProjectName)(options, 'application');
43
49
  const initTask = await (0, application_1.default)(host, {
44
50
  ...options,
45
51
  directory: options.appProjectRoot,
@@ -47,7 +53,7 @@ async function hostGenerator(host, schema) {
47
53
  // The target use-case is loading remotes as child routes, thus always enable routing.
48
54
  routing: true,
49
55
  skipFormat: true,
50
- alwaysGenerateProjectJson: true,
56
+ useProjectJson: true,
51
57
  });
52
58
  tasks.push(initTask);
53
59
  const remotesWithPorts = [];
@@ -78,25 +84,22 @@ async function hostGenerator(host, schema) {
78
84
  }
79
85
  }
80
86
  (0, add_module_federation_files_1.addModuleFederationFiles)(host, options, remotesWithPorts);
81
- (0, update_module_federation_project_1.updateModuleFederationProject)(host, options);
87
+ (0, update_module_federation_project_1.updateModuleFederationProject)(host, options, true);
82
88
  (0, update_module_federation_e2e_project_1.updateModuleFederationE2eProject)(host, options);
83
89
  (0, update_module_federation_tsconfig_1.updateModuleFederationTsconfig)(host, options);
84
90
  if (options.ssr) {
85
- const setupSsrTask = await (0, setup_ssr_1.default)(host, {
86
- project: options.projectName,
87
- serverPort: options.devServerPort,
88
- skipFormat: true,
89
- });
90
- 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
+ }
91
99
  const setupSsrForHostTask = await (0, setup_ssr_for_host_1.setupSsrForHost)(host, options, options.projectName, remotesWithPorts);
92
100
  tasks.push(setupSsrForHostTask);
93
101
  const projectConfig = (0, devkit_1.readProjectConfiguration)(host, options.projectName);
94
- if (options.bundler === 'rspack') {
95
- projectConfig.targets.server.executor = '@nx/rspack:rspack';
96
- projectConfig.targets.server.options.rspackConfig = (0, devkit_1.joinPathFragments)(projectConfig.root, `rspack.server.config.${options.typescriptConfiguration ? 'ts' : 'js'}`);
97
- delete projectConfig.targets.server.options.webpackConfig;
98
- }
99
- else {
102
+ if (options.bundler !== 'rspack') {
100
103
  projectConfig.targets.server.options.webpackConfig = (0, devkit_1.joinPathFragments)(projectConfig.root, `webpack.server.config.${options.typescriptConfiguration ? 'ts' : 'js'}`);
101
104
  }
102
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
  }
@@ -4,6 +4,8 @@ exports.reactInitGenerator = reactInitGenerator;
4
4
  const devkit_1 = require("@nx/devkit");
5
5
  const versions_1 = require("../../utils/versions");
6
6
  const version_utils_1 = require("../../utils/version-utils");
7
+ const add_plugin_1 = require("@nx/devkit/src/utils/add-plugin");
8
+ const router_plugin_1 = require("../../plugins/router-plugin");
7
9
  async function reactInitGenerator(tree, schema) {
8
10
  const tasks = [];
9
11
  if (!schema.skipPackageJson) {
@@ -16,6 +18,27 @@ async function reactInitGenerator(tree, schema) {
16
18
  '@nx/react': versions_1.nxVersion,
17
19
  }, undefined, schema.keepExistingVersions));
18
20
  }
21
+ const nxJson = (0, devkit_1.readNxJson)(tree);
22
+ schema.addPlugin ??=
23
+ process.env.NX_ADD_PLUGINS !== 'false' &&
24
+ nxJson.useInferencePlugins !== false;
25
+ if (schema.addPlugin) {
26
+ await (0, add_plugin_1.addPlugin)(tree, await (0, devkit_1.createProjectGraphAsync)(), '@nx/react/router-plugin', router_plugin_1.createNodesV2, {
27
+ buildTargetName: ['build', 'react-router:build', 'react-router-build'],
28
+ devTargetName: ['dev', 'react-router:dev', 'react-router-dev'],
29
+ startTargetName: ['start', 'react-router-serve', 'react-router-start'],
30
+ watchDepsTargetName: [
31
+ 'watch-deps',
32
+ 'react-router:watch-deps',
33
+ 'react-router-watch-deps',
34
+ ],
35
+ buildDepsTargetName: [
36
+ 'build-deps',
37
+ 'react-router:build-deps',
38
+ 'react-router-build-deps',
39
+ ],
40
+ }, schema.updatePackageScripts);
41
+ }
19
42
  if (!schema.skipFormat) {
20
43
  await (0, devkit_1.formatFiles)(tree);
21
44
  }
@@ -2,4 +2,6 @@ export interface InitSchema {
2
2
  skipFormat?: boolean;
3
3
  skipPackageJson?: boolean;
4
4
  keepExistingVersions?: boolean;
5
+ updatePackageScripts?: boolean;
6
+ addPlugin?: boolean;
5
7
  }
@@ -1,4 +1,6 @@
1
1
  import { Tree } from 'nx/src/generators/tree';
2
2
  import { GeneratorCallback } from '@nx/devkit';
3
3
  import { NormalizedSchema } from '../schema';
4
- export declare function addRollupBuildTarget(host: Tree, options: NormalizedSchema): Promise<GeneratorCallback>;
4
+ export declare function addRollupBuildTarget(host: Tree, options: NormalizedSchema & {
5
+ format?: Array<'esm' | 'cjs'>;
6
+ }, external?: Set<String>): Promise<GeneratorCallback>;
@@ -4,7 +4,7 @@ exports.addRollupBuildTarget = addRollupBuildTarget;
4
4
  const devkit_1 = require("@nx/devkit");
5
5
  const maybe_js_1 = require("../../../utils/maybe-js");
6
6
  const versions_1 = require("../../../utils/versions");
7
- async function addRollupBuildTarget(host, options) {
7
+ async function addRollupBuildTarget(host, options, external = new Set(['react', 'react-dom'])) {
8
8
  const tasks = [];
9
9
  const { configurationGenerator } = (0, devkit_1.ensurePackage)('@nx/rollup', versions_1.nxVersion);
10
10
  tasks.push(await configurationGenerator(host, {
@@ -19,12 +19,11 @@ async function addRollupBuildTarget(host, options) {
19
19
  '@svgr/rollup': versions_1.svgrRollupVersion,
20
20
  }));
21
21
  }
22
- const external = ['react', 'react-dom'];
23
22
  if (options.style === '@emotion/styled') {
24
- external.push('@emotion/react/jsx-runtime');
23
+ external.add('@emotion/react/jsx-runtime');
25
24
  }
26
25
  else {
27
- external.push('react/jsx-runtime');
26
+ external.add('react/jsx-runtime');
28
27
  }
29
28
  const nxJson = (0, devkit_1.readNxJson)(host);
30
29
  const hasRollupPlugin = !!nxJson.plugins?.some((p) => typeof p === 'string'
@@ -44,8 +43,8 @@ module.exports = withNx(
44
43
  : (0, devkit_1.joinPathFragments)((0, devkit_1.offsetFromRoot)(options.projectRoot), 'dist', options.projectRoot)}',
45
44
  tsConfig: './tsconfig.lib.json',
46
45
  compiler: '${options.compiler ?? 'babel'}',
47
- external: ${JSON.stringify(external)},
48
- format: ['esm'],
46
+ external: ${JSON.stringify(Array.from(external))},
47
+ format: ${JSON.stringify(options.format ?? ['esm'])},
49
48
  assets:[{ input: '.', output: '.', glob: 'README.md'}],
50
49
  }, {
51
50
  // Provide additional rollup configuration here. See: https://rollupjs.org/configuration-options
@@ -74,7 +73,7 @@ module.exports = withNx(
74
73
  tsConfig: `${options.projectRoot}/tsconfig.lib.json`,
75
74
  project: `${options.projectRoot}/package.json`,
76
75
  entryFile: (0, maybe_js_1.maybeJs)(options, `${options.projectRoot}/src/index.ts`),
77
- external,
76
+ external: Array.from(external),
78
77
  rollupConfig: `@nx/react/plugins/bundle-rollup`,
79
78
  compiler: options.compiler ?? 'babel',
80
79
  assets: [
@@ -43,7 +43,8 @@ function createFiles(host, options) {
43
43
  });
44
44
  }
45
45
  if ((options.publishable || options.buildable) &&
46
- !options.isUsingTsSolutionConfig) {
46
+ !options.isUsingTsSolutionConfig &&
47
+ options.useProjectJson) {
47
48
  if (options.bundler === 'vite') {
48
49
  (0, devkit_1.writeJson)(host, `${options.projectRoot}/package.json`, {
49
50
  name: options.importPath,
@@ -7,7 +7,7 @@ const assertion_1 = require("../../../utils/assertion");
7
7
  const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup");
8
8
  async function normalizeOptions(host, options) {
9
9
  const isUsingTsSolutionConfig = (0, ts_solution_setup_1.isUsingTsSolutionSetup)(host);
10
- await (0, project_name_and_root_utils_1.ensureProjectName)(host, options, 'library');
10
+ await (0, project_name_and_root_utils_1.ensureRootProjectName)(options, 'library');
11
11
  const { projectName, names: projectNames, projectRoot, importPath, } = await (0, project_name_and_root_utils_1.determineProjectNameAndRootOptions)(host, {
12
12
  name: options.name,
13
13
  projectType: 'library',
@@ -41,23 +41,26 @@ async function normalizeOptions(host, options) {
41
41
  bundler,
42
42
  fileName,
43
43
  routePath: `/${projectNames.projectSimpleName}`,
44
- name: isUsingTsSolutionConfig ? importPath : projectName,
44
+ name: isUsingTsSolutionConfig && !options.name ? importPath : projectName,
45
45
  projectRoot,
46
46
  parsedTags,
47
47
  importPath,
48
+ useProjectJson: options.useProjectJson ?? !isUsingTsSolutionConfig,
48
49
  };
49
50
  // Libraries with a bundler or is publishable must also be buildable.
50
51
  normalized.buildable = Boolean(normalized.bundler !== 'none' || options.buildable || options.publishable);
51
52
  normalized.inSourceTests === normalized.minimal || normalized.inSourceTests;
52
53
  if (options.appProject) {
53
54
  const appProjectConfig = (0, devkit_1.getProjects)(host).get(options.appProject);
54
- if (appProjectConfig.projectType !== 'application') {
55
- throw new Error(`appProject expected type of "application" but got "${appProjectConfig.projectType}"`);
55
+ const appProjectType = (0, ts_solution_setup_1.getProjectType)(host, appProjectConfig.root, appProjectConfig.projectType);
56
+ if (appProjectType !== 'application') {
57
+ throw new Error(`appProject expected type of "application" but got "${appProjectType}"`);
56
58
  }
59
+ const appSourceRoot = (0, ts_solution_setup_1.getProjectSourceRoot)(host, appProjectConfig.sourceRoot, appProjectConfig.root);
57
60
  normalized.appMain =
58
61
  appProjectConfig.targets.build?.options?.main ??
59
62
  findMainEntry(host, appProjectConfig.root);
60
- normalized.appSourceRoot = (0, devkit_1.normalizePath)(appProjectConfig.sourceRoot);
63
+ normalized.appSourceRoot = (0, devkit_1.normalizePath)(appSourceRoot);
61
64
  // TODO(jack): We should use appEntryFile instead of appProject so users can directly set it rather than us inferring it.
62
65
  if (!normalized.appMain) {
63
66
  throw new Error(`Could not locate project main for ${options.appProject}`);