@module-federation/rsbuild-plugin 0.0.0-perf-devtools-20260108034446 → 0.0.0-perf-website-code-usage-20260914033703

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,7 +11,8 @@ npm install @module-federation/enhanced
11
11
  ```
12
12
 
13
13
  ### Rsbuild App
14
- ``` js
14
+
15
+ ```js
15
16
  import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
16
17
  import { defineConfig } from '@rsbuild/core';
17
18
  import { pluginReact } from '@rsbuild/plugin-react';
@@ -33,8 +34,56 @@ export default defineConfig({
33
34
  });
34
35
  ```
35
36
 
37
+ ### Rsbuild App SSR (Node target with custom environment)
38
+
39
+ Use `target: 'node'` with an explicit `environment` to apply federation to a
40
+ specific Rsbuild app environment (for example `ssr`).
41
+
42
+ ```ts
43
+ import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
44
+ import { defineConfig } from '@rsbuild/core';
45
+
46
+ export default defineConfig({
47
+ environments: {
48
+ client: {},
49
+ ssr: {},
50
+ },
51
+ plugins: [
52
+ pluginModuleFederation(
53
+ {
54
+ name: 'host',
55
+ remotes: {
56
+ remote: 'remote@http://localhost:3001/mf-manifest.json',
57
+ },
58
+ },
59
+ {
60
+ target: 'node',
61
+ environment: 'ssr',
62
+ },
63
+ ),
64
+ ],
65
+ });
66
+ ```
67
+
68
+ `target: 'dual'` support remains scoped to Rslib/Rspress workflows.
69
+
70
+ ### Default environment detection
71
+
72
+ If `environment` is omitted, the plugin will choose a default per tool:
73
+
74
+ - **Rslib**: `mf`
75
+ - **Rsbuild app**:
76
+ - `target: 'web'` → `web`
77
+ - `target: 'node'` → `node`
78
+ - **Rspress**:
79
+ - `target: 'web'` → `web`
80
+ - `target: 'node'` → `node`
81
+
82
+ You can still override with `environment` when your project uses custom names.
83
+
36
84
  ### Rslib Module
37
- ``` js
85
+
86
+ ```js
38
87
  import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
39
88
  import { defineConfig } from '@rslib/core';
40
89
 
@@ -70,3 +119,7 @@ export default defineConfig({
70
119
  ],
71
120
  });
