Skip to content

Commit 6dd1385

Browse files
fyodorovandreiColy010
andauthoredApr 27, 2023
feat(react): refactor util getModuleFederationConfig to avoid to pass function to determinate the remote url (#16488)
Co-authored-by: Colum Ferry <cferry09@gmail.com>
1 parent 0947eb4 commit 6dd1385

File tree

6 files changed

+68
-82
lines changed

6 files changed

+68
-82
lines changed
 

‎packages/angular/src/utils/mf/utils.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ProjectGraph,
1616
readCachedProjectGraph,
1717
} from '@nx/devkit';
18+
import { readCachedProjectConfiguration } from 'nx/src/project-graph/project-graph';
1819

1920
export function applyDefaultEagerPackages(
2021
sharedConfig: Record<string, SharedLibraryConfig>
@@ -42,10 +43,35 @@ export const DEFAULT_ANGULAR_PACKAGES_TO_SHARE = [
4243
'@angular/common',
4344
];
4445

46+
export function getFunctionDeterminateRemoteUrl(isServer: boolean = false) {
47+
const target = 'serve';
48+
const remoteEntry = isServer ? 'server/remoteEntry.js' : 'remoteEntry.mjs';
49+
50+
return function (remote: string) {
51+
const remoteConfiguration = readCachedProjectConfiguration(remote);
52+
const serveTarget = remoteConfiguration?.targets?.[target];
53+
54+
if (!serveTarget) {
55+
throw new Error(
56+
`Cannot automatically determine URL of remote (${remote}). Looked for property "host" in the project's "serve" target.\n
57+
You can also use the tuple syntax in your webpack config to configure your remotes. e.g. \`remotes: [['remote1', 'http://localhost:4201']]\``
58+
);
59+
}
60+
61+
const host = serveTarget.options?.host ?? 'http://localhost';
62+
const port = serveTarget.options?.port ?? 4201;
63+
return `${
64+
host.endsWith('/') ? host.slice(0, -1) : host
65+
}:${port}/${remoteEntry}`;
66+
};
67+
}
68+
4569
export async function getModuleFederationConfig(
4670
mfConfig: ModuleFederationConfig,
47-
determineRemoteUrl: (remote: string) => string,
48-
options: { isServer: boolean } = { isServer: false }
71+
options: {
72+
isServer: boolean;
73+
determineRemoteUrl?: (remote: string) => string;
74+
} = { isServer: false }
4975
) {
5076
let projectGraph: ProjectGraph;
5177
try {
@@ -107,11 +133,14 @@ export async function getModuleFederationConfig(
107133
mfConfig.additionalShared,
108134
projectGraph
109135
);
136+
const determineRemoteUrlFn =
137+
options.determineRemoteUrl ||
138+
getFunctionDeterminateRemoteUrl(options.isServer);
110139

111140
const mapRemotesFunction = options.isServer ? mapRemotesForSSR : mapRemotes;
112141
const mappedRemotes =
113142
!mfConfig.remotes || mfConfig.remotes.length === 0
114143
? {}
115-
: mapRemotesFunction(mfConfig.remotes, 'mjs', determineRemoteUrl);
144+
: mapRemotesFunction(mfConfig.remotes, 'mjs', determineRemoteUrlFn);
116145
return { sharedLibraries, sharedDependencies, mappedRemotes };
117146
}

‎packages/angular/src/utils/mf/with-module-federation-ssr.ts

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
1-
import { readCachedProjectConfiguration } from 'nx/src/project-graph/project-graph';
21
import { ModuleFederationConfig } from '@nx/devkit/src/utils/module-federation';
32
import { getModuleFederationConfig } from './utils';
43

5-
function determineRemoteUrl(remote: string) {
6-
const remoteProjectConfiguration = readCachedProjectConfiguration(remote);
7-
let publicHost = '';
8-
try {
9-
publicHost = remoteProjectConfiguration.targets.serve.options.publicHost;
10-
} catch (error) {
11-
throw new Error(
12-
`Cannot automatically determine URL of remote (${remote}). Looked for property "publicHost" in the project's "serve" target.\n
13-
You can also use the tuple syntax in your webpack config to configure your remotes. e.g. \`remotes: [['remote1', 'http://localhost:4201']]\``
14-
);
15-
}
16-
return `${
17-
publicHost.endsWith('/') ? publicHost.slice(0, -1) : publicHost
18-
}/server/remoteEntry.js`;
19-
}
20-
214
export async function withModuleFederationForSSR(
225
options: ModuleFederationConfig
236
) {
247
const { sharedLibraries, sharedDependencies, mappedRemotes } =
25-
await getModuleFederationConfig(options, determineRemoteUrl, {
8+
await getModuleFederationConfig(options, {
269
isServer: true,
2710
});
2811

‎packages/angular/src/utils/mf/with-module-federation.ts

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
11
import { ModuleFederationConfig } from '@nx/devkit/src/utils/module-federation';
22
import { getModuleFederationConfig } from './utils';
3-
import { readCachedProjectConfiguration } from 'nx/src/project-graph/project-graph';
43
import ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
54

6-
function determineRemoteUrl(remote: string) {
7-
const remoteProjectConfiguration = readCachedProjectConfiguration(remote);
8-
let publicHost = '';
9-
try {
10-
publicHost = remoteProjectConfiguration.targets.serve.options.publicHost;
11-
} catch (error) {
12-
throw new Error(
13-
`Cannot automatically determine URL of remote (${remote}). Looked for property "publicHost" in the project's "serve" target.\n
14-
You can also use the tuple syntax in your webpack config to configure your remotes. e.g. \`remotes: [['remote1', 'http://localhost:4201']]\``
15-
);
16-
}
17-
return `${
18-
publicHost.endsWith('/') ? publicHost.slice(0, -1) : publicHost
19-
}/remoteEntry.mjs`;
20-
}
21-
225
export async function withModuleFederation(options: ModuleFederationConfig) {
236
const { sharedLibraries, sharedDependencies, mappedRemotes } =
24-
await getModuleFederationConfig(options, determineRemoteUrl);
7+
await getModuleFederationConfig(options);
258

269
return (config) => ({
2710
...(config ?? {}),

‎packages/react/src/module-federation/utils.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,37 @@ import {
1414
ProjectGraph,
1515
readCachedProjectGraph,
1616
} from '@nx/devkit';
17+
import { readCachedProjectConfiguration } from 'nx/src/project-graph/project-graph';
18+
19+
export function getFunctionDeterminateRemoteUrl(isServer: boolean = false) {
20+
const target = isServer ? 'serve-server' : 'serve';
21+
const remoteEntry = isServer ? 'server/remoteEntry.js' : 'remoteEntry.js';
22+
23+
return function (remote: string) {
24+
const remoteConfiguration = readCachedProjectConfiguration(remote);
25+
const serveTarget = remoteConfiguration?.targets?.[target];
26+
27+
if (!serveTarget) {
28+
throw new Error(
29+
`Cannot automatically determine URL of remote (${remote}). Looked for property "host" in the project's "${serveTarget}" target.\n
30+
You can also use the tuple syntax in your webpack config to configure your remotes. e.g. \`remotes: [['remote1', 'http://localhost:4201']]\``
31+
);
32+
}
33+
34+
const host = serveTarget.options?.host ?? 'http://localhost';
35+
const port = serveTarget.options?.port ?? 4201;
36+
return `${
37+
host.endsWith('/') ? host.slice(0, -1) : host
38+
}:${port}/${remoteEntry}`;
39+
};
40+
}
1741

1842
export async function getModuleFederationConfig(
1943
mfConfig: ModuleFederationConfig,
20-
determineRemoteUrl: (remote: string) => string,
21-
options: { isServer: boolean } = { isServer: false }
44+
options: {
45+
isServer: boolean;
46+
determineRemoteUrl?: (remote: string) => string;
47+
} = { isServer: false }
2248
) {
2349
let projectGraph: ProjectGraph;
2450
try {
@@ -68,10 +94,13 @@ export async function getModuleFederationConfig(
6894
);
6995

7096
const mapRemotesFunction = options.isServer ? mapRemotesForSSR : mapRemotes;
97+
const determineRemoteUrlFn =
98+
options.determineRemoteUrl ||
99+
getFunctionDeterminateRemoteUrl(options.isServer);
71100
const mappedRemotes =
72101
!mfConfig.remotes || mfConfig.remotes.length === 0
73102
? {}
74-
: mapRemotesFunction(mfConfig.remotes, 'js', determineRemoteUrl);
103+
: mapRemotesFunction(mfConfig.remotes, 'js', determineRemoteUrlFn);
75104

76105
return { sharedLibraries, sharedDependencies, mappedRemotes };
77106
}

‎packages/react/src/module-federation/with-module-federation-ssr.ts

+1-20
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,13 @@
11
import { ModuleFederationConfig } from '@nx/devkit/src/utils/module-federation';
2-
import { readCachedProjectConfiguration } from 'nx/src/project-graph/project-graph';
32
import { getModuleFederationConfig } from './utils';
43

5-
function determineRemoteUrl(remote: string) {
6-
const remoteConfiguration = readCachedProjectConfiguration(remote);
7-
const serveTarget = remoteConfiguration?.targets?.['serve-server'];
8-
9-
if (!serveTarget) {
10-
throw new Error(
11-
`Cannot automatically determine URL of remote (${remote}). Looked for property "host" in the project's "serve-server" target.\n
12-
You can also use the tuple syntax in your webpack config to configure your remotes. e.g. \`remotes: [['remote1', 'http://localhost:4201']]\``
13-
);
14-
}
15-
16-
const host = serveTarget.options?.host ?? 'http://localhost';
17-
const port = serveTarget.options?.port ?? 4201;
18-
return `${
19-
host.endsWith('/') ? host.slice(0, -1) : host
20-
}:${port}/server/remoteEntry.js`;
21-
}
22-
234
export async function withModuleFederationForSSR(
245
options: ModuleFederationConfig
256
) {
267
const reactWebpackConfig = require('../../plugins/webpack');
278

289
const { sharedLibraries, sharedDependencies, mappedRemotes } =
29-
await getModuleFederationConfig(options, determineRemoteUrl, {
10+
await getModuleFederationConfig(options, {
3011
isServer: true,
3112
});
3213

‎packages/react/src/module-federation/with-module-federation.ts

+1-20
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,8 @@
11
import { ModuleFederationConfig } from '@nx/devkit/src/utils/module-federation';
2-
import { readCachedProjectConfiguration } from 'nx/src/project-graph/project-graph';
32
import { getModuleFederationConfig } from './utils';
43
import type { AsyncNxWebpackPlugin } from '@nx/webpack';
54
import ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
65

7-
function determineRemoteUrl(remote: string) {
8-
const remoteConfiguration = readCachedProjectConfiguration(remote);
9-
const serveTarget = remoteConfiguration?.targets?.serve;
10-
11-
if (!serveTarget) {
12-
throw new Error(
13-
`Cannot automatically determine URL of remote (${remote}). Looked for property "host" in the project's "serve" target.\n
14-
You can also use the tuple syntax in your webpack config to configure your remotes. e.g. \`remotes: [['remote1', 'http://localhost:4201']]\``
15-
);
16-
}
17-
18-
const host = serveTarget.options?.host ?? 'http://localhost';
19-
const port = serveTarget.options?.port ?? 4201;
20-
return `${
21-
host.endsWith('/') ? host.slice(0, -1) : host
22-
}:${port}/remoteEntry.js`;
23-
}
24-
256
/**
267
* @param {ModuleFederationConfig} options
278
* @return {Promise<AsyncNxWebpackPlugin>}
@@ -32,7 +13,7 @@ export async function withModuleFederation(
3213
const reactWebpackConfig = require('../../plugins/webpack');
3314

3415
const { sharedDependencies, sharedLibraries, mappedRemotes } =
35-
await getModuleFederationConfig(options, determineRemoteUrl);
16+
await getModuleFederationConfig(options);
3617

3718
return (config, ctx) => {
3819
config = reactWebpackConfig(config, ctx);

1 commit comments

Comments
 (1)

vercel[bot] commented on Apr 27, 2023

@vercel[bot]

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-five.vercel.app

Please sign in to comment.