@module-federation/rsbuild-plugin 0.22.1 → 0.24.0

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.
@@ -1,9 +1,10 @@
1
- import { ModuleFederationPlugin, PLUGIN_NAME } from '@module-federation/enhanced/rspack';
1
+ import { ModuleFederationPlugin, TreeShakingSharedPlugin, PLUGIN_NAME } from '@module-federation/enhanced/rspack';
2
2
  import { SSR_DIR, StatsAssetResource } from '../utils';
3
3
  import type { moduleFederationPlugin } from '@module-federation/sdk';
4
4
  import type { RsbuildPlugin, Rspack } from '@rsbuild/core';
5
5
  type ModuleFederationOptions = moduleFederationPlugin.ModuleFederationPluginOptions;
6
6
  type RSBUILD_PLUGIN_OPTIONS = {
7
+ target?: 'web' | 'node' | 'dual';
7
8
  ssr?: boolean;
8
9
  ssrDir?: string;
9
10
  environment?: string;
@@ -29,3 +30,4 @@ declare const isSSRConfig: (bundlerConfigName?: string) => boolean;
29
30
  declare const isRspressSSGConfig: (bundlerConfigName?: string) => bundlerConfigName is "node";
30
31
  export declare const pluginModuleFederation: (moduleFederationOptions: ModuleFederationOptions, rsbuildOptions?: RSBUILD_PLUGIN_OPTIONS) => RsbuildPlugin;
31
32
  export { createModuleFederationConfig } from '@module-federation/sdk';
33
+ export { TreeShakingSharedPlugin };
package/dist/index.js CHANGED
@@ -41,6 +41,7 @@ __webpack_require__.d(__webpack_exports__, {
41
41
  PLUGIN_NAME: ()=>rspack_namespaceObject.PLUGIN_NAME,
42
42
  SSR_DIR: ()=>SSR_DIR,
43
43
  isMFFormat: ()=>isMFFormat,
44
+ TreeShakingSharedPlugin: ()=>rspack_namespaceObject.TreeShakingSharedPlugin,
44
45
  createModuleFederationConfig: ()=>sdk_namespaceObject.createModuleFederationConfig
45
46
  });
46
47
  const enhanced_namespaceObject = require("@module-federation/enhanced");
@@ -160,22 +161,14 @@ const ssr_require = (0, external_node_module_namespaceObject.createRequire)(__rs
160
161
  const resolve = ssr_require.resolve;
161
162
  const SSR_DIR = 'ssr';
162
163
  const SSR_ENV_NAME = 'mf-ssr';
164
+ const ENV_NAME = 'mf';
163
165
  function setSSREnv() {
164
166
  process.env['MF_SSR_PRJ'] = 'true';
165
167
  }
166
168
  const isDev = ()=>'development' === process.env['NODE_ENV'];
167
- function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry = true, modifyPublicPath = true) {
169
+ function patchNodeConfig(config, mfConfig) {
168
170
  var _config_output;
169
171
  config.output ||= {};
170
- if (modifyPublicPath) {
171
- var _config_output1;
172
- if ('string' != typeof (null == (_config_output1 = config.output) ? void 0 : _config_output1.publicPath)) throw new Error('publicPath must be string!');
173
- const publicPath = config.output.publicPath;
174
- if ('auto' === publicPath) throw new Error('publicPath can not be "auto"!');
175
- const publicPathWithSSRDir = `${publicPath}${ssrDir}/`;
176
- config.output.publicPath = publicPathWithSSRDir;
177
- }
178
- if (callerName === CALL_NAME_MAP.RSPRESS && resetEntry) config.entry = 'data:application/node;base64,';
179
172
  config.target = 'async-node';
180
173
  const UniverseEntryChunkTrackerPlugin = ssr_require('@module-federation/node/universe-entry-chunk-tracker-plugin').default;
181
174
  config.plugins ||= [];
@@ -186,19 +179,60 @@ function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry =
186
179
  const suffix = `${(0, sdk_namespaceObject.encodeName)(uniqueName)}-[contenthash].js`;
187
180
  config.output.chunkFilename = chunkFileName.replace('.js', suffix);
188
181
  }
182
+ }
183
+ function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry = true, modifyPublicPath = true) {
184
+ config.output ||= {};
185
+ if (modifyPublicPath) {
186
+ var _config_output;
187
+ if ('string' != typeof (null == (_config_output = config.output) ? void 0 : _config_output.publicPath)) throw new Error('publicPath must be string!');
188
+ const publicPath = config.output.publicPath;
189
+ if ('auto' === publicPath) throw new Error('publicPath can not be "auto"!');
190
+ const publicPathWithSSRDir = `${publicPath}${ssrDir}/`;
191
+ config.output.publicPath = publicPathWithSSRDir;
192
+ }
193
+ if (callerName === CALL_NAME_MAP.RSPRESS && resetEntry) config.entry = 'data:application/node;base64,';
194
+ patchNodeConfig(config, mfConfig);
189
195
  return config;
190
196
  }
