@nx/rspack 21.2.1 → 21.2.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.
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": "21.2.1",
4
+ "version": "21.2.2",
5
5
  "type": "commonjs",
6
6
  "repository": {
7
7
  "type": "git",
@@ -24,10 +24,10 @@
24
24
  "generators": "./generators.json",
25
25
  "executors": "./executors.json",
26
26
  "dependencies": {
27
- "@nx/js": "21.2.1",
28
- "@nx/devkit": "21.2.1",
29
- "@nx/web": "21.2.1",
30
- "@nx/module-federation": "21.2.1",
27
+ "@nx/js": "21.2.2",
28
+ "@nx/devkit": "21.2.2",
29
+ "@nx/web": "21.2.2",
30
+ "@nx/module-federation": "21.2.2",
31
31
  "@phenomnomnominal/tsquery": "~5.0.1",
32
32
  "@rspack/core": "^1.3.8",
33
33
  "@rspack/dev-server": "^1.1.1",
@@ -57,7 +57,7 @@
57
57
  "webpack-node-externals": "^3.0.0"
58
58
  },
59
59
  "peerDependencies": {
60
- "@module-federation/enhanced": "^0.9.0",
60
+ "@module-federation/enhanced": "^0.15.0",
61
61
  "@module-federation/node": "^2.6.26"
62
62
  },
63
63
  "nx-migrations": {
@@ -3,18 +3,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.transformCjsConfigFile = transformCjsConfigFile;
4
4
  const tsquery_1 = require("@phenomnomnominal/tsquery");
5
5
  function transformCjsConfigFile(tree, configPath) {
6
+ const configContents = tree.read(configPath, 'utf-8');
7
+ const usesJsExtensions = detectJsExtensions(configContents);
6
8
  ['@nx', '@nrwl'].forEach((scope) => {
7
9
  transformComposePlugins(tree, configPath, scope);
8
10
  transformWithNx(tree, configPath, scope);
9
11
  transformWithWeb(tree, configPath, scope);
10
12
  transformWithReact(tree, configPath, scope);
11
13
  transformModuleFederationConfig(tree, configPath, scope);
12
- transformWithModuleFederation(tree, configPath, scope);
13
- transformWithModuleFederationSSR(tree, configPath, scope);
14
+ transformWithModuleFederation(tree, configPath, scope, usesJsExtensions);
15
+ transformWithModuleFederationSSR(tree, configPath, scope, usesJsExtensions);
14
16
  });
15
17
  // Add useLegacyHtmlPlugin: true to withWeb() calls
16
18
  transformWithWebCalls(tree, configPath);
17
19
  }
20
+ function detectJsExtensions(configContents) {
21
+ // Check if any require calls use .js extensions
22
+ const requireWithJsExtensionRegex = /require\s*\(\s*['"]@nx\/[^'"]*\.js['"]\s*\)/;
23
+ return requireWithJsExtensionRegex.test(configContents);
24
+ }
18
25
  function transformWithWebCalls(tree, configPath) {
19
26
  const configContents = tree.read(configPath, 'utf-8');
20
27
  const ast = tsquery_1.tsquery.ast(configContents);
@@ -204,10 +211,10 @@ function transformModuleFederationConfig(tree, configPath, scope) {
204
211
  ${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
205
212
  tree.write(configPath, newContents);
206
213
  }
207
- function transformWithModuleFederation(tree, configPath, scope) {
214
+ function transformWithModuleFederation(tree, configPath, scope, usesJsExtensions) {
208
215
  const configContents = tree.read(configPath, 'utf-8');
209
216
  const ast = tsquery_1.tsquery.ast(configContents);
210
- const HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT = `VariableDeclaration:has(Identifier[name=withModuleFederation]) > CallExpression:has(Identifier[name=require]) StringLiteral[value=${scope}/module-federation/webpack]`;
217
+ const HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT = `VariableDeclaration:has(Identifier[name=withModuleFederation]) > CallExpression:has(Identifier[name=require]) StringLiteral[value="${scope}/module-federation/webpack${usesJsExtensions ? '.js' : ''}"]`;
211
218
  const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT);
212
219
  if (nodes.length === 0) {
213
220
  return;
@@ -222,14 +229,17 @@ function transformWithModuleFederation(tree, configPath, scope) {
222
229
  if (configContents.charAt(endIndex) === ',') {
223
230
  endIndex++;
224
231
  }
225
- const newContents = `const { withModuleFederation } = require('@nx/module-federation/rspack');
232
+ const rspackImport = usesJsExtensions
233
+ ? '@nx/module-federation/rspack.js'
234
+ : '@nx/module-federation/rspack';
235
+ const newContents = `const { withModuleFederation } = require('${rspackImport}');
226
236
  ${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
227
237
  tree.write(configPath, newContents);
228
238
  }
229
- function transformWithModuleFederationSSR(tree, configPath, scope) {
239
+ function transformWithModuleFederationSSR(tree, configPath, scope, usesJsExtensions) {
230
240
  const configContents = tree.read(configPath, 'utf-8');
231
241
  const ast = tsquery_1.tsquery.ast(configContents);
232
- const HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT = `VariableDeclaration:has(Identifier[name=withModuleFederationForSSR]) > CallExpression:has(Identifier[name=require]) StringLiteral[value=${scope}/module-federation/webpack]`;
242
+ const HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT = `VariableDeclaration:has(Identifier[name=withModuleFederationForSSR]) > CallExpression:has(Identifier[name=require]) StringLiteral[value="${scope}/module-federation/webpack${usesJsExtensions ? '.js' : ''}"]`;
233
243
  const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT);
234
244
  if (nodes.length === 0) {
235
245
  return;
@@ -244,7 +254,10 @@ function transformWithModuleFederationSSR(tree, configPath, scope) {
244
254
  if (configContents.charAt(endIndex) === ',') {
245
255
  endIndex++;
246
256
  }
247
- const newContents = `const { withModuleFederationForSSR } = require('@nx/module-federation/rspack');
257
+ const rspackImport = usesJsExtensions
258
+ ? '@nx/module-federation/rspack.js'
259
+ : '@nx/module-federation/rspack';
260
+ const newContents = `const { withModuleFederationForSSR } = require('${rspackImport}');
248
261
  ${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
249
262
  tree.write(configPath, newContents);
250
263
  }
@@ -3,18 +3,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.transformEsmConfigFile = transformEsmConfigFile;
4
4
  const tsquery_1 = require("@phenomnomnominal/tsquery");
5
5
  function transformEsmConfigFile(tree, configPath) {
6
+ const configContents = tree.read(configPath, 'utf-8');
7
+ const usesJsExtensions = detectJsExtensions(configContents);
6
8
  ['@nx', '@nrwl'].forEach((scope) => {
7
9
  transformComposePlugins(tree, configPath, scope);
8
10
  transformWithNx(tree, configPath, scope);
9
11
  transformWithWeb(tree, configPath, scope);
10
12
  transformWithReact(tree, configPath, scope);
11
13
  transformModuleFederationConfig(tree, configPath, scope);
12
- transformWithModuleFederation(tree, configPath, scope);
13
- transformWithModuleFederationSSR(tree, configPath, scope);
14
+ transformWithModuleFederation(tree, configPath, scope, usesJsExtensions);
15
+ transformWithModuleFederationSSR(tree, configPath, scope, usesJsExtensions);
14
16
  });
15
17
  // Add useLegacyHtmlPlugin: true to withWeb() calls
16
18
  transformWithWebCalls(tree, configPath);
17
19
  }
20
+ function detectJsExtensions(configContents) {
21
+ // Check if any imports use .js extensions
22
+ const importWithJsExtensionRegex = /from\s+['"]@nx\/[^'"]*\.js['"]/;
23
+ return importWithJsExtensionRegex.test(configContents);
24
+ }
18
25
  function transformWithWebCalls(tree, configPath) {
19
26
  const configContents = tree.read(configPath, 'utf-8');
20
27
  const ast = tsquery_1.tsquery.ast(configContents);
@@ -182,10 +189,10 @@ function transformWithReact(tree, configPath, scope) {
182
189
  ${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
183
190
  tree.write(configPath, newContents);
184
191
  }
185
- function transformWithModuleFederation(tree, configPath, scope) {
192
+ function transformWithModuleFederation(tree, configPath, scope, usesJsExtension) {
186
193
  const configContents = tree.read(configPath, 'utf-8');
187
194
  const ast = tsquery_1.tsquery.ast(configContents);
188
- const HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT = `ImportDeclaration:has(Identifier[name=withModuleFederation]) > StringLiteral[value=${scope}/module-federation/webpack]`;
195
+ const HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT = `ImportDeclaration:has(Identifier[name=withModuleFederation]) > StringLiteral[value="${scope}/module-federation/webpack${usesJsExtension ? '.js' : ''}"]`;
189
196
  const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT);
190
197
  if (nodes.length === 0) {
191
198
  return;
@@ -200,7 +207,10 @@ function transformWithModuleFederation(tree, configPath, scope) {
200
207
  if (configContents.charAt(endIndex) === ',') {
201
208
  endIndex++;
202
209
  }
203
- const newContents = `import { withModuleFederation } from '@nx/module-federation/rspack';
210
+ const moduleFederationImport = usesJsExtension
211
+ ? '@nx/module-federation/rspack.js'
212
+ : '@nx/module-federation/rspack';
213
+ const newContents = `import { withModuleFederation } from '${moduleFederationImport}';
204
214
  ${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
205
215
  tree.write(configPath, newContents);
206
216
  }
@@ -226,10 +236,10 @@ function transformModuleFederationConfig(tree, configPath, scope) {
226
236
  ${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
227
237
  tree.write(configPath, newContents);
228
238
  }
229
- function transformWithModuleFederationSSR(tree, configPath, scope) {
239
+ function transformWithModuleFederationSSR(tree, configPath, scope, usesJsExtensions) {
230
240
  const configContents = tree.read(configPath, 'utf-8');
231
241
  const ast = tsquery_1.tsquery.ast(configContents);
232
- const HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT = `ImportDeclaration:has(Identifier[name=withModuleFederationForSSR]) > StringLiteral[value=${scope}/module-federation/webpack]`;
242
+ const HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT = `ImportDeclaration:has(Identifier[name=withModuleFederationForSSR]) > StringLiteral[value="${scope}/module-federation/webpack${usesJsExtensions ? '.js' : ''}"]`;
233
243
  const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT);
234
244
  if (nodes.length === 0) {
235
245
  return;
@@ -244,7 +254,10 @@ function transformWithModuleFederationSSR(tree, configPath, scope) {
244
254
  if (configContents.charAt(endIndex) === ',') {
245
255
  endIndex++;
246
256
  }
247
- const newContents = `import { withModuleFederationForSSR } from '@nx/module-federation/rspack';
257
+ const rspackImport = usesJsExtensions
258
+ ? '@nx/module-federation/rspack.js'
259
+ : '@nx/module-federation/rspack';
260
+ const newContents = `import { withModuleFederationForSSR } from '${rspackImport}';
248
261
  ${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
249
262
  tree.write(configPath, newContents);
250
263
  }
@@ -76,11 +76,28 @@ function applyNxIndependentConfig(options, config) {
76
76
  options.sourceMap === true ? 'source-map' : options.sourceMap;
77
77
  config.output = {
78
78
  ...(config.output ?? {}),
79
- libraryTarget: options.target === 'node'
80
- ? 'commonjs'
81
- : options.target === 'async-node'
82
- ? 'commonjs-module'
83
- : undefined,
79
+ libraryTarget: (() => {
80
+ const existingOutputConfig = config.output;
81
+ const existingLibraryTarget = existingOutputConfig?.libraryTarget;
82
+ const existingLibraryType = typeof existingOutputConfig?.library === 'object' &&
83
+ 'type' in existingOutputConfig?.library
84
+ ? existingOutputConfig?.library?.type
85
+ : undefined;
86
+ // If user is using modern library.type, don't set the deprecated libraryTarget
87
+ if (existingLibraryType !== undefined) {
88
+ return undefined;
89
+ }
90
+ // If user has set libraryTarget explicitly, use it
91
+ if (existingLibraryTarget !== undefined) {
92
+ return existingLibraryTarget;
93
+ }
94
+ // Set defaults based on target when user hasn't configured anything
95
+ if (options.target === 'node')
96
+ return 'commonjs';
97
+ if (options.target === 'async-node')
98
+ return 'commonjs-module';
99
+ return undefined;
100
+ })(),
84
101
  path: config.output?.path ??
85
102
  (options.outputPath
86
103
  ? // If path is relative, it is relative from project root (aka cwd).