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