72
121
  ```
122
+
123
+ ## Documentation
124
+
125
+ See [https://module-federation.io/guide/build-plugins/plugins-rsbuild.html](https://module-federation.io/guide/build-plugins/plugins-rsbuild.html) fordetails.
@@ -1,13 +1,15 @@
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;
10
11
  };
12
+ type ModuleFederationOptionsTransformer = (options: ModuleFederationOptions) => ModuleFederationOptions;
11
13
  type ExposedAPIType = {
12
14
  options: {
13
15
  nodePlugin?: ModuleFederationPlugin;
@@ -18,14 +20,18 @@ type ExposedAPIType = {
18
20
  nodeEnvironmentName?: string;
19
21
  };
20
22
  assetResources: Record<string, StatsAssetResource>;
23
+ getOptions: () => ModuleFederationOptions;
24
+ registerNodeTargetConfigOwner?: () => void;
25
+ registerOptionsTransformer: (transformer: ModuleFederationOptionsTransformer) => void;
21
26
  isSSRConfig: typeof isSSRConfig;
22
27
  isRspressSSGConfig: typeof isRspressSSGConfig;
23
28
  };
24
- export type { ModuleFederationOptions, ExposedAPIType };
29
+ export type { ModuleFederationOptions, ModuleFederationOptionsTransformer, ExposedAPIType, };
25
30
  declare const RSBUILD_PLUGIN_MODULE_FEDERATION_NAME = "rsbuild:module-federation-enhanced";
26
31
  export { RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, PLUGIN_NAME, SSR_DIR };
27
32
  export declare function isMFFormat(bundlerConfig: Rspack.Configuration): boolean;
28
33
  declare const isSSRConfig: (bundlerConfigName?: string) => boolean;
29
- declare const isRspressSSGConfig: (bundlerConfigName?: string) => bundlerConfigName is "node";
30
- export declare const pluginModuleFederation: (moduleFederationOptions: ModuleFederationOptions, rsbuildOptions?: RSBUILD_PLUGIN_OPTIONS) => RsbuildPlugin;
34
+ declare const isRspressSSGConfig: (bundlerConfigName?: string) => boolean;
35
+ export declare const pluginModuleFederation: (initialModuleFederationOptions: ModuleFederationOptions, rsbuildOptions?: RSBUILD_PLUGIN_OPTIONS) => RsbuildPlugin;
31
36
  export { createModuleFederationConfig } from '@module-federation/sdk';
37
+ export { TreeShakingSharedPlugin };
@@ -0,0 +1,482 @@
1
+ "use strict";
2
+ const __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
3
+ return typeof document === 'undefined'
4
+ ? new (require('url'.replace('', '')).URL)('file:' + __filename).href
5
+ : (document.currentScript && document.currentScript.src) ||
6
+ new URL('main.js', document.baseURI).href;
7
+ })();
8
+ ;
9
+ // The require scope
10
+ var __webpack_require__ = {};
11
+
12
+ // webpack/runtime/compat_get_default_export
13
+ (() => {
14
+ // getDefaultExport function for compatibility with non-ESM modules
15
+ __webpack_require__.n = (module) => {
16
+ var getter = module && module.__esModule ?
17
+ () => (module['default']) :
18
+ () => (module);
19
+ __webpack_require__.d(getter, { a: getter });
20
+ return getter;
21
+ };
22
+
23
+ })();
24
+ // webpack/runtime/define_property_getters
25
+ (() => {
26
+ __webpack_require__.d = (exports, getters, values) => {
27
+ var define = (defs, kind) => {
28
+ for(var key in defs) {
29
+ if(__webpack_require__.o(defs, key) && !__webpack_require__.o(exports, key)) {
30
+ Object.defineProperty(exports, key, { enumerable: true, [kind]: defs[key] });
31
+ }
32
+ }
33
+ };
34
+ define(getters, "get");
35
+ define(values, "value");
36
+ };
37
+ })();
38
+ // webpack/runtime/has_own_property
39
+ (() => {
40
+ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
41
+ })();
42
+ // webpack/runtime/make_namespace_object
43
+ (() => {
44
+ // define __esModule on exports
45
+ __webpack_require__.r = (exports) => {
46
+ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
47
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
48
+ }
49
+ Object.defineProperty(exports, '__esModule', { value: true });
50
+ };
51
+ })();
52
+ var __webpack_exports__ = {};
53
+ // ESM COMPAT FLAG
54
+ __webpack_require__.r(__webpack_exports__);
55
+
56
+ // EXPORTS
57
+ __webpack_require__.d(__webpack_exports__, {
58
+ PLUGIN_NAME: () => (/* reexport */ rspack_namespaceObject.PLUGIN_NAME),
59
+ RSBUILD_PLUGIN_MODULE_FEDERATION_NAME: () => (/* binding */ RSBUILD_PLUGIN_MODULE_FEDERATION_NAME),
60
+ SSR_DIR: () => (/* reexport */ external_utils_js_namespaceObject.SSR_DIR),
61
+ TreeShakingSharedPlugin: () => (/* reexport */ rspack_namespaceObject.TreeShakingSharedPlugin),
62
+ createModuleFederationConfig: () => (/* reexport */ sdk_namespaceObject.createModuleFederationConfig),
63
+ isMFFormat: () => (/* binding */ isMFFormat),
64
+ pluginModuleFederation: () => (/* binding */ pluginModuleFederation)
65
+ });
66
+
67
+ ;// CONCATENATED MODULE: external "@module-federation/enhanced"
68
+ const enhanced_namespaceObject = require("@module-federation/enhanced");
69
+ ;// CONCATENATED MODULE: external "@module-federation/enhanced/rspack"
70
+ const rspack_namespaceObject = require("@module-federation/enhanced/rspack");
71
+ ;// CONCATENATED MODULE: external "@module-federation/sdk"
72
+ const sdk_namespaceObject = require("@module-federation/sdk");
73
+ ;// CONCATENATED MODULE: external "../logger.js"
74
+ const external_logger_js_namespaceObject = require("../logger.js");
75
+ var external_logger_js_default = /*#__PURE__*/__webpack_require__.n(external_logger_js_namespaceObject);
76
+ ;// CONCATENATED MODULE: external "../utils.js"
77
+ const external_utils_js_namespaceObject = require("../utils.js");
78
+ ;// CONCATENATED MODULE: external "../constant.js"
79
+ const external_constant_js_namespaceObject = require("../constant.js");
80
+ ;// CONCATENATED MODULE: external "../utils/ssr.js"
81
+ const ssr_js_namespaceObject = require("../utils/ssr.js");
82
+ ;// CONCATENATED MODULE: ./src/cli/index.ts
83
+
84
+
85
+
86
+
87
+
88
+
89
+
90
+ const RSBUILD_PLUGIN_MODULE_FEDERATION_NAME = 'rsbuild:module-federation-enhanced';
91
+ const RSBUILD_PLUGIN_NAME = '@module-federation/rsbuild-plugin';
92
+
93
+ const LIB_FORMAT = [
94
+ 'umd',
95
+ 'modern-module'
96
+ ];
97
+ const DEFAULT_MF_ENVIRONMENT_NAME = 'mf';
98
+ const DEFAULT_WEB_ENVIRONMENT_NAME = 'web';
99
+ const DEFAULT_NODE_ENVIRONMENT_NAME = 'node';
100
+ const resolveDefaultEnvironmentName = ({ callerName, target })=>{
101
+ if (callerName === external_constant_js_namespaceObject.CALL_NAME_MAP.RSLIB) {
102
+ return DEFAULT_MF_ENVIRONMENT_NAME;
103
+ }
104
+ if (callerName === external_constant_js_namespaceObject.CALL_NAME_MAP.RSPRESS) {
105
+ if (target === 'node') {
106
+ return DEFAULT_NODE_ENVIRONMENT_NAME;
107
+ }
108
+ return DEFAULT_WEB_ENVIRONMENT_NAME;
109
+ }
110
+ if (target === 'node') {
111
+ return DEFAULT_NODE_ENVIRONMENT_NAME;
112
+ }
113
+ return DEFAULT_WEB_ENVIRONMENT_NAME;
114
+ };
115
+ function isStoryBook(rsbuildConfig) {
116
+ if (rsbuildConfig.plugins?.find((p)=>p && 'name' in p && p.name === 'module-federation-storybook-plugin')) {
117
+ return true;
118
+ }
119
+ return false;
120
+ }
121
+ function isMFFormat(bundlerConfig) {
122
+ const library = bundlerConfig.output?.library;
123
+ if (bundlerConfig.name === external_utils_js_namespaceObject.SSR_ENV_NAME) {
124
+ return true;
125
+ }
126
+ return !(typeof library === 'object' && !Array.isArray(library) && 'type' in library && // if the type is umd/modern-module or commonjs*, means this is a normal library , not mf
127
+ (LIB_FORMAT.includes(library.type) || /commonjs/.test(library.type)));
128
+ }
129
+ const isSSRConfig = (bundlerConfigName)=>Boolean(bundlerConfigName === external_utils_js_namespaceObject.SSR_ENV_NAME);
130
+ const isRspressSSGConfig = (bundlerConfigName)=>{
131
+ return [
132
+ external_constant_js_namespaceObject.RSPRESS_BUNDLER_CONFIG_NAME,
133
+ external_constant_js_namespaceObject.RSPRESS_SSG_MD_ENV_NAME
134
+ ].includes(bundlerConfigName || '');
135
+ };
136
+ const pluginModuleFederation = (initialModuleFederationOptions, rsbuildOptions)=>({
137
+ name: RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
138
+ setup: (api)=>{
139
+ let moduleFederationOptions = initialModuleFederationOptions;
140
+ let hasNodeTargetConfigOwner = false;
141
+ if (!moduleFederationOptions?.name) {
142
+ throw new Error('The module federation option "name" is required in @module-federation/rsbuild-plugin.');
143
+ }
144
+ const { target = 'web', ssr = undefined, ssrDir = external_utils_js_namespaceObject.SSR_DIR, environment: configuredEnvironment } = rsbuildOptions || {};
145
+ if (ssr) {
146
+ throw new Error("The `ssr` option is deprecated. If you want to enable SSR, please use `target: 'dual'` instead.");
147
+ }
148
+ const { callerName } = api.context;
149
+ const originalRsbuildConfig = api.getRsbuildConfig();
150
+ if (!callerName) {
151
+ throw new Error('`callerName` is undefined. Please ensure the @rsbuild/core version is higher than 1.3.21 .');
152
+ }
153
+ const isRslib = callerName === external_constant_js_namespaceObject.CALL_NAME_MAP.RSLIB;
154
+ const isRspress = callerName === external_constant_js_namespaceObject.CALL_NAME_MAP.RSPRESS;
155
+ const isRstest = callerName === 'rstest';
156
+ const isRstestCompanionActive = ()=>isRstest && hasNodeTargetConfigOwner;
157
+ const isSSR = target === 'dual';
158
+ const environment = configuredEnvironment ?? resolveDefaultEnvironmentName({
159
+ callerName,
160
+ target
161
+ });
162
+ if (isSSR && !isStoryBook(originalRsbuildConfig)) {
163
+ if (!isRslib && !isRspress) {
164
+ throw new Error(`'target' option is only supported in Rslib.`);
165
+ }
166
+ const rsbuildConfig = api.getRsbuildConfig();
167
+ if (!isRspress && (!rsbuildConfig.environments?.[environment] || Object.keys(rsbuildConfig.environments).some((key)=>key.startsWith(environment) && key !== environment))) {
168
+ throw new Error(`Please set ${RSBUILD_PLUGIN_NAME} as global plugin in rslib.config.ts if you set 'target: "dual"'.`);
169
+ }
170
+ (0,external_utils_js_namespaceObject.setSSREnv)();
171
+ }
172
+ api.modifyRsbuildConfig((config)=>{
173
+ // skip storybook
174
+ if (isStoryBook(config)) {
175
+ return;
176
+ }
177
+ // Change some default configs for remote modules
178
+ if (moduleFederationOptions.exposes) {
179
+ config.dev ||= {};
180
+ config.server ||= {};
181
+ const userConfig = api.getRsbuildConfig('original');
182
+ // Allow remote modules to be loaded by setting CORS headers
183
+ // This is required for MF to work properly across different origins
184
+ config.server.headers ||= {};
185
+ if (!config.server.headers['Access-Control-Allow-Origin'] && !(typeof userConfig.server?.cors === 'object' && userConfig.server.cors.origin)) {
186
+ const corsWarnMsgs = [
187
+ 'Detect that CORS options are not set, mf Rsbuild plugin will add default cors header: server.headers["Access-Control-Allow-Headers"] = "*". It is recommended to specify an allowlist of trusted origins in "server.cors" instead.',
188
+ 'View https://module-federation.io/guide/troubleshooting/other.html#cors-warn for more details.'
189
+ ];
190
+ !isRslib && !isRspress && external_logger_js_default().warn(corsWarnMsgs.join('\n'));
191
+ config.server.headers['Access-Control-Allow-Origin'] = '*';
192
+ }
193
+ // For remote modules, Rsbuild should send the ws request to the provider's dev server.
194
+ // This allows the provider to do HMR when the provider module is loaded in the consumer's page.
195
+ if (config.server?.port && !config.dev.client?.port) {
196
+ config.dev.client ||= {};
197
+ config.dev.client.port = config.server.port;
198
+ }
199
+ // Change the default assetPrefix to `true` for remote modules.
200
+ // This ensures that the remote module's assets can be requested by consumer apps with the correct URL.
201
+ const originalConfig = api.getRsbuildConfig('original');
202
+ if (originalConfig.dev?.assetPrefix === undefined && config.dev.assetPrefix === config.server?.base) {
203
+ config.dev.assetPrefix = true;
204
+ }
205
+ }
206
+ if (isSSR) {
207
+ if (config.environments?.[external_utils_js_namespaceObject.SSR_ENV_NAME]) {
208
+ throw new Error(`'${external_utils_js_namespaceObject.SSR_ENV_NAME}' environment is already defined.Please use another name.`);
209
+ }
210
+ const currentEnvironment = config.environments?.[environment];
211
+ if (!currentEnvironment) {
212
+ throw new Error(`Can not find environment '${environment}' when enabling SSR.`);
213
+ }
214
+ config.environments[external_utils_js_namespaceObject.SSR_ENV_NAME] = (0,external_utils_js_namespaceObject.createSSRREnvConfig)(currentEnvironment, moduleFederationOptions, ssrDir, config, callerName);
215
+ const ssgMDEnv = config.environments[external_constant_js_namespaceObject.RSPRESS_SSG_MD_ENV_NAME];
216
+ if (isRspress && ssgMDEnv) {
217
+ (0,ssr_js_namespaceObject.patchToolsTspack)(ssgMDEnv, (config, { environment })=>{
218
+ config.target = 'async-node';
219
+ });
220
+ }
221
+ } else if (target === 'node') {
222
+ const nodeTargetEnv = config.environments?.[environment];
223
+ if (!nodeTargetEnv) {
224
+ const availableEnvironments = Object.keys(config.environments || {});
225
+ const availableEnvironmentsLabel = availableEnvironments.length > 0 ? availableEnvironments.join(', ') : '(none)';
226
+ throw new Error(`Can not find environment '${environment}' when using target: 'node'. Available environments: ${availableEnvironmentsLabel}.`);
227
+ }
228
+ if (!isRstestCompanionActive()) {
229
+ (0,ssr_js_namespaceObject.patchToolsTspack)(nodeTargetEnv, (config)=>{
230
+ config.target = 'async-node';
231
+ });
232
+ }
233
+ }
234
+ });
235
+ api.modifyEnvironmentConfig((config)=>{
236
+ // Module Federation runtime uses ES6+ syntax,
237
+ // adding to include and let SWC transform it
238
+ config.source.include = [
239
+ ...config.source.include || [],
240
+ /@module-federation[\\/]/
241
+ ];
242
+ return config;
243
+ });
244
+ const exposedFederationApi = {
245
+ options: {
246
+ nodePlugin: undefined,
247
+ browserPlugin: undefined,
248
+ rspressSSGPlugin: undefined,
249
+ distOutputDir: undefined,
250
+ browserEnvironmentName: undefined,
251
+ nodeEnvironmentName: undefined
252
+ },
253
+ assetResources: {},
254
+ getOptions: ()=>moduleFederationOptions,
255
+ registerNodeTargetConfigOwner: ()=>{
256
+ hasNodeTargetConfigOwner = true;
257
+ },
258
+ registerOptionsTransformer: (transformer)=>{
259
+ moduleFederationOptions = transformer(moduleFederationOptions);
260
+ },
261
+ isSSRConfig,
262
+ isRspressSSGConfig
263
+ };
264
+ api.expose(RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, exposedFederationApi);
265
+ const defaultBrowserEnvironmentName = environment;
266
+ api.processAssets({
267
+ stage: 'report'
268
+ }, ({ assets, environment: envContext })=>{
269
+ if (moduleFederationOptions.manifest === false) {
270
+ return;
271
+ }
272
+ const assetFileNames = (0,sdk_namespaceObject.getManifestFileName)(moduleFederationOptions.manifest);
273
+ const expectedBrowserEnv = exposedFederationApi.options.browserEnvironmentName ?? defaultBrowserEnvironmentName;
274
+ const expectedNodeEnv = exposedFederationApi.options.nodeEnvironmentName ?? external_utils_js_namespaceObject.SSR_ENV_NAME;
275
+ const envName = envContext.name;
276
+ if (envName !== expectedBrowserEnv && envName !== expectedNodeEnv) {
277
+ return;
278
+ }
279
+ const assetResources = exposedFederationApi.assetResources;
280
+ const targetResources = assetResources[envName] || (assetResources[envName] = {});
281
+ const statsAsset = assets[assetFileNames.statsFileName];
282
+ if (statsAsset) {
283
+ try {
284
+ const raw = statsAsset.source();
285
+ const content = typeof raw === 'string' ? raw : raw.toString();
286
+ targetResources.stats = {
287
+ data: JSON.parse(content),
288
+ filename: assetFileNames.statsFileName
289
+ };
290
+ } catch (err) {
291
+ const message = err instanceof Error ? err.message : String(err);
292
+ external_logger_js_default().error(`Failed to parse stats asset "${assetFileNames.statsFileName}" for environment "${envName}": ${message} `);
293
+ }
294
+ }
295
+ const manifestAsset = assets[assetFileNames.manifestFileName];
296
+ if (manifestAsset) {
297
+ try {
298
+ const raw = manifestAsset.source();
299
+ const content = typeof raw === 'string' ? raw : raw.toString();
300
+ targetResources.manifest = {
301
+ data: JSON.parse(content),
302
+ filename: assetFileNames.manifestFileName
303
+ };
304
+ } catch (err) {
305
+ const message = err instanceof Error ? err.message : String(err);
306
+ external_logger_js_default().error(`Failed to parse manifest asset "${assetFileNames.manifestFileName}" for environment "${envName}": ${message} `);
307
+ }
308
+ }
309
+ });
310
+ api.onBeforeCreateCompiler(({ bundlerConfigs })=>{
311
+ if (!bundlerConfigs) {
312
+ throw new Error('Can not get bundlerConfigs!');
313
+ }
314
+ const sharedOptions = (0,enhanced_namespaceObject.parseOptions)(moduleFederationOptions.shared || [], (item, key)=>{
315
+ if (typeof item !== 'string') throw new Error('Unexpected array in shared');
316
+ const config = item === key || !(0,sdk_namespaceObject.isRequiredVersion)(item) ? {
317
+ import: item
318
+ } : {
319
+ import: key,
320
+ requiredVersion: item
321
+ };
322
+ return config;
323
+ }, (item)=>item);
324
+ const shared = sharedOptions.map(([name])=>name.endsWith('/') ? name.slice(0, -1) : name);
325
+ bundlerConfigs.forEach((bundlerConfig)=>{
326
+ const bundlerConfigName = bundlerConfig.name || '';
327
+ const isConfiguredEnvironmentConfig = bundlerConfigName === environment;
328
+ const isNodeTargetEnvironmentConfig = target === 'node' && bundlerConfigName === environment;
329
+ const isRspressSSGEnvironmentConfig = isRspressSSGConfig(bundlerConfig.name);
330
+ const isActiveRspressSSGEnvironmentConfig = isRspress && isRspressSSGEnvironmentConfig;
331
+ const shouldUseSSRPluginConfig = isSSRConfig(bundlerConfig.name) || isNodeTargetEnvironmentConfig && !isRstestCompanionActive();
332
+ if (target === 'node' && !isNodeTargetEnvironmentConfig && !isActiveRspressSSGEnvironmentConfig) {
333
+ return;
334
+ }
335
+ // For non-node targets, scope each plugin instance to its configured
336
+ // environment plus explicit SSR/SSG environments. This prevents a
337
+ // browser-targeted instance from mutating SSR configs.
338
+ if (target !== 'node' && !isConfiguredEnvironmentConfig && !shouldUseSSRPluginConfig && !isActiveRspressSSGEnvironmentConfig) {
339
+ return;
340
+ }
341
+ if (!isMFFormat(bundlerConfig) && !isRspress && !isNodeTargetEnvironmentConfig) {
342
+ return;
343
+ } else if (isStoryBook(originalRsbuildConfig)) {
344
+ bundlerConfig.output.uniqueName = `${moduleFederationOptions.name} -storybook - host`;
345
+ } else {
346
+ // mf
347
+ (0,external_utils_js_namespaceObject.autoDeleteSplitChunkCacheGroups)(moduleFederationOptions, bundlerConfig?.optimization?.splitChunks);
348
+ (0,external_utils_js_namespaceObject.addDataFetchExposes)(moduleFederationOptions.exposes, shouldUseSSRPluginConfig);
349
+ const ssrModuleFederationOptions = shouldUseSSRPluginConfig ? (0,external_utils_js_namespaceObject.createSSRMFConfig)(moduleFederationOptions) : undefined;
350
+ if (!isRstestCompanionActive()) {
351
+ if (bundlerConfig.optimization?.runtimeChunk) {
352
+ external_logger_js_default().warn('`optimization.runtimeChunk` is disabled because Module Federation requires its runtime to remain with each entry. The Rstest federation companion is the supported exception because its worker loads the shared runtime explicitly.');
353
+ }
354
+ delete bundlerConfig.optimization?.runtimeChunk;
355
+ }
356
+ const externals = bundlerConfig.externals;
357
+ if (Array.isArray(externals)) {
358
+ const sharedModules = new Set();
359
+ bundlerConfig.externals = externals.filter((ext)=>{
360
+ let sharedModule;
361
+ if ((0,external_utils_js_namespaceObject.isRegExp)(ext)) {
362
+ const match = shared.some((dep)=>{
363
+ if (ext.test(dep) || ext.test("@module-federation/rsbuild-plugin")) {
364
+ sharedModule = dep;
365
+ return true;
366
+ }
367
+ return false;
368
+ });
369
+ match && sharedModule && sharedModules.add(sharedModule);
370
+ return !match;
371
+ }
372
+ if (typeof ext === 'string') {
373
+ if (ext === "@module-federation/rsbuild-plugin") {
374
+ return false;
375
+ }
376
+ const match = shared.some((dep)=>{
377
+ if (dep === ext) {
378
+ sharedModule = dep;
379
+ }
380
+ return dep === ext;
381
+ });
382
+ if (match) {
383
+ sharedModule && sharedModules.add(sharedModule);
384
+ return false;
385
+ }
386
+ return true;
387
+ }
388
+ return true;
389
+ });
390
+ if (sharedModules.size > 0) {
391
+ for (const sharedModule of sharedModules){
392
+ external_logger_js_default().log(`${sharedModule} is removed from externals because it is a shared module.`);
393
+ }
394
+ }
395
+ }
396
+ if (!bundlerConfig.output?.chunkLoadingGlobal && !shouldUseSSRPluginConfig && !isActiveRspressSSGEnvironmentConfig && target !== 'node') {
397
+ bundlerConfig.output.chunkLoadingGlobal = `chunk_${moduleFederationOptions.name}`;
398
+ }
399
+ if (isNodeTargetEnvironmentConfig && !isRstestCompanionActive()) {
400
+ (0,ssr_js_namespaceObject.patchNodeConfig)(bundlerConfig, ssrModuleFederationOptions ?? moduleFederationOptions);
401
+ }
402
+ // `uniqueName` is required for react refresh to work
403
+ if (!bundlerConfig.output?.uniqueName) {
404
+ bundlerConfig.output.uniqueName = moduleFederationOptions.name;
405
+ }
406
+ // Set default publicPath to 'auto' if not explicitly configured
407
+ // This allows remote chunks to load from the same origin as the remote application's manifest
408
+ if (bundlerConfig.output?.publicPath === undefined && !shouldUseSSRPluginConfig && !isActiveRspressSSGEnvironmentConfig) {
409
+ bundlerConfig.output.publicPath = 'auto';
410
+ }
411
+ if (!bundlerConfig.plugins.find((p)=>p && p.name === rspack_namespaceObject.PLUGIN_NAME)) {
412
+ if (shouldUseSSRPluginConfig || isNodeTargetEnvironmentConfig) {
413
+ const ssrMFConfig = ssrModuleFederationOptions ?? (isRstestCompanionActive() ? moduleFederationOptions : (0,external_utils_js_namespaceObject.createSSRMFConfig)(moduleFederationOptions));
414
+ exposedFederationApi.options.nodePlugin = new rspack_namespaceObject.ModuleFederationPlugin(ssrMFConfig);
415
+ exposedFederationApi.options.nodeEnvironmentName = bundlerConfig.name || external_utils_js_namespaceObject.SSR_ENV_NAME;
416
+ bundlerConfig.plugins.push(exposedFederationApi.options.nodePlugin);
417
+ return;
418
+ } else if (isActiveRspressSSGEnvironmentConfig) {
419
+ const mfConfig = {
420
+ ...(0,external_utils_js_namespaceObject.createSSRMFConfig)(moduleFederationOptions),
421
+ // expose in mf-ssg env
422
+ exposes: {},
423
+ manifest: false,
424
+ library: undefined
425
+ };
426
+ (0,external_utils_js_namespaceObject.patchSSRRspackConfig)(bundlerConfig, mfConfig, external_constant_js_namespaceObject.RSPRESS_SSR_DIR, callerName, false, false);
427
+ bundlerConfig.output ||= {};
428
+ bundlerConfig.output.publicPath = '/';
429
+ // MF depend on asyncChunks
430
+ bundlerConfig.output.asyncChunks = undefined;
431
+ const p = new rspack_namespaceObject.ModuleFederationPlugin(mfConfig);
432
+ if (bundlerConfig.name === external_constant_js_namespaceObject.RSPRESS_BUNDLER_CONFIG_NAME) {
433
+ exposedFederationApi.options.rspressSSGPlugin = p;
434
+ }
435
+ bundlerConfig.plugins.push(p);
436
+ return;
437
+ }
438
+ exposedFederationApi.options.browserPlugin = new rspack_namespaceObject.ModuleFederationPlugin(moduleFederationOptions);
439
+ exposedFederationApi.options.distOutputDir = bundlerConfig.output?.path || '';
440
+ exposedFederationApi.options.browserEnvironmentName = bundlerConfig.name || defaultBrowserEnvironmentName;
441
+ bundlerConfig.plugins.push(exposedFederationApi.options.browserPlugin);
442
+ }
443
+ }
444
+ });
445
+ });
446
+ const generateMergedStatsAndManifest = ()=>{
447
+ const { distOutputDir, browserEnvironmentName, nodeEnvironmentName } = exposedFederationApi.options;
448
+ if (!distOutputDir || !browserEnvironmentName || !nodeEnvironmentName) {
449
+ return;
450
+ }
451
+ const assetResources = exposedFederationApi.assetResources;
452
+ const browserAssets = assetResources[browserEnvironmentName];
453
+ const nodeAssets = assetResources[nodeEnvironmentName];
454
+ if (!browserAssets || !nodeAssets) {
455
+ return;
456
+ }
457
+ try {
458
+ (0,external_utils_js_namespaceObject.updateStatsAndManifest)(nodeAssets, browserAssets, distOutputDir);
459
+ } catch (err) {
460
+ external_logger_js_default().error(err);
461
+ }
462
+ };
463
+ api.onDevCompileDone(generateMergedStatsAndManifest);
464
+ api.onAfterBuild(generateMergedStatsAndManifest);
465
+ }
466
+ });
467
+
468
+
469
+
470
+ exports.PLUGIN_NAME = __webpack_exports__.PLUGIN_NAME;
471
+ exports.RSBUILD_PLUGIN_MODULE_FEDERATION_NAME = __webpack_exports__.RSBUILD_PLUGIN_MODULE_FEDERATION_NAME;
472
+ exports.SSR_DIR = __webpack_exports__.SSR_DIR;
473
+ exports.TreeShakingSharedPlugin = __webpack_exports__.TreeShakingSharedPlugin;
474
+ exports.createModuleFederationConfig = __webpack_exports__.createModuleFederationConfig;
475
+ exports.isMFFormat = __webpack_exports__.isMFFormat;
476
+ exports.pluginModuleFederation = __webpack_exports__.pluginModuleFederation;
477
+ for(var __rspack_i in __webpack_exports__) {
478
+ if(["PLUGIN_NAME","RSBUILD_PLUGIN_MODULE_FEDERATION_NAME","SSR_DIR","TreeShakingSharedPlugin","createModuleFederationConfig","isMFFormat","pluginModuleFederation"].indexOf(__rspack_i) === -1) {
479
+ exports[__rspack_i] = __webpack_exports__[__rspack_i];
480
+ }
481
+ }
482
+ Object.defineProperty(exports, '__esModule', { value: true });