nx/packages/react/src/module-federation/with-module-federation-ssr.ts
Colum Ferry f208acde54
fix(module-federation): optimization should not be overwritten #27201 (#27376)
<!-- 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 -->
`withModuleFederation` is overwriting `config.optimization` rather than
merging it.


## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
`config.optimization` should be merged

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #27201
2024-08-12 15:29:55 +01:00

73 lines
2.0 KiB
TypeScript

import {
ModuleFederationConfig,
NxModuleFederationConfigOverride,
} from '@nx/webpack/src/utils/module-federation';
import { getModuleFederationConfig } from './utils';
export async function withModuleFederationForSSR(
options: ModuleFederationConfig,
configOverride?: NxModuleFederationConfigOverride
) {
if (global.NX_GRAPH_CREATION) {
return (config) => config;
}
const { sharedLibraries, sharedDependencies, mappedRemotes } =
await getModuleFederationConfig(options, {
isServer: true,
});
return (config) => {
config.target = false;
config.output.uniqueName = options.name;
config.optimization = {
...(config.optimization ?? {}),
runtimeChunk: false,
};
config.plugins.push(
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 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;
};
}