Skip to content

Commit

Permalink
feat(module-federation): consolidate module federation utils into mod…
Browse files Browse the repository at this point in the history
…ule-federation package (#28919)

- feat(module-federation): consolidate module federation utils into
module-federation package
- chore(module-federation): fix tests and linting

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
Our current support for Module Federation relies on utilities that are
spread and duplicated across the `@nx/webpack` package and the
`@nx/rspack` package.



## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
Now that we have a `@nx/module-federation` package, dedupe the utils and
consolidate them into a single package

## Todo
- [x] Migrations for React + Angular to install `@nx/module-federation`
and point `ModuleFederationConfig` export to that package from
webpack.config and rspack.config files
  • Loading branch information
Coly010 authored Nov 18, 2024
1 parent 0407b7a commit 76d61ea
Show file tree
Hide file tree
Showing 99 changed files with 995 additions and 2,156 deletions.
3 changes: 1 addition & 2 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,9 @@ rust-toolchain @nrwl/nx-native-reviewers
/packages/web/** @nrwl/nx-js-reviewers
/e2e/web/** @nrwl/nx-js-reviewers
/packages/webpack/** @nrwl/nx-js-reviewers
/packages/webpack/src/utils/module-federation @jaysoo @Coly010
/e2e/webpack/** @nrwl/nx-js-reviewers
/packages/rspack/** @nrwl/nx-js-reviewers
/packages/rspack/src/utils/module-federation @jaysoo @Coly010
/packages/rspack/src/utils/module-federation @nrwl/nx-js-reviewers
/e2e/rspack/** @nrwl/nx-js-reviewers
/packages/esbuild/** @nrwl/nx-js-reviewers
/e2e/esbuild/** @nrwl/nx-js-reviewers
Expand Down
6 changes: 6 additions & 0 deletions packages/angular/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@
"version": "19.6.1-beta.0",
"description": "Ensure Target Defaults are set correctly for Module Federation.",
"factory": "./src/migrations/update-19-6-1/ensure-depends-on-for-mf"
},
"update-20-2-0-update-module-federation-config-import": {
"cli": "nx",
"version": "20.2.0-beta.2",
"description": "Update the ModuleFederationConfig import use @nx/module-federation.",
"factory": "./src/migrations/update-20-2-0/migrate-mf-imports-to-new-package"
}
},
"packageJsonUpdates": {
Expand Down
1 change: 1 addition & 0 deletions packages/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@nx/js": "file:../js",
"@nx/eslint": "file:../eslint",
"@nx/webpack": "file:../webpack",
"@nx/module-federation": "file:../module-federation",
"@nx/web": "file:../web",
"@nx/workspace": "file:../workspace",
"piscina": "^4.4.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { workspaceDataDirectory } from 'nx/src/utils/cache-directory';
import { fork } from 'node:child_process';
import { join } from 'node:path';
import { createWriteStream } from 'node:fs';
import type { StaticRemotesConfig } from '@nx/webpack/src/utils/module-federation/parse-static-remotes-config';
import type { StaticRemotesConfig } from '@nx/module-federation/src/utils';

export async function buildStaticRemotes(
staticRemotesConfig: StaticRemotesConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type Schema } from '../schema';
import fileServerExecutor from '@nx/web/src/executors/file-server/file-server.impl';
import { join } from 'path';
import { cpSync } from 'fs';
import type { StaticRemotesConfig } from '@nx/webpack/src/utils/module-federation/parse-static-remotes-config';
import type { StaticRemotesConfig } from '@nx/module-federation/src/utils';

export function startStaticRemotesFileServer(
staticRemotesConfig: StaticRemotesConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import {
import {
getModuleFederationConfig,
getRemotes,
} from '@nx/webpack/src/utils/module-federation';
startRemoteProxies,
parseStaticRemotesConfig,
} from '@nx/module-federation/src/utils';
import { waitForPortOpen } from '@nx/web/src/utils/wait-for-port-open';
import fileServerExecutor from '@nx/web/src/executors/file-server/file-server.impl';
import { createBuilderContext } from 'nx/src/adapter/ngcli-adapter';
Expand All @@ -30,8 +32,6 @@ import {
} from '../../builders/utilities/module-federation';
import { extname, join } from 'path';
import { existsSync } from 'fs';
import { startRemoteProxies } from '@nx/webpack/src/utils/module-federation/start-remote-proxies';
import { parseStaticRemotesConfig } from '@nx/webpack/src/utils/module-federation/parse-static-remotes-config';

export async function* moduleFederationDevServerExecutor(
schema: Schema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { workspaceDataDirectory } from 'nx/src/utils/cache-directory';
import { fork } from 'node:child_process';
import { join } from 'node:path';
import { createWriteStream } from 'node:fs';
import type { StaticRemotesConfig } from '@nx/webpack/src/utils/module-federation/parse-static-remotes-config';
import type { StaticRemotesConfig } from '@nx/module-federation/src/utils';

export async function buildStaticRemotes(
staticRemotesConfig: StaticRemotesConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type Schema } from '../schema';
import fileServerExecutor from '@nx/web/src/executors/file-server/file-server.impl';
import { join } from 'path';
import { cpSync, rmSync } from 'fs';
import type { StaticRemotesConfig } from '@nx/webpack/src/utils/module-federation/parse-static-remotes-config';
import type { StaticRemotesConfig } from '@nx/module-federation/src/utils';
import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable';

export function startStaticRemotes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import type { Schema } from './schema';
import {
getModuleFederationConfig,
getRemotes,
} from '@nx/webpack/src/utils/module-federation';
import { parseStaticSsrRemotesConfig } from '@nx/webpack/src/utils/module-federation/parse-static-remotes-config';
parseStaticSsrRemotesConfig,
startSsrRemoteProxies,
} from '@nx/module-federation/src/utils';
import { buildStaticRemotes } from './lib/build-static-remotes';
import { startRemotes } from './lib/start-dev-remotes';
import { startStaticRemotes } from './lib/start-static-remotes';
Expand All @@ -24,7 +25,6 @@ import { eachValueFrom } from '@nx/devkit/src/utils/rxjs-for-await';
import { createBuilderContext } from 'nx/src/adapter/ngcli-adapter';
import { normalizeOptions } from './lib/normalize-options';
import { waitForPortOpen } from '@nx/web/src/utils/wait-for-port-open';
import { startSsrRemoteProxies } from '@nx/webpack/src/utils/module-federation/start-ssr-remote-proxies';
import { getInstalledAngularVersionInfo } from '../utilities/angular-version-utils';

export async function* moduleFederationSsrDevServerExecutor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ export default bootstrap;
exports[`Host App Generator --ssr should generate the correct files for standalone when --typescript=true 4`] = `"import('./src/main.server');"`;
exports[`Host App Generator --ssr should generate the correct files for standalone when --typescript=true 5`] = `
"import { ModuleFederationConfig } from '@nx/webpack';
"import { ModuleFederationConfig } from '@nx/module-federation';
const config: ModuleFederationConfig = {
name: 'test',
Expand Down Expand Up @@ -880,7 +880,7 @@ export default AppServerModule;
exports[`Host App Generator --ssr should generate the correct files when --typescript=true 5`] = `"import('./src/main.server');"`;
exports[`Host App Generator --ssr should generate the correct files when --typescript=true 6`] = `
"import { ModuleFederationConfig } from '@nx/webpack';
"import { ModuleFederationConfig } from '@nx/module-federation';
const config: ModuleFederationConfig = {
name: 'test',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ export default AppServerModule;
exports[`MF Remote App Generator --ssr should generate the correct files when --typescriptConfiguration=true 5`] = `"import('./src/main.server');"`;
exports[`MF Remote App Generator --ssr should generate the correct files when --typescriptConfiguration=true 6`] = `
"import { ModuleFederationConfig } from '@nx/webpack';
"import { ModuleFederationConfig } from '@nx/module-federation';
const config: ModuleFederationConfig = {
name: 'test',
Expand Down Expand Up @@ -691,7 +691,7 @@ bootstrapApplication(RemoteEntryComponent, appConfig).catch((err) =>
`;
exports[`MF Remote App Generator should generate the a remote setup for standalone components when --typescriptConfiguration=true 2`] = `
"import { ModuleFederationConfig } from '@nx/webpack';
"import { ModuleFederationConfig } from '@nx/module-federation';
const config: ModuleFederationConfig = {
name: 'test',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ module.exports = {
`;

exports[`Init MF should add a remote application and add it to a specified host applications webpack config that contains a remote application already when --typescriptConfiguration=true 1`] = `
"import { ModuleFederationConfig } from '@nx/webpack';
"import { ModuleFederationConfig } from '@nx/module-federation';
const config: ModuleFederationConfig = {
name: 'app1',
Expand Down Expand Up @@ -148,7 +148,7 @@ module.exports = {
`;

exports[`Init MF should add a remote application and add it to a specified host applications webpack config when no other remote has been added to it when --typescriptConfiguration=true 1`] = `
"import { ModuleFederationConfig } from '@nx/webpack';
"import { ModuleFederationConfig } from '@nx/module-federation';
const config: ModuleFederationConfig = {
name: 'app1',
Expand Down Expand Up @@ -284,7 +284,7 @@ export default withModuleFederation(config, { dts: false });
`;

exports[`Init MF should create webpack and mf configs correctly when --typescriptConfiguration=true 2`] = `
"import { ModuleFederationConfig } from '@nx/webpack';
"import { ModuleFederationConfig } from '@nx/module-federation';
const config: ModuleFederationConfig = {
name: 'app1',
Expand Down Expand Up @@ -324,7 +324,7 @@ export default withModuleFederation(config, { dts: false });
`;

exports[`Init MF should create webpack and mf configs correctly when --typescriptConfiguration=true 4`] = `
"import { ModuleFederationConfig } from '@nx/webpack';
"import { ModuleFederationConfig } from '@nx/module-federation';
const config: ModuleFederationConfig = {
name: 'remote1',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ModuleFederationConfig } from '@nx/webpack';
import { ModuleFederationConfig } from '@nx/module-federation';

const config: ModuleFederationConfig = {
name: '<%= name %>',<% if(type === 'host') { %>
Expand Down
Loading

0 comments on commit 76d61ea

Please sign in to comment.