Skip to content

Commit

Permalink
feat(react): add withModuleFederationForSSR function (#13276)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 authored Nov 22, 2022
1 parent b902849 commit 8be2980
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 53 deletions.
4 changes: 4 additions & 0 deletions packages/react/module-federation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { withModuleFederation } from './src/module-federation/with-module-federation';
import { withModuleFederationForSSR } from './src/module-federation/with-module-federation-ssr';

export { withModuleFederation };
export { withModuleFederationForSSR };

// Support for older generated code: `const withModuleFederation = require('@nrwl/react/module-federation')`
module.exports = withModuleFederation;

// Allow newer generated code to work: `const { withModuleFederation } = require(...)`;
module.exports.withModuleFederation = withModuleFederation;
module.exports.withModuleFederationForSSR = withModuleFederationForSSR;
65 changes: 65 additions & 0 deletions packages/react/src/module-federation/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
applyAdditionalShared,
applySharedFunction,
createProjectGraphAsync,
getDependentPackagesForProject,
mapRemotes,
mapRemotesForSSR,
ModuleFederationConfig,
ProjectConfiguration,
ProjectGraph,
readCachedProjectGraph,
sharePackages,
shareWorkspaceLibraries,
} from '@nrwl/devkit';

export async function getModuleFederationConfig(
mfConfig: ModuleFederationConfig,
determineRemoteUrl: (remote: string) => string,
options: { isServer: boolean } = { isServer: false }
) {
let projectGraph: ProjectGraph<ProjectConfiguration>;
try {
projectGraph = readCachedProjectGraph();
} catch (e) {
projectGraph = await createProjectGraphAsync();
}

const project = projectGraph.nodes[mfConfig.name]?.data;

if (!project) {
throw Error(
`Cannot find project "${mfConfig.name}". Check that the name is correct in module-federation.config.js`
);
}

const dependencies = getDependentPackagesForProject(
projectGraph,
mfConfig.name
);
const sharedLibraries = shareWorkspaceLibraries(
dependencies.workspaceLibraries
);

const npmPackages = sharePackages(dependencies.npmPackages);

const sharedDependencies = {
...sharedLibraries.getLibraries(),
...npmPackages,
};

applySharedFunction(sharedDependencies, mfConfig.shared);
applyAdditionalShared(
sharedDependencies,
mfConfig.additionalShared,
projectGraph
);

const mapRemotesFunction = options.isServer ? mapRemotesForSSR : mapRemotes;
const mappedRemotes =
!mfConfig.remotes || mfConfig.remotes.length === 0
? {}
: mapRemotesFunction(mfConfig.remotes, 'js', determineRemoteUrl);

return { sharedLibraries, sharedDependencies, mappedRemotes };
}
64 changes: 64 additions & 0 deletions packages/react/src/module-federation/with-module-federation-ssr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { ModuleFederationConfig } from '@nrwl/devkit';
import { readCachedProjectConfiguration } from 'nx/src/project-graph/project-graph';
import { getModuleFederationConfig } from './utils';

function determineRemoteUrl(remote: string) {
const remoteConfiguration = readCachedProjectConfiguration(remote);
const serveTarget = remoteConfiguration?.targets?.serve;

if (!serveTarget) {
throw new Error(
`Cannot automatically determine URL of remote (${remote}). Looked for property "host" in the project's "serve" target.\n
You can also use the tuple syntax in your webpack config to configure your remotes. e.g. \`remotes: [['remote1', '//localhost:4201']]\``
);
}

const host = serveTarget.options?.host ?? '//localhost';
const port = serveTarget.options?.port ?? 4201;
return `${
host.endsWith('/') ? host.slice(0, -1) : host
}:${port}/server/remoteEntry.js`;
}

export async function withModuleFederationForSSR(
options: ModuleFederationConfig
) {
const reactWebpackConfig = require('../../plugins/webpack');

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

return (config) => {
config = reactWebpackConfig(config);

config.target = false;
config.output.uniqueName = options.name;
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,
},
{}
),
sharedLibraries.getReplacementPlugin()
);

return config;
};
}
57 changes: 4 additions & 53 deletions packages/react/src/module-federation/with-module-federation.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import {
applyAdditionalShared,
applySharedFunction,
createProjectGraphAsync,
getDependentPackagesForProject,
mapRemotes,
ModuleFederationConfig,
ProjectConfiguration,
ProjectGraph,
readCachedProjectGraph,
sharePackages,
shareWorkspaceLibraries,
} from '@nrwl/devkit';
import { ModuleFederationConfig } from '@nrwl/devkit';
import { readCachedProjectConfiguration } from 'nx/src/project-graph/project-graph';
import { getModuleFederationConfig } from './utils';
import ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');

function determineRemoteUrl(remote: string) {
Expand All @@ -34,42 +23,9 @@ function determineRemoteUrl(remote: string) {

export async function withModuleFederation(options: ModuleFederationConfig) {
const reactWebpackConfig = require('../../plugins/webpack');
let projectGraph: ProjectGraph<ProjectConfiguration>;
try {
projectGraph = readCachedProjectGraph();
} catch (e) {
projectGraph = await createProjectGraphAsync();
}

const project = projectGraph.nodes[options.name]?.data;

if (!project) {
throw Error(
`Cannot find project "${options.name}". Check that the name is correct in module-federation.config.js`
);
}

const dependencies = getDependentPackagesForProject(
projectGraph,
options.name
);
const sharedLibraries = shareWorkspaceLibraries(
dependencies.workspaceLibraries
);

const npmPackages = sharePackages(dependencies.npmPackages);

const sharedDependencies = {
...sharedLibraries.getLibraries(),
...npmPackages,
};

applySharedFunction(sharedDependencies, options.shared);
applyAdditionalShared(
sharedDependencies,
options.additionalShared,
projectGraph
);
const { sharedDependencies, sharedLibraries, mappedRemotes } =
await getModuleFederationConfig(options, determineRemoteUrl);

return (config) => {
config = reactWebpackConfig(config);
Expand All @@ -85,11 +41,6 @@ export async function withModuleFederation(options: ModuleFederationConfig) {
outputModule: true,
};

const mappedRemotes =
!options.remotes || options.remotes.length === 0
? {}
: mapRemotes(options.remotes, 'js', determineRemoteUrl);

config.plugins.push(
new ModuleFederationPlugin({
name: options.name,
Expand Down
1 change: 1 addition & 0 deletions scripts/depcheck/missing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const IGNORE_MATCHES_IN_PACKAGE = {
'stylus-loader',
'swc-loader',
'tsconfig-paths-webpack-plugin',
'@module-federation/node',
],
rollup: ['@swc/core'],
storybook: [
Expand Down

0 comments on commit 8be2980

Please sign in to comment.