Skip to content

Commit 3b50184

Browse files
Coly010jaysoo
authored andcommitted
fix(react): setup mf env var as input for rspack (#29584)
## Current Behavior The Module Federation `NxRuntimeLibraryControlPlugin` relies on an environment variable that is set during the build/serve process. This could potentially lead to issues with cache restoration. The impact should be minimal as the runtime control plugin should only be added to the module federation config when the env var is set. It should only be set by the `module-federation-dev-server` executor which is invoked during serve - a non-cacheable task already. ## Expected Behavior Ensure the env var is set as an input to ensure maximum accuracy with module federation builds via rspack executor
1 parent 028ba03 commit 3b50184

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

packages/react/migrations.json

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
"version": "20.3.0-beta.2",
4848
"description": "If workspace includes Module Federation projects, ensure the new @nx/module-federation package is installed.",
4949
"factory": "./src/migrations/update-20-3-0/ensure-nx-module-federation-package"
50+
},
51+
"add-mf-env-var-to-target-defaults": {
52+
"cli": "nx",
53+
"version": "20.4.0-beta.0",
54+
"description": "Add NX_MF_DEV_REMOTES to inputs for task hashing when '@nx/webpack:webpack' or '@nx/rspack:rspack' is used for Module Federation.",
55+
"factory": "./src/migrations/update-18-0-0/add-mf-env-var-to-target-defaults"
5056
}
5157
},
5258
"packageJsonUpdates": {

packages/react/src/generators/host/host.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export async function hostGenerator(
146146
);
147147
}
148148

149-
addMfEnvToTargetDefaultInputs(host);
149+
addMfEnvToTargetDefaultInputs(host, options.bundler);
150150

151151
const installTask = addDependenciesToPackageJson(
152152
host,

packages/react/src/generators/remote/remote.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export async function remoteGenerator(host: Tree, schema: Schema) {
218218
);
219219
}
220220

221-
addMfEnvToTargetDefaultInputs(host);
221+
addMfEnvToTargetDefaultInputs(host, options.bundler);
222222

223223
const installTask = addDependenciesToPackageJson(
224224
host,

packages/react/src/migrations/update-18-0-0/add-mf-env-var-to-target-defaults.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import {
88
import { addMfEnvToTargetDefaultInputs } from '../../utils/add-mf-env-to-inputs';
99

1010
export default async function (tree: Tree) {
11-
if (!hasModuleFederationProject(tree)) {
11+
const bundler = hasModuleFederationProject(tree);
12+
if (!bundler) {
1213
return;
1314
}
14-
addMfEnvToTargetDefaultInputs(tree);
15+
addMfEnvToTargetDefaultInputs(tree, bundler);
1516

1617
await formatFiles(tree);
1718
}
@@ -22,15 +23,18 @@ function hasModuleFederationProject(tree: Tree) {
2223
const targets = project.targets || {};
2324
for (const [_, target] of Object.entries(targets)) {
2425
if (
25-
target.executor === '@nx/webpack:webpack' &&
26-
(tree.exists(
26+
tree.exists(
2727
joinPathFragments(project.root, 'module-federation.config.ts')
2828
) ||
29-
tree.exists(
30-
joinPathFragments(project.root, 'module-federation.config.js')
31-
))
29+
tree.exists(
30+
joinPathFragments(project.root, 'module-federation.config.js')
31+
)
3232
) {
33-
return true;
33+
if (target.executor === '@nx/webpack:webpack') {
34+
return 'webpack';
35+
} else if (target.executor === '@nx/rspack:rspack') {
36+
return 'rspack';
37+
}
3438
}
3539
}
3640
}
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
import { type Tree, readNxJson, updateNxJson } from '@nx/devkit';
22

3-
export function addMfEnvToTargetDefaultInputs(tree: Tree) {
3+
export function addMfEnvToTargetDefaultInputs(
4+
tree: Tree,
5+
bundler: 'rspack' | 'webpack'
6+
) {
47
const nxJson = readNxJson(tree);
5-
const webpackExecutor = '@nx/webpack:webpack';
8+
const executor =
9+
bundler === 'rspack' ? '@nx/rspack:rspack' : '@nx/webpack:webpack';
610
const mfEnvVar = 'NX_MF_DEV_REMOTES';
711

812
nxJson.targetDefaults ??= {};
9-
nxJson.targetDefaults[webpackExecutor] ??= {};
10-
nxJson.targetDefaults[webpackExecutor].inputs ??= [
11-
'production',
12-
'^production',
13-
];
14-
nxJson.targetDefaults[webpackExecutor].dependsOn ??= ['^build'];
13+
nxJson.targetDefaults[executor] ??= {};
14+
nxJson.targetDefaults[executor].inputs ??= ['production', '^production'];
15+
nxJson.targetDefaults[executor].dependsOn ??= ['^build'];
1516

1617
let mfEnvVarExists = false;
17-
for (const input of nxJson.targetDefaults[webpackExecutor].inputs) {
18+
for (const input of nxJson.targetDefaults[executor].inputs) {
1819
if (typeof input === 'object' && input['env'] === mfEnvVar) {
1920
mfEnvVarExists = true;
2021
break;
2122
}
2223
}
2324
if (!mfEnvVarExists) {
24-
nxJson.targetDefaults[webpackExecutor].inputs.push({ env: mfEnvVar });
25+
nxJson.targetDefaults[executor].inputs.push({ env: mfEnvVar });
2526
}
26-
nxJson.targetDefaults[webpackExecutor].cache = true;
27+
nxJson.targetDefaults[executor].cache = true;
2728
updateNxJson(tree, nxJson);
2829
}

0 commit comments

Comments
 (0)