197
+ function patchToolsTspack(envConfig, fn) {
198
+ var _envConfig_tools;
199
+ const rspackArr = [];
200
+ if (null == (_envConfig_tools = envConfig.tools) ? void 0 : _envConfig_tools.rspack) {
201
+ var _envConfig_tools1, _envConfig_tools2;
202
+ if (Array.isArray(null == (_envConfig_tools1 = envConfig.tools) ? void 0 : _envConfig_tools1.rspack)) {
203
+ var _envConfig_tools3;
204
+ rspackArr.push(...null == (_envConfig_tools3 = envConfig.tools) ? void 0 : _envConfig_tools3.rspack);
205
+ } else if ('function' == typeof (null == (_envConfig_tools2 = envConfig.tools) ? void 0 : _envConfig_tools2.rspack)) {
206
+ var _envConfig_tools4;
207
+ rspackArr.push(null == (_envConfig_tools4 = envConfig.tools) ? void 0 : _envConfig_tools4.rspack);
208
+ }
209
+ }
210
+ envConfig.tools ||= {};
211
+ envConfig.tools.rspack = [
212
+ ...rspackArr,
213
+ fn
214
+ ];
215
+ }
191
216
  function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerName) {
192
- var _ssrEnvConfig_output, _ssrEnvConfig_output_distPath, _ssrEnvConfig_output1, _rsbuildConfig_output_distPath, _rsbuildConfig_output;
193
- const ssrEnvConfig = {
194
- ...envConfig,
195
- tools: {
196
- rspack: (config, { environment })=>{
197
- if (environment.name !== SSR_ENV_NAME) return;
198
- patchSSRRspackConfig(config, mfConfig, ssrDir, callerName);
199
- }
217
+ var _envConfig_tools, _ssrEnvConfig_output, _ssrEnvConfig_output_distPath, _ssrEnvConfig_output1, _rsbuildConfig_output_distPath, _rsbuildConfig_output;
218
+ const rspackArr = [];
219
+ if (null == (_envConfig_tools = envConfig.tools) ? void 0 : _envConfig_tools.rspack) {
220
+ var _envConfig_tools1, _envConfig_tools2;
221
+ if (Array.isArray(null == (_envConfig_tools1 = envConfig.tools) ? void 0 : _envConfig_tools1.rspack)) {
222
+ var _envConfig_tools3;
223
+ rspackArr.push(...null == (_envConfig_tools3 = envConfig.tools) ? void 0 : _envConfig_tools3.rspack);
224
+ } else if ('function' == typeof (null == (_envConfig_tools2 = envConfig.tools) ? void 0 : _envConfig_tools2.rspack)) {
225
+ var _envConfig_tools4;
226
+ rspackArr.push(null == (_envConfig_tools4 = envConfig.tools) ? void 0 : _envConfig_tools4.rspack);
200
227
  }
228
+ }
229
+ const ssrEnvConfig = {
230
+ ...envConfig
201
231
  };
232
+ patchToolsTspack(ssrEnvConfig, (config, { environment })=>{
233
+ if (environment.name !== SSR_ENV_NAME) return;
234
+ patchSSRRspackConfig(config, mfConfig, ssrDir, callerName);
235
+ });
202
236
  ssrEnvConfig.output = {
203
237
  ...ssrEnvConfig.output,
204
238
  target: 'node',
@@ -210,26 +244,30 @@ function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerN
210
244
  };
211
245
  return ssrEnvConfig;
212
246
  }
213
- function createSSRMFConfig(mfConfig) {
247
+ function patchNodeMFConfig(mfConfig) {
214
248
  var _mfConfig_library;
249
+ if (mfConfig.remotes) mfConfig.remoteType = "script";
250
+ mfConfig.exposes = {
251
+ ...mfConfig.exposes
252
+ };
253
+ mfConfig.library = {
254
+ ...mfConfig.library,
255
+ name: mfConfig.name,
256
+ type: (null == (_mfConfig_library = mfConfig.library) ? void 0 : _mfConfig_library.type) ?? 'commonjs-module'
257
+ };
258
+ mfConfig.runtimePlugins = [
259
+ ...mfConfig.runtimePlugins || []
260
+ ];
261
+ mfConfig.runtimePlugins.push(resolve('@module-federation/node/runtimePlugin'));
262
+ if (isDev()) mfConfig.runtimePlugins.push(resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'));
263
+ }
264
+ function createSSRMFConfig(mfConfig) {
215
265
  const ssrMFConfig = {
216
- ...mfConfig,
217
- exposes: {
218
- ...mfConfig.exposes
219
- },
220
- library: {
221
- ...mfConfig.library,
222
- name: mfConfig.name,
223
- type: (null == (_mfConfig_library = mfConfig.library) ? void 0 : _mfConfig_library.type) ?? 'commonjs-module'
224
- },
225
- dts: false,
226
- dev: false,
227
- runtimePlugins: [
228
- ...mfConfig.runtimePlugins || []
229
- ]
266
+ ...mfConfig
230
267
  };
231
- ssrMFConfig.runtimePlugins.push(resolve('@module-federation/node/runtimePlugin'));
232
- if (isDev()) ssrMFConfig.runtimePlugins.push(resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'));
268
+ patchNodeMFConfig(ssrMFConfig);
269
+ ssrMFConfig.dts = false;
270
+ ssrMFConfig.dev = false;
233
271
  return ssrMFConfig;
234
272
  }
235
273
  function isRegExp(target) {
@@ -258,18 +296,19 @@ const isRspressSSGConfig = (bundlerConfigName)=>"node" === bundlerConfigName;
258
296
  const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
259
297
  name: RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
260
298
  setup: (api)=>{
261
- const { ssr, ssrDir = SSR_DIR, environment = DEFAULT_MF_ENVIRONMENT_NAME } = rsbuildOptions || {};
299
+ const { target = 'web', ssr, ssrDir = SSR_DIR, environment = DEFAULT_MF_ENVIRONMENT_NAME } = rsbuildOptions || {};
300
+ if (ssr) throw new Error("The `ssr` option is deprecated. If you want to enable SSR, please use `target: 'dual'` instead.");
262
301
  const { callerName } = api.context;
263
302
  const originalRsbuildConfig = api.getRsbuildConfig();
264
303
  if (!callerName) throw new Error('`callerName` is undefined. Please ensure the @rsbuild/core version is higher than 1.3.21 .');
265
304
  const isRslib = callerName === CALL_NAME_MAP.RSLIB;
266
305
  const isRspress = callerName === CALL_NAME_MAP.RSPRESS;
267
- const isSSR = Boolean(ssr);
306
+ const isSSR = 'dual' === target;
268
307
  if (isSSR && !isStoryBook(originalRsbuildConfig)) {
269
308
  var _rsbuildConfig_environments;
270
- if (!isRslib && !isRspress) throw new Error("'ssr' option is only supported in rslib.");
309
+ if (!isRslib && !isRspress) throw new Error("'target' option is only supported in Rslib.");
271
310
  const rsbuildConfig = api.getRsbuildConfig();
272
- if (!(null == (_rsbuildConfig_environments = rsbuildConfig.environments) ? void 0 : _rsbuildConfig_environments[environment]) || Object.keys(rsbuildConfig.environments).some((key)=>key.startsWith(environment) && key !== environment)) throw new Error(`Please set ${RSBUILD_PLUGIN_NAME} as global plugin in rslib.config.ts if you set 'ssr:true' .`);
311
+ if (!(null == (_rsbuildConfig_environments = rsbuildConfig.environments) ? void 0 : _rsbuildConfig_environments[environment]) || Object.keys(rsbuildConfig.environments).some((key)=>key.startsWith(environment) && key !== environment)) throw new Error(`Please set ${RSBUILD_PLUGIN_NAME} as global plugin in rslib.config.ts if you set 'target: "dual"'.`);
273
312
  setSSREnv();
274
313
  }
275
314
  const sharedOptions = (0, enhanced_namespaceObject.parseOptions)(moduleFederationOptions.shared || [], (item, key)=>{
@@ -281,7 +320,7 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
281
320
  import: item
282
321
  };
283
322
  return config;
284
- }, (item, key)=>item);
323
+ }, (item)=>item);
285
324
  const shared = sharedOptions.map((shared)=>shared[0].endsWith('/') ? shared[0].slice(0, -1) : shared[0]);
286
325
  api.modifyRsbuildConfig((config)=>{
287
326
  if (isStoryBook(config)) return;
@@ -308,8 +347,13 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
308
347
  }
309
348
  if (isSSR) {
310
349
  var _config_environments, _config_environments1;
311
- if (null == (_config_environments = config.environments) ? void 0 : _config_environments[SSR_ENV_NAME]) throw new Error(`'${SSR_ENV_NAME}' environment is already defined. Please use another name.`);
350
+ if (null == (_config_environments = config.environments) ? void 0 : _config_environments[SSR_ENV_NAME]) throw new Error(`'${SSR_ENV_NAME}' environment is already defined.Please use another name.`);
312
351
  config.environments[SSR_ENV_NAME] = createSSRREnvConfig(null == (_config_environments1 = config.environments) ? void 0 : _config_environments1[environment], moduleFederationOptions, ssrDir, config, callerName);
352
+ } else if ('node' === target) {
353
+ const mfEnv = config.environments[ENV_NAME];
354
+ patchToolsTspack(mfEnv, (config, { environment })=>{
355
+ config.target = 'async-node';
356
+ });
313
357
  }
314
358
  });
315
359
  api.modifyEnvironmentConfig((config)=>{
@@ -354,7 +398,7 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
354
398
  };
355
399
  } catch (err) {
356
400
  const message = err instanceof Error ? err.message : String(err);
357
- src_logger.error(`Failed to parse stats asset "${assetFileNames.statsFileName}" for environment "${envName}": ${message}`);
401
+ src_logger.error(`Failed to parse stats asset "${assetFileNames.statsFileName}" for environment "${envName}": ${message} `);
358
402
  }
359
403
  const manifestAsset = assets[assetFileNames.manifestFileName];
360
404
  if (manifestAsset) try {
@@ -366,16 +410,16 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
366
410
  };
367
411
  } catch (err) {
368
412
  const message = err instanceof Error ? err.message : String(err);
369
- src_logger.error(`Failed to parse manifest asset "${assetFileNames.manifestFileName}" for environment "${envName}": ${message}`);
413
+ src_logger.error(`Failed to parse manifest asset "${assetFileNames.manifestFileName}" for environment "${envName}": ${message} `);
370
414
  }
371
415
  });
