@module-federation/modern-js 0.0.0-next-20250218110948 → 0.0.0-next-20250219033929

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.
@@ -29,6 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  var configPlugin_exports = {};
30
30
  __export(configPlugin_exports, {
31
31
  default: () => configPlugin_default,
32
+ modifyBundlerConfig: () => modifyBundlerConfig,
32
33
  moduleFederationConfigPlugin: () => moduleFederationConfigPlugin,
33
34
  setEnv: () => setEnv
34
35
  });
@@ -41,6 +42,17 @@ function setEnv(enableSSR) {
41
42
  process.env["MF_SSR_PRJ"] = "true";
42
43
  }
43
44
  }
45
+ function modifyBundlerConfig(options) {
46
+ const { mfConfig, config, isServer, modernjsConfig, remoteIpStrategy = "ipv4", bundlerType } = options;
47
+ (0, import_utils.patchMFConfig)(mfConfig, isServer, remoteIpStrategy);
48
+ (0, import_utils.patchBundlerConfig)({
49
+ bundlerType,
50
+ bundlerConfig: config,
51
+ isServer,
52
+ modernjsConfig,
53
+ mfConfig
54
+ });
55
+ }
44
56
  const moduleFederationConfigPlugin = (userConfig) => ({
45
57
  name: "@modern-js/plugin-module-federation-config",
46
58
  pre: [
@@ -56,20 +68,6 @@ const moduleFederationConfigPlugin = (userConfig) => ({
56
68
  const ssrConfig = userConfig.ssrConfig || JSON.parse(JSON.stringify(mfConfig));
57
69
  userConfig.ssrConfig = ssrConfig;
58
70
  userConfig.csrConfig = csrConfig;
59
- api.modifyBundlerChain((chain, { isProd, isServer }) => {
60
- const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
61
- (0, import_utils.addMyTypes2Ignored)(chain, isServer ? ssrConfig : csrConfig);
62
- const targetMFConfig = isServer ? ssrConfig : csrConfig;
63
- (0, import_utils.patchMFConfig)(targetMFConfig, isServer, userConfig.remoteIpStrategy || "ipv4");
64
- (0, import_utils.patchBundlerConfig)({
65
- // @ts-expect-error chain type is not correct
66
- chain,
67
- isServer,
68
- modernjsConfig,
69
- mfConfig
70
- });
71
- userConfig.distOutputDir = chain.output.get("path") || import_path.default.resolve(process.cwd(), "dist");
72
- });
73
71
  api.config(() => {
74
72
  var _userConfig_userConfig, _modernjsConfig_server, _modernjsConfig_source, _modernjsConfig_source1, _modernjsConfig_dev;
75
73
  const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
@@ -85,6 +83,33 @@ const moduleFederationConfigPlugin = (userConfig) => ({
85
83
  var _modernjsConfig_source_enableAsyncEntry;
86
84
  return {
87
85
  tools: {
86
+ bundlerChain(chain, { isServer }) {
87
+ (0, import_utils.addMyTypes2Ignored)(chain, isServer ? ssrConfig : csrConfig);
88
+ },
89
+ rspack(config, { isServer }) {
90
+ var _config_output;
91
+ modifyBundlerConfig({
92
+ bundlerType,
93
+ mfConfig: isServer ? ssrConfig : csrConfig,
94
+ config,
95
+ isServer,
96
+ modernjsConfig,
97
+ remoteIpStrategy: userConfig.remoteIpStrategy
98
+ });
99
+ userConfig.distOutputDir = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || import_path.default.resolve(process.cwd(), "dist");
100
+ },
101
+ webpack(config, { isServer }) {
102
+ var _config_output;
103
+ modifyBundlerConfig({
104
+ bundlerType,
105
+ mfConfig: isServer ? ssrConfig : csrConfig,
106
+ config,
107
+ isServer,
108
+ modernjsConfig,
109
+ remoteIpStrategy: userConfig.remoteIpStrategy
110
+ });
111
+ userConfig.distOutputDir = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || import_path.default.resolve(process.cwd(), "dist");
112
+ },
88
113
  devServer: {
89
114
  headers: {
90
115
  "Access-Control-Allow-Origin": "*",
@@ -113,6 +138,7 @@ const moduleFederationConfigPlugin = (userConfig) => ({
113
138
  var configPlugin_default = moduleFederationConfigPlugin;
114
139
  // Annotate the CommonJS export names for ESM import in node:
115
140
  0 && (module.exports = {
141
+ modifyBundlerConfig,
116
142
  moduleFederationConfigPlugin,
117
143
  setEnv
118
144
  });
@@ -43,31 +43,37 @@ const moduleFederationPlugin = (userConfig = {}) => {
43
43
  name: "@modern-js/plugin-module-federation",
44
44
  setup: async (api) => {
45
45
  const modernjsConfig = api.getConfig();
46
- api.modifyBundlerChain((chain, { isProd, isServer }) => {
47
- const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
48
- const browserPluginOptions = internalModernPluginOptions.csrConfig;
49
- const MFPlugin = bundlerType === "webpack" ? import_enhanced.ModuleFederationPlugin : import_rspack.ModuleFederationPlugin;
50
- if (!isServer) {
51
- chain.plugin("plugin-module-federation").use(MFPlugin, [
52
- browserPluginOptions
53
- ]).init((Plugin, args) => {
54
- internalModernPluginOptions.browserPlugin = new Plugin(args[0]);
55
- return internalModernPluginOptions.browserPlugin;
56
- });
57
- }
58
- if (bundlerType === "webpack") {
59
- var _modernjsConfig_source;
60
- const enableAsyncEntry = (_modernjsConfig_source = modernjsConfig.source) === null || _modernjsConfig_source === void 0 ? void 0 : _modernjsConfig_source.enableAsyncEntry;
61
- if (!enableAsyncEntry && browserPluginOptions.async !== false) {
62
- const asyncBoundaryPluginOptions = typeof browserPluginOptions.async === "object" ? browserPluginOptions.async : {
63
- eager: (module2) => module2 && /\.federation/.test((module2 === null || module2 === void 0 ? void 0 : module2.request) || ""),
64
- excludeChunk: (chunk) => chunk.name === browserPluginOptions.name
65
- };
66
- chain.plugin("async-boundary-plugin").use(import_enhanced.AsyncBoundaryPlugin, [
67
- asyncBoundaryPluginOptions
68
- ]);
46
+ api.config(() => {
47
+ return {
48
+ tools: {
49
+ rspack(config, { isServer }) {
50
+ const browserPluginOptions = internalModernPluginOptions.csrConfig;
51
+ if (!isServer) {
52
+ var _config_plugins;
53
+ internalModernPluginOptions.browserPlugin = new import_rspack.ModuleFederationPlugin(browserPluginOptions);
54
+ (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(internalModernPluginOptions.browserPlugin);
55
+ }
56
+ },
57
+ webpack(config, { isServer }) {
58
+ var _modernjsConfig_source;
59
+ const browserPluginOptions = internalModernPluginOptions.csrConfig;
60
+ if (!isServer) {
61
+ var _config_plugins;
62
+ internalModernPluginOptions.browserPlugin = new import_enhanced.ModuleFederationPlugin(browserPluginOptions);
63
+ (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(internalModernPluginOptions.browserPlugin);
64
+ }
65
+ const enableAsyncEntry = (_modernjsConfig_source = modernjsConfig.source) === null || _modernjsConfig_source === void 0 ? void 0 : _modernjsConfig_source.enableAsyncEntry;
66
+ if (!enableAsyncEntry && browserPluginOptions.async !== false) {
67
+ var _config_plugins1;
68
+ const asyncBoundaryPluginOptions = typeof browserPluginOptions.async === "object" ? browserPluginOptions.async : {
69
+ eager: (module2) => module2 && /\.federation/.test((module2 === null || module2 === void 0 ? void 0 : module2.request) || ""),
70
+ excludeChunk: (chunk) => chunk.name === browserPluginOptions.name
71
+ };
72
+ (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(new import_enhanced.AsyncBoundaryPlugin(asyncBoundaryPluginOptions));
73
+ }
74
+ }
69
75
  }
70
- }
76
+ };
71
77
  });
72
78
  },
73
79
  usePlugins: [
@@ -76,25 +76,33 @@ const moduleFederationSSRPlugin = (pluginOptions) => ({
76
76
  plugins
77
77
  };
78
78
  });
79
- api.modifyBundlerChain((chain, { isProd, isServer }) => {
80
- const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
81
- const MFPlugin = bundlerType === "webpack" ? import_webpack.ModuleFederationPlugin : import_rspack.ModuleFederationPlugin;
82
- if (isServer) {
83
- if (!pluginOptions.nodePlugin) {
84
- chain.plugin("plugin-module-federation-server").use(MFPlugin, [
85
- pluginOptions.ssrConfig
86
- ]).init((Plugin, args) => {
87
- pluginOptions.nodePlugin = new Plugin(args[0]);
88
- return pluginOptions.nodePlugin;
89
- });
90
- }
91
- } else {
92
- pluginOptions.distOutputDir = pluginOptions.distOutputDir || chain.output.get("path") || import_path.default.resolve(process.cwd(), "dist");
93
- }
94
- });
95
79
  api.config(() => {
96
80
  return {
97
81
  tools: {
82
+ rspack(config, { isServer }) {
83
+ if (isServer) {
84
+ if (!pluginOptions.nodePlugin) {
85
+ var _config_plugins;
86
+ pluginOptions.nodePlugin = new import_rspack.ModuleFederationPlugin(pluginOptions.ssrConfig);
87
+ (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(pluginOptions.nodePlugin);
88
+ }
89
+ } else {
90
+ var _config_output;
91
+ pluginOptions.distOutputDir = pluginOptions.distOutputDir || ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || import_path.default.resolve(process.cwd(), "dist");
92
+ }
93
+ },
94
+ webpack(config, { isServer }) {
95
+ if (isServer) {
96
+ if (!pluginOptions.nodePlugin) {
97
+ var _config_plugins;
98
+ pluginOptions.nodePlugin = new import_webpack.ModuleFederationPlugin(pluginOptions.ssrConfig);
99
+ (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(pluginOptions.nodePlugin);
100
+ }
101
+ } else {
102
+ var _config_output;
103
+ pluginOptions.distOutputDir = pluginOptions.distOutputDir || ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || import_path.default.resolve(process.cwd(), "dist");
104
+ }
105
+ },
98
106
  devServer: {
99
107
  before: [
100
108
  (req, res, next) => {
@@ -32,6 +32,7 @@ __export(utils_exports, {
32
32
  getIPV4: () => getIPV4,
33
33
  getMFConfig: () => getMFConfig,
34
34
  patchBundlerConfig: () => patchBundlerConfig,
35
+ patchIgnoreWarning: () => patchIgnoreWarning,
35
36
  patchMFConfig: () => patchMFConfig
36
37
  });
37
38
  module.exports = __toCommonJS(utils_exports);
@@ -172,20 +173,19 @@ const patchMFConfig = (mfConfig, isServer, remoteIpStrategy) => {
172
173
  mfConfig.dev = false;
173
174
  return mfConfig;
174
175
  };
175
- function patchIgnoreWarning(chain) {
176
- const ignoreWarnings = chain.get("ignoreWarnings") || [];
176
+ function patchIgnoreWarning(bundlerConfig) {
177
+ bundlerConfig.ignoreWarnings = bundlerConfig.ignoreWarnings || [];
177
178
  const ignoredMsgs = [
178
179
  "external script",
179
180
  "process.env.WS_NO_BUFFER_UTIL",
180
181
  `Can't resolve 'utf-8-validate`
181
182
  ];
182
- ignoreWarnings.push((warning) => {
183
+ bundlerConfig.ignoreWarnings.push((warning) => {
183
184
  if (ignoredMsgs.some((msg) => warning.message.includes(msg))) {
184
185
  return true;
185
186
  }
186
187
  return false;
187
188
  });
188
- chain.ignoreWarnings(ignoreWarnings);
189
189
  }
190
190
  function addMyTypes2Ignored(chain, mfConfig) {
191
191
  const watchOptions = chain.get("watchOptions");
@@ -224,44 +224,55 @@ function addMyTypes2Ignored(chain, mfConfig) {
224
224
  });
225
225
  }
226
226
  function patchBundlerConfig(options) {
227
- var _modernjsConfig_server, _modernjsConfig_deploy;
228
- const { chain, modernjsConfig, isServer, mfConfig } = options;
227
+ var _modernjsConfig_server, _bundlerConfig_optimization, _bundlerConfig_optimization1, _bundlerConfig_output, _modernjsConfig_deploy;
228
+ const { bundlerConfig, modernjsConfig, isServer, mfConfig, bundlerType } = options;
229
229
  const enableSSR = Boolean((_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
230
- chain.optimization.runtimeChunk(false);
231
- patchIgnoreWarning(chain);
232
- if (!chain.output.get("chunkLoadingGlobal")) {
233
- chain.output.chunkLoadingGlobal(`chunk_${mfConfig.name}`);
234
- }
235
- if (!chain.output.get("uniqueName")) {
236
- chain.output.uniqueName(mfConfig.name);
230
+ (_bundlerConfig_optimization = bundlerConfig.optimization) === null || _bundlerConfig_optimization === void 0 ? true : delete _bundlerConfig_optimization.runtimeChunk;
231
+ patchIgnoreWarning(bundlerConfig);
232
+ if (bundlerConfig.output) {
233
+ var _bundlerConfig_output1, _bundlerConfig_output2;
234
+ if (!((_bundlerConfig_output1 = bundlerConfig.output) === null || _bundlerConfig_output1 === void 0 ? void 0 : _bundlerConfig_output1.chunkLoadingGlobal)) {
235
+ bundlerConfig.output.chunkLoadingGlobal = `chunk_${mfConfig.name}`;
236
+ }
237
+ if (!((_bundlerConfig_output2 = bundlerConfig.output) === null || _bundlerConfig_output2 === void 0 ? void 0 : _bundlerConfig_output2.uniqueName)) {
238
+ bundlerConfig.output.uniqueName = mfConfig.name;
239
+ }
237
240
  }
238
- const optimizationConfig = chain.optimization.entries();
239
241
  if (!isServer) {
240
- (0, import_utils.autoDeleteSplitChunkCacheGroups)(mfConfig, optimizationConfig);
242
+ (0, import_utils.autoDeleteSplitChunkCacheGroups)(mfConfig, bundlerConfig);
241
243
  }
242
- if (!isServer && enableSSR && typeof (optimizationConfig === null || optimizationConfig === void 0 ? void 0 : optimizationConfig.splitChunks) === "object" && optimizationConfig.splitChunks.cacheGroups) {
243
- optimizationConfig.splitChunks.chunks = "async";
244
+ if (!isServer && enableSSR && typeof ((_bundlerConfig_optimization1 = bundlerConfig.optimization) === null || _bundlerConfig_optimization1 === void 0 ? void 0 : _bundlerConfig_optimization1.splitChunks) === "object" && bundlerConfig.optimization.splitChunks.cacheGroups) {
245
+ bundlerConfig.optimization.splitChunks.chunks = "async";
244
246
  import_logger.default.warn(`splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"`);
245
247
  }
246
- if (isDev && chain.output.get("publicPath") === "auto") {
248
+ if (isDev && ((_bundlerConfig_output = bundlerConfig.output) === null || _bundlerConfig_output === void 0 ? void 0 : _bundlerConfig_output.publicPath) === "auto") {
247
249
  var _modernjsConfig_dev, _modernjsConfig_server1;
248
250
  const port = ((_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.port) || ((_modernjsConfig_server1 = modernjsConfig.server) === null || _modernjsConfig_server1 === void 0 ? void 0 : _modernjsConfig_server1.port) || 8080;
249
251
  const publicPath = `http://localhost:${port}/`;
250
- chain.output.publicPath(publicPath);
252
+ bundlerConfig.output.publicPath = publicPath;
251
253
  }
252
254
  if (isServer && enableSSR) {
253
- const uniqueName = mfConfig.name || chain.output.get("uniqueName");
254
- const chunkFileName = chain.output.get("chunkFilename");
255
- if (typeof chunkFileName === "string" && uniqueName && !chunkFileName.includes(uniqueName)) {
255
+ const { output } = bundlerConfig;
256
+ const uniqueName = mfConfig.name || (output === null || output === void 0 ? void 0 : output.uniqueName);
257
+ const chunkFileName = output === null || output === void 0 ? void 0 : output.chunkFilename;
258
+ if (output && typeof chunkFileName === "string" && uniqueName && !chunkFileName.includes(uniqueName)) {
256
259
  const suffix = `${(0, import_sdk.encodeName)(uniqueName)}-[chunkhash].js`;
257
- chain.output.chunkFilename(chunkFileName.replace(".js", suffix));
260
+ output.chunkFilename = chunkFileName.replace(".js", suffix);
258
261
  }
259
262
  }
260
263
  if (isDev && enableSSR && !isServer) {
261
- chain.resolve.fallback.set("crypto", false).set("stream", false).set("vm", false);
264
+ bundlerConfig.resolve.fallback = {
265
+ ...bundlerConfig.resolve.fallback,
266
+ crypto: false,
267
+ stream: false,
268
+ vm: false
269
+ };
262
270
  }
263
271
  if (((_modernjsConfig_deploy = modernjsConfig.deploy) === null || _modernjsConfig_deploy === void 0 ? void 0 : _modernjsConfig_deploy.microFrontend) && Object.keys(mfConfig.exposes || {}).length) {
264
- chain.optimization.usedExports(false);
272
+ if (!bundlerConfig.optimization) {
273
+ bundlerConfig.optimization = {};
274
+ }
275
+ bundlerConfig.optimization.usedExports = false;
265
276
  }
266
277
  }
267
278
  const localIpv4 = "127.0.0.1";
@@ -317,5 +328,6 @@ const SHARED_SPLIT_CHUNK_MAP = {
317
328
  getIPV4,
318
329
  getMFConfig,
319
330
  patchBundlerConfig,
331
+ patchIgnoreWarning,
320
332
  patchMFConfig
321
333
  });
@@ -109,3 +109,63 @@ const mfConfig = {
109
109
  });
110
110
  });
111
111
  });
112
+ (0, import_vitest.describe)("patchBundlerConfig", async () => {
113
+ (0, import_vitest.it)("patchBundlerConfig: server", async () => {
114
+ const bundlerConfig = {
115
+ output: {
116
+ publicPath: "auto"
117
+ }
118
+ };
119
+ (0, import_utils.patchBundlerConfig)({
120
+ bundlerType: "webpack",
121
+ bundlerConfig,
122
+ isServer: true,
123
+ modernjsConfig: {
124
+ server: {
125
+ ssr: {
126
+ mode: "stream"
127
+ }
128
+ }
129
+ },
130
+ mfConfig
131
+ });
132
+ const expectedConfig = {
133
+ output: {
134
+ chunkLoadingGlobal: "chunk_host",
135
+ publicPath: "auto",
136
+ uniqueName: "host"
137
+ }
138
+ };
139
+ bundlerConfig === null || bundlerConfig === void 0 ? true : delete bundlerConfig.ignoreWarnings;
140
+ (0, import_vitest.expect)(bundlerConfig).toStrictEqual(expectedConfig);
141
+ });
142
+ (0, import_vitest.it)("patchBundlerConfig: client", async () => {
143
+ const bundlerConfig = {
144
+ output: {
145
+ publicPath: "auto"
146
+ }
147
+ };
148
+ (0, import_utils.patchBundlerConfig)({
149
+ bundlerType: "webpack",
150
+ bundlerConfig,
151
+ isServer: false,
152
+ modernjsConfig: {
153
+ server: {
154
+ ssr: {
155
+ mode: "stream"
156
+ }
157
+ }
158
+ },
159
+ mfConfig
160
+ });
161
+ const expectedConfig = {
162
+ output: {
163
+ chunkLoadingGlobal: "chunk_host",
164
+ publicPath: "auto",
165
+ uniqueName: "host"
166
+ }
167
+ };
168
+ bundlerConfig === null || bundlerConfig === void 0 ? true : delete bundlerConfig.ignoreWarnings;
169
+ (0, import_vitest.expect)(bundlerConfig).toStrictEqual(expectedConfig);
170
+ });
171
+ });
@@ -8,6 +8,17 @@ function setEnv(enableSSR) {
8
8
  process.env["MF_SSR_PRJ"] = "true";
9
9
  }
10
10
  }
11
+ function modifyBundlerConfig(options) {
12
+ var mfConfig = options.mfConfig, config = options.config, isServer = options.isServer, modernjsConfig = options.modernjsConfig, _options_remoteIpStrategy = options.remoteIpStrategy, remoteIpStrategy = _options_remoteIpStrategy === void 0 ? "ipv4" : _options_remoteIpStrategy, bundlerType = options.bundlerType;
13
+ patchMFConfig(mfConfig, isServer, remoteIpStrategy);
14
+ patchBundlerConfig({
15
+ bundlerType,
16
+ bundlerConfig: config,
17
+ isServer,
18
+ modernjsConfig,
19
+ mfConfig
20
+ });
21
+ }
11
22
  var moduleFederationConfigPlugin = function(userConfig) {
12
23
  return {
13
24
  name: "@modern-js/plugin-module-federation-config",
@@ -34,21 +45,6 @@ var moduleFederationConfigPlugin = function(userConfig) {
34
45
  ssrConfig = userConfig.ssrConfig || JSON.parse(JSON.stringify(mfConfig));
35
46
  userConfig.ssrConfig = ssrConfig;
36
47
  userConfig.csrConfig = csrConfig;
37
- api.modifyBundlerChain(function(chain, param) {
38
- var isProd = param.isProd, isServer = param.isServer;
39
- var bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
40
- addMyTypes2Ignored(chain, isServer ? ssrConfig : csrConfig);
41
- var targetMFConfig = isServer ? ssrConfig : csrConfig;
42
- patchMFConfig(targetMFConfig, isServer, userConfig.remoteIpStrategy || "ipv4");
43
- patchBundlerConfig({
44
- // @ts-expect-error chain type is not correct
45
- chain,
46
- isServer,
47
- modernjsConfig,
48
- mfConfig
49
- });
50
- userConfig.distOutputDir = chain.output.get("path") || path.resolve(process.cwd(), "dist");
51
- });
52
48
  api.config(function() {
53
49
  var _userConfig_userConfig, _modernjsConfig_server, _modernjsConfig_source, _modernjsConfig_source1, _modernjsConfig_dev;
54
50
  var bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
@@ -64,6 +60,36 @@ var moduleFederationConfigPlugin = function(userConfig) {
64
60
  var _modernjsConfig_source_enableAsyncEntry;
65
61
  return {
66
62
  tools: {
63
+ bundlerChain: function bundlerChain(chain, param) {
64
+ var isServer = param.isServer;
65
+ addMyTypes2Ignored(chain, isServer ? ssrConfig : csrConfig);
66
+ },
67
+ rspack: function rspack(config, param) {
68
+ var isServer = param.isServer;
69
+ var _config_output;
70
+ modifyBundlerConfig({
71
+ bundlerType,
72
+ mfConfig: isServer ? ssrConfig : csrConfig,
73
+ config,
74
+ isServer,
75
+ modernjsConfig,
76
+ remoteIpStrategy: userConfig.remoteIpStrategy
77
+ });
78
+ userConfig.distOutputDir = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || path.resolve(process.cwd(), "dist");
79
+ },
80
+ webpack: function webpack(config, param) {
81
+ var isServer = param.isServer;
82
+ var _config_output;
83
+ modifyBundlerConfig({
84
+ bundlerType,
85
+ mfConfig: isServer ? ssrConfig : csrConfig,
86
+ config,
87
+ isServer,
88
+ modernjsConfig,
89
+ remoteIpStrategy: userConfig.remoteIpStrategy
90
+ });
91
+ userConfig.distOutputDir = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || path.resolve(process.cwd(), "dist");
92
+ },
67
93
  devServer: {
68
94
  headers: {
69
95
  "Access-Control-Allow-Origin": "*",
@@ -102,6 +128,7 @@ var moduleFederationConfigPlugin = function(userConfig) {
102
128
  var configPlugin_default = moduleFederationConfigPlugin;
103
129
  export {
104
130
  configPlugin_default as default,
131
+ modifyBundlerConfig,
105
132
  moduleFederationConfigPlugin,
106
133
  setEnv
107
134
  };
@@ -24,36 +24,43 @@ var moduleFederationPlugin = function() {
24
24
  var modernjsConfig;
25
25
  return _ts_generator(this, function(_state) {
26
26
  modernjsConfig = api.getConfig();
27
- api.modifyBundlerChain(function(chain, param) {
28
- var isProd = param.isProd, isServer = param.isServer;
29
- var bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
30
- var browserPluginOptions = internalModernPluginOptions.csrConfig;
31
- var MFPlugin = bundlerType === "webpack" ? WebpackModuleFederationPlugin : RspackModuleFederationPlugin;
32
- if (!isServer) {
33
- chain.plugin("plugin-module-federation").use(MFPlugin, [
34
- browserPluginOptions
35
- ]).init(function(Plugin, args) {
36
- internalModernPluginOptions.browserPlugin = new Plugin(args[0]);
37
- return internalModernPluginOptions.browserPlugin;
38
- });
39
- }
40
- if (bundlerType === "webpack") {
41
- var _modernjsConfig_source;
42
- var enableAsyncEntry = (_modernjsConfig_source = modernjsConfig.source) === null || _modernjsConfig_source === void 0 ? void 0 : _modernjsConfig_source.enableAsyncEntry;
43
- if (!enableAsyncEntry && browserPluginOptions.async !== false) {
44
- var asyncBoundaryPluginOptions = _type_of(browserPluginOptions.async) === "object" ? browserPluginOptions.async : {
45
- eager: function(module) {
46
- return module && /\.federation/.test((module === null || module === void 0 ? void 0 : module.request) || "");
47
- },
48
- excludeChunk: function(chunk) {
49
- return chunk.name === browserPluginOptions.name;
27
+ api.config(function() {
28
+ return {
29
+ tools: {
30
+ rspack: function rspack(config, param) {
31
+ var isServer = param.isServer;
32
+ var browserPluginOptions = internalModernPluginOptions.csrConfig;
33
+ if (!isServer) {
34
+ var _config_plugins;
35
+ internalModernPluginOptions.browserPlugin = new RspackModuleFederationPlugin(browserPluginOptions);
36
+ (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(internalModernPluginOptions.browserPlugin);
50
37
  }
51
- };
52
- chain.plugin("async-boundary-plugin").use(AsyncBoundaryPlugin, [
53
- asyncBoundaryPluginOptions
54
- ]);
38
+ },
39
+ webpack: function webpack(config, param) {
40
+ var isServer = param.isServer;
41
+ var _modernjsConfig_source;
42
+ var browserPluginOptions = internalModernPluginOptions.csrConfig;
43
+ if (!isServer) {
44
+ var _config_plugins;
45
+ internalModernPluginOptions.browserPlugin = new WebpackModuleFederationPlugin(browserPluginOptions);
46
+ (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(internalModernPluginOptions.browserPlugin);
47
+ }
48
+ var enableAsyncEntry = (_modernjsConfig_source = modernjsConfig.source) === null || _modernjsConfig_source === void 0 ? void 0 : _modernjsConfig_source.enableAsyncEntry;
49
+ if (!enableAsyncEntry && browserPluginOptions.async !== false) {
50
+ var _config_plugins1;
51
+ var asyncBoundaryPluginOptions = _type_of(browserPluginOptions.async) === "object" ? browserPluginOptions.async : {
52
+ eager: function(module) {
53
+ return module && /\.federation/.test((module === null || module === void 0 ? void 0 : module.request) || "");
54
+ },
55
+ excludeChunk: function(chunk) {
56
+ return chunk.name === browserPluginOptions.name;
57
+ }
58
+ };
59
+ (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(new AsyncBoundaryPlugin(asyncBoundaryPluginOptions));
60
+ }
61
+ }
55
62
  }
56
- }
63
+ };
57
64
  });
58
65
  return [
59
66
  2
@@ -49,26 +49,35 @@ var moduleFederationSSRPlugin = function(pluginOptions) {
49
49
  plugins
50
50
  };
51
51
  });
52
- api.modifyBundlerChain(function(chain, param) {
53
- var isProd = param.isProd, isServer = param.isServer;
54
- var bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
55
- var MFPlugin = bundlerType === "webpack" ? ModuleFederationPlugin : RspackModuleFederationPlugin;
56
- if (isServer) {
57
- if (!pluginOptions.nodePlugin) {
58
- chain.plugin("plugin-module-federation-server").use(MFPlugin, [
59
- pluginOptions.ssrConfig
60
- ]).init(function(Plugin, args) {
61
- pluginOptions.nodePlugin = new Plugin(args[0]);
62
- return pluginOptions.nodePlugin;
63
- });
64
- }
65
- } else {
66
- pluginOptions.distOutputDir = pluginOptions.distOutputDir || chain.output.get("path") || path.resolve(process.cwd(), "dist");
67
- }
68
- });
69
52
  api.config(function() {
70
53
  return {
71
54
  tools: {
55
+ rspack: function rspack(config, param) {
56
+ var isServer = param.isServer;
57
+ if (isServer) {
58
+ if (!pluginOptions.nodePlugin) {
59
+ var _config_plugins;
60
+ pluginOptions.nodePlugin = new RspackModuleFederationPlugin(pluginOptions.ssrConfig);
61
+ (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(pluginOptions.nodePlugin);
62
+ }
63
+ } else {
64
+ var _config_output;
65
+ pluginOptions.distOutputDir = pluginOptions.distOutputDir || ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || path.resolve(process.cwd(), "dist");
66
+ }
67
+ },
68
+ webpack: function webpack(config, param) {
69
+ var isServer = param.isServer;
70
+ if (isServer) {
71
+ if (!pluginOptions.nodePlugin) {
72
+ var _config_plugins;
73
+ pluginOptions.nodePlugin = new ModuleFederationPlugin(pluginOptions.ssrConfig);
74
+ (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(pluginOptions.nodePlugin);
75
+ }
76
+ } else {
77
+ var _config_output;
78
+ pluginOptions.distOutputDir = pluginOptions.distOutputDir || ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || path.resolve(process.cwd(), "dist");
79
+ }
80
+ },
72
81
  devServer: {
73
82
  before: [
74
83
  function(req, res, next) {
@@ -166,14 +166,14 @@ var patchMFConfig = function(mfConfig, isServer, remoteIpStrategy) {
166
166
  mfConfig.dev = false;
167
167
  return mfConfig;
168
168
  };
169
- function patchIgnoreWarning(chain) {
170
- var ignoreWarnings = chain.get("ignoreWarnings") || [];
169
+ function patchIgnoreWarning(bundlerConfig) {
170
+ bundlerConfig.ignoreWarnings = bundlerConfig.ignoreWarnings || [];
171
171
  var ignoredMsgs = [
172
172
  "external script",
173
173
  "process.env.WS_NO_BUFFER_UTIL",
174
174
  "Can't resolve 'utf-8-validate"
175
175
  ];
176
- ignoreWarnings.push(function(warning) {
176
+ bundlerConfig.ignoreWarnings.push(function(warning) {
177
177
  if (ignoredMsgs.some(function(msg) {
178
178
  return warning.message.includes(msg);
179
179
  })) {
@@ -181,7 +181,6 @@ function patchIgnoreWarning(chain) {
181
181
  }
182
182
  return false;
183
183
  });
184
- chain.ignoreWarnings(ignoreWarnings);
185
184
  }
186
185
  function addMyTypes2Ignored(chain, mfConfig) {
187
186
  var watchOptions = chain.get("watchOptions");
@@ -216,44 +215,54 @@ function addMyTypes2Ignored(chain, mfConfig) {
216
215
  }));
217
216
  }
218
217
  function patchBundlerConfig(options) {
219
- var _modernjsConfig_server, _modernjsConfig_deploy;
220
- var chain = options.chain, modernjsConfig = options.modernjsConfig, isServer = options.isServer, mfConfig = options.mfConfig;
218
+ var _modernjsConfig_server, _bundlerConfig_optimization, _bundlerConfig_optimization1, _bundlerConfig_output, _modernjsConfig_deploy;
219
+ var bundlerConfig = options.bundlerConfig, modernjsConfig = options.modernjsConfig, isServer = options.isServer, mfConfig = options.mfConfig, bundlerType = options.bundlerType;
221
220
  var enableSSR = Boolean((_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
222
- chain.optimization.runtimeChunk(false);
223
- patchIgnoreWarning(chain);
224
- if (!chain.output.get("chunkLoadingGlobal")) {
225
- chain.output.chunkLoadingGlobal("chunk_".concat(mfConfig.name));
226
- }
227
- if (!chain.output.get("uniqueName")) {
228
- chain.output.uniqueName(mfConfig.name);
221
+ (_bundlerConfig_optimization = bundlerConfig.optimization) === null || _bundlerConfig_optimization === void 0 ? true : delete _bundlerConfig_optimization.runtimeChunk;
222
+ patchIgnoreWarning(bundlerConfig);
223
+ if (bundlerConfig.output) {
224
+ var _bundlerConfig_output1, _bundlerConfig_output2;
225
+ if (!((_bundlerConfig_output1 = bundlerConfig.output) === null || _bundlerConfig_output1 === void 0 ? void 0 : _bundlerConfig_output1.chunkLoadingGlobal)) {
226
+ bundlerConfig.output.chunkLoadingGlobal = "chunk_".concat(mfConfig.name);
227
+ }
228
+ if (!((_bundlerConfig_output2 = bundlerConfig.output) === null || _bundlerConfig_output2 === void 0 ? void 0 : _bundlerConfig_output2.uniqueName)) {
229
+ bundlerConfig.output.uniqueName = mfConfig.name;
230
+ }
229
231
  }
230
- var optimizationConfig = chain.optimization.entries();
231
232
  if (!isServer) {
232
- autoDeleteSplitChunkCacheGroups(mfConfig, optimizationConfig);
233
+ autoDeleteSplitChunkCacheGroups(mfConfig, bundlerConfig);
233
234
  }
234
- if (!isServer && enableSSR && _type_of(optimizationConfig === null || optimizationConfig === void 0 ? void 0 : optimizationConfig.splitChunks) === "object" && optimizationConfig.splitChunks.cacheGroups) {
235
- optimizationConfig.splitChunks.chunks = "async";
235
+ if (!isServer && enableSSR && _type_of((_bundlerConfig_optimization1 = bundlerConfig.optimization) === null || _bundlerConfig_optimization1 === void 0 ? void 0 : _bundlerConfig_optimization1.splitChunks) === "object" && bundlerConfig.optimization.splitChunks.cacheGroups) {
236
+ bundlerConfig.optimization.splitChunks.chunks = "async";
236
237
  logger.warn('splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"');
237
238
  }
238
- if (isDev && chain.output.get("publicPath") === "auto") {
239
+ if (isDev && ((_bundlerConfig_output = bundlerConfig.output) === null || _bundlerConfig_output === void 0 ? void 0 : _bundlerConfig_output.publicPath) === "auto") {
239
240
  var _modernjsConfig_dev, _modernjsConfig_server1;
240
241
  var port = ((_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.port) || ((_modernjsConfig_server1 = modernjsConfig.server) === null || _modernjsConfig_server1 === void 0 ? void 0 : _modernjsConfig_server1.port) || 8080;
241
242
  var publicPath = "http://localhost:".concat(port, "/");
242
- chain.output.publicPath(publicPath);
243
+ bundlerConfig.output.publicPath = publicPath;
243
244
  }
244
245
  if (isServer && enableSSR) {
245
- var uniqueName = mfConfig.name || chain.output.get("uniqueName");
246
- var chunkFileName = chain.output.get("chunkFilename");
247
- if (typeof chunkFileName === "string" && uniqueName && !chunkFileName.includes(uniqueName)) {
246
+ var output = bundlerConfig.output;
247
+ var uniqueName = mfConfig.name || (output === null || output === void 0 ? void 0 : output.uniqueName);
248
+ var chunkFileName = output === null || output === void 0 ? void 0 : output.chunkFilename;
249
+ if (output && typeof chunkFileName === "string" && uniqueName && !chunkFileName.includes(uniqueName)) {
248
250
  var suffix = "".concat(encodeName(uniqueName), "-[chunkhash].js");
249
- chain.output.chunkFilename(chunkFileName.replace(".js", suffix));
251
+ output.chunkFilename = chunkFileName.replace(".js", suffix);
250
252
  }
251
253
  }
252
254
  if (isDev && enableSSR && !isServer) {
253
- chain.resolve.fallback.set("crypto", false).set("stream", false).set("vm", false);
255
+ bundlerConfig.resolve.fallback = _object_spread_props(_object_spread({}, bundlerConfig.resolve.fallback), {
256
+ crypto: false,
257
+ stream: false,
258
+ vm: false
259
+ });
254
260
  }
255
261
  if (((_modernjsConfig_deploy = modernjsConfig.deploy) === null || _modernjsConfig_deploy === void 0 ? void 0 : _modernjsConfig_deploy.microFrontend) && Object.keys(mfConfig.exposes || {}).length) {
256
- chain.optimization.usedExports(false);
262
+ if (!bundlerConfig.optimization) {
263
+ bundlerConfig.optimization = {};
264
+ }
265
+ bundlerConfig.optimization.usedExports = false;
257
266
  }
258
267
  }
259
268
  var localIpv4 = "127.0.0.1";
@@ -308,5 +317,6 @@ export {
308
317
  getIPV4,
309
318
  getMFConfig,
310
319
  patchBundlerConfig,
320
+ patchIgnoreWarning,
311
321
  patchMFConfig
312
322
  };
@@ -1,7 +1,7 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
3
  import { it, expect, describe } from "vitest";
4
- import { patchMFConfig, getIPV4 } from "./utils";
4
+ import { patchMFConfig, patchBundlerConfig, getIPV4 } from "./utils";
5
5
  var mfConfig = {
6
6
  name: "host",
7
7
  filename: "remoteEntry.js",
@@ -105,3 +105,80 @@ describe("patchMFConfig", /* @__PURE__ */ _async_to_generator(function() {
105
105
  ];
106
106
  });
107
107
  }));
108
+ describe("patchBundlerConfig", /* @__PURE__ */ _async_to_generator(function() {
109
+ return _ts_generator(this, function(_state) {
110
+ it("patchBundlerConfig: server", /* @__PURE__ */ _async_to_generator(function() {
111
+ var bundlerConfig, expectedConfig;
112
+ return _ts_generator(this, function(_state2) {
113
+ bundlerConfig = {
114
+ output: {
115
+ publicPath: "auto"
116
+ }
117
+ };
118
+ patchBundlerConfig({
119
+ bundlerType: "webpack",
120
+ bundlerConfig,
121
+ isServer: true,
122
+ modernjsConfig: {
123
+ server: {
124
+ ssr: {
125
+ mode: "stream"
126
+ }
127
+ }
128
+ },
129
+ mfConfig
130
+ });
131
+ expectedConfig = {
132
+ output: {
133
+ chunkLoadingGlobal: "chunk_host",
134
+ publicPath: "auto",
135
+ uniqueName: "host"
136
+ }
137
+ };
138
+ bundlerConfig === null || bundlerConfig === void 0 ? true : delete bundlerConfig.ignoreWarnings;
139
+ expect(bundlerConfig).toStrictEqual(expectedConfig);
140
+ return [
141
+ 2
142
+ ];
143
+ });
144
+ }));
145
+ it("patchBundlerConfig: client", /* @__PURE__ */ _async_to_generator(function() {
146
+ var bundlerConfig, expectedConfig;
147
+ return _ts_generator(this, function(_state2) {
148
+ bundlerConfig = {
149
+ output: {
150
+ publicPath: "auto"
151
+ }
152
+ };
153
+ patchBundlerConfig({
154
+ bundlerType: "webpack",
155
+ bundlerConfig,
156
+ isServer: false,
157
+ modernjsConfig: {
158
+ server: {
159
+ ssr: {
160
+ mode: "stream"
161
+ }
162
+ }
163
+ },
164
+ mfConfig
165
+ });
166
+ expectedConfig = {
167
+ output: {
168
+ chunkLoadingGlobal: "chunk_host",
169
+ publicPath: "auto",
170
+ uniqueName: "host"
171
+ }
172
+ };
173
+ bundlerConfig === null || bundlerConfig === void 0 ? true : delete bundlerConfig.ignoreWarnings;
174
+ expect(bundlerConfig).toStrictEqual(expectedConfig);
175
+ return [
176
+ 2
177
+ ];
178
+ });
179
+ }));
180
+ return [
181
+ 2
182
+ ];
183
+ });
184
+ }));
@@ -6,6 +6,17 @@ function setEnv(enableSSR) {
6
6
  process.env["MF_SSR_PRJ"] = "true";
7
7
  }
8
8
  }
9
+ function modifyBundlerConfig(options) {
10
+ const { mfConfig, config, isServer, modernjsConfig, remoteIpStrategy = "ipv4", bundlerType } = options;
11
+ patchMFConfig(mfConfig, isServer, remoteIpStrategy);
12
+ patchBundlerConfig({
13
+ bundlerType,
14
+ bundlerConfig: config,
15
+ isServer,
16
+ modernjsConfig,
17
+ mfConfig
18
+ });
19
+ }
9
20
  const moduleFederationConfigPlugin = (userConfig) => ({
10
21
  name: "@modern-js/plugin-module-federation-config",
11
22
  pre: [
@@ -21,20 +32,6 @@ const moduleFederationConfigPlugin = (userConfig) => ({
21
32
  const ssrConfig = userConfig.ssrConfig || JSON.parse(JSON.stringify(mfConfig));
22
33
  userConfig.ssrConfig = ssrConfig;
23
34
  userConfig.csrConfig = csrConfig;
24
- api.modifyBundlerChain((chain, { isProd, isServer }) => {
25
- const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
26
- addMyTypes2Ignored(chain, isServer ? ssrConfig : csrConfig);
27
- const targetMFConfig = isServer ? ssrConfig : csrConfig;
28
- patchMFConfig(targetMFConfig, isServer, userConfig.remoteIpStrategy || "ipv4");
29
- patchBundlerConfig({
30
- // @ts-expect-error chain type is not correct
31
- chain,
32
- isServer,
33
- modernjsConfig,
34
- mfConfig
35
- });
36
- userConfig.distOutputDir = chain.output.get("path") || path.resolve(process.cwd(), "dist");
37
- });
38
35
  api.config(() => {
39
36
  var _userConfig_userConfig, _modernjsConfig_server, _modernjsConfig_source, _modernjsConfig_source1, _modernjsConfig_dev;
40
37
  const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
@@ -50,6 +47,33 @@ const moduleFederationConfigPlugin = (userConfig) => ({
50
47
  var _modernjsConfig_source_enableAsyncEntry;
51
48
  return {
52
49
  tools: {
50
+ bundlerChain(chain, { isServer }) {
51
+ addMyTypes2Ignored(chain, isServer ? ssrConfig : csrConfig);
52
+ },
53
+ rspack(config, { isServer }) {
54
+ var _config_output;
55
+ modifyBundlerConfig({
56
+ bundlerType,
57
+ mfConfig: isServer ? ssrConfig : csrConfig,
58
+ config,
59
+ isServer,
60
+ modernjsConfig,
61
+ remoteIpStrategy: userConfig.remoteIpStrategy
62
+ });
63
+ userConfig.distOutputDir = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || path.resolve(process.cwd(), "dist");
64
+ },
65
+ webpack(config, { isServer }) {
66
+ var _config_output;
67
+ modifyBundlerConfig({
68
+ bundlerType,
69
+ mfConfig: isServer ? ssrConfig : csrConfig,
70
+ config,
71
+ isServer,
72
+ modernjsConfig,
73
+ remoteIpStrategy: userConfig.remoteIpStrategy
74
+ });
75
+ userConfig.distOutputDir = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || path.resolve(process.cwd(), "dist");
76
+ },
53
77
  devServer: {
54
78
  headers: {
55
79
  "Access-Control-Allow-Origin": "*",
@@ -78,6 +102,7 @@ const moduleFederationConfigPlugin = (userConfig) => ({
78
102
  var configPlugin_default = moduleFederationConfigPlugin;
79
103
  export {
80
104
  configPlugin_default as default,
105
+ modifyBundlerConfig,
81
106
  moduleFederationConfigPlugin,
82
107
  setEnv
83
108
  };
@@ -17,31 +17,37 @@ const moduleFederationPlugin = (userConfig = {}) => {
17
17
  name: "@modern-js/plugin-module-federation",
18
18
  setup: async (api) => {
19
19
  const modernjsConfig = api.getConfig();
20
- api.modifyBundlerChain((chain, { isProd, isServer }) => {
21
- const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
22
- const browserPluginOptions = internalModernPluginOptions.csrConfig;
23
- const MFPlugin = bundlerType === "webpack" ? WebpackModuleFederationPlugin : RspackModuleFederationPlugin;
24
- if (!isServer) {
25
- chain.plugin("plugin-module-federation").use(MFPlugin, [
26
- browserPluginOptions
27
- ]).init((Plugin, args) => {
28
- internalModernPluginOptions.browserPlugin = new Plugin(args[0]);
29
- return internalModernPluginOptions.browserPlugin;
30
- });
31
- }
32
- if (bundlerType === "webpack") {
33
- var _modernjsConfig_source;
34
- const enableAsyncEntry = (_modernjsConfig_source = modernjsConfig.source) === null || _modernjsConfig_source === void 0 ? void 0 : _modernjsConfig_source.enableAsyncEntry;
35
- if (!enableAsyncEntry && browserPluginOptions.async !== false) {
36
- const asyncBoundaryPluginOptions = typeof browserPluginOptions.async === "object" ? browserPluginOptions.async : {
37
- eager: (module) => module && /\.federation/.test((module === null || module === void 0 ? void 0 : module.request) || ""),
38
- excludeChunk: (chunk) => chunk.name === browserPluginOptions.name
39
- };
40
- chain.plugin("async-boundary-plugin").use(AsyncBoundaryPlugin, [
41
- asyncBoundaryPluginOptions
42
- ]);
20
+ api.config(() => {
21
+ return {
22
+ tools: {
23
+ rspack(config, { isServer }) {
24
+ const browserPluginOptions = internalModernPluginOptions.csrConfig;
25
+ if (!isServer) {
26
+ var _config_plugins;
27
+ internalModernPluginOptions.browserPlugin = new RspackModuleFederationPlugin(browserPluginOptions);
28
+ (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(internalModernPluginOptions.browserPlugin);
29
+ }
30
+ },
31
+ webpack(config, { isServer }) {
32
+ var _modernjsConfig_source;
33
+ const browserPluginOptions = internalModernPluginOptions.csrConfig;
34
+ if (!isServer) {
35
+ var _config_plugins;
36
+ internalModernPluginOptions.browserPlugin = new WebpackModuleFederationPlugin(browserPluginOptions);
37
+ (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(internalModernPluginOptions.browserPlugin);
38
+ }
39
+ const enableAsyncEntry = (_modernjsConfig_source = modernjsConfig.source) === null || _modernjsConfig_source === void 0 ? void 0 : _modernjsConfig_source.enableAsyncEntry;
40
+ if (!enableAsyncEntry && browserPluginOptions.async !== false) {
41
+ var _config_plugins1;
42
+ const asyncBoundaryPluginOptions = typeof browserPluginOptions.async === "object" ? browserPluginOptions.async : {
43
+ eager: (module) => module && /\.federation/.test((module === null || module === void 0 ? void 0 : module.request) || ""),
44
+ excludeChunk: (chunk) => chunk.name === browserPluginOptions.name
45
+ };
46
+ (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(new AsyncBoundaryPlugin(asyncBoundaryPluginOptions));
47
+ }
48
+ }
43
49
  }
44
- }
50
+ };
45
51
  });
46
52
  },
47
53
  usePlugins: [
@@ -41,25 +41,33 @@ const moduleFederationSSRPlugin = (pluginOptions) => ({
41
41
  plugins
42
42
  };
43
43
  });
44
- api.modifyBundlerChain((chain, { isProd, isServer }) => {
45
- const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
46
- const MFPlugin = bundlerType === "webpack" ? ModuleFederationPlugin : RspackModuleFederationPlugin;
47
- if (isServer) {
48
- if (!pluginOptions.nodePlugin) {
49
- chain.plugin("plugin-module-federation-server").use(MFPlugin, [
50
- pluginOptions.ssrConfig
51
- ]).init((Plugin, args) => {
52
- pluginOptions.nodePlugin = new Plugin(args[0]);
53
- return pluginOptions.nodePlugin;
54
- });
55
- }
56
- } else {
57
- pluginOptions.distOutputDir = pluginOptions.distOutputDir || chain.output.get("path") || path.resolve(process.cwd(), "dist");
58
- }
59
- });
60
44
  api.config(() => {
61
45
  return {
62
46
  tools: {
47
+ rspack(config, { isServer }) {
48
+ if (isServer) {
49
+ if (!pluginOptions.nodePlugin) {
50
+ var _config_plugins;
51
+ pluginOptions.nodePlugin = new RspackModuleFederationPlugin(pluginOptions.ssrConfig);
52
+ (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(pluginOptions.nodePlugin);
53
+ }
54
+ } else {
55
+ var _config_output;
56
+ pluginOptions.distOutputDir = pluginOptions.distOutputDir || ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || path.resolve(process.cwd(), "dist");
57
+ }
58
+ },
59
+ webpack(config, { isServer }) {
60
+ if (isServer) {
61
+ if (!pluginOptions.nodePlugin) {
62
+ var _config_plugins;
63
+ pluginOptions.nodePlugin = new ModuleFederationPlugin(pluginOptions.ssrConfig);
64
+ (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(pluginOptions.nodePlugin);
65
+ }
66
+ } else {
67
+ var _config_output;
68
+ pluginOptions.distOutputDir = pluginOptions.distOutputDir || ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || path.resolve(process.cwd(), "dist");
69
+ }
70
+ },
63
71
  devServer: {
64
72
  before: [
65
73
  (req, res, next) => {
@@ -135,20 +135,19 @@ const patchMFConfig = (mfConfig, isServer, remoteIpStrategy) => {
135
135
  mfConfig.dev = false;
136
136
  return mfConfig;
137
137
  };
138
- function patchIgnoreWarning(chain) {
139
- const ignoreWarnings = chain.get("ignoreWarnings") || [];
138
+ function patchIgnoreWarning(bundlerConfig) {
139
+ bundlerConfig.ignoreWarnings = bundlerConfig.ignoreWarnings || [];
140
140
  const ignoredMsgs = [
141
141
  "external script",
142
142
  "process.env.WS_NO_BUFFER_UTIL",
143
143
  `Can't resolve 'utf-8-validate`
144
144
  ];
145
- ignoreWarnings.push((warning) => {
145
+ bundlerConfig.ignoreWarnings.push((warning) => {
146
146
  if (ignoredMsgs.some((msg) => warning.message.includes(msg))) {
147
147
  return true;
148
148
  }
149
149
  return false;
150
150
  });
151
- chain.ignoreWarnings(ignoreWarnings);
152
151
  }
153
152
  function addMyTypes2Ignored(chain, mfConfig) {
154
153
  const watchOptions = chain.get("watchOptions");
@@ -187,44 +186,55 @@ function addMyTypes2Ignored(chain, mfConfig) {
187
186
  });
188
187
  }
189
188
  function patchBundlerConfig(options) {
190
- var _modernjsConfig_server, _modernjsConfig_deploy;
191
- const { chain, modernjsConfig, isServer, mfConfig } = options;
189
+ var _modernjsConfig_server, _bundlerConfig_optimization, _bundlerConfig_optimization1, _bundlerConfig_output, _modernjsConfig_deploy;
190
+ const { bundlerConfig, modernjsConfig, isServer, mfConfig, bundlerType } = options;
192
191
  const enableSSR = Boolean((_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
193
- chain.optimization.runtimeChunk(false);
194
- patchIgnoreWarning(chain);
195
- if (!chain.output.get("chunkLoadingGlobal")) {
196
- chain.output.chunkLoadingGlobal(`chunk_${mfConfig.name}`);
197
- }
198
- if (!chain.output.get("uniqueName")) {
199
- chain.output.uniqueName(mfConfig.name);
192
+ (_bundlerConfig_optimization = bundlerConfig.optimization) === null || _bundlerConfig_optimization === void 0 ? true : delete _bundlerConfig_optimization.runtimeChunk;
193
+ patchIgnoreWarning(bundlerConfig);
194
+ if (bundlerConfig.output) {
195
+ var _bundlerConfig_output1, _bundlerConfig_output2;
196
+ if (!((_bundlerConfig_output1 = bundlerConfig.output) === null || _bundlerConfig_output1 === void 0 ? void 0 : _bundlerConfig_output1.chunkLoadingGlobal)) {
197
+ bundlerConfig.output.chunkLoadingGlobal = `chunk_${mfConfig.name}`;
198
+ }
199
+ if (!((_bundlerConfig_output2 = bundlerConfig.output) === null || _bundlerConfig_output2 === void 0 ? void 0 : _bundlerConfig_output2.uniqueName)) {
200
+ bundlerConfig.output.uniqueName = mfConfig.name;
201
+ }
200
202
  }
201
- const optimizationConfig = chain.optimization.entries();
202
203
  if (!isServer) {
203
- autoDeleteSplitChunkCacheGroups(mfConfig, optimizationConfig);
204
+ autoDeleteSplitChunkCacheGroups(mfConfig, bundlerConfig);
204
205
  }
205
- if (!isServer && enableSSR && typeof (optimizationConfig === null || optimizationConfig === void 0 ? void 0 : optimizationConfig.splitChunks) === "object" && optimizationConfig.splitChunks.cacheGroups) {
206
- optimizationConfig.splitChunks.chunks = "async";
206
+ if (!isServer && enableSSR && typeof ((_bundlerConfig_optimization1 = bundlerConfig.optimization) === null || _bundlerConfig_optimization1 === void 0 ? void 0 : _bundlerConfig_optimization1.splitChunks) === "object" && bundlerConfig.optimization.splitChunks.cacheGroups) {
207
+ bundlerConfig.optimization.splitChunks.chunks = "async";
207
208
  logger.warn(`splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"`);
208
209
  }
209
- if (isDev && chain.output.get("publicPath") === "auto") {
210
+ if (isDev && ((_bundlerConfig_output = bundlerConfig.output) === null || _bundlerConfig_output === void 0 ? void 0 : _bundlerConfig_output.publicPath) === "auto") {
210
211
  var _modernjsConfig_dev, _modernjsConfig_server1;
211
212
  const port = ((_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.port) || ((_modernjsConfig_server1 = modernjsConfig.server) === null || _modernjsConfig_server1 === void 0 ? void 0 : _modernjsConfig_server1.port) || 8080;
212
213
  const publicPath = `http://localhost:${port}/`;
213
- chain.output.publicPath(publicPath);
214
+ bundlerConfig.output.publicPath = publicPath;
214
215
  }
215
216
  if (isServer && enableSSR) {
216
- const uniqueName = mfConfig.name || chain.output.get("uniqueName");
217
- const chunkFileName = chain.output.get("chunkFilename");
218
- if (typeof chunkFileName === "string" && uniqueName && !chunkFileName.includes(uniqueName)) {
217
+ const { output } = bundlerConfig;
218
+ const uniqueName = mfConfig.name || (output === null || output === void 0 ? void 0 : output.uniqueName);
219
+ const chunkFileName = output === null || output === void 0 ? void 0 : output.chunkFilename;
220
+ if (output && typeof chunkFileName === "string" && uniqueName && !chunkFileName.includes(uniqueName)) {
219
221
  const suffix = `${encodeName(uniqueName)}-[chunkhash].js`;
220
- chain.output.chunkFilename(chunkFileName.replace(".js", suffix));
222
+ output.chunkFilename = chunkFileName.replace(".js", suffix);
221
223
  }
222
224
  }
223
225
  if (isDev && enableSSR && !isServer) {
224
- chain.resolve.fallback.set("crypto", false).set("stream", false).set("vm", false);
226
+ bundlerConfig.resolve.fallback = {
227
+ ...bundlerConfig.resolve.fallback,
228
+ crypto: false,
229
+ stream: false,
230
+ vm: false
231
+ };
225
232
  }
226
233
  if (((_modernjsConfig_deploy = modernjsConfig.deploy) === null || _modernjsConfig_deploy === void 0 ? void 0 : _modernjsConfig_deploy.microFrontend) && Object.keys(mfConfig.exposes || {}).length) {
227
- chain.optimization.usedExports(false);
234
+ if (!bundlerConfig.optimization) {
235
+ bundlerConfig.optimization = {};
236
+ }
237
+ bundlerConfig.optimization.usedExports = false;
228
238
  }
229
239
  }
230
240
  const localIpv4 = "127.0.0.1";
@@ -279,5 +289,6 @@ export {
279
289
  getIPV4,
280
290
  getMFConfig,
281
291
  patchBundlerConfig,
292
+ patchIgnoreWarning,
282
293
  patchMFConfig
283
294
  };
@@ -1,5 +1,5 @@
1
1
  import { it, expect, describe } from "vitest";
2
- import { patchMFConfig, getIPV4 } from "./utils";
2
+ import { patchMFConfig, patchBundlerConfig, getIPV4 } from "./utils";
3
3
  const mfConfig = {
4
4
  name: "host",
5
5
  filename: "remoteEntry.js",
@@ -86,3 +86,63 @@ describe("patchMFConfig", async () => {
86
86
  });
87
87
  });
88
88
  });
89
+ describe("patchBundlerConfig", async () => {
90
+ it("patchBundlerConfig: server", async () => {
91
+ const bundlerConfig = {
92
+ output: {
93
+ publicPath: "auto"
94
+ }
95
+ };
96
+ patchBundlerConfig({
97
+ bundlerType: "webpack",
98
+ bundlerConfig,
99
+ isServer: true,
100
+ modernjsConfig: {
101
+ server: {
102
+ ssr: {
103
+ mode: "stream"
104
+ }
105
+ }
106
+ },
107
+ mfConfig
108
+ });
109
+ const expectedConfig = {
110
+ output: {
111
+ chunkLoadingGlobal: "chunk_host",
112
+ publicPath: "auto",
113
+ uniqueName: "host"
114
+ }
115
+ };
116
+ bundlerConfig === null || bundlerConfig === void 0 ? true : delete bundlerConfig.ignoreWarnings;
117
+ expect(bundlerConfig).toStrictEqual(expectedConfig);
118
+ });
119
+ it("patchBundlerConfig: client", async () => {
120
+ const bundlerConfig = {
121
+ output: {
122
+ publicPath: "auto"
123
+ }
124
+ };
125
+ patchBundlerConfig({
126
+ bundlerType: "webpack",
127
+ bundlerConfig,
128
+ isServer: false,
129
+ modernjsConfig: {
130
+ server: {
131
+ ssr: {
132
+ mode: "stream"
133
+ }
134
+ }
135
+ },
136
+ mfConfig
137
+ });
138
+ const expectedConfig = {
139
+ output: {
140
+ chunkLoadingGlobal: "chunk_host",
141
+ publicPath: "auto",
142
+ uniqueName: "host"
143
+ }
144
+ };
145
+ bundlerConfig === null || bundlerConfig === void 0 ? true : delete bundlerConfig.ignoreWarnings;
146
+ expect(bundlerConfig).toStrictEqual(expectedConfig);
147
+ });
148
+ });
@@ -1,5 +1,15 @@
1
- import type { CliPluginFuture, AppTools } from '@modern-js/app-tools';
1
+ import type { CliPluginFuture, AppTools, UserConfig, Bundler } from '@modern-js/app-tools';
2
+ import type { BundlerConfig } from '../interfaces/bundler';
2
3
  import type { InternalModernPluginOptions } from '../types';
4
+ import { moduleFederationPlugin } from '@module-federation/sdk';
3
5
  export declare function setEnv(enableSSR: boolean): void;
6
+ export declare function modifyBundlerConfig<T extends Bundler>(options: {
7
+ bundlerType: Bundler;
8
+ mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions;
9
+ config: BundlerConfig<T>;
10
+ isServer: boolean;
11
+ modernjsConfig: UserConfig<AppTools>;
12
+ remoteIpStrategy?: 'ipv4' | 'inherit';
13
+ }): void;
4
14
  export declare const moduleFederationConfigPlugin: (userConfig: InternalModernPluginOptions) => CliPluginFuture<AppTools>;
5
15
  export default moduleFederationConfigPlugin;
@@ -1,15 +1,17 @@
1
1
  import { moduleFederationPlugin } from '@module-federation/sdk';
2
2
  import { PluginOptions } from '../types';
3
- import type { BundlerChainConfig } from '../interfaces/bundler';
3
+ import type { BundlerConfig, BundlerChainConfig } from '../interfaces/bundler';
4
4
  import type { webpack, UserConfig, AppTools, Rspack, Bundler } from '@modern-js/app-tools';
5
5
  export type ConfigType<T> = T extends 'webpack' ? webpack.Configuration : T extends 'rspack' ? Rspack.Configuration : never;
6
6
  export declare const getMFConfig: (userConfig: PluginOptions) => Promise<moduleFederationPlugin.ModuleFederationPluginOptions>;
7
7
  export declare const patchMFConfig: (mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions, isServer: boolean, remoteIpStrategy?: "ipv4" | "inherit") => moduleFederationPlugin.ModuleFederationPluginOptions;
8
+ export declare function patchIgnoreWarning<T extends Bundler>(bundlerConfig: BundlerConfig<T>): void;
8
9
  export declare function addMyTypes2Ignored(chain: BundlerChainConfig, mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions): void;
9
10
  export declare function patchBundlerConfig<T extends Bundler>(options: {
10
- chain: BundlerChainConfig;
11
+ bundlerConfig: BundlerConfig<T>;
11
12
  isServer: boolean;
12
13
  modernjsConfig: UserConfig<AppTools>;
14
+ bundlerType: Bundler;
13
15
  mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions;
14
16
  }): void;
15
17
  export declare const getIPV4: () => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/modern-js",
3
- "version": "0.0.0-next-20250218110948",
3
+ "version": "0.0.0-next-20250219033929",
4
4
  "files": [
5
5
  "dist/",
6
6
  "types.d.ts",
@@ -89,10 +89,10 @@
89
89
  "@swc/helpers": "0.5.13",
90
90
  "node-fetch": "~3.3.0",
91
91
  "react-error-boundary": "4.1.2",
92
- "@module-federation/rsbuild-plugin": "0.0.0-next-20250218110948",
93
- "@module-federation/enhanced": "0.0.0-next-20250218110948",
94
- "@module-federation/node": "0.0.0-next-20250218110948",
95
- "@module-federation/sdk": "0.0.0-next-20250218110948"
92
+ "@module-federation/rsbuild-plugin": "0.0.0-next-20250219033929",
93
+ "@module-federation/enhanced": "0.0.0-next-20250219033929",
94
+ "@module-federation/node": "0.0.0-next-20250219033929",
95
+ "@module-federation/sdk": "0.0.0-next-20250219033929"
96
96
  },
97
97
  "devDependencies": {
98
98
  "@modern-js/app-tools": "2.64.0",
@@ -100,7 +100,7 @@
100
100
  "@modern-js/module-tools": "2.64.0",
101
101
  "@modern-js/runtime": "2.64.0",
102
102
  "@modern-js/tsconfig": "2.64.0",
103
- "@module-federation/manifest": "0.0.0-next-20250218110948"
103
+ "@module-federation/manifest": "0.0.0-next-20250219033929"
104
104
  },
105
105
  "peerDependencies": {
106
106
  "react": ">=17",