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

feat(module-federation): add nx-runtime-library-control-plugin #26816

Merged
merged 1 commit into from
Jul 18, 2024
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
2 changes: 1 addition & 1 deletion e2e/angular/src/projects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('Angular Projects', () => {
console.log(
`The current es2015 bundle size is ${es2015BundleSize / 1000} KB`
);
expect(es2015BundleSize).toBeLessThanOrEqual(220000);
expect(es2015BundleSize).toBeLessThanOrEqual(221000);

// check unit tests
runCLI(
Expand Down
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
105 changes: 64 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,75 @@ 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:
process.env.NX_MF_DEV_REMOTES &&
!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;
};
}
105 changes: 64 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,73 @@ 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:
process.env.NX_MF_DEV_REMOTES &&
!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
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,30 @@ export async function withModuleFederationForSSR(
* Apply user-defined config overrides
*/
...(configOverride ? configOverride : {}),
runtimePlugins:
process.env.NX_MF_DEV_REMOTES &&
!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