372
416
  api.onBeforeCreateCompiler(({ bundlerConfigs })=>{
373
417
  if (!bundlerConfigs) throw new Error('Can not get bundlerConfigs!');
374
418
  bundlerConfigs.forEach((bundlerConfig)=>{
375
419
  if (!isMFFormat(bundlerConfig) && !isRspress) return;
376
- if (isStoryBook(originalRsbuildConfig)) bundlerConfig.output.uniqueName = `${moduleFederationOptions.name}-storybook-host`;
420
+ if (isStoryBook(originalRsbuildConfig)) bundlerConfig.output.uniqueName = `${moduleFederationOptions.name} -storybook - host`;
377
421
  else {
378
- var _bundlerConfig_optimization, _bundlerConfig_optimization1, _bundlerConfig_output, _bundlerConfig_output1;
422
+ var _bundlerConfig_optimization, _bundlerConfig_optimization1, _bundlerConfig_output, _bundlerConfig_output1, _bundlerConfig_output2;
379
423
  autoDeleteSplitChunkCacheGroups(moduleFederationOptions, null == bundlerConfig ? void 0 : null == (_bundlerConfig_optimization = bundlerConfig.optimization) ? void 0 : _bundlerConfig_optimization.splitChunks);
380
424
  addDataFetchExposes(moduleFederationOptions.exposes, isSSRConfig(bundlerConfig.name));
381
425
  null == (_bundlerConfig_optimization1 = bundlerConfig.optimization) || delete _bundlerConfig_optimization1.runtimeChunk;
@@ -410,13 +454,18 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
410
454
  });
411
455
  if (sharedModules.size > 0) for (const sharedModule of sharedModules)src_logger.log(`${sharedModule} is removed from externals because it is a shared module.`);
412
456
  }
413
- if (!(null == (_bundlerConfig_output = bundlerConfig.output) ? void 0 : _bundlerConfig_output.chunkLoadingGlobal) && !isSSRConfig(bundlerConfig.name) && !isRspressSSGConfig(bundlerConfig.name)) {
457
+ if (!(null == (_bundlerConfig_output = bundlerConfig.output) ? void 0 : _bundlerConfig_output.chunkLoadingGlobal) && !isSSRConfig(bundlerConfig.name) && !isRspressSSGConfig(bundlerConfig.name) && 'node' !== target) {
414
458
  bundlerConfig.output.chunkLoading = 'jsonp';
415
- bundlerConfig.output.chunkLoadingGlobal = `chunk_${moduleFederationOptions.name}`;
459
+ bundlerConfig.output.chunkLoadingGlobal = `chunk_${moduleFederationOptions.name} `;
460
+ }
461
+ if ('node' === target && isMFFormat(bundlerConfig)) {
462
+ patchNodeConfig(bundlerConfig, moduleFederationOptions);
463
+ patchNodeMFConfig(moduleFederationOptions);
416
464
  }
417
465
  if (!(null == (_bundlerConfig_output1 = bundlerConfig.output) ? void 0 : _bundlerConfig_output1.uniqueName)) bundlerConfig.output.uniqueName = moduleFederationOptions.name;
466
+ if ((null == (_bundlerConfig_output2 = bundlerConfig.output) ? void 0 : _bundlerConfig_output2.publicPath) === void 0 && !isSSRConfig(bundlerConfig.name) && !isRspressSSGConfig(bundlerConfig.name)) bundlerConfig.output.publicPath = 'auto';
418
467
  if (!bundlerConfig.plugins.find((p)=>p && p.name === rspack_namespaceObject.PLUGIN_NAME)) {
419
- var _bundlerConfig_output2;
468
+ var _bundlerConfig_output3;
420
469
  if (isSSRConfig(bundlerConfig.name)) {
421
470
  generateMergedStatsAndManifestOptions.options.nodePlugin = new rspack_namespaceObject.ModuleFederationPlugin(createSSRMFConfig(moduleFederationOptions));
422
471
  generateMergedStatsAndManifestOptions.options.nodeEnvironmentName = bundlerConfig.name || SSR_ENV_NAME;
@@ -439,7 +488,7 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
439
488
  return;
440
489
  }
441
490
  generateMergedStatsAndManifestOptions.options.browserPlugin = new rspack_namespaceObject.ModuleFederationPlugin(moduleFederationOptions);
442
- generateMergedStatsAndManifestOptions.options.distOutputDir = (null == (_bundlerConfig_output2 = bundlerConfig.output) ? void 0 : _bundlerConfig_output2.path) || '';
491
+ generateMergedStatsAndManifestOptions.options.distOutputDir = (null == (_bundlerConfig_output3 = bundlerConfig.output) ? void 0 : _bundlerConfig_output3.path) || '';
443
492
  generateMergedStatsAndManifestOptions.options.browserEnvironmentName = bundlerConfig.name || defaultBrowserEnvironmentName;
444
493
  bundlerConfig.plugins.push(generateMergedStatsAndManifestOptions.options.browserPlugin);
445
494
  }
@@ -470,6 +519,7 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
470
519
  exports.PLUGIN_NAME = __webpack_exports__.PLUGIN_NAME;
471
520
  exports.RSBUILD_PLUGIN_MODULE_FEDERATION_NAME = __webpack_exports__.RSBUILD_PLUGIN_MODULE_FEDERATION_NAME;
472
521
  exports.SSR_DIR = __webpack_exports__.SSR_DIR;
522
+ exports.TreeShakingSharedPlugin = __webpack_exports__.TreeShakingSharedPlugin;
473
523
  exports.createModuleFederationConfig = __webpack_exports__.createModuleFederationConfig;
474
524
  exports.isMFFormat = __webpack_exports__.isMFFormat;
475
525
  exports.pluginModuleFederation = __webpack_exports__.pluginModuleFederation;
@@ -477,6 +527,7 @@ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
477
527
  "PLUGIN_NAME",
478
528
  "RSBUILD_PLUGIN_MODULE_FEDERATION_NAME",
479
529
  "SSR_DIR",
530
+ "TreeShakingSharedPlugin",
480
531
  "createModuleFederationConfig",
481
532
  "isMFFormat",
482
533
  "pluginModuleFederation"
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { parseOptions } from "@module-federation/enhanced";
2
- import { ModuleFederationPlugin, PLUGIN_NAME } from "@module-federation/enhanced/rspack";
2
+ import { ModuleFederationPlugin, PLUGIN_NAME, TreeShakingSharedPlugin } from "@module-federation/enhanced/rspack";
3
3
  import { TEMP_DIR, createLogger, createModuleFederationConfig, encodeName, getManifestFileName, isRequiredVersion } from "@module-federation/sdk";
4
4
  import util from "util";
5
5
  import fs_extra from "fs-extra";
@@ -112,22 +112,14 @@ const ssr_require = createRequire(import.meta.url);
112
112
  const resolve = ssr_require.resolve;
113
113
  const SSR_DIR = 'ssr';
114
114
  const SSR_ENV_NAME = 'mf-ssr';
115
+ const ENV_NAME = 'mf';
115
116
  function setSSREnv() {
116
117
  process.env['MF_SSR_PRJ'] = 'true';
117
118
  }
118
119
  const isDev = ()=>'development' === process.env['NODE_ENV'];
119
- function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry = true, modifyPublicPath = true) {
120
+ function patchNodeConfig(config, mfConfig) {
120
121
  var _config_output;
121
122
  config.output ||= {};
122
- if (modifyPublicPath) {
123
- var _config_output1;
124
- if ('string' != typeof (null == (_config_output1 = config.output) ? void 0 : _config_output1.publicPath)) throw new Error('publicPath must be string!');
125
- const publicPath = config.output.publicPath;
126
- if ('auto' === publicPath) throw new Error('publicPath can not be "auto"!');
127
- const publicPathWithSSRDir = `${publicPath}${ssrDir}/`;
128
- config.output.publicPath = publicPathWithSSRDir;
129
- }
130
- if (callerName === CALL_NAME_MAP.RSPRESS && resetEntry) config.entry = 'data:application/node;base64,';
131
123
  config.target = 'async-node';
132
124
  const UniverseEntryChunkTrackerPlugin = ssr_require('@module-federation/node/universe-entry-chunk-tracker-plugin').default;
133
125
  config.plugins ||= [];
@@ -138,19 +130,60 @@ function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry =
138
130
  const suffix = `${encodeName(uniqueName)}-[contenthash].js`;
139
131
  config.output.chunkFilename = chunkFileName.replace('.js', suffix);
140
132
  }
133
+ }
134
+ function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry = true, modifyPublicPath = true) {
135
+ config.output ||= {};
136
+ if (modifyPublicPath) {
137
+ var _config_output;
138
+ if ('string' != typeof (null == (_config_output = config.output) ? void 0 : _config_output.publicPath)) throw new Error('publicPath must be string!');
139
+ const publicPath = config.output.publicPath;
140
+ if ('auto' === publicPath) throw new Error('publicPath can not be "auto"!');
141
+ const publicPathWithSSRDir = `${publicPath}${ssrDir}/`;
142
+ config.output.publicPath = publicPathWithSSRDir;
143
+ }
144
+ if (callerName === CALL_NAME_MAP.RSPRESS && resetEntry) config.entry = 'data:application/node;base64,';
145
+ patchNodeConfig(config, mfConfig);
141
146
  return config;
142
147
  }
148
+ function patchToolsTspack(envConfig, fn) {
149
+ var _envConfig_tools;
150
+ const rspackArr = [];
151
+ if (null == (_envConfig_tools = envConfig.tools) ? void 0 : _envConfig_tools.rspack) {
152
+ var _envConfig_tools1, _envConfig_tools2;
153
+ if (Array.isArray(null == (_envConfig_tools1 = envConfig.tools) ? void 0 : _envConfig_tools1.rspack)) {
154
+ var _envConfig_tools3;
155
+ rspackArr.push(...null == (_envConfig_tools3 = envConfig.tools) ? void 0 : _envConfig_tools3.rspack);
156
+ } else if ('function' == typeof (null == (_envConfig_tools2 = envConfig.tools) ? void 0 : _envConfig_tools2.rspack)) {
157
+ var _envConfig_tools4;
158
+ rspackArr.push(null == (_envConfig_tools4 = envConfig.tools) ? void 0 : _envConfig_tools4.rspack);
159
+ }
160
+ }
161
+ envConfig.tools ||= {};
162
+ envConfig.tools.rspack = [
163
+ ...rspackArr,
164
+ fn
165
+ ];
166
+ }
143
167
  function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerName) {
144
- var _ssrEnvConfig_output, _ssrEnvConfig_output_distPath, _ssrEnvConfig_output1, _rsbuildConfig_output_distPath, _rsbuildConfig_output;
145
- const ssrEnvConfig = {
146
- ...envConfig,
147
- tools: {
148
- rspack: (config, { environment })=>{
149
- if (environment.name !== SSR_ENV_NAME) return;
150
- patchSSRRspackConfig(config, mfConfig, ssrDir, callerName);
151
- }
168
+ var _envConfig_tools, _ssrEnvConfig_output, _ssrEnvConfig_output_distPath, _ssrEnvConfig_output1, _rsbuildConfig_output_distPath, _rsbuildConfig_output;
169
+ const rspackArr = [];
170
+ if (null == (_envConfig_tools = envConfig.tools) ? void 0 : _envConfig_tools.rspack) {
171
+ var _envConfig_tools1, _envConfig_tools2;
172
+ if (Array.isArray(null == (_envConfig_tools1 = envConfig.tools) ? void 0 : _envConfig_tools1.rspack)) {
173
+ var _envConfig_tools3;
174
+ rspackArr.push(...null == (_envConfig_tools3 = envConfig.tools) ? void 0 : _envConfig_tools3.rspack);
175
+ } else if ('function' == typeof (null == (_envConfig_tools2 = envConfig.tools) ? void 0 : _envConfig_tools2.rspack)) {
176
+ var _envConfig_tools4;
177
+ rspackArr.push(null == (_envConfig_tools4 = envConfig.tools) ? void 0 : _envConfig_tools4.rspack);
152
178
  }
179
+ }
180
+ const ssrEnvConfig = {
181
+ ...envConfig
153
182
  };
183
+ patchToolsTspack(ssrEnvConfig, (config, { environment })=>{
184
+ if (environment.name !== SSR_ENV_NAME) return;
185
+ patchSSRRspackConfig(config, mfConfig, ssrDir, callerName);
186
+ });
154
187
  ssrEnvConfig.output = {
155
188
  ...ssrEnvConfig.output,
156
189
  target: 'node',
@@ -162,26 +195,30 @@ function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerN
162
195
  };
163
196
  return ssrEnvConfig;
164
197
  }
165
- function createSSRMFConfig(mfConfig) {
198
+ function patchNodeMFConfig(mfConfig) {
166
199
  var _mfConfig_library;
200
+ if (mfConfig.remotes) mfConfig.remoteType = "script";
201
+ mfConfig.exposes = {
202
+ ...mfConfig.exposes
203
+ };
204
+ mfConfig.library = {
205
+ ...mfConfig.library,
206
+ name: mfConfig.name,
207
+ type: (null == (_mfConfig_library = mfConfig.library) ? void 0 : _mfConfig_library.type) ?? 'commonjs-module'
208
+ };
209
+ mfConfig.runtimePlugins = [
210
+ ...mfConfig.runtimePlugins || []
211
+ ];
212
+ mfConfig.runtimePlugins.push(resolve('@module-federation/node/runtimePlugin'));
213
+ if (isDev()) mfConfig.runtimePlugins.push(resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'));
214
+ }
215
+ function createSSRMFConfig(mfConfig) {
167
216
  const ssrMFConfig = {
168
- ...mfConfig,
169
- exposes: {
170
- ...mfConfig.exposes
171
- },
172
- library: {
173
- ...mfConfig.library,
174
- name: mfConfig.name,
175
- type: (null == (_mfConfig_library = mfConfig.library) ? void 0 : _mfConfig_library.type) ?? 'commonjs-module'
176
- },
177
- dts: false,
178
- dev: false,
179
- runtimePlugins: [
180
- ...mfConfig.runtimePlugins || []
181
- ]
217
+ ...mfConfig
182
218
  };
183
- ssrMFConfig.runtimePlugins.push(resolve('@module-federation/node/runtimePlugin'));
184
- if (isDev()) ssrMFConfig.runtimePlugins.push(resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'));
219
+ patchNodeMFConfig(ssrMFConfig);
220
+ ssrMFConfig.dts = false;
221
+ ssrMFConfig.dev = false;
185
222
  return ssrMFConfig;
186
223
  }
187
224
  function isRegExp(target) {
@@ -210,18 +247,19 @@ const isRspressSSGConfig = (bundlerConfigName)=>"node" === bundlerConfigName;
210
247
  const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
211
248
  name: RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
212
249
  setup: (api)=>{
213
- const { ssr, ssrDir = SSR_DIR, environment = DEFAULT_MF_ENVIRONMENT_NAME } = rsbuildOptions || {};
250
+ const { target = 'web', ssr, ssrDir = SSR_DIR, environment = DEFAULT_MF_ENVIRONMENT_NAME } = rsbuildOptions || {};
251
+ if (ssr) throw new Error("The `ssr` option is deprecated. If you want to enable SSR, please use `target: 'dual'` instead.");
214
252
  const { callerName } = api.context;
215
253
  const originalRsbuildConfig = api.getRsbuildConfig();
216
254
  if (!callerName) throw new Error('`callerName` is undefined. Please ensure the @rsbuild/core version is higher than 1.3.21 .');
217
255
  const isRslib = callerName === CALL_NAME_MAP.RSLIB;
218
256
  const isRspress = callerName === CALL_NAME_MAP.RSPRESS;
219
- const isSSR = Boolean(ssr);
257
+ const isSSR = 'dual' === target;
220
258
  if (isSSR && !isStoryBook(originalRsbuildConfig)) {
221
259
  var _rsbuildConfig_environments;
222
- if (!isRslib && !isRspress) throw new Error("'ssr' option is only supported in rslib.");
260
+ if (!isRslib && !isRspress) throw new Error("'target' option is only supported in Rslib.");
223
261
  const rsbuildConfig = api.getRsbuildConfig();
224
- if (!(null == (_rsbuildConfig_environments = rsbuildConfig.environments) ? void 0 : _rsbuildConfig_environments[environment]) || Object.keys(rsbuildConfig.environments).some((key)=>key.startsWith(environment) && key !== environment)) throw new Error(`Please set ${RSBUILD_PLUGIN_NAME} as global plugin in rslib.config.ts if you set 'ssr:true' .`);
262
+ if (!(null == (_rsbuildConfig_environments = rsbuildConfig.environments) ? void 0 : _rsbuildConfig_environments[environment]) || Object.keys(rsbuildConfig.environments).some((key)=>key.startsWith(environment) && key !== environment)) throw new Error(`Please set ${RSBUILD_PLUGIN_NAME} as global plugin in rslib.config.ts if you set 'target: "dual"'.`);
225
263
  setSSREnv();
226
264
  }
227
265
  const sharedOptions = parseOptions(moduleFederationOptions.shared || [], (item, key)=>{
@@ -233,7 +271,7 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
233
271
  import: item
234
272
  };
235
273
  return config;
236
- }, (item, key)=>item);
274
+ }, (item)=>item);
237
275
  const shared = sharedOptions.map((shared)=>shared[0].endsWith('/') ? shared[0].slice(0, -1) : shared[0]);
238
276
  api.modifyRsbuildConfig((config)=>{
239
277
  if (isStoryBook(config)) return;
@@ -260,8 +298,13 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
260
298
  }
261
299
  if (isSSR) {
262
300
  var _config_environments, _config_environments1;
263
- if (null == (_config_environments = config.environments) ? void 0 : _config_environments[SSR_ENV_NAME]) throw new Error(`'${SSR_ENV_NAME}' environment is already defined. Please use another name.`);
301
+ if (null == (_config_environments = config.environments) ? void 0 : _config_environments[SSR_ENV_NAME]) throw new Error(`'${SSR_ENV_NAME}' environment is already defined.Please use another name.`);
264
302
  config.environments[SSR_ENV_NAME] = createSSRREnvConfig(null == (_config_environments1 = config.environments) ? void 0 : _config_environments1[environment], moduleFederationOptions, ssrDir, config, callerName);
303
+ } else if ('node' === target) {
304
+ const mfEnv = config.environments[ENV_NAME];
305
+ patchToolsTspack(mfEnv, (config, { environment })=>{
306
+ config.target = 'async-node';
307
+ });
265
308
  }
266
309
  });
267
310
  api.modifyEnvironmentConfig((config)=>{
@@ -306,7 +349,7 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
306
349
  };
307
350
  } catch (err) {
308
351
  const message = err instanceof Error ? err.message : String(err);
309
- src_logger.error(`Failed to parse stats asset "${assetFileNames.statsFileName}" for environment "${envName}": ${message}`);
352
+ src_logger.error(`Failed to parse stats asset "${assetFileNames.statsFileName}" for environment "${envName}": ${message} `);
310
353
  }
311
354
  const manifestAsset = assets[assetFileNames.manifestFileName];
312
355
  if (manifestAsset) try {
@@ -318,16 +361,16 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
318
361
  };
319
362
  } catch (err) {
320
363
  const message = err instanceof Error ? err.message : String(err);
321
- src_logger.error(`Failed to parse manifest asset "${assetFileNames.manifestFileName}" for environment "${envName}": ${message}`);
364
+ src_logger.error(`Failed to parse manifest asset "${assetFileNames.manifestFileName}" for environment "${envName}": ${message} `);
322
365
  }
323
366
  });
