@nx/rspack 20.1.0 → 20.2.0-beta.1

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 (38) hide show
  1. package/package.json +8 -6
  2. package/src/executors/dev-server/dev-server.impl.js +12 -2
  3. package/src/executors/dev-server/lib/get-dev-server-config.js +1 -1
  4. package/src/executors/rspack/lib/normalize-options.d.ts +3 -0
  5. package/src/executors/rspack/lib/normalize-options.js +40 -0
  6. package/src/executors/rspack/rspack.impl.js +7 -3
  7. package/src/plugins/utils/apply-base-config.d.ts +5 -0
  8. package/src/plugins/utils/apply-base-config.js +348 -0
  9. package/src/plugins/utils/apply-react-config.d.ts +5 -0
  10. package/src/plugins/utils/apply-react-config.js +45 -0
  11. package/src/plugins/utils/apply-web-config.js +44 -45
  12. package/src/plugins/utils/get-terser-ecma-version.d.ts +1 -0
  13. package/src/plugins/utils/get-terser-ecma-version.js +35 -0
  14. package/src/plugins/utils/hash-format.js +7 -1
  15. package/src/plugins/utils/loaders/stylesheet-loaders.js +1 -1
  16. package/src/plugins/utils/models.d.ts +9 -8
  17. package/src/plugins/utils/plugins/generate-package-json-plugin.d.ts +15 -0
  18. package/src/plugins/utils/plugins/generate-package-json-plugin.js +45 -0
  19. package/src/plugins/utils/plugins/normalize-options.d.ts +4 -0
  20. package/src/plugins/utils/plugins/normalize-options.js +170 -0
  21. package/src/plugins/utils/plugins/nx-tsconfig-paths-rspack-plugin.d.ts +10 -0
  22. package/src/plugins/utils/plugins/nx-tsconfig-paths-rspack-plugin.js +62 -0
  23. package/src/plugins/utils/plugins/rspack-nx-build-coordination-plugin.d.ts +11 -0
  24. package/src/plugins/utils/plugins/rspack-nx-build-coordination-plugin.js +94 -0
  25. package/src/plugins/utils/plugins/stats-json-plugin.d.ts +4 -0
  26. package/src/plugins/utils/plugins/stats-json-plugin.js +13 -0
  27. package/src/utils/config.js +3 -2
  28. package/src/utils/create-compiler.js +25 -4
  29. package/src/utils/get-copy-patterns.js +1 -1
  30. package/src/utils/module-federation/share.js +2 -2
  31. package/src/utils/module-federation/with-module-federation/utils.js +1 -1
  32. package/src/utils/normalize-assets.js +1 -1
  33. package/src/utils/with-nx.d.ts +8 -3
  34. package/src/utils/with-nx.js +26 -184
  35. package/src/utils/with-react.d.ts +6 -1
  36. package/src/utils/with-react.js +3 -57
  37. package/src/utils/with-web.d.ts +0 -1
  38. package/src/utils/with-web.js +2 -2
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nx/rspack",
3
3
  "description": "The Nx Plugin for Rspack contains executors and generators that support building applications using Rspack.",
4
- "version": "20.1.0",
4
+ "version": "20.2.0-beta.1",
5
5
  "type": "commonjs",
