Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react): setup mf env var as input for rspack #29584

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/react/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
"version": "20.3.0-beta.2",
"description": "If workspace includes Module Federation projects, ensure the new @nx/module-federation package is installed.",
"factory": "./src/migrations/update-20-3-0/ensure-nx-module-federation-package"
},
"add-mf-env-var-to-target-defaults": {
"cli": "nx",
"version": "20.4.0-beta.0",
"description": "Add NX_MF_DEV_REMOTES to inputs for task hashing when '@nx/webpack:webpack' or '@nx/rspack:rspack' is used for Module Federation.",
"factory": "./src/migrations/update-18-0-0/add-mf-env-var-to-target-defaults"
}
},
"packageJsonUpdates": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/generators/host/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export async function hostGenerator(
);
}

addMfEnvToTargetDefaultInputs(host);
addMfEnvToTargetDefaultInputs(host, options.bundler);

const installTask = addDependenciesToPackageJson(
host,
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/generators/remote/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export async function remoteGenerator(host: Tree, schema: Schema) {
);
}

addMfEnvToTargetDefaultInputs(host);
addMfEnvToTargetDefaultInputs(host, options.bundler);

const installTask = addDependenciesToPackageJson(
host,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import {
import { addMfEnvToTargetDefaultInputs } from '../../utils/add-mf-env-to-inputs';

export default async function (tree: Tree) {
if (!hasModuleFederationProject(tree)) {
const bundler = hasModuleFederationProject(tree);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be another migration for 20.4 for projects created after v18?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!bundler) {
return;
}
addMfEnvToTargetDefaultInputs(tree);
addMfEnvToTargetDefaultInputs(tree, bundler);

await formatFiles(tree);
}
Expand All @@ -22,15 +23,18 @@ function hasModuleFederationProject(tree: Tree) {
const targets = project.targets || {};
for (const [_, target] of Object.entries(targets)) {
if (
target.executor === '@nx/webpack:webpack' &&
(tree.exists(
tree.exists(
joinPathFragments(project.root, 'module-federation.config.ts')
) ||
tree.exists(
joinPathFragments(project.root, 'module-federation.config.js')
))
tree.exists(
joinPathFragments(project.root, 'module-federation.config.js')
)
) {
return true;
if (target.executor === '@nx/webpack:webpack') {
return 'webpack';
} else if (target.executor === '@nx/rspack:rspack') {
return 'rspack';
}
}
}
}
Expand Down
23 changes: 12 additions & 11 deletions packages/react/src/utils/add-mf-env-to-inputs.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { type Tree, readNxJson, updateNxJson } from '@nx/devkit';

export function addMfEnvToTargetDefaultInputs(tree: Tree) {
export function addMfEnvToTargetDefaultInputs(
tree: Tree,
bundler: 'rspack' | 'webpack'
) {
const nxJson = readNxJson(tree);
const webpackExecutor = '@nx/webpack:webpack';
const executor =
bundler === 'rspack' ? '@nx/rspack:rspack' : '@nx/webpack:webpack';
const mfEnvVar = 'NX_MF_DEV_REMOTES';

nxJson.targetDefaults ??= {};
nxJson.targetDefaults[webpackExecutor] ??= {};
nxJson.targetDefaults[webpackExecutor].inputs ??= [
'production',
'^production',
];
nxJson.targetDefaults[webpackExecutor].dependsOn ??= ['^build'];
nxJson.targetDefaults[executor] ??= {};
nxJson.targetDefaults[executor].inputs ??= ['production', '^production'];
nxJson.targetDefaults[executor].dependsOn ??= ['^build'];

let mfEnvVarExists = false;
for (const input of nxJson.targetDefaults[webpackExecutor].inputs) {
for (const input of nxJson.targetDefaults[executor].inputs) {
if (typeof input === 'object' && input['env'] === mfEnvVar) {
mfEnvVarExists = true;
break;
}
}
if (!mfEnvVarExists) {
nxJson.targetDefaults[webpackExecutor].inputs.push({ env: mfEnvVar });
nxJson.targetDefaults[executor].inputs.push({ env: mfEnvVar });
}
nxJson.targetDefaults[webpackExecutor].cache = true;
nxJson.targetDefaults[executor].cache = true;
updateNxJson(tree, nxJson);
}
Loading