324
367
  api.onBeforeCreateCompiler(({ bundlerConfigs })=>{
325
368
  if (!bundlerConfigs) throw new Error('Can not get bundlerConfigs!');
326
369
  bundlerConfigs.forEach((bundlerConfig)=>{
327
370
  if (!isMFFormat(bundlerConfig) && !isRspress) return;
328
- if (isStoryBook(originalRsbuildConfig)) bundlerConfig.output.uniqueName = `${moduleFederationOptions.name}-storybook-host`;
371
+ if (isStoryBook(originalRsbuildConfig)) bundlerConfig.output.uniqueName = `${moduleFederationOptions.name} -storybook - host`;
329
372
  else {
330
- var _bundlerConfig_optimization, _bundlerConfig_optimization1, _bundlerConfig_output, _bundlerConfig_output1;
373
+ var _bundlerConfig_optimization, _bundlerConfig_optimization1, _bundlerConfig_output, _bundlerConfig_output1, _bundlerConfig_output2;
331
374
  autoDeleteSplitChunkCacheGroups(moduleFederationOptions, null == bundlerConfig ? void 0 : null == (_bundlerConfig_optimization = bundlerConfig.optimization) ? void 0 : _bundlerConfig_optimization.splitChunks);
332
375
  addDataFetchExposes(moduleFederationOptions.exposes, isSSRConfig(bundlerConfig.name));
333
376
  null == (_bundlerConfig_optimization1 = bundlerConfig.optimization) || delete _bundlerConfig_optimization1.runtimeChunk;
@@ -362,13 +405,18 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
362
405
  });
363
406
  if (sharedModules.size > 0) for (const sharedModule of sharedModules)src_logger.log(`${sharedModule} is removed from externals because it is a shared module.`);
364
407
  }
365
- if (!(null == (_bundlerConfig_output = bundlerConfig.output) ? void 0 : _bundlerConfig_output.chunkLoadingGlobal) && !isSSRConfig(bundlerConfig.name) && !isRspressSSGConfig(bundlerConfig.name)) {
408
+ if (!(null == (_bundlerConfig_output = bundlerConfig.output) ? void 0 : _bundlerConfig_output.chunkLoadingGlobal) && !isSSRConfig(bundlerConfig.name) && !isRspressSSGConfig(bundlerConfig.name) && 'node' !== target) {
366
409
  bundlerConfig.output.chunkLoading = 'jsonp';
367
- bundlerConfig.output.chunkLoadingGlobal = `chunk_${moduleFederationOptions.name}`;
410
+ bundlerConfig.output.chunkLoadingGlobal = `chunk_${moduleFederationOptions.name} `;
411
+ }
412
+ if ('node' === target && isMFFormat(bundlerConfig)) {
413
+ patchNodeConfig(bundlerConfig, moduleFederationOptions);
414
+ patchNodeMFConfig(moduleFederationOptions);
368
415
  }
369
416
  if (!(null == (_bundlerConfig_output1 = bundlerConfig.output) ? void 0 : _bundlerConfig_output1.uniqueName)) bundlerConfig.output.uniqueName = moduleFederationOptions.name;
417
+ if ((null == (_bundlerConfig_output2 = bundlerConfig.output) ? void 0 : _bundlerConfig_output2.publicPath) === void 0 && !isSSRConfig(bundlerConfig.name) && !isRspressSSGConfig(bundlerConfig.name)) bundlerConfig.output.publicPath = 'auto';
370
418
  if (!bundlerConfig.plugins.find((p)=>p && p.name === PLUGIN_NAME)) {
371
- var _bundlerConfig_output2;
419
+ var _bundlerConfig_output3;
372
420
  if (isSSRConfig(bundlerConfig.name)) {
373
421
  generateMergedStatsAndManifestOptions.options.nodePlugin = new ModuleFederationPlugin(createSSRMFConfig(moduleFederationOptions));
374
422
  generateMergedStatsAndManifestOptions.options.nodeEnvironmentName = bundlerConfig.name || SSR_ENV_NAME;
@@ -391,7 +439,7 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
391
439
  return;
392
440
  }
393
441
  generateMergedStatsAndManifestOptions.options.browserPlugin = new ModuleFederationPlugin(moduleFederationOptions);
394
- generateMergedStatsAndManifestOptions.options.distOutputDir = (null == (_bundlerConfig_output2 = bundlerConfig.output) ? void 0 : _bundlerConfig_output2.path) || '';
442
+ generateMergedStatsAndManifestOptions.options.distOutputDir = (null == (_bundlerConfig_output3 = bundlerConfig.output) ? void 0 : _bundlerConfig_output3.path) || '';
395
443
  generateMergedStatsAndManifestOptions.options.browserEnvironmentName = bundlerConfig.name || defaultBrowserEnvironmentName;
396
444
  bundlerConfig.plugins.push(generateMergedStatsAndManifestOptions.options.browserPlugin);
397
445
  }
@@ -419,4 +467,4 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
419
467
  });
420
468
  }