6
6
  "repository": {
7
7
  "type": "git",
@@ -24,31 +24,33 @@
24
24
  "generators": "./generators.json",
25
25
  "executors": "./executors.json",
26
26
  "dependencies": {
27
- "@nx/js": "20.1.0",
28
- "@nx/devkit": "20.1.0",
29
- "@nx/web": "20.1.0",
27
+ "@nx/js": "20.2.0-beta.1",
28
+ "@nx/devkit": "20.2.0-beta.1",
29
+ "@nx/web": "20.2.0-beta.1",
30
30
  "@phenomnomnominal/tsquery": "~5.0.1",
31
31
  "@rspack/core": "^1.0.4",
32
32
  "@rspack/dev-server": "^1.0.4",
33
33
  "@rspack/plugin-react-refresh": "^1.0.0",
34
34
  "autoprefixer": "^10.4.9",
35
+ "browserslist": "^4.21.4",
35
36
  "chalk": "~4.1.0",
36
37
  "css-loader": "^6.4.0",
37
38
  "enquirer": "~2.3.6",
38
39
  "express": "^4.19.2",
40
+ "fork-ts-checker-webpack-plugin": "7.2.13",
39
41
  "http-proxy-middleware": "^3.0.3",
40
42
  "less-loader": "11.1.0",
41
43
  "license-webpack-plugin": "^4.0.2",
42
44
  "loader-utils": "^2.0.3",
43
45
  "sass": "^1.42.1",
44
46
  "sass-loader": "^12.2.0",
47
+ "source-map-loader": "^5.0.0",
45
48
  "style-loader": "^3.3.0",
46
49
  "postcss-import": "~14.1.0",
47
50
  "postcss-loader": "^8.1.1",
48
51
  "postcss": "^8.4.38",
49
- "tsconfig-paths": "^4.1.2",
50
52
  "tslib": "^2.3.0",
51
- "webpack-subresource-integrity": "^5.1.0"
53
+ "webpack-node-externals": "^3.0.0"
52
54
  },
53
55
  "peerDependencies": {
54
56
  "@module-federation/enhanced": "~0.6.0",
@@ -7,6 +7,7 @@ const dev_server_1 = require("@rspack/dev-server");
7
7
  const create_compiler_1 = require("../../utils/create-compiler");
8
8
  const mode_utils_1 = require("../../utils/mode-utils");
9
9
  const get_dev_server_config_1 = require("./lib/get-dev-server-config");
10
+ const normalize_options_1 = require("../rspack/lib/normalize-options");
10
11
  async function* runExecutor(options, context) {
11
12
  process.env.NODE_ENV ??= options.mode ?? 'development';
12
13
  if ((0, mode_utils_1.isMode)(process.env.NODE_ENV)) {
@@ -14,8 +15,17 @@ async function* runExecutor(options, context) {
14
15
  }
15
16
  const buildTarget = (0, devkit_1.parseTargetString)(options.buildTarget, context.projectGraph);
16
17
  const buildOptions = (0, devkit_1.readTargetOptions)(buildTarget, context);
17
- let devServerConfig = (0, get_dev_server_config_1.getDevServerOptions)(context.root, options, buildOptions);
18
- const compiler = await (0, create_compiler_1.createCompiler)({ ...buildOptions, devServer: devServerConfig, mode: options.mode }, context);
18
+ process.env.NX_BUILD_LIBS_FROM_SOURCE = `${buildOptions.buildLibsFromSource}`;
19
+ process.env.NX_BUILD_TARGET = options.buildTarget;
20
+ const metadata = context.projectsConfigurations.projects[context.projectName];
21
+ const sourceRoot = metadata.sourceRoot;
22
+ const normalizedBuildOptions = (0, normalize_options_1.normalizeOptions)(buildOptions, context.root, metadata.root, sourceRoot);
23
+ let devServerConfig = (0, get_dev_server_config_1.getDevServerOptions)(context.root, options, normalizedBuildOptions);
24
+ const compiler = await (0, create_compiler_1.createCompiler)({
25
+ ...normalizedBuildOptions,
26
+ devServer: devServerConfig,
27
+ mode: options.mode,
28
+ }, context);
19
29
  // Use the first one if it's MultiCompiler
20
30
  // https://webpack.js.org/configuration/dev-server/#root:~:text=Be%20aware%20that%20when%20exporting%20multiple%20configurations%20only%20the%20devServer%20options%20for%20the%20first%20configuration%20will%20be%20taken%20into%20account%20and%20used%20for%20all%20the%20configurations%20in%20the%20array.
21
31
  const firstCompiler = (0, create_compiler_1.isMultiCompiler)(compiler)
@@ -22,7 +22,7 @@ function getDevServerOptions(root, serveOptions, buildOptions) {
22
22
  }
23
23
  const config = {
24
24
  host: serveOptions.host,
25
- port: serveOptions.port,
25
+ port: serveOptions.port ?? 4200,
26
26
  headers: { 'Access-Control-Allow-Origin': '*' },
27
27
  historyApiFallback: {
28
28
  index: buildOptions.index &&
@@ -0,0 +1,3 @@
1
+ import type { RspackExecutorSchema, NormalizedRspackExecutorSchema } from '../schema';
2
+ export declare function normalizeOptions(options: RspackExecutorSchema, root: string, projectRoot: string, sourceRoot: string): NormalizedRspackExecutorSchema;
3
+ export declare function normalizePluginPath(pluginPath: void | string, root: string): string;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalizeOptions = normalizeOptions;
4
+ exports.normalizePluginPath = normalizePluginPath;
5
+ const path_1 = require("path");
6
+ const normalize_options_1 = require("../../../plugins/utils/plugins/normalize-options");
7
+ function normalizeOptions(options, root, projectRoot, sourceRoot) {
8
+ const normalizedOptions = {
9
+ ...options,
10
+ root,
11
+ projectRoot,
12
+ sourceRoot,
13
+ target: options.target ?? 'web',
14
+ outputFileName: options.outputFileName ?? 'main.js',
15
+ rspackConfig: normalizePluginPath(options.rspackConfig, root),
16
+ fileReplacements: (0, normalize_options_1.normalizeFileReplacements)(root, options.fileReplacements),
17
+ optimization: typeof options.optimization !== 'object'
18
+ ? {
19
+ scripts: options.optimization,
20
+ styles: options.optimization,
21
+ }
22
+ : options.optimization,
23
+ };
24
+ if (options.assets) {
25
+ normalizedOptions.assets = (0, normalize_options_1.normalizeAssets)(options.assets, root, sourceRoot, projectRoot, false // executor assets are relative to workspace root for consistency
26
+ );
27
+ }
28
+ return normalizedOptions;
29
+ }
30
+ function normalizePluginPath(pluginPath, root) {
31
+ if (!pluginPath) {
32
+ return '';
33
+ }
34
+ try {
35
+ return require.resolve(pluginPath);
36
+ }
37
+ catch {
38
+ return (0, path_1.resolve)(root, pluginPath);
39
+ }
40
+ }
@@ -9,6 +9,7 @@ const fs_1 = require("fs");
9
9
  const path = tslib_1.__importStar(require("path"));
10
10
  const create_compiler_1 = require("../../utils/create-compiler");
11
11
  const mode_utils_1 = require("../../utils/mode-utils");
12
+ const normalize_options_1 = require("./lib/normalize-options");
12
13
  async function* runExecutor(options, context) {
13
14
  process.env.NODE_ENV ??= options.mode ?? 'production';
14
15
  options.target ??= 'web';
@@ -23,7 +24,10 @@ async function* runExecutor(options, context) {
23
24
  force: true,
24
25
  recursive: true,
25
26
  });
26
- const compiler = await (0, create_compiler_1.createCompiler)(options, context);
27
+ const metadata = context.projectsConfigurations.projects[context.projectName];
28
+ const sourceRoot = metadata.sourceRoot;
29
+ const normalizedOptions = (0, normalize_options_1.normalizeOptions)(options, context.root, metadata.root, sourceRoot);
30
+ const compiler = await (0, create_compiler_1.createCompiler)(normalizedOptions, context);
27
31
  const iterable = (0, async_iterable_1.createAsyncIterable)(async ({ next, done }) => {
28
32
  if (options.watch) {
29
33
  const watcher = compiler.watch({}, async (err, stats) => {
@@ -45,7 +49,7 @@ async function* runExecutor(options, context) {
45
49
  }
46
50
  next({
47
51
  success: !stats.hasErrors(),
48
- outfile: path.resolve(context.root, options.outputPath, 'main.js'),
52
+ outfile: path.resolve(context.root, normalizedOptions.outputPath, 'main.js'),
49
53
  });
50
54
  });
51
55
  registerCleanupCallback(() => {
@@ -75,7 +79,7 @@ async function* runExecutor(options, context) {
75
79
  }
76
80
  next({
77
81
  success: !stats.hasErrors(),
78
- outfile: path.resolve(context.root, options.outputPath, 'main.js'),
82
+ outfile: path.resolve(context.root, normalizedOptions.outputPath, 'main.js'),
79
83
  });
80
84
  done();
81
85
  });
@@ -0,0 +1,5 @@
1
+ import { Configuration, RspackOptionsNormalized } from '@rspack/core';
2
+ import { NormalizedNxAppRspackPluginOptions } from './models';
3
+ export declare function applyBaseConfig(options: NormalizedNxAppRspackPluginOptions, config?: Partial<RspackOptionsNormalized | Configuration>, { useNormalizedEntry, }?: {
4
+ useNormalizedEntry?: boolean;
5
+ }): void;
@@ -0,0 +1,348 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyBaseConfig = applyBaseConfig;
4
+ const tslib_1 = require("tslib");
5
+ const path = tslib_1.__importStar(require("path"));
6
+ const license_webpack_plugin_1 = require("license-webpack-plugin");
7
+ const core_1 = require("@rspack/core");
8
+ const js_1 = require("@nx/js");
9
+ const stats_json_plugin_1 = require("./plugins/stats-json-plugin");
10
+ const generate_package_json_plugin_1 = require("./plugins/generate-package-json-plugin");
11
+ const hash_format_1 = require("./hash-format");
12
+ const nx_tsconfig_paths_rspack_plugin_1 = require("./plugins/nx-tsconfig-paths-rspack-plugin");
13
+ const get_terser_ecma_version_1 = require("./get-terser-ecma-version");
14
+ const nodeExternals = require("webpack-node-externals");
15
+ const IGNORED_RSPACK_WARNINGS = [
16
+ /The comment file/i,
17
+ /could not find any license/i,
18
+ ];
19
+ const extensions = ['...', '.ts', '.tsx', '.mjs', '.js', '.jsx'];
20
+ const mainFields = ['module', 'main'];
21
+ function applyBaseConfig(options, config = {}, { useNormalizedEntry, } = {}) {
22
+ // Defaults that was applied from executor schema previously.
23
+ options.deleteOutputPath ??= true;
24
+ options.externalDependencies ??= 'all';
25
+ options.fileReplacements ??= [];
26
+ options.memoryLimit ??= 2048;
27
+ options.transformers ??= [];
28
+ options.progress ??= true;
29
+ applyNxIndependentConfig(options, config);
30
+ // Some of the options only work during actual tasks, not when reading the rspack config during CreateNodes.
31
+ if (global.NX_GRAPH_CREATION)
32
+ return;
33
+ applyNxDependentConfig(options, config, { useNormalizedEntry });
34
+ }
35
+ function applyNxIndependentConfig(options, config) {
36
+ const isProd = process.env.NODE_ENV === 'production' || options.mode === 'production';
37
+ const hashFormat = (0, hash_format_1.getOutputHashFormat)(options.outputHashing);
38
+ config.context = path.join(options.root, options.projectRoot);
39
+ config.target ??= options.target;
40
+ config.node = false;
41
+ config.mode =
42
+ // When the target is Node avoid any optimizations, such as replacing `process.env.NODE_ENV` with build time value.
43
+ config.target === 'node'
44
+ ? 'none'
45
+ : // Otherwise, make sure it matches `process.env.NODE_ENV`.
46
+ // When mode is development or production, rspack will automatically
47
+ // configure DefinePlugin to replace `process.env.NODE_ENV` with the
48
+ // build-time value. Thus, we need to make sure it's the same value to
49
+ // avoid conflicts.
50
+ //
51
+ // When the NODE_ENV is something else (e.g. test), then set it to none
52
+ // to prevent extra behavior from rspack.
53
+ options.mode ??
54
+ (process.env.NODE_ENV === 'development' ||
55
+ process.env.NODE_ENV === 'production'
56
+ ? process.env.NODE_ENV
57
+ : 'none');
58
+ // When target is Node, the Webpack mode will be set to 'none' which disables in memory caching and causes a full rebuild on every change.
59
+ // So to mitigate this we enable in memory caching when target is Node and in watch mode.
60
+ config.cache = options.target === 'node' && options.watch ? true : undefined;
61
+ config.devtool =
62
+ options.sourceMap === 'hidden'
63
+ ? 'hidden-source-map'
64
+ : options.sourceMap
65
+ ? 'source-map'
66
+ : false;
67
+ config.output = {
68
+ ...(config.output ?? {}),
69
+ libraryTarget: config.output?.libraryTarget ??
70
+ (options.target === 'node' ? 'commonjs' : undefined),
71
+ path: config.output?.path ??
72
+ (options.outputPath
73
+ ? // If path is relative, it is relative from project root (aka cwd).
74
+ // Otherwise, it is relative to workspace root (legacy behavior).
75
+ options.outputPath.startsWith('.')
76
+ ? path.join(options.root, options.projectRoot, options.outputPath)
77
+ : path.join(options.root, options.outputPath)
78
+ : undefined),
79
+ filename: config.output?.filename ??
80
+ (options.outputHashing ? `[name]${hashFormat.script}.js` : '[name].js'),
81
+ chunkFilename: config.output?.chunkFilename ??
82
+ (options.outputHashing ? `[name]${hashFormat.chunk}.js` : '[name].js'),
83
+ hashFunction: config.output?.hashFunction ?? 'xxhash64',
84
+ // Disabled for performance
85
+ pathinfo: config.output?.pathinfo ?? false,
86
+ };
87
+ config.watch = options.watch;
88
+ config.watchOptions = {
89
+ poll: options.poll,
90
+ };
91
+ config.profile = options.statsJson;
92
+ config.performance = {
93
+ ...config.performance,
94
+ hints: false,
95
+ };
96
+ config.ignoreWarnings = [
97
+ (x) => IGNORED_RSPACK_WARNINGS.some((r) => typeof x === 'string' ? r.test(x) : r.test(x.message)),
98
+ ];
99
+ config.optimization = !isProd
100
+ ? undefined
101
+ : {
102
+ ...(config.optimization ?? {}),
103
+ sideEffects: true,
104
+ minimize: typeof options.optimization === 'object'
105
+ ? !!options.optimization.scripts
106
+ : !!options.optimization,
107
+ minimizer: [
108
+ new core_1.SwcJsMinimizerRspackPlugin({
109
+ extractComments: false,
110
+ minimizerOptions: {
111
+ // this needs to be false to allow toplevel variables to be used in the global scope
112
+ // important especially for module-federation which operates as such
113
+ module: false,
114
+ mangle: {
115
+ keep_classnames: true,
116
+ },
117
+ format: {
118
+ ecma: (0, get_terser_ecma_version_1.getTerserEcmaVersion)(path.join(options.root, options.projectRoot)),
119
+ ascii_only: true,
120
+ comments: false,
121
+ webkit: true,
122
+ safari10: true,
123
+ },
124
+ },
125
+ }),
126
+ ],
127
+ runtimeChunk: false,
128
+ concatenateModules: true,
129
+ };
130
+ config.stats = {
131
+ hash: true,
132
+ timings: false,
133
+ cached: false,
134
+ cachedAssets: false,
135
+ modules: false,
136
+ warnings: true,
137
+ errors: true,
138
+ colors: !options.verbose && !options.statsJson,
139
+ chunks: !options.verbose,
140
+ assets: !!options.verbose,
141
+ chunkOrigins: !!options.verbose,
142
+ chunkModules: !!options.verbose,
143
+ children: !!options.verbose,
144
+ reasons: !!options.verbose,
145
+ version: !!options.verbose,
146
+ errorDetails: !!options.verbose,
147
+ moduleTrace: !!options.verbose,
148
+ usedExports: !!options.verbose,
149
+ };
150
+ /**
151
+ * Initialize properties that get set when rspack is used during task execution.
152
+ * These properties may be used by consumers who expect them to not be undefined.
153
+ *
154
+ * When @nx/rspack/plugin resolves the config, it is not during a task, and therefore
155
+ * these values are not set, which can lead to errors being thrown when reading
156
+ * the rspack options from the resolved file.
157
+ */
158
+ config.entry ??= {};
159
+ config.resolve ??= {};
160
+ config.module ??= {};
161
+ config.plugins ??= [];
162
+ config.externals ??= [];
163
+ }
164
+ function applyNxDependentConfig(options, config, { useNormalizedEntry } = {}) {
165
+ const tsConfig = options.tsConfig ?? (0, js_1.getRootTsConfigPath)();
166
+ const plugins = [];
167
+ const executorContext = {
168
+ projectName: options.projectName,
169
+ targetName: options.targetName,
170
+ projectGraph: options.projectGraph,
171
+ configurationName: options.configurationName,
172
+ root: options.root,
173
+ };
174
+ plugins.push(new nx_tsconfig_paths_rspack_plugin_1.NxTsconfigPathsRspackPlugin({ ...options, tsConfig }));
175
+ if (!options?.skipTypeChecking) {
176
+ const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
177
+ plugins.push(new ForkTsCheckerWebpackPlugin({
178
+ typescript: {
179
+ configFile: path.isAbsolute(tsConfig)
180
+ ? tsConfig
181
+ : path.join(options.root, tsConfig),
182
+ memoryLimit: options.memoryLimit || 2018,
183
+ },
184
+ }));
185
+ }
186
+ const entries = [];
187
+ if (options.main) {
188
+ const mainEntry = options.outputFileName
189
+ ? path.parse(options.outputFileName).name
190
+ : 'main';
191
+ entries.push({
192
+ name: mainEntry,
193
+ import: [path.resolve(options.root, options.main)],
194
+ });
195
+ }
196
+ if (options.additionalEntryPoints) {
197
+ for (const { entryName, entryPath } of options.additionalEntryPoints) {
198
+ entries.push({
199
+ name: entryName,
200
+ import: [path.resolve(options.root, entryPath)],
201
+ });
202
+ }
203
+ }
204
+ if (options.polyfills) {
205
+ entries.push({
206
+ name: 'polyfills',
207
+ import: [path.resolve(options.root, options.polyfills)],
208
+ });
209
+ }
210
+ config.entry ??= {};
211
+ entries.forEach((entry) => {
212
+ if (useNormalizedEntry) {
213
+ config.entry[entry.name] = { import: entry.import };
214
+ }
215
+ else {
216
+ config.entry[entry.name] = entry.import;
217
+ }
218
+ });
219
+ if (options.progress) {
220
+ plugins.push(new core_1.ProgressPlugin({ profile: options.verbose }));
221
+ }
222
+ if (options.extractLicenses) {
223
+ plugins.push(new license_webpack_plugin_1.LicenseWebpackPlugin({
224
+ stats: {
225
+ warnings: false,
226
+ errors: false,
227
+ },
228
+ perChunkOutput: false,
229
+ outputFilename: `3rdpartylicenses.txt`,
230
+ }));
231
+ }
232
+ if (Array.isArray(options.assets) && options.assets.length > 0) {
233
+ plugins.push(new core_1.CopyRspackPlugin({
234
+ patterns: options.assets.map((asset) => {
235
+ return {
236
+ context: asset.input,
237
+ // Now we remove starting slash to make Webpack place it from the output root.
238
+ to: asset.output,
239
+ from: asset.glob,
240
+ globOptions: {
241
+ ignore: [
242
+ '.gitkeep',
243
+ '**/.DS_Store',
244
+ '**/Thumbs.db',
245
+ ...(asset.ignore ?? []),
246
+ ],
247
+ dot: true,
248
+ },
249
+ };
250
+ }),
251
+ }));
252
+ }
253
+ if (options.generatePackageJson && executorContext) {
254
+ plugins.push(new generate_package_json_plugin_1.GeneratePackageJsonPlugin({ ...options, tsConfig }));
255
+ }
256
+ if (options.statsJson) {
257
+ plugins.push(new stats_json_plugin_1.StatsJsonPlugin());
258
+ }
259
+ const externals = [];
260
+ if (options.target === 'node' && options.externalDependencies === 'all') {
261
+ const modulesDir = `${options.root}/node_modules`;
262
+ externals.push(nodeExternals({ modulesDir }));
263
+ }
264
+ else if (Array.isArray(options.externalDependencies)) {
265
+ externals.push(function (ctx, callback) {
266
+ if (options.externalDependencies.includes(ctx.request)) {
267
+ // not bundled
268
+ return callback(null, `commonjs ${ctx.request}`);
269
+ }
270
+ // bundled
271
+ callback();
272
+ });
273
+ }
274
+ config.resolve = {
275
+ ...config.resolve,
276
+ extensions: [...(config?.resolve?.extensions ?? []), ...extensions],
277
+ alias: {
278
+ ...(config.resolve?.alias ?? {}),
279
+ ...(options.fileReplacements?.reduce((aliases, replacement) => ({
280
+ ...aliases,
281
+ [replacement.replace]: replacement.with,
282
+ }), {}) ?? {}),
283
+ },
284
+ mainFields: config.resolve?.mainFields ?? mainFields,
285
+ };
286
+ config.externals = externals;
287
+ // Enabled for performance
288
+ config.cache = true;
289
+ config.module = {
290
+ ...config.module,
291
+ rules: [
292
+ ...(config?.module?.rules ?? []),
293
+ options.sourceMap && {
294
+ test: /\.js$/,
295
+ enforce: 'pre',
296
+ loader: require.resolve('source-map-loader'),
297
+ },
298
+ {
299
+ // There's an issue resolving paths without fully specified extensions
300
+ // See: https://github.com/graphql/graphql-js/issues/2721
301
+ // TODO(jack): Add a flag to turn this option on like Next.js does via experimental flag.
302
+ // See: https://github.com/vercel/next.js/pull/29880
303
+ test: /\.m?jsx?$/,
304
+ resolve: {
305
+ fullySpecified: false,
306
+ },
307
+ },
308
+ // There's an issue when using buildable libs and .js files (instead of .ts files),
309
+ // where the wrong type is used (commonjs vs esm) resulting in export-imports throwing errors.
310
+ // See: https://github.com/nrwl/nx/issues/10990
311
+ {
312
+ test: /\.js$/,
313
+ type: 'javascript/auto',
314
+ },
315
+ // Rspack's docs only suggest swc for TS compilation
316
+ //https://rspack.dev/guide/tech/typescript
317
+ {
318
+ test: /\.([jt])sx?$/,
319
+ loader: 'builtin:swc-loader',
320
+ exclude: /node_modules/,
321
+ type: 'javascript/auto',
322
+ options: {
323
+ jsc: {
324
+ parser: {
325
+ syntax: 'typescript',
326
+ decorators: true,
327
+ tsx: true,
328
+ },
329
+ transform: {
330
+ react: {
331
+ runtime: 'automatic',
332
+ pragma: 'React.createElement',
333
+ pragmaFrag: 'React.Fragment',
334
+ throwIfNamespace: true,
335
+ // Config.mode is already set based on options.mode and `process.env.NODE_ENV`
336
+ development: config.mode === 'development',
337
+ refresh: config.mode === 'development',
338
+ useBuiltins: false,
339
+ },
340
+ },
341
+ },
342
+ },
343
+ },
344
+ ].filter((r) => !!r),
345
+ };
346
+ config.plugins ??= [];
347
+ config.plugins.push(...plugins);
348
+ }
@@ -0,0 +1,5 @@
1
+ import { Configuration, RspackOptionsNormalized } from '@rspack/core';
2
+ import { SvgrOptions } from './models';
3
+ export declare function applyReactConfig(options: {
4
+ svgr?: boolean | SvgrOptions;
5
+ }, config?: Partial<RspackOptionsNormalized | Configuration>): void;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyReactConfig = applyReactConfig;
4
+ function applyReactConfig(options, config = {}) {
5
+ if (global.NX_GRAPH_CREATION)
6
+ return;
7
+ addHotReload(config);
8
+ if (options.svgr !== false || typeof options.svgr === 'object') {
9
+ removeSvgLoaderIfPresent(config);
10
+ const defaultSvgrOptions = {
11
+ svgo: false,
12
+ titleProp: true,
13
+ ref: true,
14
+ };
15
+ const svgrOptions = typeof options.svgr === 'object' ? options.svgr : defaultSvgrOptions;
16
+ config.module.rules.push({
17
+ test: /\.svg$/i,
18
+ type: 'asset',
19
+ resourceQuery: /react/, // *.svg?react
20
+ }, {
21
+ test: /\.svg$/i,
22
+ issuer: /\.[jt]sx?$/,
23
+ resourceQuery: { not: [/react/] }, // exclude react component if *.svg?react
24
+ use: [{ loader: '@svgr/webpack', options: svgrOptions }],
25
+ });
26
+ }
27
+ // enable rspack node api
28
+ config.node = {
29
+ __dirname: true,
30
+ __filename: true,
31
+ };
32
+ }
33
+ function removeSvgLoaderIfPresent(config) {
34
+ const svgLoaderIdx = config.module.rules.findIndex((rule) => typeof rule === 'object' && rule.test.toString().includes('svg'));
35
+ if (svgLoaderIdx === -1)
36
+ return;
37
+ config.module.rules.splice(svgLoaderIdx, 1);
38
+ }
39
+ function addHotReload(config) {
40
+ const ReactRefreshPlugin = require('@rspack/plugin-react-refresh');
41
+ const isDev = process.env.NODE_ENV === 'development' || config.mode === 'development';
42
+ if (isDev) {
43
+ config.plugins.push(new ReactRefreshPlugin({ overlay: false }));
44
+ }
45
+ }