From 681605c6d9681a626b7b78db170443003b843fdb Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Tue, 2 Jul 2024 10:58:05 +0100 Subject: [PATCH] feat(module-federation): expose API for users to override module federation plugin options --- package.json | 1 + packages/angular/migrations.json | 9 +++++++++ .../src/utils/backward-compatible-versions.ts | 4 ++-- .../src/utils/mf/with-module-federation-ssr.ts | 12 ++++++++++-- .../angular/src/utils/mf/with-module-federation.ts | 14 ++++++++++++-- packages/angular/src/utils/versions.ts | 2 +- packages/react/migrations.json | 9 +++++++++ .../with-module-federation-ssr.ts | 12 ++++++++++-- .../module-federation/with-module-federation.ts | 12 ++++++++++-- packages/react/src/utils/versions.ts | 2 +- packages/webpack/package.json | 1 + .../src/utils/module-federation/models/index.ts | 12 ++++++++++++ pnpm-lock.yaml | 9 ++++++--- 13 files changed, 84 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 1a8c07bbd7d76..ad4917823fc7f 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "@jest/test-result": "^29.4.1", "@jest/types": "^29.4.1", "@module-federation/enhanced": "^0.2.3", + "@module-federation/sdk": "^0.2.3", "@monodon/rust": "1.3.3", "@napi-rs/cli": "2.14.0", "@nestjs/cli": "^10.0.2", diff --git a/packages/angular/migrations.json b/packages/angular/migrations.json index 8232cf5a50664..55dd4bd3b924d 100644 --- a/packages/angular/migrations.json +++ b/packages/angular/migrations.json @@ -1933,6 +1933,15 @@ "alwaysAddToPackageJson": false } } + }, + "19.4.0-module-federation": { + "version": "19.4.0-rc.2", + "packages": { + "@module-federation/node": { + "version": "^2.3.0", + "alwaysAddToPackageJson": false + } + } } } } diff --git a/packages/angular/src/utils/backward-compatible-versions.ts b/packages/angular/src/utils/backward-compatible-versions.ts index b32c391e7c586..392bec4315533 100644 --- a/packages/angular/src/utils/backward-compatible-versions.ts +++ b/packages/angular/src/utils/backward-compatible-versions.ts @@ -48,7 +48,7 @@ export const backwardCompatibleVersions: VersionMap = { expressVersion: '~4.18.2', typesExpressVersion: '4.17.14', browserSyncVersion: '^3.0.0', - moduleFederationNodeVersion: '~1.0.5', + moduleFederationNodeVersion: '~2.3.0', moduleFederationEnhancedVersion: '~0.2.3', angularEslintVersion: '~16.0.0', tailwindVersion: '^3.0.2', @@ -75,7 +75,7 @@ export const backwardCompatibleVersions: VersionMap = { expressVersion: '~4.18.2', typesExpressVersion: '4.17.14', browserSyncVersion: '^3.0.0', - moduleFederationNodeVersion: '~1.0.5', + moduleFederationNodeVersion: '~2.3.0', moduleFederationEnhancedVersion: '~0.2.3', angularEslintVersion: '~17.3.0', tailwindVersion: '^3.0.2', diff --git a/packages/angular/src/utils/mf/with-module-federation-ssr.ts b/packages/angular/src/utils/mf/with-module-federation-ssr.ts index 9cbdeafb19a9b..a125470e800ed 100644 --- a/packages/angular/src/utils/mf/with-module-federation-ssr.ts +++ b/packages/angular/src/utils/mf/with-module-federation-ssr.ts @@ -1,8 +1,12 @@ -import { ModuleFederationConfig } from '@nx/webpack/src/utils/module-federation'; +import type { + ModuleFederationConfig, + NxModuleFederationConfigOverride, +} from '@nx/webpack/src/utils/module-federation'; import { getModuleFederationConfig } from './utils'; export async function withModuleFederationForSSR( - options: ModuleFederationConfig + options: ModuleFederationConfig, + configOverride?: NxModuleFederationConfigOverride ) { if (global.NX_GRAPH_CREATION) { return (config) => config; @@ -45,6 +49,10 @@ export async function withModuleFederationForSSR( type: 'commonjs-module', }, isServer: true, + /** + * Apply user-defined config override + */ + ...(configOverride ? configOverride : {}), }, {} ), diff --git a/packages/angular/src/utils/mf/with-module-federation.ts b/packages/angular/src/utils/mf/with-module-federation.ts index 44edd3b1c6559..91d3a575c06a8 100644 --- a/packages/angular/src/utils/mf/with-module-federation.ts +++ b/packages/angular/src/utils/mf/with-module-federation.ts @@ -1,8 +1,14 @@ -import { ModuleFederationConfig } from '@nx/webpack/src/utils/module-federation'; +import type { + ModuleFederationConfig, + NxModuleFederationConfigOverride, +} from '@nx/webpack/src/utils/module-federation'; import { getModuleFederationConfig } from './utils'; import { ModuleFederationPlugin } from '@module-federation/enhanced/webpack'; -export async function withModuleFederation(options: ModuleFederationConfig) { +export async function withModuleFederation( + options: ModuleFederationConfig, + configOverride?: NxModuleFederationConfigOverride +) { if (global.NX_GRAPH_CREATION) { return (config) => config; } @@ -44,6 +50,10 @@ export async function withModuleFederation(options: ModuleFederationConfig) { library: { type: 'module', }, + /** + * Apply user-defined config override + */ + ...(configOverride ? configOverride : {}), }), sharedLibraries.getReplacementPlugin(), ], diff --git a/packages/angular/src/utils/versions.ts b/packages/angular/src/utils/versions.ts index 7a02581931461..71bf2c2816a2c 100644 --- a/packages/angular/src/utils/versions.ts +++ b/packages/angular/src/utils/versions.ts @@ -14,7 +14,7 @@ export const typesCorsVersion = '~2.8.5'; export const expressVersion = '~4.18.2'; export const typesExpressVersion = '4.17.14'; export const browserSyncVersion = '^3.0.0'; -export const moduleFederationNodeVersion = '~1.0.5'; +export const moduleFederationNodeVersion = '^2.3.0'; export const moduleFederationEnhancedVersion = '~0.2.3'; export const angularEslintVersion = '^18.0.1'; diff --git a/packages/react/migrations.json b/packages/react/migrations.json index db1edccd9f3d2..ecee1b13f5dbd 100644 --- a/packages/react/migrations.json +++ b/packages/react/migrations.json @@ -202,6 +202,15 @@ "alwaysAddToPackageJson": false } } + }, + "19.4.0-module-federation": { + "version": "19.4.0-rc.2", + "packages": { + "@module-federation/node": { + "version": "^2.3.0", + "alwaysAddToPackageJson": false + } + } } } } diff --git a/packages/react/src/module-federation/with-module-federation-ssr.ts b/packages/react/src/module-federation/with-module-federation-ssr.ts index cd68d6efa0f17..c1ca0f6065b93 100644 --- a/packages/react/src/module-federation/with-module-federation-ssr.ts +++ b/packages/react/src/module-federation/with-module-federation-ssr.ts @@ -1,8 +1,12 @@ -import { ModuleFederationConfig } from '@nx/webpack/src/utils/module-federation'; +import { + ModuleFederationConfig, + NxModuleFederationConfigOverride, +} from '@nx/webpack/src/utils/module-federation'; import { getModuleFederationConfig } from './utils'; export async function withModuleFederationForSSR( - options: ModuleFederationConfig + options: ModuleFederationConfig, + configOverride?: NxModuleFederationConfigOverride ) { if (global.NX_GRAPH_CREATION) { return (config) => config; @@ -34,6 +38,10 @@ export async function withModuleFederationForSSR( type: 'commonjs-module', }, isServer: true, + /** + * Apply user-defined config overrides + */ + ...(configOverride ? configOverride : {}), }, {} ), diff --git a/packages/react/src/module-federation/with-module-federation.ts b/packages/react/src/module-federation/with-module-federation.ts index e74a5c3d77f81..c336882583250 100644 --- a/packages/react/src/module-federation/with-module-federation.ts +++ b/packages/react/src/module-federation/with-module-federation.ts @@ -1,4 +1,7 @@ -import { ModuleFederationConfig } from '@nx/webpack/src/utils/module-federation'; +import { + ModuleFederationConfig, + NxModuleFederationConfigOverride, +} from '@nx/webpack/src/utils/module-federation'; import { getModuleFederationConfig } from './utils'; import type { AsyncNxComposableWebpackPlugin } from '@nx/webpack'; import { ModuleFederationPlugin } from '@module-federation/enhanced/webpack'; @@ -11,7 +14,8 @@ const isVarOrWindow = (libType?: string) => * @return {Promise} */ export async function withModuleFederation( - options: ModuleFederationConfig + options: ModuleFederationConfig, + configOverride?: NxModuleFederationConfigOverride ): Promise { if (global.NX_GRAPH_CREATION) { return (config) => config; @@ -62,6 +66,10 @@ export async function withModuleFederation( * { appY: 'appY@http://localhost:3002/remoteEntry.js' } */ ...(isGlobal ? { remoteType: 'script' } : {}), + /** + * Apply user-defined config overrides + */ + ...(configOverride ? configOverride : {}), }), sharedLibraries.getReplacementPlugin() ); diff --git a/packages/react/src/utils/versions.ts b/packages/react/src/utils/versions.ts index 7cc099f2758f3..3729d936279eb 100755 --- a/packages/react/src/utils/versions.ts +++ b/packages/react/src/utils/versions.ts @@ -54,7 +54,7 @@ export const typesExpressVersion = '4.17.17'; export const isbotVersion = '^3.6.5'; export const corsVersion = '~2.8.5'; export const typesCorsVersion = '~2.8.12'; -export const moduleFederationNodeVersion = '~1.0.5'; +export const moduleFederationNodeVersion = '^2.3.0'; export const moduleFederationEnhancedVersion = '~0.2.3'; // style preprocessors diff --git a/packages/webpack/package.json b/packages/webpack/package.json index d49f9672032d3..5547b48e6355a 100644 --- a/packages/webpack/package.json +++ b/packages/webpack/package.json @@ -32,6 +32,7 @@ "dependencies": { "@babel/core": "^7.23.2", "@phenomnomnominal/tsquery": "~5.0.1", + "@module-federation/sdk": "^0.2.3", "ajv": "^8.12.0", "autoprefixer": "^10.4.9", "babel-loader": "^9.1.2", diff --git a/packages/webpack/src/utils/module-federation/models/index.ts b/packages/webpack/src/utils/module-federation/models/index.ts index 3c2d2d9c260ec..2a1150a9cfe92 100644 --- a/packages/webpack/src/utils/module-federation/models/index.ts +++ b/packages/webpack/src/utils/module-federation/models/index.ts @@ -1,4 +1,5 @@ import type { NormalModuleReplacementPlugin } from 'webpack'; +import type { moduleFederationPlugin } from '@module-federation/sdk'; export type ModuleFederationLibrary = { type: string; name: string }; @@ -46,6 +47,17 @@ export interface ModuleFederationConfig { additionalShared?: AdditionalSharedConfig; } +export type NxModuleFederationConfigOverride = Omit< + moduleFederationPlugin.ModuleFederationPluginOptions, + | 'exposes' + | 'remotes' + | 'name' + | 'library' + | 'shared' + | 'filename' + | 'remoteType' +>; + export type WorkspaceLibrarySecondaryEntryPoint = { name: string; path: string; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bbb572b5bfb9a..c033f1a366d0a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -227,6 +227,9 @@ devDependencies: '@module-federation/enhanced': specifier: ^0.2.3 version: 0.2.3(typescript@5.4.2)(webpack@5.88.0) + '@module-federation/sdk': + specifier: ^0.2.3 + version: 0.2.3 '@monodon/rust': specifier: 1.3.3 version: 1.3.3(@swc-node/register@1.9.1)(@swc/core@1.5.7)(@types/node@18.19.8)(typescript@5.4.2)(verdaccio@5.31.0) @@ -481,7 +484,7 @@ devDependencies: version: 0.3.1(immer@9.0.16)(xstate@4.34.0) '@xstate/inspect': specifier: 0.7.0 - version: 0.7.0(ws@8.16.0)(xstate@4.34.0) + version: 0.7.0(ws@8.17.1)(xstate@4.34.0) '@xstate/react': specifier: 3.0.1 version: 3.0.1(@types/react@18.3.1)(react@18.3.1)(xstate@4.34.0) @@ -16385,7 +16388,7 @@ packages: xstate: 4.34.0 dev: true - /@xstate/inspect@0.7.0(ws@8.16.0)(xstate@4.34.0): + /@xstate/inspect@0.7.0(ws@8.17.1)(xstate@4.34.0): resolution: {integrity: sha512-3wrTf8TfBYprH1gBFdxmOQUBDpBazlICWvGdFzr8IHFL4MbiexEZdAsL2QC/WAmW9BqNYTWTwgfbvKHKg+FrlA==} peerDependencies: '@types/ws': ^8.0.0 @@ -16396,7 +16399,7 @@ packages: optional: true dependencies: fast-safe-stringify: 2.1.1 - ws: 8.16.0 + ws: 8.17.1 xstate: 4.34.0 dev: true