421
469
  });
422
- export { PLUGIN_NAME, RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, SSR_DIR, createModuleFederationConfig, isMFFormat, pluginModuleFederation };
470
+ export { PLUGIN_NAME, RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, SSR_DIR, TreeShakingSharedPlugin, createModuleFederationConfig, isMFFormat, pluginModuleFederation };
@@ -1,24 +1,20 @@
1
- import type { EnvironmentConfig, RsbuildConfig, Rspack } from '@rsbuild/core';
1
+ import type { EnvironmentConfig, RsbuildConfig, Rspack, EnvironmentContext } from '@rsbuild/core';
2
2
  import type { moduleFederationPlugin } from '@module-federation/sdk';
3
3
  export declare const SSR_DIR = "ssr";
4
4
  export declare const SSR_ENV_NAME = "mf-ssr";
5
+ export declare const ENV_NAME = "mf";
5
6
  export declare function setSSREnv(): void;
7
+ export declare function patchNodeConfig(config: Rspack.Configuration, mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions): void;
6
8
  export declare function patchSSRRspackConfig(config: Rspack.Configuration, mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions, ssrDir: string, callerName?: string, resetEntry?: boolean, modifyPublicPath?: boolean): Rspack.RspackOptions;
9
+ export declare function patchToolsTspack(envConfig: EnvironmentConfig, fn: (config: Rspack.RspackOptions, { environment }: {
10
+ environment: EnvironmentContext;
11
+ }) => void): void;
7
12
  export declare function createSSRREnvConfig(envConfig: EnvironmentConfig, mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions, ssrDir: string, rsbuildConfig: RsbuildConfig, callerName?: string): EnvironmentConfig;
