fix(module-federation): ensure @nx/module-federation package is installed for users (#29416)

## Current Behavior
The `@nx/module-federation` package is a direct dependency of some of
the other packages in the Nx Plugin ecosystem.
It should be resolved correctly by package managers such that it can be
used when setting up MF projects.

However, some users are facing issues with module resolutions where the
package is not found as expected.

## Expected Behavior
Install the package directly for the user to ensure module resolution
works as expected.

## Related Issue(s)

Fixes #29269
This commit is contained in:
Colum Ferry 2024-12-19 14:09:48 +00:00 committed by GitHub
parent dfd5014792
commit 656d69b466
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 148 additions and 0 deletions

View File

@ -332,6 +332,12 @@
},
"description": "Remove the deprecated 'tailwindConfig' option from ng-packagr executors. Tailwind CSS configurations located at the project or workspace root will be picked up automatically.",
"factory": "./src/migrations/update-20-2-0/remove-tailwind-config-from-ng-packagr-executors"
},
"ensure-nx-module-federation-package": {
"cli": "nx",
"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"
}
},
"packageJsonUpdates": {

View File

@ -51,6 +51,7 @@ export async function setupMf(tree: Tree, rawOptions: Schema) {
{
'@nx/web': nxVersion,
'@nx/webpack': nxVersion,
'@nx/module-federation': nxVersion,
}
);
}
@ -82,6 +83,7 @@ export async function setupMf(tree: Tree, rawOptions: Schema) {
{
'@nx/webpack': nxVersion,
'@module-federation/enhanced': moduleFederationEnhancedVersion,
'@nx/module-federation': nxVersion,
}
);
}

View File

@ -3,9 +3,11 @@ import {
formatFiles,
readProjectConfiguration,
visitNotIgnoredFiles,
addDependenciesToPackageJson,
} from '@nx/devkit';
import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils';
import { tsquery } from '@phenomnomnominal/tsquery';
import { nxVersion } from '../../utils/versions';
const NX_ANGULAR_MODULE_FEDERATION_IMPORT_SELECTOR =
'ImportDeclaration > StringLiteral[value=@nx/angular/module-federation], VariableStatement CallExpression:has(Identifier[name=require]) > StringLiteral[value=@nx/angular/module-federation]';
@ -53,5 +55,15 @@ export default async function migrateWithMfImport(tree: Tree) {
});
}
if (projects.size !== 0) {
addDependenciesToPackageJson(
tree,
{},
{
'@nx/module-federation': nxVersion,
}
);
}
await formatFiles(tree);
}

View File

@ -0,0 +1,30 @@
import {
type Tree,
readProjectConfiguration,
addDependenciesToPackageJson,
} from '@nx/devkit';
import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils';
import { nxVersion } from '../../utils/versions';
export default async function ensureMfPackage(tree: Tree) {
const projects = new Set<string>();
forEachExecutorOptions(
tree,
'@nx/angular:module-federation-dev-server',
(options, project, target) => {
const projectConfig = readProjectConfiguration(tree, project);
projects.add(projectConfig.root);
}
);
if (projects.size !== 0) {
addDependenciesToPackageJson(
tree,
{},
{
'@nx/module-federation': nxVersion,
}
);
}
}

View File

@ -41,6 +41,12 @@
"version": "20.2.0-beta.2",
"description": "Update the withModuleFederation import use @nx/module-federation/webpack.",
"factory": "./src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package"
},
"ensure-nx-module-federation-package": {
"cli": "nx",
"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"
}
},
"packageJsonUpdates": {

View File

@ -153,6 +153,7 @@ export async function hostGenerator(
{ '@module-federation/enhanced': moduleFederationEnhancedVersion },
{
'@nx/web': nxVersion,
'@nx/module-federation': nxVersion,
}
);
tasks.push(installTask);

View File

@ -226,6 +226,7 @@ export async function remoteGenerator(host: Tree, schema: Schema) {
{
'@module-federation/enhanced': moduleFederationEnhancedVersion,
'@nx/web': nxVersion,
'@nx/module-federation': nxVersion,
}
);
tasks.push(installTask);

View File

@ -3,9 +3,11 @@ import {
formatFiles,
readProjectConfiguration,
visitNotIgnoredFiles,
addDependenciesToPackageJson,
} from '@nx/devkit';
import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils';
import { tsquery } from '@phenomnomnominal/tsquery';
import { nxVersion } from '../../utils/versions';
const NX_RSPACK_MODULE_FEDERATION_IMPORT_SELECTOR =
'ImportDeclaration > StringLiteral[value=@nx/react/module-federation], VariableStatement CallExpression:has(Identifier[name=require]) > StringLiteral[value=@nx/react/module-federation]';
@ -53,5 +55,15 @@ export default async function migrateWithMfImport(tree: Tree) {
});
}
if (projects.size !== 0) {
addDependenciesToPackageJson(
tree,
{},
{
'@nx/module-federation': nxVersion,
}
);
}
await formatFiles(tree);
}

View File

@ -0,0 +1,30 @@
import {
type Tree,
readProjectConfiguration,
addDependenciesToPackageJson,
} from '@nx/devkit';
import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils';
import { nxVersion } from '../../utils/versions';
export default async function ensureMfPackage(tree: Tree) {
const projects = new Set<string>();
forEachExecutorOptions(
tree,
'@nx/react:module-federation-dev-server',
(options, project, target) => {
const projectConfig = readProjectConfiguration(tree, project);
projects.add(projectConfig.root);
}
);
if (projects.size !== 0) {
addDependenciesToPackageJson(
tree,
{},
{
'@nx/module-federation': nxVersion,
}
);
}
}

View File

@ -5,6 +5,12 @@
"version": "20.2.0-beta.3",
"description": "Update the withModuleFederation import use @nx/module-federation/rspack.",
"factory": "./src/migrations/update-20-2-0/migrate-with-mf-import-to-new-package"
},
"ensure-nx-module-federation-package": {
"cli": "nx",
"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"
}
},
"packageJsonUpdates": {

View File

@ -3,9 +3,11 @@ import {
formatFiles,
readProjectConfiguration,
visitNotIgnoredFiles,
addDependenciesToPackageJson,
} from '@nx/devkit';
import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils';
import { tsquery } from '@phenomnomnominal/tsquery';
import { nxVersion } from '../../utils/versions';
const NX_RSPACK_MODULE_FEDERATION_IMPORT_SELECTOR =
'ImportDeclaration > StringLiteral[value=@nx/rspack/module-federation], VariableStatement CallExpression:has(Identifier[name=require]) > StringLiteral[value=@nx/rspack/module-federation]';
@ -53,5 +55,15 @@ export default async function migrateWithMfImport(tree: Tree) {
});
}
if (projects.size !== 0) {
addDependenciesToPackageJson(
tree,
{},
{
'@nx/module-federation': nxVersion,
}
);
}
await formatFiles(tree);
}

View File

@ -0,0 +1,30 @@
import {
type Tree,
readProjectConfiguration,
addDependenciesToPackageJson,
} from '@nx/devkit';
import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils';
import { nxVersion } from '../../utils/versions';
export default async function ensureMfPackage(tree: Tree) {
const projects = new Set<string>();
forEachExecutorOptions(
tree,
'@nx/rspack:module-federation-dev-server',
(options, project, target) => {
const projectConfig = readProjectConfiguration(tree, project);
projects.add(projectConfig.root);
}
);
if (projects.size !== 0) {
addDependenciesToPackageJson(
tree,
{},
{
'@nx/module-federation': nxVersion,
}
);
}
}