@module-federation/modern-js 0.11.3 → 0.11.4
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/dist/cjs/cli/configPlugin.js +258 -11
- package/dist/cjs/cli/{utils.spec.js → configPlugin.spec.js} +3 -2
- package/dist/cjs/cli/index.js +1 -1
- package/dist/cjs/cli/manifest.js +13 -9
- package/dist/cjs/cli/ssrPlugin.js +58 -10
- package/dist/cjs/cli/utils.js +0 -235
- package/dist/cjs/constant.js +0 -3
- package/dist/esm/cli/configPlugin.js +278 -8
- package/dist/esm/cli/{utils.spec.js → configPlugin.spec.js} +2 -1
- package/dist/esm/cli/index.js +1 -1
- package/dist/esm/cli/manifest.js +13 -9
- package/dist/esm/cli/ssrPlugin.js +61 -11
- package/dist/esm/cli/utils.js +0 -260
- package/dist/esm/constant.js +0 -2
- package/dist/esm-node/cli/configPlugin.js +251 -8
- package/dist/esm-node/cli/{utils.spec.js → configPlugin.spec.js} +2 -1
- package/dist/esm-node/cli/index.js +1 -1
- package/dist/esm-node/cli/manifest.js +13 -9
- package/dist/esm-node/cli/ssrPlugin.js +58 -10
- package/dist/esm-node/cli/utils.js +0 -231
- package/dist/esm-node/constant.js +0 -2
- package/dist/types/cli/configPlugin.d.ts +16 -2
- package/dist/types/cli/manifest.d.ts +1 -1
- package/dist/types/cli/utils.d.ts +1 -14
- package/dist/types/constant.d.ts +0 -1
- package/dist/types/types/index.d.ts +3 -1
- package/package.json +8 -7
- /package/dist/types/cli/{utils.spec.d.ts → configPlugin.spec.d.ts} +0 -0
|
@@ -1,11 +1,237 @@
|
|
|
1
1
|
import path from "path";
|
|
2
|
-
import {
|
|
2
|
+
import { getIPV4, isWebTarget, skipByTarget } from "./utils";
|
|
3
|
+
import { encodeName } from "@module-federation/sdk";
|
|
4
|
+
import { bundle } from "@modern-js/node-bundle-require";
|
|
5
|
+
import { LOCALHOST, PLUGIN_IDENTIFIER } from "../constant";
|
|
6
|
+
import { autoDeleteSplitChunkCacheGroups } from "@module-federation/rsbuild-plugin/utils";
|
|
7
|
+
import logger from "./logger";
|
|
8
|
+
const defaultPath = path.resolve(process.cwd(), "module-federation.config.ts");
|
|
9
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
3
10
|
function setEnv(enableSSR) {
|
|
4
11
|
if (enableSSR) {
|
|
5
12
|
process.env["MF_DISABLE_EMIT_STATS"] = "true";
|
|
6
13
|
process.env["MF_SSR_PRJ"] = "true";
|
|
7
14
|
}
|
|
8
15
|
}
|
|
16
|
+
const getMFConfig = async (userConfig) => {
|
|
17
|
+
const { config, configPath } = userConfig;
|
|
18
|
+
if (config) {
|
|
19
|
+
return config;
|
|
20
|
+
}
|
|
21
|
+
const mfConfigPath = configPath ? configPath : defaultPath;
|
|
22
|
+
const preBundlePath = await bundle(mfConfigPath);
|
|
23
|
+
const mfConfig = (await import(preBundlePath)).default;
|
|
24
|
+
return mfConfig;
|
|
25
|
+
};
|
|
26
|
+
const injectRuntimePlugins = (runtimePlugin, runtimePlugins) => {
|
|
27
|
+
if (!runtimePlugins.includes(runtimePlugin)) {
|
|
28
|
+
runtimePlugins.push(runtimePlugin);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const replaceRemoteUrl = (mfConfig, remoteIpStrategy) => {
|
|
32
|
+
if (remoteIpStrategy && remoteIpStrategy === "inherit") {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (!mfConfig.remotes) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const ipv4 = getIPV4();
|
|
39
|
+
const handleRemoteObject = (remoteObject) => {
|
|
40
|
+
Object.keys(remoteObject).forEach((remoteKey) => {
|
|
41
|
+
const remote = remoteObject[remoteKey];
|
|
42
|
+
if (Array.isArray(remote)) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (typeof remote === "string" && remote.includes(LOCALHOST)) {
|
|
46
|
+
remoteObject[remoteKey] = remote.replace(LOCALHOST, ipv4);
|
|
47
|
+
}
|
|
48
|
+
if (typeof remote === "object" && !Array.isArray(remote.external) && remote.external.includes(LOCALHOST)) {
|
|
49
|
+
remote.external = remote.external.replace(LOCALHOST, ipv4);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
if (Array.isArray(mfConfig.remotes)) {
|
|
54
|
+
mfConfig.remotes.forEach((remoteObject) => {
|
|
55
|
+
if (typeof remoteObject === "string") {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
handleRemoteObject(remoteObject);
|
|
59
|
+
});
|
|
60
|
+
} else if (typeof mfConfig.remotes !== "string") {
|
|
61
|
+
handleRemoteObject(mfConfig.remotes);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const patchDTSConfig = (mfConfig, isServer) => {
|
|
65
|
+
if (isServer) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const ModernJSRuntime = "@modern-js/runtime/mf";
|
|
69
|
+
if (mfConfig.dts !== false) {
|
|
70
|
+
var _mfConfig_dts, _mfConfig_dts1;
|
|
71
|
+
if (typeof mfConfig.dts === "boolean" || mfConfig.dts === void 0) {
|
|
72
|
+
mfConfig.dts = {
|
|
73
|
+
consumeTypes: {
|
|
74
|
+
runtimePkgs: [
|
|
75
|
+
ModernJSRuntime
|
|
76
|
+
]
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
} else if (((_mfConfig_dts = mfConfig.dts) === null || _mfConfig_dts === void 0 ? void 0 : _mfConfig_dts.consumeTypes) || ((_mfConfig_dts1 = mfConfig.dts) === null || _mfConfig_dts1 === void 0 ? void 0 : _mfConfig_dts1.consumeTypes) === void 0) {
|
|
80
|
+
var _mfConfig_dts2;
|
|
81
|
+
if (typeof mfConfig.dts.consumeTypes === "boolean" || ((_mfConfig_dts2 = mfConfig.dts) === null || _mfConfig_dts2 === void 0 ? void 0 : _mfConfig_dts2.consumeTypes) === void 0) {
|
|
82
|
+
mfConfig.dts.consumeTypes = {
|
|
83
|
+
runtimePkgs: [
|
|
84
|
+
ModernJSRuntime
|
|
85
|
+
]
|
|
86
|
+
};
|
|
87
|
+
} else {
|
|
88
|
+
mfConfig.dts.consumeTypes.runtimePkgs = mfConfig.dts.consumeTypes.runtimePkgs || [];
|
|
89
|
+
if (!mfConfig.dts.consumeTypes.runtimePkgs.includes(ModernJSRuntime)) {
|
|
90
|
+
mfConfig.dts.consumeTypes.runtimePkgs.push(ModernJSRuntime);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const patchMFConfig = (mfConfig, isServer, remoteIpStrategy) => {
|
|
97
|
+
replaceRemoteUrl(mfConfig, remoteIpStrategy);
|
|
98
|
+
if (mfConfig.remoteType === void 0) {
|
|
99
|
+
mfConfig.remoteType = "script";
|
|
100
|
+
}
|
|
101
|
+
if (!mfConfig.name) {
|
|
102
|
+
throw new Error(`${PLUGIN_IDENTIFIER} mfConfig.name can not be empty!`);
|
|
103
|
+
}
|
|
104
|
+
const runtimePlugins = [
|
|
105
|
+
...mfConfig.runtimePlugins || []
|
|
106
|
+
];
|
|
107
|
+
patchDTSConfig(mfConfig, isServer);
|
|
108
|
+
injectRuntimePlugins(require.resolve("@module-federation/modern-js/shared-strategy"), runtimePlugins);
|
|
109
|
+
if (isDev) {
|
|
110
|
+
injectRuntimePlugins(require.resolve("@module-federation/modern-js/resolve-entry-ipv4"), runtimePlugins);
|
|
111
|
+
}
|
|
112
|
+
if (isServer) {
|
|
113
|
+
injectRuntimePlugins(require.resolve("@module-federation/node/runtimePlugin"), runtimePlugins);
|
|
114
|
+
if (isDev) {
|
|
115
|
+
injectRuntimePlugins(require.resolve("@module-federation/node/record-dynamic-remote-entry-hash-plugin"), runtimePlugins);
|
|
116
|
+
}
|
|
117
|
+
injectRuntimePlugins(require.resolve("@module-federation/modern-js/inject-node-fetch"), runtimePlugins);
|
|
118
|
+
if (!mfConfig.library) {
|
|
119
|
+
mfConfig.library = {
|
|
120
|
+
type: "commonjs-module",
|
|
121
|
+
name: mfConfig.name
|
|
122
|
+
};
|
|
123
|
+
} else {
|
|
124
|
+
if (!mfConfig.library.type) {
|
|
125
|
+
mfConfig.library.type = "commonjs-module";
|
|
126
|
+
}
|
|
127
|
+
if (!mfConfig.library.name) {
|
|
128
|
+
mfConfig.library.name = mfConfig.name;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
mfConfig.runtimePlugins = runtimePlugins;
|
|
133
|
+
if (!isServer) {
|
|
134
|
+
var _mfConfig_library;
|
|
135
|
+
if (((_mfConfig_library = mfConfig.library) === null || _mfConfig_library === void 0 ? void 0 : _mfConfig_library.type) === "commonjs-module") {
|
|
136
|
+
mfConfig.library.type = "global";
|
|
137
|
+
}
|
|
138
|
+
return mfConfig;
|
|
139
|
+
}
|
|
140
|
+
mfConfig.dts = false;
|
|
141
|
+
mfConfig.dev = false;
|
|
142
|
+
return mfConfig;
|
|
143
|
+
};
|
|
144
|
+
function patchIgnoreWarning(chain) {
|
|
145
|
+
const ignoreWarnings = chain.get("ignoreWarnings") || [];
|
|
146
|
+
const ignoredMsgs = [
|
|
147
|
+
"external script",
|
|
148
|
+
"process.env.WS_NO_BUFFER_UTIL",
|
|
149
|
+
`Can't resolve 'utf-8-validate`
|
|
150
|
+
];
|
|
151
|
+
ignoreWarnings.push((warning) => {
|
|
152
|
+
if (ignoredMsgs.some((msg) => warning.message.includes(msg))) {
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
});
|
|
157
|
+
chain.ignoreWarnings(ignoreWarnings);
|
|
158
|
+
}
|
|
159
|
+
function addMyTypes2Ignored(chain, mfConfig) {
|
|
160
|
+
const watchOptions = chain.get("watchOptions");
|
|
161
|
+
if (!watchOptions || !watchOptions.ignored) {
|
|
162
|
+
chain.watchOptions({
|
|
163
|
+
ignored: /[\\/](?:\.git|node_modules|@mf-types)[\\/]/
|
|
164
|
+
});
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const ignored = watchOptions.ignored;
|
|
168
|
+
const DEFAULT_IGNORED_GLOB = "**/@mf-types/**";
|
|
169
|
+
if (Array.isArray(ignored)) {
|
|
170
|
+
if (mfConfig.dts !== false && typeof mfConfig.dts === "object" && typeof mfConfig.dts.consumeTypes === "object" && mfConfig.dts.consumeTypes.remoteTypesFolder) {
|
|
171
|
+
chain.watchOptions({
|
|
172
|
+
...watchOptions,
|
|
173
|
+
ignored: ignored.concat(`**/${mfConfig.dts.consumeTypes.remoteTypesFolder}/**`)
|
|
174
|
+
});
|
|
175
|
+
} else {
|
|
176
|
+
chain.watchOptions({
|
|
177
|
+
...watchOptions,
|
|
178
|
+
ignored: ignored.concat(DEFAULT_IGNORED_GLOB)
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (typeof ignored !== "string") {
|
|
184
|
+
chain.watchOptions({
|
|
185
|
+
...watchOptions,
|
|
186
|
+
ignored: /[\\/](?:\.git|node_modules|@mf-types)[\\/]/
|
|
187
|
+
});
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
chain.watchOptions({
|
|
191
|
+
...watchOptions,
|
|
192
|
+
ignored: ignored.concat(DEFAULT_IGNORED_GLOB)
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
function patchBundlerConfig(options) {
|
|
196
|
+
var _modernjsConfig_deploy;
|
|
197
|
+
const { chain, modernjsConfig, isServer, mfConfig, enableSSR } = options;
|
|
198
|
+
chain.optimization.delete("runtimeChunk");
|
|
199
|
+
patchIgnoreWarning(chain);
|
|
200
|
+
if (!chain.output.get("chunkLoadingGlobal")) {
|
|
201
|
+
chain.output.chunkLoadingGlobal(`chunk_${mfConfig.name}`);
|
|
202
|
+
}
|
|
203
|
+
if (!chain.output.get("uniqueName")) {
|
|
204
|
+
chain.output.uniqueName(mfConfig.name);
|
|
205
|
+
}
|
|
206
|
+
const splitChunkConfig = chain.optimization.splitChunks.entries();
|
|
207
|
+
if (!isServer) {
|
|
208
|
+
autoDeleteSplitChunkCacheGroups(mfConfig, splitChunkConfig);
|
|
209
|
+
}
|
|
210
|
+
if (!isServer && enableSSR && splitChunkConfig && typeof splitChunkConfig === "object" && splitChunkConfig.cacheGroups) {
|
|
211
|
+
splitChunkConfig.chunks = "async";
|
|
212
|
+
logger.warn(`splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"`);
|
|
213
|
+
}
|
|
214
|
+
if (isDev && chain.output.get("publicPath") === "auto") {
|
|
215
|
+
var _modernjsConfig_dev, _modernjsConfig_server;
|
|
216
|
+
const port = ((_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.port) || ((_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.port) || 8080;
|
|
217
|
+
const publicPath = `http://localhost:${port}/`;
|
|
218
|
+
chain.output.publicPath(publicPath);
|
|
219
|
+
}
|
|
220
|
+
if (isServer && enableSSR) {
|
|
221
|
+
const uniqueName = mfConfig.name || chain.output.get("uniqueName");
|
|
222
|
+
const chunkFileName = chain.output.get("chunkFilename");
|
|
223
|
+
if (typeof chunkFileName === "string" && uniqueName && !chunkFileName.includes(uniqueName)) {
|
|
224
|
+
const suffix = `${encodeName(uniqueName)}-[chunkhash].js`;
|
|
225
|
+
chain.output.chunkFilename(chunkFileName.replace(".js", suffix));
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (isDev && enableSSR && !isServer) {
|
|
229
|
+
chain.resolve.fallback.set("crypto", false).set("stream", false).set("vm", false);
|
|
230
|
+
}
|
|
231
|
+
if (((_modernjsConfig_deploy = modernjsConfig.deploy) === null || _modernjsConfig_deploy === void 0 ? void 0 : _modernjsConfig_deploy.microFrontend) && Object.keys(mfConfig.exposes || {}).length) {
|
|
232
|
+
chain.optimization.usedExports(false);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
9
235
|
const moduleFederationConfigPlugin = (userConfig) => ({
|
|
10
236
|
name: "@modern-js/plugin-module-federation-config",
|
|
11
237
|
pre: [
|
|
@@ -23,7 +249,7 @@ const moduleFederationConfigPlugin = (userConfig) => ({
|
|
|
23
249
|
userConfig.ssrConfig = ssrConfig;
|
|
24
250
|
userConfig.csrConfig = csrConfig;
|
|
25
251
|
var _userConfig_userConfig_ssr;
|
|
26
|
-
const enableSSR = (_userConfig_userConfig_ssr = (_userConfig_userConfig = userConfig.userConfig) === null || _userConfig_userConfig === void 0 ? void 0 : _userConfig_userConfig.ssr) !== null && _userConfig_userConfig_ssr !== void 0 ? _userConfig_userConfig_ssr : Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
252
|
+
const enableSSR = Boolean((_userConfig_userConfig_ssr = (_userConfig_userConfig = userConfig.userConfig) === null || _userConfig_userConfig === void 0 ? void 0 : _userConfig_userConfig.ssr) !== null && _userConfig_userConfig_ssr !== void 0 ? _userConfig_userConfig_ssr : Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr));
|
|
27
253
|
api.modifyBundlerChain((chain) => {
|
|
28
254
|
const target = chain.get("target");
|
|
29
255
|
if (skipByTarget(target)) {
|
|
@@ -44,7 +270,7 @@ const moduleFederationConfigPlugin = (userConfig) => ({
|
|
|
44
270
|
userConfig.distOutputDir = chain.output.get("path") || path.resolve(process.cwd(), "dist");
|
|
45
271
|
});
|
|
46
272
|
api.config(() => {
|
|
47
|
-
var _modernjsConfig_source, _modernjsConfig_source1, _modernjsConfig_dev;
|
|
273
|
+
var _modernjsConfig_tools, _userConfig_csrConfig, _modernjsConfig_source, _modernjsConfig_source1, _modernjsConfig_dev;
|
|
48
274
|
const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
|
|
49
275
|
const ipv4 = getIPV4();
|
|
50
276
|
if (userConfig.remoteIpStrategy === void 0) {
|
|
@@ -54,15 +280,28 @@ const moduleFederationConfigPlugin = (userConfig) => ({
|
|
|
54
280
|
userConfig.remoteIpStrategy = "ipv4";
|
|
55
281
|
}
|
|
56
282
|
}
|
|
283
|
+
const devServerConfig = (_modernjsConfig_tools = modernjsConfig.tools) === null || _modernjsConfig_tools === void 0 ? void 0 : _modernjsConfig_tools.devServer;
|
|
284
|
+
const corsWarnMsgs = [
|
|
285
|
+
"View https://module-federation.io/guide/troubleshooting/other.html#cors-warn for more details."
|
|
286
|
+
];
|
|
287
|
+
if (typeof devServerConfig !== "object" || !("headers" in devServerConfig)) {
|
|
288
|
+
corsWarnMsgs.unshift('Detect devServer.headers is empty, mf modern plugin will add default cors header: devServer.headers["Access-Control-Allow-Headers"] = "*". It is recommended to specify an allowlist of trusted origins instead.');
|
|
289
|
+
}
|
|
290
|
+
const exposes = (_userConfig_csrConfig = userConfig.csrConfig) === null || _userConfig_csrConfig === void 0 ? void 0 : _userConfig_csrConfig.exposes;
|
|
291
|
+
const hasExposes = exposes && Array.isArray(exposes) ? exposes.length : Object.keys(exposes !== null && exposes !== void 0 ? exposes : {}).length;
|
|
292
|
+
if (corsWarnMsgs.length > 1 && hasExposes) {
|
|
293
|
+
logger.warn(corsWarnMsgs.join("\n"));
|
|
294
|
+
}
|
|
295
|
+
const corsHeaders = hasExposes ? {
|
|
296
|
+
"Access-Control-Allow-Origin": "*",
|
|
297
|
+
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
|
|
298
|
+
"Access-Control-Allow-Headers": "*"
|
|
299
|
+
} : void 0;
|
|
57
300
|
var _modernjsConfig_source_enableAsyncEntry;
|
|
58
301
|
return {
|
|
59
302
|
tools: {
|
|
60
303
|
devServer: {
|
|
61
|
-
headers:
|
|
62
|
-
"Access-Control-Allow-Origin": "*",
|
|
63
|
-
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
|
|
64
|
-
"Access-Control-Allow-Headers": "*"
|
|
65
|
-
}
|
|
304
|
+
headers: corsHeaders
|
|
66
305
|
}
|
|
67
306
|
},
|
|
68
307
|
source: {
|
|
@@ -84,9 +323,13 @@ const moduleFederationConfigPlugin = (userConfig) => ({
|
|
|
84
323
|
});
|
|
85
324
|
var configPlugin_default = moduleFederationConfigPlugin;
|
|
86
325
|
export {
|
|
326
|
+
addMyTypes2Ignored,
|
|
87
327
|
configPlugin_default as default,
|
|
328
|
+
getMFConfig,
|
|
88
329
|
isWebTarget,
|
|
89
330
|
moduleFederationConfigPlugin,
|
|
331
|
+
patchBundlerConfig,
|
|
332
|
+
patchMFConfig,
|
|
90
333
|
setEnv,
|
|
91
334
|
skipByTarget
|
|
92
335
|
};
|
|
@@ -12,7 +12,7 @@ const moduleFederationPlugin = (userConfig = {}) => {
|
|
|
12
12
|
distOutputDir: "",
|
|
13
13
|
originPluginOptions: userConfig,
|
|
14
14
|
remoteIpStrategy: userConfig === null || userConfig === void 0 ? void 0 : userConfig.remoteIpStrategy,
|
|
15
|
-
userConfig
|
|
15
|
+
userConfig: userConfig || {}
|
|
16
16
|
};
|
|
17
17
|
return {
|
|
18
18
|
name: "@modern-js/plugin-module-federation",
|
|
@@ -1,25 +1,29 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import { fs } from "@modern-js/utils";
|
|
3
|
-
function mergeStats(browserStats, nodeStats
|
|
3
|
+
function mergeStats(browserStats, nodeStats) {
|
|
4
4
|
const ssrRemoteEntry = nodeStats.metaData.remoteEntry;
|
|
5
|
-
ssrRemoteEntry.path = ssrDir;
|
|
6
5
|
browserStats.metaData.ssrRemoteEntry = ssrRemoteEntry;
|
|
6
|
+
if ("publicPath" in browserStats.metaData) {
|
|
7
|
+
browserStats.metaData.ssrPublicPath = nodeStats.metaData.publicPath;
|
|
8
|
+
}
|
|
7
9
|
return browserStats;
|
|
8
10
|
}
|
|
9
|
-
function mergeManifest(browserManifest, nodeManifest
|
|
11
|
+
function mergeManifest(browserManifest, nodeManifest) {
|
|
10
12
|
const ssrRemoteEntry = nodeManifest.metaData.remoteEntry;
|
|
11
|
-
ssrRemoteEntry.path = ssrDir;
|
|
12
13
|
browserManifest.metaData.ssrRemoteEntry = ssrRemoteEntry;
|
|
14
|
+
if ("publicPath" in browserManifest.metaData) {
|
|
15
|
+
browserManifest.metaData.ssrPublicPath = nodeManifest.metaData.publicPath;
|
|
16
|
+
}
|
|
13
17
|
return browserManifest;
|
|
14
18
|
}
|
|
15
|
-
function mergeStatsAndManifest(nodePlugin, browserPlugin
|
|
19
|
+
function mergeStatsAndManifest(nodePlugin, browserPlugin) {
|
|
16
20
|
const nodeResourceInfo = nodePlugin.statsResourceInfo;
|
|
17
21
|
const browserResourceInfo = browserPlugin.statsResourceInfo;
|
|
18
22
|
if (!browserResourceInfo || !nodeResourceInfo || !browserResourceInfo.stats || !nodeResourceInfo.stats || !browserResourceInfo.manifest || !nodeResourceInfo.manifest) {
|
|
19
23
|
throw new Error("can not get browserResourceInfo or nodeResourceInfo");
|
|
20
24
|
}
|
|
21
|
-
const mergedStats = mergeStats(browserResourceInfo.stats.stats, nodeResourceInfo.stats.stats
|
|
22
|
-
const mergedManifest = mergeManifest(browserResourceInfo.manifest.manifest, nodeResourceInfo.manifest.manifest
|
|
25
|
+
const mergedStats = mergeStats(browserResourceInfo.stats.stats, nodeResourceInfo.stats.stats);
|
|
26
|
+
const mergedManifest = mergeManifest(browserResourceInfo.manifest.manifest, nodeResourceInfo.manifest.manifest);
|
|
23
27
|
return {
|
|
24
28
|
mergedStats,
|
|
25
29
|
mergedStatsFilePath: browserResourceInfo.stats.filename,
|
|
@@ -27,8 +31,8 @@ function mergeStatsAndManifest(nodePlugin, browserPlugin, ssrDir) {
|
|
|
27
31
|
mergedManifestFilePath: browserResourceInfo.manifest.filename
|
|
28
32
|
};
|
|
29
33
|
}
|
|
30
|
-
function updateStatsAndManifest(nodePlugin, browserPlugin, outputDir
|
|
31
|
-
const { mergedStats, mergedStatsFilePath, mergedManifest, mergedManifestFilePath } = mergeStatsAndManifest(nodePlugin, browserPlugin
|
|
34
|
+
function updateStatsAndManifest(nodePlugin, browserPlugin, outputDir) {
|
|
35
|
+
const { mergedStats, mergedStatsFilePath, mergedManifest, mergedManifestFilePath } = mergeStatsAndManifest(nodePlugin, browserPlugin);
|
|
32
36
|
fs.writeFileSync(path.resolve(outputDir, mergedStatsFilePath), JSON.stringify(mergedStats, null, 2));
|
|
33
37
|
fs.writeFileSync(path.resolve(outputDir, mergedManifestFilePath), JSON.stringify(mergedManifest, null, 2));
|
|
34
38
|
}
|
|
@@ -5,7 +5,6 @@ import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-
|
|
|
5
5
|
import UniverseEntryChunkTrackerPlugin from "@module-federation/node/universe-entry-chunk-tracker-plugin";
|
|
6
6
|
import { updateStatsAndManifest } from "./manifest";
|
|
7
7
|
import { isDev } from "./constant";
|
|
8
|
-
import { MODERN_JS_SERVER_DIR } from "../constant";
|
|
9
8
|
import logger from "./logger";
|
|
10
9
|
import { isWebTarget, skipByTarget } from "./utils";
|
|
11
10
|
function setEnv() {
|
|
@@ -13,6 +12,54 @@ function setEnv() {
|
|
|
13
12
|
process.env["MF_SSR_PRJ"] = "true";
|
|
14
13
|
}
|
|
15
14
|
const CHAIN_MF_PLUGIN_ID = "plugin-module-federation-server";
|
|
15
|
+
const mfSSRRsbuildPlugin = (pluginOptions) => {
|
|
16
|
+
return {
|
|
17
|
+
name: "@modern-js/plugin-mf-post-config",
|
|
18
|
+
pre: [
|
|
19
|
+
"@modern-js/builder-plugin-ssr"
|
|
20
|
+
],
|
|
21
|
+
setup(api) {
|
|
22
|
+
if (pluginOptions.csrConfig.getPublicPath) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
let csrOutputPath = "";
|
|
26
|
+
let ssrOutputPath = "";
|
|
27
|
+
let ssrEnv = "";
|
|
28
|
+
api.modifyEnvironmentConfig((config, { name }) => {
|
|
29
|
+
const target = config.output.target;
|
|
30
|
+
if (skipByTarget(target)) {
|
|
31
|
+
return config;
|
|
32
|
+
}
|
|
33
|
+
if (isWebTarget(target)) {
|
|
34
|
+
csrOutputPath = config.output.distPath.root;
|
|
35
|
+
} else {
|
|
36
|
+
ssrOutputPath = config.output.distPath.root;
|
|
37
|
+
ssrEnv = name;
|
|
38
|
+
}
|
|
39
|
+
return config;
|
|
40
|
+
});
|
|
41
|
+
const modifySSRPublicPath = (config, utils) => {
|
|
42
|
+
if (ssrEnv !== utils.environment.name) {
|
|
43
|
+
return config;
|
|
44
|
+
}
|
|
45
|
+
const userSSRConfig = pluginOptions.userConfig.ssr ? typeof pluginOptions.userConfig.ssr === "object" ? pluginOptions.userConfig.ssr : {} : {};
|
|
46
|
+
if (userSSRConfig.distOutputDir) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
config.output.publicPath = `${config.output.publicPath}${path.relative(csrOutputPath, ssrOutputPath)}/`;
|
|
50
|
+
return config;
|
|
51
|
+
};
|
|
52
|
+
api.modifyWebpackConfig((config, utils) => {
|
|
53
|
+
modifySSRPublicPath(config, utils);
|
|
54
|
+
return config;
|
|
55
|
+
});
|
|
56
|
+
api.modifyRspackConfig((config, utils) => {
|
|
57
|
+
modifySSRPublicPath(config, utils);
|
|
58
|
+
return config;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
};
|
|
16
63
|
const moduleFederationSSRPlugin = (pluginOptions) => ({
|
|
17
64
|
name: "@modern-js/plugin-module-federation-ssr",
|
|
18
65
|
pre: [
|
|
@@ -24,8 +71,6 @@ const moduleFederationSSRPlugin = (pluginOptions) => ({
|
|
|
24
71
|
const modernjsConfig = api.getConfig();
|
|
25
72
|
var _pluginOptions_userConfig_ssr;
|
|
26
73
|
const enableSSR = (_pluginOptions_userConfig_ssr = (_pluginOptions_userConfig = pluginOptions.userConfig) === null || _pluginOptions_userConfig === void 0 ? void 0 : _pluginOptions_userConfig.ssr) !== null && _pluginOptions_userConfig_ssr !== void 0 ? _pluginOptions_userConfig_ssr : Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
27
|
-
let csrOutputPath = "";
|
|
28
|
-
let ssrOutputPath = "";
|
|
29
74
|
if (!enableSSR) {
|
|
30
75
|
return;
|
|
31
76
|
}
|
|
@@ -70,20 +115,23 @@ const moduleFederationSSRPlugin = (pluginOptions) => ({
|
|
|
70
115
|
if (isDev) {
|
|
71
116
|
chain.plugin("UniverseEntryChunkTrackerPlugin").use(UniverseEntryChunkTrackerPlugin);
|
|
72
117
|
}
|
|
118
|
+
const userSSRConfig = pluginOptions.userConfig.ssr ? typeof pluginOptions.userConfig.ssr === "object" ? pluginOptions.userConfig.ssr : {} : {};
|
|
119
|
+
const publicPath = chain.output.get("publicPath");
|
|
120
|
+
if (userSSRConfig.distOutputDir && publicPath) {
|
|
121
|
+
chain.output.publicPath(`${publicPath}${userSSRConfig.distOutputDir}/`);
|
|
122
|
+
}
|
|
73
123
|
}
|
|
74
124
|
if (isDev && isWeb) {
|
|
75
125
|
chain.externals({
|
|
76
126
|
"@module-federation/node/utils": "NOT_USED_IN_BROWSER"
|
|
77
127
|
});
|
|
78
128
|
}
|
|
79
|
-
if (!isWeb) {
|
|
80
|
-
ssrOutputPath = chain.output.get("path") || path.resolve(process.cwd(), `dist/${MODERN_JS_SERVER_DIR}`);
|
|
81
|
-
} else {
|
|
82
|
-
csrOutputPath = chain.output.get("path") || path.resolve(process.cwd(), "dist");
|
|
83
|
-
}
|
|
84
129
|
});
|
|
85
130
|
api.config(() => {
|
|
86
131
|
return {
|
|
132
|
+
builderPlugins: [
|
|
133
|
+
mfSSRRsbuildPlugin(pluginOptions)
|
|
134
|
+
],
|
|
87
135
|
tools: {
|
|
88
136
|
devServer: {
|
|
89
137
|
before: [
|
|
@@ -116,11 +164,11 @@ const moduleFederationSSRPlugin = (pluginOptions) => ({
|
|
|
116
164
|
});
|
|
117
165
|
api.onAfterBuild(() => {
|
|
118
166
|
const { nodePlugin, browserPlugin, distOutputDir } = pluginOptions;
|
|
119
|
-
updateStatsAndManifest(nodePlugin, browserPlugin, distOutputDir
|
|
167
|
+
updateStatsAndManifest(nodePlugin, browserPlugin, distOutputDir);
|
|
120
168
|
});
|
|
121
169
|
api.onDevCompileDone(() => {
|
|
122
170
|
const { nodePlugin, browserPlugin, distOutputDir } = pluginOptions;
|
|
123
|
-
updateStatsAndManifest(nodePlugin, browserPlugin, distOutputDir
|
|
171
|
+
updateStatsAndManifest(nodePlugin, browserPlugin, distOutputDir);
|
|
124
172
|
});
|
|
125
173
|
}
|
|
126
174
|
});
|