13
+ export declare function patchNodeMFConfig(mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions): void;
8
14
  export declare function createSSRMFConfig(mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions): {
9
- exposes: {};
10
- library: {
11
- name: string | undefined;
12
- type: string;
13
- amdContainer?: moduleFederationPlugin.AmdContainer;
14
- auxiliaryComment?: moduleFederationPlugin.AuxiliaryComment;
15
- export?: moduleFederationPlugin.LibraryExport;
16
- umdNamedDefine?: moduleFederationPlugin.UmdNamedDefine;
17
- };
18
- dts: boolean;
19
- dev: boolean;
20
- runtimePlugins: (string | [string, Record<string, unknown>])[];
15
+ exposes?: moduleFederationPlugin.Exposes;
21
16
  filename?: string;
17
+ library?: moduleFederationPlugin.LibraryOptions;
22
18
  name?: string;
23
19
  remoteType?: moduleFederationPlugin.ExternalsType;
24
20
  remotes?: moduleFederationPlugin.Remotes;
@@ -26,9 +22,12 @@ export declare function createSSRMFConfig(mfConfig: moduleFederationPlugin.Modul
26
22
  shareScope?: string | string[];
27
23
  shareStrategy?: moduleFederationPlugin.SharedStrategy;
28
24
  shared?: moduleFederationPlugin.Shared;
25
+ runtimePlugins?: (string | [string, Record<string, unknown>])[];
29
26
  getPublicPath?: string;
30
27
  implementation?: string;
31
28
  manifest?: boolean | moduleFederationPlugin.PluginManifestOptions;
29
+ dev?: boolean | moduleFederationPlugin.PluginDevOptions;
30
+ dts?: boolean | moduleFederationPlugin.PluginDtsOptions;
32
31
  dataPrefetch?: moduleFederationPlugin.DataPrefetch;
33
32
  virtualRuntimeEntry?: boolean;
34
33
  experiments?: {
@@ -45,4 +44,8 @@ export declare function createSSRMFConfig(mfConfig: moduleFederationPlugin.Modul
45
44
  disableAlias?: boolean;
46
45
  };
47
46
  async?: boolean | moduleFederationPlugin.AsyncBoundaryOptions;
47
+ treeShakingDir?: string;
48
+ injectTreeShakingUsedExports?: boolean;
49
+ treeShakingSharedExcludePlugins?: string[];
50
+ treeShakingSharedPlugins?: string[];
48
51
  };
package/dist/utils.js CHANGED
@@ -164,18 +164,9 @@ function setSSREnv() {
164
164
  process.env['MF_SSR_PRJ'] = 'true';
165
165
  }
166
166
  const isDev = ()=>'development' === process.env['NODE_ENV'];
167
- function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry = true, modifyPublicPath = true) {
167
+ function patchNodeConfig(config, mfConfig) {
168
168
  var _config_output;
169
169
  config.output ||= {};
170
- if (modifyPublicPath) {
171
- var _config_output1;
172
- if ('string' != typeof (null == (_config_output1 = config.output) ? void 0 : _config_output1.publicPath)) throw new Error('publicPath must be string!');
173
- const publicPath = config.output.publicPath;
174
- if ('auto' === publicPath) throw new Error('publicPath can not be "auto"!');
175
- const publicPathWithSSRDir = `${publicPath}${ssrDir}/`;
176
- config.output.publicPath = publicPathWithSSRDir;
177
- }
178
- if (callerName === CALL_NAME_MAP.RSPRESS && resetEntry) config.entry = 'data:application/node;base64,';
179
170
  config.target = 'async-node';
180
171
  const UniverseEntryChunkTrackerPlugin = ssr_require('@module-federation/node/universe-entry-chunk-tracker-plugin').default;
181
172
  config.plugins ||= [];
@@ -186,19 +177,60 @@ function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry =
186
177
  const suffix = `${(0, sdk_namespaceObject.encodeName)(uniqueName)}-[contenthash].js`;
187
178
  config.output.chunkFilename = chunkFileName.replace('.js', suffix);
188
179
  }
180
+ }
181
+ function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry = true, modifyPublicPath = true) {
182
+ config.output ||= {};
183
+ if (modifyPublicPath) {
184
+ var _config_output;
185
+ if ('string' != typeof (null == (_config_output = config.output) ? void 0 : _config_output.publicPath)) throw new Error('publicPath must be string!');
186
+ const publicPath = config.output.publicPath;
187
+ if ('auto' === publicPath) throw new Error('publicPath can not be "auto"!');
188
+ const publicPathWithSSRDir = `${publicPath}${ssrDir}/`;
189
+ config.output.publicPath = publicPathWithSSRDir;
190
+ }
191
+ if (callerName === CALL_NAME_MAP.RSPRESS && resetEntry) config.entry = 'data:application/node;base64,';
192
+ patchNodeConfig(config, mfConfig);
189
193
  return config;
190
194
  }
