Skip to content

Commit

Permalink
feat(module-federation): add nx-runtime-library-control-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 authored and jaysoo committed Jul 17, 2024
1 parent 3a40d37 commit cde0c7d
Show file tree
Hide file tree
Showing 18 changed files with 424 additions and 1,302 deletions.
3 changes: 2 additions & 1 deletion packages/angular/ng-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"magic-string",
"enquirer",
"find-cache-dir",
"piscina"
"piscina",
"webpack"
],
"keepLifecycleScripts": true
}
1 change: 1 addition & 0 deletions packages/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"semver": "^7.5.3",
"tslib": "^2.3.0",
"webpack-merge": "^5.8.0",
"webpack": "^5.88.0",
"@module-federation/enhanced": "~0.2.3",
"@nx/devkit": "file:../devkit",
"@nx/js": "file:../js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export function executeModuleFederationDevSSRBuilder(
? options.devRemotes
: [options.devRemotes];

// Set NX_MF_DEV_REMOTES for the Nx Runtime Library Control Plugin
process.env.NX_MF_DEV_REMOTES = JSON.stringify(devServeRemotes);

validateDevRemotes({ devRemotes: devServeRemotes }, workspaceProjects);

const remotesToSkip = new Set(options.skipRemotes ?? []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ export async function* moduleFederationDevServerExecutor(
pathToManifestFile
);

// Set NX_MF_DEV_REMOTES for the Nx Runtime Library Control Plugin
process.env.NX_MF_DEV_REMOTES = JSON.stringify(
remotes.devRemotes.map((r) => (typeof r === 'string' ? r : r.remoteName))
);

if (remotes.devRemotes.length > 0 && !schema.staticRemotesPort) {
options.staticRemotesPort = options.devRemotes.reduce((portToUse, r) => {
const remoteName = typeof r === 'string' ? r : r.remoteName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type Tree, readNxJson, updateNxJson } from '@nx/devkit';
export function addMfEnvToTargetDefaultInputs(tree: Tree) {
const nxJson = readNxJson(tree);
const webpackExecutor = '@nx/angular:webpack-browser';
const mfEnvVar = 'NX_MF_DEV_SERVER_STATIC_REMOTES';
const mfEnvVar = 'NX_MF_DEV_REMOTES';

nxJson.targetDefaults ??= {};
nxJson.targetDefaults[webpackExecutor] ??= {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('addMfEnvVarToTargetDefaults', () => {
"production",
"^production",
{
"env": "NX_MF_DEV_SERVER_STATIC_REMOTES",
"env": "NX_MF_DEV_REMOTES",
},
],
},
Expand Down Expand Up @@ -109,7 +109,7 @@ describe('addMfEnvVarToTargetDefaults', () => {
"inputs": [
"^build",
{
"env": "NX_MF_DEV_SERVER_STATIC_REMOTES",
"env": "NX_MF_DEV_REMOTES",
},
],
},
Expand Down
103 changes: 62 additions & 41 deletions packages/angular/src/utils/mf/with-module-federation-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,73 @@ export async function withModuleFederationForSSR(
if (global.NX_GRAPH_CREATION) {
return (config) => config;
}

const { sharedLibraries, sharedDependencies, mappedRemotes } =
await getModuleFederationConfig(options, {
isServer: true,
});

return (config) => ({
...(config ?? {}),
target: false,
output: {
...(config.output ?? {}),
uniqueName: options.name,
},
optimization: {
...(config.optimization ?? {}),
runtimeChunk: false,
},
resolve: {
...(config.resolve ?? {}),
alias: {
...(config.resolve?.alias ?? {}),
...sharedLibraries.getAliases(),
return (config) => {
const updatedConfig = {
...(config ?? {}),
target: false,
output: {
...(config.output ?? {}),
uniqueName: options.name,
},
},
plugins: [
...(config.plugins ?? []),
new (require('@module-federation/node').UniversalFederationPlugin)(
{
name: options.name,
filename: 'remoteEntry.js',
exposes: options.exposes,
remotes: mappedRemotes,
shared: {
...sharedDependencies,
},
library: {
type: 'commonjs-module',
},
isServer: true,
/**
* Apply user-defined config override
*/
...(configOverride ? configOverride : {}),
optimization: {
...(config.optimization ?? {}),
runtimeChunk: false,
},
resolve: {
...(config.resolve ?? {}),
alias: {
...(config.resolve?.alias ?? {}),
...sharedLibraries.getAliases(),
},
{}
),
sharedLibraries.getReplacementPlugin(),
],
});
},
plugins: [
...(config.plugins ?? []),
new (require('@module-federation/node').UniversalFederationPlugin)(
{
name: options.name,
filename: 'remoteEntry.js',
exposes: options.exposes,
remotes: mappedRemotes,
shared: {
...sharedDependencies,
},
library: {
type: 'commonjs-module',
},
isServer: true,
/**
* Apply user-defined config override
*/
...(configOverride ? configOverride : {}),
runtimePlugins: !options.disableNxRuntimeLibraryControlPlugin
? [
...(configOverride?.runtimePlugins ?? []),
require.resolve(
'@nx/webpack/src/utils/module-federation/plugins/runtime-library-control.plugin.js'
),
]
: configOverride?.runtimePlugins,
},
{}
),
sharedLibraries.getReplacementPlugin(),
],
};

// The env var is only set from the module-federation-dev-server
// Attach the runtime plugin
updatedConfig.plugins.push(
new (require('webpack').DefinePlugin)({
'process.env.NX_MF_DEV_REMOTES': process.env.NX_MF_DEV_REMOTES,
})
);

return updatedConfig;
};
}
103 changes: 62 additions & 41 deletions packages/angular/src/utils/mf/with-module-federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,71 @@ export async function withModuleFederation(
if (global.NX_GRAPH_CREATION) {
return (config) => config;
}

const { sharedLibraries, sharedDependencies, mappedRemotes } =
await getModuleFederationConfig(options);

return (config) => ({
...(config ?? {}),
output: {
...(config.output ?? {}),
uniqueName: options.name,
publicPath: 'auto',
},
optimization: {
...(config.optimization ?? {}),
runtimeChunk: false,
},
resolve: {
...(config.resolve ?? {}),
alias: {
...(config.resolve?.alias ?? {}),
...sharedLibraries.getAliases(),
return (config) => {
const updatedConfig = {
...(config ?? {}),
output: {
...(config.output ?? {}),
uniqueName: options.name,
publicPath: 'auto',
},
},
experiments: {
...(config.experiments ?? {}),
outputModule: true,
},
plugins: [
...(config.plugins ?? []),
new ModuleFederationPlugin({
name: options.name,
filename: 'remoteEntry.mjs',
exposes: options.exposes,
remotes: mappedRemotes,
shared: {
...sharedDependencies,
},
library: {
type: 'module',
optimization: {
...(config.optimization ?? {}),
runtimeChunk: false,
},
resolve: {
...(config.resolve ?? {}),
alias: {
...(config.resolve?.alias ?? {}),
...sharedLibraries.getAliases(),
},
/**
* Apply user-defined config override
*/
...(configOverride ? configOverride : {}),
}),
sharedLibraries.getReplacementPlugin(),
],
});
},
experiments: {
...(config.experiments ?? {}),
outputModule: true,
},
plugins: [
...(config.plugins ?? []),
new ModuleFederationPlugin({
name: options.name,
filename: 'remoteEntry.mjs',
exposes: options.exposes,
remotes: mappedRemotes,
shared: {
...sharedDependencies,
},
library: {
type: 'module',
},
/**
* Apply user-defined config override
*/
...(configOverride ? configOverride : {}),
runtimePlugins: !options.disableNxRuntimeLibraryControlPlugin
? [
...(configOverride?.runtimePlugins ?? []),
require.resolve(
'@nx/webpack/src/utils/module-federation/plugins/runtime-library-control.plugin.js'
),
]
: configOverride?.runtimePlugins,
}),
sharedLibraries.getReplacementPlugin(),
],
};

// The env var is only set from the module-federation-dev-server
// Attach the runtime plugin
updatedConfig.plugins.push(
new (require('webpack').DefinePlugin)({
'process.env.NX_MF_DEV_REMOTES': process.env.NX_MF_DEV_REMOTES,
})
);

return updatedConfig;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ export default async function* moduleFederationDevServer(
pathToManifestFile
);

// Set NX_MF_DEV_REMOTES for the Nx Runtime Library Control Plugin
process.env.NX_MF_DEV_REMOTES = JSON.stringify(
remotes.devRemotes.map((r) => (typeof r === 'string' ? r : r.remoteName))
);

if (remotes.devRemotes.length > 0 && !initialStaticRemotesPorts) {
options.staticRemotesPort = options.devRemotes.reduce((portToUse, r) => {
const remoteName = typeof r === 'string' ? r : r.remoteName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ export default async function* moduleFederationSsrDevServer(
? options.devRemotes
: [options.devRemotes];

// Set NX_MF_DEV_REMOTES for the Nx Runtime Library Control Plugin
process.env.NX_MF_DEV_REMOTES = JSON.stringify(devServeApps);

for (const app of knownRemotes) {
const [appName] = Array.isArray(app) ? app : [app];
const isDev = devServeApps.includes(appName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('addMfEnvVarToTargetDefaults', () => {
"production",
"^production",
{
"env": "NX_MF_DEV_SERVER_STATIC_REMOTES",
"env": "NX_MF_DEV_REMOTES",
},
],
},
Expand Down Expand Up @@ -109,7 +109,7 @@ describe('addMfEnvVarToTargetDefaults', () => {
"inputs": [
"^build",
{
"env": "NX_MF_DEV_SERVER_STATIC_REMOTES",
"env": "NX_MF_DEV_REMOTES",
},
],
},
Expand Down
16 changes: 16 additions & 0 deletions packages/react/src/module-federation/with-module-federation-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,28 @@ export async function withModuleFederationForSSR(
* Apply user-defined config overrides
*/
...(configOverride ? configOverride : {}),
runtimePlugins: !options.disableNxRuntimeLibraryControlPlugin
? [
...(configOverride?.runtimePlugins ?? []),
require.resolve(
'@nx/webpack/src/utils/module-federation/plugins/runtime-library-control.plugin.js'
),
]
: configOverride?.runtimePlugins,
},
{}
),
sharedLibraries.getReplacementPlugin()
);

// The env var is only set from the module-federation-dev-server
// Attach the runtime plugin
config.plugins.push(
new (require('webpack').DefinePlugin)({
'process.env.NX_MF_DEV_REMOTES': process.env.NX_MF_DEV_REMOTES,
})
);

return config;
};
}
17 changes: 17 additions & 0 deletions packages/react/src/module-federation/with-module-federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export async function withModuleFederation(
if (global.NX_GRAPH_CREATION) {
return (config) => config;
}

const { sharedDependencies, sharedLibraries, mappedRemotes } =
await getModuleFederationConfig(options);
const isGlobal = isVarOrWindow(options.library?.type);
Expand Down Expand Up @@ -70,10 +71,26 @@ export async function withModuleFederation(
* Apply user-defined config overrides
*/
...(configOverride ? configOverride : {}),
runtimePlugins: !options.disableNxRuntimeLibraryControlPlugin
? [
...(configOverride?.runtimePlugins ?? []),
require.resolve(
'@nx/webpack/src/utils/module-federation/plugins/runtime-library-control.plugin.js'
),
]
: configOverride?.runtimePlugins,
}),
sharedLibraries.getReplacementPlugin()
);

// The env var is only set from the module-federation-dev-server
// Attach the runtime plugin
config.plugins.push(
new (require('webpack').DefinePlugin)({
'process.env.NX_MF_DEV_REMOTES': process.env.NX_MF_DEV_REMOTES,
})
);

return config;
};
}
Loading

0 comments on commit cde0c7d

Please sign in to comment.