@nx/module-federation 0.0.0-pr-29047-57d3073 → 0.0.0-pr-28808-eac89e6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/rspack.d.ts +2 -0
- package/rspack.js +5 -0
- package/src/with-module-federation/rspack/utils.d.ts +12 -0
- package/src/with-module-federation/rspack/utils.js +74 -0
- package/src/with-module-federation/rspack/with-module-federation-ssr.d.ts +4 -0
- package/src/with-module-federation/rspack/with-module-federation-ssr.js +62 -0
- package/src/with-module-federation/rspack/with-module-federation.d.ts +9 -0
- package/src/with-module-federation/rspack/with-module-federation.js +70 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nx/module-federation",
|
|
3
3
|
"description": "The Nx Plugin for Module Federation contains executors and utilities that support building applications using Module Federation.",
|
|
4
|
-
"version": "0.0.0-pr-
|
|
4
|
+
"version": "0.0.0-pr-28808-eac89e6",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -25,11 +25,11 @@
|
|
|
25
25
|
"executors": "./executors.json",
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"tslib": "^2.3.0",
|
|
28
|
-
"@nx/devkit": "0.0.0-pr-
|
|
29
|
-
"@nx/js": "0.0.0-pr-
|
|
28
|
+
"@nx/devkit": "0.0.0-pr-28808-eac89e6",
|
|
29
|
+
"@nx/js": "0.0.0-pr-28808-eac89e6",
|
|
30
30
|
"picocolors": "^1.1.0",
|
|
31
31
|
"webpack": "5.88.0",
|
|
32
|
-
"@rspack/core": "1.1.
|
|
32
|
+
"@rspack/core": "1.1.3",
|
|
33
33
|
"@module-federation/enhanced": "0.7.6",
|
|
34
34
|
"@module-federation/node": "2.6.11",
|
|
35
35
|
"@module-federation/sdk": "0.7.6",
|
package/rspack.d.ts
ADDED
package/rspack.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./src/with-module-federation/rspack/with-module-federation"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./src/with-module-federation/rspack/with-module-federation-ssr"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ModuleFederationConfig } from '../../utils';
|
|
2
|
+
export declare function getFunctionDeterminateRemoteUrl(isServer?: boolean): (remote: string) => string;
|
|
3
|
+
export declare function getModuleFederationConfig(mfConfig: ModuleFederationConfig, options?: {
|
|
4
|
+
isServer: boolean;
|
|
5
|
+
determineRemoteUrl?: (remote: string) => string;
|
|
6
|
+
}): Promise<{
|
|
7
|
+
sharedLibraries: import("../../utils").SharedWorkspaceLibraryConfig;
|
|
8
|
+
sharedDependencies: {
|
|
9
|
+
[x: string]: import("../../utils").SharedLibraryConfig;
|
|
10
|
+
};
|
|
11
|
+
mappedRemotes: {};
|
|
12
|
+
}>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getFunctionDeterminateRemoteUrl = getFunctionDeterminateRemoteUrl;
|
|
4
|
+
exports.getModuleFederationConfig = getModuleFederationConfig;
|
|
5
|
+
const devkit_1 = require("@nx/devkit");
|
|
6
|
+
const project_graph_1 = require("nx/src/project-graph/project-graph");
|
|
7
|
+
const utils_1 = require("../../utils");
|
|
8
|
+
function getFunctionDeterminateRemoteUrl(isServer = false) {
|
|
9
|
+
const target = 'serve';
|
|
10
|
+
const remoteEntry = isServer ? 'server/remoteEntry.js' : 'remoteEntry.js';
|
|
11
|
+
return function (remote) {
|
|
12
|
+
const mappedStaticRemotesFromEnv = process.env
|
|
13
|
+
.NX_MF_DEV_SERVER_STATIC_REMOTES
|
|
14
|
+
? JSON.parse(process.env.NX_MF_DEV_SERVER_STATIC_REMOTES)
|
|
15
|
+
: undefined;
|
|
16
|
+
if (mappedStaticRemotesFromEnv && mappedStaticRemotesFromEnv[remote]) {
|
|
17
|
+
return `${mappedStaticRemotesFromEnv[remote]}/${remoteEntry}`;
|
|
18
|
+
}
|
|
19
|
+
let remoteConfiguration = null;
|
|
20
|
+
try {
|
|
21
|
+
remoteConfiguration = (0, project_graph_1.readCachedProjectConfiguration)(remote);
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
throw new Error(`Cannot find remote: "${remote}". Check that the remote name is correct in your module federation config file.\n`);
|
|
25
|
+
}
|
|
26
|
+
const serveTarget = remoteConfiguration?.targets?.[target];
|
|
27
|
+
if (!serveTarget) {
|
|
28
|
+
throw new Error(`Cannot automatically determine URL of remote (${remote}). Looked for property "host" in the project's "${serveTarget}" target.\n
|
|
29
|
+
You can also use the tuple syntax in your rspack config to configure your remotes. e.g. \`remotes: [['remote1', 'http://localhost:4201']]\``);
|
|
30
|
+
}
|
|
31
|
+
const host = serveTarget.options?.host ??
|
|
32
|
+
`http${serveTarget.options.ssl ? 's' : ''}://localhost`;
|
|
33
|
+
const port = serveTarget.options?.port ?? 4201;
|
|
34
|
+
return `${host.endsWith('/') ? host.slice(0, -1) : host}:${port}/${remoteEntry}`;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
async function getModuleFederationConfig(mfConfig, options = { isServer: false }) {
|
|
38
|
+
let projectGraph;
|
|
39
|
+
try {
|
|
40
|
+
projectGraph = (0, devkit_1.readCachedProjectGraph)();
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
projectGraph = await (0, devkit_1.createProjectGraphAsync)();
|
|
44
|
+
}
|
|
45
|
+
const project = projectGraph.nodes[mfConfig.name]?.data;
|
|
46
|
+
if (!project) {
|
|
47
|
+
throw Error(`Cannot find project "${mfConfig.name}". Check that the name is correct in module-federation.config.js`);
|
|
48
|
+
}
|
|
49
|
+
const dependencies = (0, utils_1.getDependentPackagesForProject)(projectGraph, mfConfig.name);
|
|
50
|
+
if (mfConfig.shared) {
|
|
51
|
+
dependencies.workspaceLibraries = dependencies.workspaceLibraries.filter((lib) => mfConfig.shared(lib.importKey, {}) !== false);
|
|
52
|
+
dependencies.npmPackages = dependencies.npmPackages.filter((pkg) => mfConfig.shared(pkg, {}) !== false);
|
|
53
|
+
}
|
|
54
|
+
const sharedLibraries = (0, utils_1.shareWorkspaceLibraries)(dependencies.workspaceLibraries);
|
|
55
|
+
const npmPackages = (0, utils_1.sharePackages)(dependencies.npmPackages);
|
|
56
|
+
const sharedDependencies = {
|
|
57
|
+
...sharedLibraries.getLibraries(project.root),
|
|
58
|
+
...npmPackages,
|
|
59
|
+
};
|
|
60
|
+
(0, utils_1.applySharedFunction)(sharedDependencies, mfConfig.shared);
|
|
61
|
+
(0, utils_1.applyAdditionalShared)(sharedDependencies, mfConfig.additionalShared, projectGraph);
|
|
62
|
+
// Choose the correct mapRemotes function based on the server state.
|
|
63
|
+
const mapRemotesFunction = options.isServer ? utils_1.mapRemotesForSSR : utils_1.mapRemotes;
|
|
64
|
+
// Determine the URL function, either from provided options or by using a default.
|
|
65
|
+
const determineRemoteUrlFunction = options.determineRemoteUrl
|
|
66
|
+
? options.determineRemoteUrl
|
|
67
|
+
: getFunctionDeterminateRemoteUrl(options.isServer);
|
|
68
|
+
// Map the remotes if they exist, otherwise default to an empty object.
|
|
69
|
+
let mappedRemotes = {};
|
|
70
|
+
if (mfConfig.remotes && mfConfig.remotes.length > 0) {
|
|
71
|
+
mappedRemotes = mapRemotesFunction(mfConfig.remotes, 'js', determineRemoteUrlFunction, true);
|
|
72
|
+
}
|
|
73
|
+
return { sharedLibraries, sharedDependencies, mappedRemotes };
|
|
74
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ModuleFederationConfig, NxModuleFederationConfigOverride } from '../../utils';
|
|
2
|
+
export declare function withModuleFederationForSSR(options: ModuleFederationConfig, configOverride?: NxModuleFederationConfigOverride): Promise<(config: any, { context }: {
|
|
3
|
+
context: any;
|
|
4
|
+
}) => any>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withModuleFederationForSSR = withModuleFederationForSSR;
|
|
4
|
+
const core_1 = require("@rspack/core");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
async function withModuleFederationForSSR(options, configOverride) {
|
|
7
|
+
if (global.NX_GRAPH_CREATION) {
|
|
8
|
+
return (config) => config;
|
|
9
|
+
}
|
|
10
|
+
const { sharedLibraries, sharedDependencies, mappedRemotes } = await (0, utils_1.getModuleFederationConfig)(options, {
|
|
11
|
+
isServer: true,
|
|
12
|
+
});
|
|
13
|
+
return (config, { context }) => {
|
|
14
|
+
config.target = 'async-node';
|
|
15
|
+
config.output.uniqueName = options.name;
|
|
16
|
+
config.output.library = {
|
|
17
|
+
type: 'commonjs-module',
|
|
18
|
+
};
|
|
19
|
+
config.optimization = {
|
|
20
|
+
...(config.optimization ?? {}),
|
|
21
|
+
runtimeChunk: false,
|
|
22
|
+
};
|
|
23
|
+
config.plugins.push(
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
25
|
+
new (require('@module-federation/enhanced/rspack').ModuleFederationPlugin)({
|
|
26
|
+
name: options.name.replace(/-/g, '_'),
|
|
27
|
+
filename: 'remoteEntry.js',
|
|
28
|
+
exposes: options.exposes,
|
|
29
|
+
remotes: mappedRemotes,
|
|
30
|
+
shared: {
|
|
31
|
+
...sharedDependencies,
|
|
32
|
+
},
|
|
33
|
+
isServer: true,
|
|
34
|
+
library: {
|
|
35
|
+
type: 'commonjs-module',
|
|
36
|
+
},
|
|
37
|
+
remoteType: 'script',
|
|
38
|
+
/**
|
|
39
|
+
* Apply user-defined config overrides
|
|
40
|
+
*/
|
|
41
|
+
...(configOverride ? configOverride : {}),
|
|
42
|
+
runtimePlugins: process.env.NX_MF_DEV_REMOTES &&
|
|
43
|
+
!options.disableNxRuntimeLibraryControlPlugin
|
|
44
|
+
? [
|
|
45
|
+
...(configOverride?.runtimePlugins ?? []),
|
|
46
|
+
require.resolve('@module-federation/node/runtimePlugin'),
|
|
47
|
+
require.resolve('@nx/module-federation/src/utils/plugins/runtime-library-control.plugin.js'),
|
|
48
|
+
]
|
|
49
|
+
: [
|
|
50
|
+
...(configOverride?.runtimePlugins ?? []),
|
|
51
|
+
require.resolve('@module-federation/node/runtimePlugin'),
|
|
52
|
+
],
|
|
53
|
+
virtualRuntimeEntry: true,
|
|
54
|
+
}, {}), sharedLibraries.getReplacementPlugin());
|
|
55
|
+
// The env var is only set from the module-federation-dev-server
|
|
56
|
+
// Attach the runtime plugin
|
|
57
|
+
config.plugins.push(new core_1.DefinePlugin({
|
|
58
|
+
'process.env.NX_MF_DEV_REMOTES': process.env.NX_MF_DEV_REMOTES,
|
|
59
|
+
}));
|
|
60
|
+
return config;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Configuration } from '@rspack/core';
|
|
2
|
+
import { ModuleFederationConfig, NxModuleFederationConfigOverride } from '../../utils';
|
|
3
|
+
/**
|
|
4
|
+
* @param {ModuleFederationConfig} options
|
|
5
|
+
* @param {NxModuleFederationConfigOverride} configOverride
|
|
6
|
+
*/
|
|
7
|
+
export declare function withModuleFederation(options: ModuleFederationConfig, configOverride?: NxModuleFederationConfigOverride): Promise<(config: Configuration, { context }: {
|
|
8
|
+
context: any;
|
|
9
|
+
}) => Configuration>;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withModuleFederation = withModuleFederation;
|
|
4
|
+
const rspack_1 = require("@module-federation/enhanced/rspack");
|
|
5
|
+
const core_1 = require("@rspack/core");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
const isVarOrWindow = (libType) => libType === 'var' || libType === 'window';
|
|
8
|
+
/**
|
|
9
|
+
* @param {ModuleFederationConfig} options
|
|
10
|
+
* @param {NxModuleFederationConfigOverride} configOverride
|
|
11
|
+
*/
|
|
12
|
+
async function withModuleFederation(options, configOverride) {
|
|
13
|
+
if (global.NX_GRAPH_CREATION) {
|
|
14
|
+
return function makeConfig(config) {
|
|
15
|
+
return config;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
const { sharedDependencies, sharedLibraries, mappedRemotes } = await (0, utils_1.getModuleFederationConfig)(options);
|
|
19
|
+
const isGlobal = isVarOrWindow(options.library?.type);
|
|
20
|
+
return function makeConfig(config, { context }) {
|
|
21
|
+
config.output.uniqueName = options.name;
|
|
22
|
+
config.output.publicPath = 'auto';
|
|
23
|
+
if (isGlobal) {
|
|
24
|
+
config.output.scriptType = 'text/javascript';
|
|
25
|
+
}
|
|
26
|
+
config.optimization = {
|
|
27
|
+
...(config.optimization ?? {}),
|
|
28
|
+
runtimeChunk: false,
|
|
29
|
+
};
|
|
30
|
+
if (config.mode === 'development' &&
|
|
31
|
+
Object.keys(mappedRemotes).length > 1 &&
|
|
32
|
+
!options.exposes) {
|
|
33
|
+
config.optimization.runtimeChunk = 'single';
|
|
34
|
+
}
|
|
35
|
+
config.plugins.push(new rspack_1.ModuleFederationPlugin({
|
|
36
|
+
name: options.name.replace(/-/g, '_'),
|
|
37
|
+
filename: 'remoteEntry.js',
|
|
38
|
+
exposes: options.exposes,
|
|
39
|
+
remotes: mappedRemotes,
|
|
40
|
+
shared: {
|
|
41
|
+
...sharedDependencies,
|
|
42
|
+
},
|
|
43
|
+
/**
|
|
44
|
+
* remoteType: 'script' is required for the remote to be loaded as a script tag.
|
|
45
|
+
* remotes will need to be defined as:
|
|
46
|
+
* { appX: 'appX@http://localhost:3001/remoteEntry.js' }
|
|
47
|
+
* { appY: 'appY@http://localhost:3002/remoteEntry.js' }
|
|
48
|
+
*/
|
|
49
|
+
...(isGlobal ? { remoteType: 'script' } : {}),
|
|
50
|
+
/**
|
|
51
|
+
* Apply user-defined config overrides
|
|
52
|
+
*/
|
|
53
|
+
...(configOverride ? configOverride : {}),
|
|
54
|
+
runtimePlugins: process.env.NX_MF_DEV_REMOTES &&
|
|
55
|
+
!options.disableNxRuntimeLibraryControlPlugin
|
|
56
|
+
? [
|
|
57
|
+
...(configOverride?.runtimePlugins ?? []),
|
|
58
|
+
require.resolve('@nx/module-federation/src/utils/plugins/runtime-library-control.plugin.js'),
|
|
59
|
+
]
|
|
60
|
+
: configOverride?.runtimePlugins,
|
|
61
|
+
virtualRuntimeEntry: true,
|
|
62
|
+
}), sharedLibraries.getReplacementPlugin());
|
|
63
|
+
// The env var is only set from the module-federation-dev-server
|
|
64
|
+
// Attach the runtime plugin
|
|
65
|
+
config.plugins.push(new core_1.DefinePlugin({
|
|
66
|
+
'process.env.NX_MF_DEV_REMOTES': process.env.NX_MF_DEV_REMOTES,
|
|
67
|
+
}));
|
|
68
|
+
return config;
|
|
69
|
+
};
|
|
70
|
+
}
|