195
+ function patchToolsTspack(envConfig, fn) {
196
+ var _envConfig_tools;
197
+ const rspackArr = [];
198
+ if (null == (_envConfig_tools = envConfig.tools) ? void 0 : _envConfig_tools.rspack) {
199
+ var _envConfig_tools1, _envConfig_tools2;
200
+ if (Array.isArray(null == (_envConfig_tools1 = envConfig.tools) ? void 0 : _envConfig_tools1.rspack)) {
201
+ var _envConfig_tools3;
202
+ rspackArr.push(...null == (_envConfig_tools3 = envConfig.tools) ? void 0 : _envConfig_tools3.rspack);
203
+ } else if ('function' == typeof (null == (_envConfig_tools2 = envConfig.tools) ? void 0 : _envConfig_tools2.rspack)) {
204
+ var _envConfig_tools4;
205
+ rspackArr.push(null == (_envConfig_tools4 = envConfig.tools) ? void 0 : _envConfig_tools4.rspack);
206
+ }
207
+ }
208
+ envConfig.tools ||= {};
209
+ envConfig.tools.rspack = [
210
+ ...rspackArr,
211
+ fn
212
+ ];
213
+ }
191
214
  function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerName) {
192
- var _ssrEnvConfig_output, _ssrEnvConfig_output_distPath, _ssrEnvConfig_output1, _rsbuildConfig_output_distPath, _rsbuildConfig_output;
193
- const ssrEnvConfig = {
194
- ...envConfig,
195
- tools: {
196
- rspack: (config, { environment })=>{
197
- if (environment.name !== SSR_ENV_NAME) return;
198
- patchSSRRspackConfig(config, mfConfig, ssrDir, callerName);
199
- }
215
+ var _envConfig_tools, _ssrEnvConfig_output, _ssrEnvConfig_output_distPath, _ssrEnvConfig_output1, _rsbuildConfig_output_distPath, _rsbuildConfig_output;
216
+ const rspackArr = [];
217
+ if (null == (_envConfig_tools = envConfig.tools) ? void 0 : _envConfig_tools.rspack) {
218
+ var _envConfig_tools1, _envConfig_tools2;
219
+ if (Array.isArray(null == (_envConfig_tools1 = envConfig.tools) ? void 0 : _envConfig_tools1.rspack)) {
220
+ var _envConfig_tools3;
221
+ rspackArr.push(...null == (_envConfig_tools3 = envConfig.tools) ? void 0 : _envConfig_tools3.rspack);
222
+ } else if ('function' == typeof (null == (_envConfig_tools2 = envConfig.tools) ? void 0 : _envConfig_tools2.rspack)) {
223
+ var _envConfig_tools4;
224
+ rspackArr.push(null == (_envConfig_tools4 = envConfig.tools) ? void 0 : _envConfig_tools4.rspack);
200
225
  }
226
+ }
227
+ const ssrEnvConfig = {
228
+ ...envConfig
201
229
  };
230
+ patchToolsTspack(ssrEnvConfig, (config, { environment })=>{
231
+ if (environment.name !== SSR_ENV_NAME) return;
232
+ patchSSRRspackConfig(config, mfConfig, ssrDir, callerName);
233
+ });
202
234
  ssrEnvConfig.output = {
203
235
  ...ssrEnvConfig.output,
204
236
  target: 'node',
@@ -210,26 +242,30 @@ function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerN
210
242
  };
211
243
  return ssrEnvConfig;
212
244
  }
213
- function createSSRMFConfig(mfConfig) {
245
+ function patchNodeMFConfig(mfConfig) {
214
246
  var _mfConfig_library;
247
+ if (mfConfig.remotes) mfConfig.remoteType = "script";
248
+ mfConfig.exposes = {
249
+ ...mfConfig.exposes
250
+ };
251
+ mfConfig.library = {
252
+ ...mfConfig.library,
253
+ name: mfConfig.name,
254
+ type: (null == (_mfConfig_library = mfConfig.library) ? void 0 : _mfConfig_library.type) ?? 'commonjs-module'
255
+ };
256
+ mfConfig.runtimePlugins = [
257
+ ...mfConfig.runtimePlugins || []
258
+ ];
259
+ mfConfig.runtimePlugins.push(resolve('@module-federation/node/runtimePlugin'));
260
+ if (isDev()) mfConfig.runtimePlugins.push(resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'));
261
+ }
262
+ function createSSRMFConfig(mfConfig) {
215
263
  const ssrMFConfig = {
216
- ...mfConfig,
217
- exposes: {
218
- ...mfConfig.exposes
219
- },
220
- library: {
221
- ...mfConfig.library,
222
- name: mfConfig.name,
223
- type: (null == (_mfConfig_library = mfConfig.library) ? void 0 : _mfConfig_library.type) ?? 'commonjs-module'
224
- },
225
- dts: false,
226
- dev: false,
227
- runtimePlugins: [
228
- ...mfConfig.runtimePlugins || []
229
- ]
264
+ ...mfConfig
230
265
  };
231
- ssrMFConfig.runtimePlugins.push(resolve('@module-federation/node/runtimePlugin'));
232
- if (isDev()) ssrMFConfig.runtimePlugins.push(resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'));
266
+ patchNodeMFConfig(ssrMFConfig);
267
+ ssrMFConfig.dts = false;
268
+ ssrMFConfig.dev = false;
233
269
  return ssrMFConfig;
234
270
  }
235
271
  function isRegExp(target) {
package/dist/utils.mjs CHANGED
@@ -111,18 +111,9 @@ function setSSREnv() {
111
111
  process.env['MF_SSR_PRJ'] = 'true';
112
112
  }
113
113
  const isDev = ()=>'development' === process.env['NODE_ENV'];
114
- function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry = true, modifyPublicPath = true) {
114
+ function patchNodeConfig(config, mfConfig) {
115
115
  var _config_output;
116
116
  config.output ||= {};
117
- if (modifyPublicPath) {
118
- var _config_output1;
119
- if ('string' != typeof (null == (_config_output1 = config.output) ? void 0 : _config_output1.publicPath)) throw new Error('publicPath must be string!');
120
- const publicPath = config.output.publicPath;
121
- if ('auto' === publicPath) throw new Error('publicPath can not be "auto"!');
122
- const publicPathWithSSRDir = `${publicPath}${ssrDir}/`;
123
- config.output.publicPath = publicPathWithSSRDir;
124
- }
125
- if (callerName === CALL_NAME_MAP.RSPRESS && resetEntry) config.entry = 'data:application/node;base64,';
126
117
  config.target = 'async-node';
127
118
  const UniverseEntryChunkTrackerPlugin = ssr_require('@module-federation/node/universe-entry-chunk-tracker-plugin').default;
128
119
  config.plugins ||= [];
@@ -133,19 +124,60 @@ function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry =
133
124
  const suffix = `${encodeName(uniqueName)}-[contenthash].js`;
134
125
  config.output.chunkFilename = chunkFileName.replace('.js', suffix);
135
126
  }
127
+ }
128
+ function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry = true, modifyPublicPath = true) {
129
+ config.output ||= {};
130
+ if (modifyPublicPath) {
131
+ var _config_output;
132
+ if ('string' != typeof (null == (_config_output = config.output) ? void 0 : _config_output.publicPath)) throw new Error('publicPath must be string!');
133
+ const publicPath = config.output.publicPath;
134
+ if ('auto' === publicPath) throw new Error('publicPath can not be "auto"!');
135
+ const publicPathWithSSRDir = `${publicPath}${ssrDir}/`;
136
+ config.output.publicPath = publicPathWithSSRDir;
137
+ }
138
+ if (callerName === CALL_NAME_MAP.RSPRESS && resetEntry) config.entry = 'data:application/node;base64,';
139
+ patchNodeConfig(config, mfConfig);
136
140
  return config;
137
141
  }
142
+ function patchToolsTspack(envConfig, fn) {
143
+ var _envConfig_tools;
144
+ const rspackArr = [];
145
+ if (null == (_envConfig_tools = envConfig.tools) ? void 0 : _envConfig_tools.rspack) {
146
+ var _envConfig_tools1, _envConfig_tools2;
147
+ if (Array.isArray(null == (_envConfig_tools1 = envConfig.tools) ? void 0 : _envConfig_tools1.rspack)) {
148
+ var _envConfig_tools3;
149
+ rspackArr.push(...null == (_envConfig_tools3 = envConfig.tools) ? void 0 : _envConfig_tools3.rspack);
150
+ } else if ('function' == typeof (null == (_envConfig_tools2 = envConfig.tools) ? void 0 : _envConfig_tools2.rspack)) {
151
+ var _envConfig_tools4;
152
+ rspackArr.push(null == (_envConfig_tools4 = envConfig.tools) ? void 0 : _envConfig_tools4.rspack);
153
+ }
154
+ }
155
+ envConfig.tools ||= {};
156
+ envConfig.tools.rspack = [
157
+ ...rspackArr,
158
+ fn
159
+ ];
160
+ }
138
161
  function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerName) {
139
- var _ssrEnvConfig_output, _ssrEnvConfig_output_distPath, _ssrEnvConfig_output1, _rsbuildConfig_output_distPath, _rsbuildConfig_output;
140
- const ssrEnvConfig = {
141
- ...envConfig,
142
- tools: {
143
- rspack: (config, { environment })=>{
144
- if (environment.name !== SSR_ENV_NAME) return;
145
- patchSSRRspackConfig(config, mfConfig, ssrDir, callerName);
146
- }
162
+ var _envConfig_tools, _ssrEnvConfig_output, _ssrEnvConfig_output_distPath, _ssrEnvConfig_output1, _rsbuildConfig_output_distPath, _rsbuildConfig_output;
163
+ const rspackArr = [];
164
+ if (null == (_envConfig_tools = envConfig.tools) ? void 0 : _envConfig_tools.rspack) {
165
+ var _envConfig_tools1, _envConfig_tools2;
166
+ if (Array.isArray(null == (_envConfig_tools1 = envConfig.tools) ? void 0 : _envConfig_tools1.rspack)) {
167
+ var _envConfig_tools3;
168
+ rspackArr.push(...null == (_envConfig_tools3 = envConfig.tools) ? void 0 : _envConfig_tools3.rspack);
169
+ } else if ('function' == typeof (null == (_envConfig_tools2 = envConfig.tools) ? void 0 : _envConfig_tools2.rspack)) {
170
+ var _envConfig_tools4;
171
+ rspackArr.push(null == (_envConfig_tools4 = envConfig.tools) ? void 0 : _envConfig_tools4.rspack);
147
172
  }
173
+ }
174
+ const ssrEnvConfig = {
175
+ ...envConfig
148
176
  };
177
+ patchToolsTspack(ssrEnvConfig, (config, { environment })=>{
178
+ if (environment.name !== SSR_ENV_NAME) return;
179
+ patchSSRRspackConfig(config, mfConfig, ssrDir, callerName);
180
+ });
149
181
  ssrEnvConfig.output = {
150
182
  ...ssrEnvConfig.output,
151
183
  target: 'node',
@@ -157,26 +189,30 @@ function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerN
157
189
  };
158
190
  return ssrEnvConfig;
159
191
  }
160
- function createSSRMFConfig(mfConfig) {
192
+ function patchNodeMFConfig(mfConfig) {
161
193
  var _mfConfig_library;
194
+ if (mfConfig.remotes) mfConfig.remoteType = "script";
195
+ mfConfig.exposes = {
196
+ ...mfConfig.exposes
197
+ };
198
+ mfConfig.library = {
199
+ ...mfConfig.library,
200
+ name: mfConfig.name,
201
+ type: (null == (_mfConfig_library = mfConfig.library) ? void 0 : _mfConfig_library.type) ?? 'commonjs-module'
202
+ };
203
+ mfConfig.runtimePlugins = [
204
+ ...mfConfig.runtimePlugins || []
205
+ ];
206
+ mfConfig.runtimePlugins.push(resolve('@module-federation/node/runtimePlugin'));
207
+ if (isDev()) mfConfig.runtimePlugins.push(resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'));
208
+ }
209
+ function createSSRMFConfig(mfConfig) {
162
210
  const ssrMFConfig = {
163
- ...mfConfig,
164
- exposes: {
165
- ...mfConfig.exposes
166
- },
167
- library: {
168
- ...mfConfig.library,
169
- name: mfConfig.name,
170
- type: (null == (_mfConfig_library = mfConfig.library) ? void 0 : _mfConfig_library.type) ?? 'commonjs-module'
171
- },
172
- dts: false,
173
- dev: false,
174
- runtimePlugins: [
175
- ...mfConfig.runtimePlugins || []
176
- ]
211
+ ...mfConfig
177
212
  };
178
- ssrMFConfig.runtimePlugins.push(resolve('@module-federation/node/runtimePlugin'));
179
- if (isDev()) ssrMFConfig.runtimePlugins.push(resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'));
213
+ patchNodeMFConfig(ssrMFConfig);
214
+ ssrMFConfig.dts = false;
215
+ ssrMFConfig.dev = false;
180
216
  return ssrMFConfig;
181
217
  }
182
218
  function isRegExp(target) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/rsbuild-plugin",
3
- "version": "0.22.1",
3
+ "version": "0.24.0",
4
4
  "description": "Module Federation plugin for Rsbuild",
5
5
  "homepage": "https://module-federation.io",
6
6
  "bugs": {
@@ -50,16 +50,16 @@
50
50
  ],
51
51
  "dependencies": {
52
52
  "fs-extra": "11.3.0",
53
- "@module-federation/node": "2.7.27",
54
- "@module-federation/sdk": "0.22.1",
55
- "@module-federation/enhanced": "0.22.1"
53
+ "@module-federation/sdk": "0.24.0",
54
+ "@module-federation/enhanced": "0.24.0",
55
+ "@module-federation/node": "2.7.29"
56
56
  },
57
57
  "devDependencies": {
58
58
  "@rslib/core": "^0.12.4",
59
59
  "@rsbuild/core": "^1.3.21"
60
60
  },
61
61
  "peerDependencies": {
62
- "@rsbuild/core": "^1.3.21"
62
+ "@rsbuild/core": "^1.3.21 || ^2.0.0-0"
63
63
  },
64
64
  "peerDependenciesMeta": {
65
65
  "@rsbuild/core": {