@module-federation/modern-js 0.11.2 → 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.
@@ -28,21 +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");
47
+ var import_utils2 = require("@module-federation/rsbuild-plugin/utils");
48
+ var import_logger = __toESM(require("./logger"));
49
+ const defaultPath = import_path.default.resolve(process.cwd(), "module-federation.config.ts");
50
+ const isDev = process.env.NODE_ENV === "development";
40
51
  function setEnv(enableSSR) {
41
52
  if (enableSSR) {
42
53
  process.env["MF_DISABLE_EMIT_STATS"] = "true";
43
54
  process.env["MF_SSR_PRJ"] = "true";
44
55
  }
45
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
+ }
46
276
  const moduleFederationConfigPlugin = (userConfig) => ({
47
277
  name: "@modern-js/plugin-module-federation-config",
48
278
  pre: [
@@ -54,23 +284,23 @@ const moduleFederationConfigPlugin = (userConfig) => ({
54
284
  setup: async (api) => {
55
285
  var _userConfig_userConfig, _modernjsConfig_server;
56
286
  const modernjsConfig = api.getConfig();
57
- const mfConfig = await (0, import_utils.getMFConfig)(userConfig.originPluginOptions);
287
+ const mfConfig = await getMFConfig(userConfig.originPluginOptions);
58
288
  const csrConfig = userConfig.csrConfig || JSON.parse(JSON.stringify(mfConfig));
59
289
  const ssrConfig = userConfig.ssrConfig || JSON.parse(JSON.stringify(mfConfig));
60
290
  userConfig.ssrConfig = ssrConfig;
61
291
  userConfig.csrConfig = csrConfig;
62
292
  var _userConfig_userConfig_ssr;
63
- 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);
293
+ 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));
64
294
  api.modifyBundlerChain((chain) => {
65
295
  const target = chain.get("target");
66
296
  if ((0, import_utils.skipByTarget)(target)) {
67
297
  return;
68
298
  }
69
299
  const isWeb = (0, import_utils.isWebTarget)(target);
70
- (0, import_utils.addMyTypes2Ignored)(chain, !isWeb ? ssrConfig : csrConfig);
300
+ addMyTypes2Ignored(chain, !isWeb ? ssrConfig : csrConfig);
71
301
  const targetMFConfig = !isWeb ? ssrConfig : csrConfig;
72
- (0, import_utils.patchMFConfig)(targetMFConfig, !isWeb, userConfig.remoteIpStrategy || "ipv4");
73
- (0, import_utils.patchBundlerConfig)({
302
+ patchMFConfig(targetMFConfig, !isWeb, userConfig.remoteIpStrategy || "ipv4");
303
+ patchBundlerConfig({
74
304
  // @ts-expect-error chain type is not correct
75
305
  chain,
76
306
  isServer: !isWeb,
@@ -81,7 +311,7 @@ const moduleFederationConfigPlugin = (userConfig) => ({
81
311
  userConfig.distOutputDir = chain.output.get("path") || import_path.default.resolve(process.cwd(), "dist");
82
312
  });
83
313
  api.config(() => {
84
- var _modernjsConfig_source, _modernjsConfig_source1, _modernjsConfig_dev;
314
+ var _modernjsConfig_tools, _userConfig_csrConfig, _modernjsConfig_source, _modernjsConfig_source1, _modernjsConfig_dev;
85
315
  const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
86
316
  const ipv4 = (0, import_utils.getIPV4)();
87
317
  if (userConfig.remoteIpStrategy === void 0) {
@@ -91,15 +321,28 @@ const moduleFederationConfigPlugin = (userConfig) => ({
91
321
  userConfig.remoteIpStrategy = "ipv4";
92
322
  }
93
323
  }
324
+ const devServerConfig = (_modernjsConfig_tools = modernjsConfig.tools) === null || _modernjsConfig_tools === void 0 ? void 0 : _modernjsConfig_tools.devServer;
325
+ const corsWarnMsgs = [
326
+ "View https://module-federation.io/guide/troubleshooting/other.html#cors-warn for more details."
327
+ ];
328
+ if (typeof devServerConfig !== "object" || !("headers" in devServerConfig)) {
329
+ 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.');
330
+ }
331
+ const exposes = (_userConfig_csrConfig = userConfig.csrConfig) === null || _userConfig_csrConfig === void 0 ? void 0 : _userConfig_csrConfig.exposes;
332
+ const hasExposes = exposes && Array.isArray(exposes) ? exposes.length : Object.keys(exposes !== null && exposes !== void 0 ? exposes : {}).length;
333
+ if (corsWarnMsgs.length > 1 && hasExposes) {
334
+ import_logger.default.warn(corsWarnMsgs.join("\n"));
335
+ }
336
+ const corsHeaders = hasExposes ? {
337
+ "Access-Control-Allow-Origin": "*",
338
+ "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
339
+ "Access-Control-Allow-Headers": "*"
340
+ } : void 0;
94
341
  var _modernjsConfig_source_enableAsyncEntry;
95
342
  return {
96
343
  tools: {
97
344
  devServer: {
98
- headers: {
99
- "Access-Control-Allow-Origin": "*",
100
- "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
101
- "Access-Control-Allow-Headers": "*"
102
- }
345
+ headers: corsHeaders
103
346
  }
104
347
  },
105
348
  source: {
@@ -122,8 +365,12 @@ const moduleFederationConfigPlugin = (userConfig) => ({
122
365
  var configPlugin_default = moduleFederationConfigPlugin;
123
366
  // Annotate the CommonJS export names for ESM import in node:
124
367
  0 && (module.exports = {
368
+ addMyTypes2Ignored,
369
+ getMFConfig,
125
370
  isWebTarget,
126
371
  moduleFederationConfigPlugin,
372
+ patchBundlerConfig,
373
+ patchMFConfig,
127
374
  setEnv,
128
375
  skipByTarget
129
376
  });
@@ -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, import_utils.patchMFConfig)(patchedConfig, true);
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, import_utils.patchMFConfig)(patchedConfig, false);
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",
@@ -38,7 +38,7 @@ const moduleFederationPlugin = (userConfig = {}) => {
38
38
  distOutputDir: "",
39
39
  originPluginOptions: userConfig,
40
40
  remoteIpStrategy: userConfig === null || userConfig === void 0 ? void 0 : userConfig.remoteIpStrategy,
41
- userConfig
41
+ userConfig: userConfig || {}
42
42
  };
43
43
  return {
44
44
  name: "@modern-js/plugin-module-federation",
@@ -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, ssrDir) {
36
+ function mergeStats(browserStats, nodeStats) {
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 = nodeStats.metaData.publicPath;
41
+ }
40
42
  return browserStats;
41
43
  }
42
- function mergeManifest(browserManifest, nodeManifest, ssrDir) {
44
+ function mergeManifest(browserManifest, nodeManifest) {
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 = nodeManifest.metaData.publicPath;
49
+ }
46
50
  return browserManifest;
47
51
  }
48
- function mergeStatsAndManifest(nodePlugin, browserPlugin, ssrDir) {
52
+ function mergeStatsAndManifest(nodePlugin, browserPlugin) {
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, ssrDir);
55
- const mergedManifest = mergeManifest(browserResourceInfo.manifest.manifest, nodeResourceInfo.manifest.manifest, ssrDir);
58
+ const mergedStats = mergeStats(browserResourceInfo.stats.stats, nodeResourceInfo.stats.stats);
59
+ const mergedManifest = mergeManifest(browserResourceInfo.manifest.manifest, nodeResourceInfo.manifest.manifest);
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, ssrDir) {
64
- const { mergedStats, mergedStatsFilePath, mergedManifest, mergedManifestFilePath } = mergeStatsAndManifest(nodePlugin, browserPlugin, ssrDir);
67
+ function updateStatsAndManifest(nodePlugin, browserPlugin, outputDir) {
68
+ const { mergedStats, mergedStatsFilePath, mergedManifest, mergedManifestFilePath } = mergeStatsAndManifest(nodePlugin, browserPlugin);
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,54 @@ function setEnv() {
49
48
  process.env["MF_SSR_PRJ"] = "true";
50
49
  }
51
50
  const CHAIN_MF_PLUGIN_ID = "plugin-module-federation-server";
51
+ const mfSSRRsbuildPlugin = (pluginOptions) => {
52
+ return {
53
+ name: "@modern-js/plugin-mf-post-config",
54
+ pre: [
55
+ "@modern-js/builder-plugin-ssr"
56
+ ],
57
+ setup(api) {
58
+ if (pluginOptions.csrConfig.getPublicPath) {
59
+ return;
60
+ }
61
+ let csrOutputPath = "";
62
+ let ssrOutputPath = "";
63
+ let ssrEnv = "";
64
+ api.modifyEnvironmentConfig((config, { name }) => {
65
+ const target = config.output.target;
66
+ if ((0, import_utils2.skipByTarget)(target)) {
67
+ return config;
68
+ }
69
+ if ((0, import_utils2.isWebTarget)(target)) {
70
+ csrOutputPath = config.output.distPath.root;
71
+ } else {
72
+ ssrOutputPath = config.output.distPath.root;
73
+ ssrEnv = name;
74
+ }
75
+ return config;
76
+ });
77
+ const modifySSRPublicPath = (config, utils) => {
78
+ if (ssrEnv !== utils.environment.name) {
79
+ return config;
80
+ }
81
+ const userSSRConfig = pluginOptions.userConfig.ssr ? typeof pluginOptions.userConfig.ssr === "object" ? pluginOptions.userConfig.ssr : {} : {};
82
+ if (userSSRConfig.distOutputDir) {
83
+ return;
84
+ }
85
+ config.output.publicPath = `${config.output.publicPath}${import_path.default.relative(csrOutputPath, ssrOutputPath)}/`;
86
+ return config;
87
+ };
88
+ api.modifyWebpackConfig((config, utils) => {
89
+ modifySSRPublicPath(config, utils);
90
+ return config;
91
+ });
92
+ api.modifyRspackConfig((config, utils) => {
93
+ modifySSRPublicPath(config, utils);
94
+ return config;
95
+ });
96
+ }
97
+ };
98
+ };
52
99
  const moduleFederationSSRPlugin = (pluginOptions) => ({
53
100
  name: "@modern-js/plugin-module-federation-ssr",
54
101
  pre: [
@@ -60,8 +107,6 @@ const moduleFederationSSRPlugin = (pluginOptions) => ({
60
107
  const modernjsConfig = api.getConfig();
61
108
  var _pluginOptions_userConfig_ssr;
62
109
  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
110
  if (!enableSSR) {
66
111
  return;
67
112
  }
@@ -106,20 +151,23 @@ const moduleFederationSSRPlugin = (pluginOptions) => ({
106
151
  if (import_constant.isDev) {
107
152
  chain.plugin("UniverseEntryChunkTrackerPlugin").use(import_universe_entry_chunk_tracker_plugin.default);
108
153
  }
154
+ const userSSRConfig = pluginOptions.userConfig.ssr ? typeof pluginOptions.userConfig.ssr === "object" ? pluginOptions.userConfig.ssr : {} : {};
155
+ const publicPath = chain.output.get("publicPath");
156
+ if (userSSRConfig.distOutputDir && publicPath) {
157
+ chain.output.publicPath(`${publicPath}${userSSRConfig.distOutputDir}/`);
158
+ }
109
159
  }
110
160
  if (import_constant.isDev && isWeb) {
111
161
  chain.externals({
112
162
  "@module-federation/node/utils": "NOT_USED_IN_BROWSER"
113
163
  });
114
164
  }
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
165
  });
121
166
  api.config(() => {
122
167
  return {
168
+ builderPlugins: [
169
+ mfSSRRsbuildPlugin(pluginOptions)
170
+ ],
123
171
  tools: {
124
172
  devServer: {
125
173
  before: [
@@ -152,11 +200,11 @@ const moduleFederationSSRPlugin = (pluginOptions) => ({
152
200
  });
153
201
  api.onAfterBuild(() => {
154
202
  const { nodePlugin, browserPlugin, distOutputDir } = pluginOptions;
155
- (0, import_manifest.updateStatsAndManifest)(nodePlugin, browserPlugin, distOutputDir, import_path.default.relative(csrOutputPath, ssrOutputPath));
203
+ (0, import_manifest.updateStatsAndManifest)(nodePlugin, browserPlugin, distOutputDir);
156
204
  });
157
205
  api.onDevCompileDone(() => {
158
206
  const { nodePlugin, browserPlugin, distOutputDir } = pluginOptions;
159
- (0, import_manifest.updateStatsAndManifest)(nodePlugin, browserPlugin, distOutputDir, import_path.default.relative(csrOutputPath, ssrOutputPath));
207
+ (0, import_manifest.updateStatsAndManifest)(nodePlugin, browserPlugin, distOutputDir);
160
208
  });
161
209
  }
162
210
  });