@module-federation/rsbuild-plugin 2.8.1 → 2.9.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.
- package/dist/cli/index.d.ts +6 -2
- package/dist/cli/index.js +94 -80
- package/dist/cli/index.mjs +94 -80
- package/dist/utils/ssr.js +9 -2
- package/dist/utils/ssr.mjs +9 -2
- package/package.json +4 -4
package/dist/cli/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ type RSBUILD_PLUGIN_OPTIONS = {
|
|
|
9
9
|
ssrDir?: string;
|
|
10
10
|
environment?: string;
|
|
11
11
|
};
|
|
12
|
+
type ModuleFederationOptionsTransformer = (options: ModuleFederationOptions) => ModuleFederationOptions;
|
|
12
13
|
type ExposedAPIType = {
|
|
13
14
|
options: {
|
|
14
15
|
nodePlugin?: ModuleFederationPlugin;
|
|
@@ -19,15 +20,18 @@ type ExposedAPIType = {
|
|
|
19
20
|
nodeEnvironmentName?: string;
|
|
20
21
|
};
|
|
21
22
|
assetResources: Record<string, StatsAssetResource>;
|
|
23
|
+
getOptions: () => ModuleFederationOptions;
|
|
24
|
+
registerNodeTargetConfigOwner?: () => void;
|
|
25
|
+
registerOptionsTransformer: (transformer: ModuleFederationOptionsTransformer) => void;
|
|
22
26
|
isSSRConfig: typeof isSSRConfig;
|
|
23
27
|
isRspressSSGConfig: typeof isRspressSSGConfig;
|
|
24
28
|
};
|
|
25
|
-
export type { ModuleFederationOptions, ExposedAPIType };
|
|
29
|
+
export type { ModuleFederationOptions, ModuleFederationOptionsTransformer, ExposedAPIType, };
|
|
26
30
|
declare const RSBUILD_PLUGIN_MODULE_FEDERATION_NAME = "rsbuild:module-federation-enhanced";
|
|
27
31
|
export { RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, PLUGIN_NAME, SSR_DIR };
|
|
28
32
|
export declare function isMFFormat(bundlerConfig: Rspack.Configuration): boolean;
|
|
29
33
|
declare const isSSRConfig: (bundlerConfigName?: string) => boolean;
|
|
30
34
|
declare const isRspressSSGConfig: (bundlerConfigName?: string) => boolean;
|
|
31
|
-
export declare const pluginModuleFederation: (
|
|
35
|
+
export declare const pluginModuleFederation: (initialModuleFederationOptions: ModuleFederationOptions, rsbuildOptions?: RSBUILD_PLUGIN_OPTIONS) => RsbuildPlugin;
|
|
32
36
|
export { createModuleFederationConfig } from '@module-federation/sdk';
|
|
33
37
|
export { TreeShakingSharedPlugin };
|
package/dist/cli/index.js
CHANGED
|
@@ -133,9 +133,11 @@ const isRspressSSGConfig = (bundlerConfigName)=>{
|
|
|
133
133
|
external_constant_js_namespaceObject.RSPRESS_SSG_MD_ENV_NAME
|
|
134
134
|
].includes(bundlerConfigName || '');
|
|
135
135
|
};
|
|
136
|
-
const pluginModuleFederation = (
|
|
136
|
+
const pluginModuleFederation = (initialModuleFederationOptions, rsbuildOptions)=>({
|
|
137
137
|
name: RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
|
|
138
138
|
setup: (api)=>{
|
|
139
|
+
let moduleFederationOptions = initialModuleFederationOptions;
|
|
140
|
+
let hasNodeTargetConfigOwner = false;
|
|
139
141
|
if (!moduleFederationOptions?.name) {
|
|
140
142
|
throw new Error('The module federation option "name" is required in @module-federation/rsbuild-plugin.');
|
|
141
143
|
}
|
|
@@ -150,6 +152,8 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
150
152
|
}
|
|
151
153
|
const isRslib = callerName === external_constant_js_namespaceObject.CALL_NAME_MAP.RSLIB;
|
|
152
154
|
const isRspress = callerName === external_constant_js_namespaceObject.CALL_NAME_MAP.RSPRESS;
|
|
155
|
+
const isRstest = callerName === 'rstest';
|
|
156
|
+
const isRstestCompanionActive = ()=>isRstest && hasNodeTargetConfigOwner;
|
|
153
157
|
const isSSR = target === 'dual';
|
|
154
158
|
const environment = configuredEnvironment ?? resolveDefaultEnvironmentName({
|
|
155
159
|
callerName,
|
|
@@ -165,18 +169,6 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
165
169
|
}
|
|
166
170
|
(0,external_utils_js_namespaceObject.setSSREnv)();
|
|
167
171
|
}
|
|
168
|
-
const sharedOptions = (0,enhanced_namespaceObject.parseOptions)(moduleFederationOptions.shared || [], (item, key)=>{
|
|
169
|
-
if (typeof item !== 'string') throw new Error('Unexpected array in shared');
|
|
170
|
-
const config = item === key || !(0,sdk_namespaceObject.isRequiredVersion)(item) ? {
|
|
171
|
-
import: item
|
|
172
|
-
} : {
|
|
173
|
-
import: key,
|
|
174
|
-
requiredVersion: item
|
|
175
|
-
};
|
|
176
|
-
return config;
|
|
177
|
-
}, (item)=>item);
|
|
178
|
-
// shared[0] is the shared name
|
|
179
|
-
const shared = sharedOptions.map((shared)=>shared[0].endsWith('/') ? shared[0].slice(0, -1) : shared[0]);
|
|
180
172
|
api.modifyRsbuildConfig((config)=>{
|
|
181
173
|
// skip storybook
|
|
182
174
|
if (isStoryBook(config)) {
|
|
@@ -233,9 +225,11 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
233
225
|
const availableEnvironmentsLabel = availableEnvironments.length > 0 ? availableEnvironments.join(', ') : '(none)';
|
|
234
226
|
throw new Error(`Can not find environment '${environment}' when using target: 'node'. Available environments: ${availableEnvironmentsLabel}.`);
|
|
235
227
|
}
|
|
236
|
-
(
|
|
237
|
-
|
|
238
|
-
|
|
228
|
+
if (!isRstestCompanionActive()) {
|
|
229
|
+
(0,ssr_js_namespaceObject.patchToolsTspack)(nodeTargetEnv, (config)=>{
|
|
230
|
+
config.target = 'async-node';
|
|
231
|
+
});
|
|
232
|
+
}
|
|
239
233
|
}
|
|
240
234
|
});
|
|
241
235
|
api.modifyEnvironmentConfig((config)=>{
|
|
@@ -247,7 +241,7 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
247
241
|
];
|
|
248
242
|
return config;
|
|
249
243
|
});
|
|
250
|
-
const
|
|
244
|
+
const exposedFederationApi = {
|
|
251
245
|
options: {
|
|
252
246
|
nodePlugin: undefined,
|
|
253
247
|
browserPlugin: undefined,
|
|
@@ -257,65 +251,84 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
257
251
|
nodeEnvironmentName: undefined
|
|
258
252
|
},
|
|
259
253
|
assetResources: {},
|
|
254
|
+
getOptions: ()=>moduleFederationOptions,
|
|
255
|
+
registerNodeTargetConfigOwner: ()=>{
|
|
256
|
+
hasNodeTargetConfigOwner = true;
|
|
257
|
+
},
|
|
258
|
+
registerOptionsTransformer: (transformer)=>{
|
|
259
|
+
moduleFederationOptions = transformer(moduleFederationOptions);
|
|
260
|
+
},
|
|
260
261
|
isSSRConfig,
|
|
261
262
|
isRspressSSGConfig
|
|
262
263
|
};
|
|
263
|
-
api.expose(RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
|
|
264
|
+
api.expose(RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, exposedFederationApi);
|
|
264
265
|
const defaultBrowserEnvironmentName = environment;
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
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} `);
|
|
291
293
|
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
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} `);
|
|
305
307
|
}
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
+
}
|
|
309
|
+
});
|
|
308
310
|
api.onBeforeCreateCompiler(({ bundlerConfigs })=>{
|
|
309
311
|
if (!bundlerConfigs) {
|
|
310
312
|
throw new Error('Can not get bundlerConfigs!');
|
|
311
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);
|
|
312
325
|
bundlerConfigs.forEach((bundlerConfig)=>{
|
|
313
326
|
const bundlerConfigName = bundlerConfig.name || '';
|
|
314
327
|
const isConfiguredEnvironmentConfig = bundlerConfigName === environment;
|
|
315
328
|
const isNodeTargetEnvironmentConfig = target === 'node' && bundlerConfigName === environment;
|
|
316
329
|
const isRspressSSGEnvironmentConfig = isRspressSSGConfig(bundlerConfig.name);
|
|
317
330
|
const isActiveRspressSSGEnvironmentConfig = isRspress && isRspressSSGEnvironmentConfig;
|
|
318
|
-
const shouldUseSSRPluginConfig = isSSRConfig(bundlerConfig.name) || isNodeTargetEnvironmentConfig;
|
|
331
|
+
const shouldUseSSRPluginConfig = isSSRConfig(bundlerConfig.name) || isNodeTargetEnvironmentConfig && !isRstestCompanionActive();
|
|
319
332
|
if (target === 'node' && !isNodeTargetEnvironmentConfig && !isActiveRspressSSGEnvironmentConfig) {
|
|
320
333
|
return;
|
|
321
334
|
}
|
|
@@ -334,7 +347,12 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
334
347
|
(0,external_utils_js_namespaceObject.autoDeleteSplitChunkCacheGroups)(moduleFederationOptions, bundlerConfig?.optimization?.splitChunks);
|
|
335
348
|
(0,external_utils_js_namespaceObject.addDataFetchExposes)(moduleFederationOptions.exposes, shouldUseSSRPluginConfig);
|
|
336
349
|
const ssrModuleFederationOptions = shouldUseSSRPluginConfig ? (0,external_utils_js_namespaceObject.createSSRMFConfig)(moduleFederationOptions) : undefined;
|
|
337
|
-
|
|
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
|
+
}
|
|
338
356
|
const externals = bundlerConfig.externals;
|
|
339
357
|
if (Array.isArray(externals)) {
|
|
340
358
|
const sharedModules = new Set();
|
|
@@ -378,7 +396,7 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
378
396
|
if (!bundlerConfig.output?.chunkLoadingGlobal && !shouldUseSSRPluginConfig && !isActiveRspressSSGEnvironmentConfig && target !== 'node') {
|
|
379
397
|
bundlerConfig.output.chunkLoadingGlobal = `chunk_${moduleFederationOptions.name}`;
|
|
380
398
|
}
|
|
381
|
-
if (isNodeTargetEnvironmentConfig) {
|
|
399
|
+
if (isNodeTargetEnvironmentConfig && !isRstestCompanionActive()) {
|
|
382
400
|
(0,ssr_js_namespaceObject.patchNodeConfig)(bundlerConfig, ssrModuleFederationOptions ?? moduleFederationOptions);
|
|
383
401
|
}
|
|
384
402
|
// `uniqueName` is required for react refresh to work
|
|
@@ -391,11 +409,11 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
391
409
|
bundlerConfig.output.publicPath = 'auto';
|
|
392
410
|
}
|
|
393
411
|
if (!bundlerConfig.plugins.find((p)=>p && p.name === rspack_namespaceObject.PLUGIN_NAME)) {
|
|
394
|
-
if (shouldUseSSRPluginConfig) {
|
|
395
|
-
const ssrMFConfig = ssrModuleFederationOptions ?? (0,external_utils_js_namespaceObject.createSSRMFConfig)(moduleFederationOptions);
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
bundlerConfig.plugins.push(
|
|
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);
|
|
399
417
|
return;
|
|
400
418
|
} else if (isActiveRspressSSGEnvironmentConfig) {
|
|
401
419
|
const mfConfig = {
|
|
@@ -412,25 +430,25 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
412
430
|
bundlerConfig.output.asyncChunks = undefined;
|
|
413
431
|
const p = new rspack_namespaceObject.ModuleFederationPlugin(mfConfig);
|
|
414
432
|
if (bundlerConfig.name === external_constant_js_namespaceObject.RSPRESS_BUNDLER_CONFIG_NAME) {
|
|
415
|
-
|
|
433
|
+
exposedFederationApi.options.rspressSSGPlugin = p;
|
|
416
434
|
}
|
|
417
435
|
bundlerConfig.plugins.push(p);
|
|
418
436
|
return;
|
|
419
437
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
bundlerConfig.plugins.push(
|
|
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);
|
|
424
442
|
}
|
|
425
443
|
}
|
|
426
444
|
});
|
|
427
445
|
});
|
|
428
446
|
const generateMergedStatsAndManifest = ()=>{
|
|
429
|
-
const { distOutputDir, browserEnvironmentName, nodeEnvironmentName } =
|
|
447
|
+
const { distOutputDir, browserEnvironmentName, nodeEnvironmentName } = exposedFederationApi.options;
|
|
430
448
|
if (!distOutputDir || !browserEnvironmentName || !nodeEnvironmentName) {
|
|
431
449
|
return;
|
|
432
450
|
}
|
|
433
|
-
const assetResources =
|
|
451
|
+
const assetResources = exposedFederationApi.assetResources;
|
|
434
452
|
const browserAssets = assetResources[browserEnvironmentName];
|
|
435
453
|
const nodeAssets = assetResources[nodeEnvironmentName];
|
|
436
454
|
if (!browserAssets || !nodeAssets) {
|
|
@@ -442,12 +460,8 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
442
460
|
external_logger_js_default().error(err);
|
|
443
461
|
}
|
|
444
462
|
};
|
|
445
|
-
api.onDevCompileDone(
|
|
446
|
-
|
|
447
|
-
});
|
|
448
|
-
api.onAfterBuild(()=>{
|
|
449
|
-
generateMergedStatsAndManifest();
|
|
450
|
-
});
|
|
463
|
+
api.onDevCompileDone(generateMergedStatsAndManifest);
|
|
464
|
+
api.onAfterBuild(generateMergedStatsAndManifest);
|
|
451
465
|
}
|
|
452
466
|
});
|
|
453
467
|
|
package/dist/cli/index.mjs
CHANGED
|
@@ -66,9 +66,11 @@ const isRspressSSGConfig = (bundlerConfigName)=>{
|
|
|
66
66
|
RSPRESS_SSG_MD_ENV_NAME
|
|
67
67
|
].includes(bundlerConfigName || '');
|
|
68
68
|
};
|
|
69
|
-
const pluginModuleFederation = (
|
|
69
|
+
const pluginModuleFederation = (initialModuleFederationOptions, rsbuildOptions)=>({
|
|
70
70
|
name: RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
|
|
71
71
|
setup: (api)=>{
|
|
72
|
+
let moduleFederationOptions = initialModuleFederationOptions;
|
|
73
|
+
let hasNodeTargetConfigOwner = false;
|
|
72
74
|
if (!moduleFederationOptions?.name) {
|
|
73
75
|
throw new Error('The module federation option "name" is required in @module-federation/rsbuild-plugin.');
|
|
74
76
|
}
|
|
@@ -83,6 +85,8 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
83
85
|
}
|
|
84
86
|
const isRslib = callerName === CALL_NAME_MAP.RSLIB;
|
|
85
87
|
const isRspress = callerName === CALL_NAME_MAP.RSPRESS;
|
|
88
|
+
const isRstest = callerName === 'rstest';
|
|
89
|
+
const isRstestCompanionActive = ()=>isRstest && hasNodeTargetConfigOwner;
|
|
86
90
|
const isSSR = target === 'dual';
|
|
87
91
|
const environment = configuredEnvironment ?? resolveDefaultEnvironmentName({
|
|
88
92
|
callerName,
|
|
@@ -98,18 +102,6 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
98
102
|
}
|
|
99
103
|
setSSREnv();
|
|
100
104
|
}
|
|
101
|
-
const sharedOptions = parseOptions(moduleFederationOptions.shared || [], (item, key)=>{
|
|
102
|
-
if (typeof item !== 'string') throw new Error('Unexpected array in shared');
|
|
103
|
-
const config = item === key || !isRequiredVersion(item) ? {
|
|
104
|
-
import: item
|
|
105
|
-
} : {
|
|
106
|
-
import: key,
|
|
107
|
-
requiredVersion: item
|
|
108
|
-
};
|
|
109
|
-
return config;
|
|
110
|
-
}, (item)=>item);
|
|
111
|
-
// shared[0] is the shared name
|
|
112
|
-
const shared = sharedOptions.map((shared)=>shared[0].endsWith('/') ? shared[0].slice(0, -1) : shared[0]);
|
|
113
105
|
api.modifyRsbuildConfig((config)=>{
|
|
114
106
|
// skip storybook
|
|
115
107
|
if (isStoryBook(config)) {
|
|
@@ -166,9 +158,11 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
166
158
|
const availableEnvironmentsLabel = availableEnvironments.length > 0 ? availableEnvironments.join(', ') : '(none)';
|
|
167
159
|
throw new Error(`Can not find environment '${environment}' when using target: 'node'. Available environments: ${availableEnvironmentsLabel}.`);
|
|
168
160
|
}
|
|
169
|
-
|
|
170
|
-
config
|
|
171
|
-
|
|
161
|
+
if (!isRstestCompanionActive()) {
|
|
162
|
+
patchToolsTspack(nodeTargetEnv, (config)=>{
|
|
163
|
+
config.target = 'async-node';
|
|
164
|
+
});
|
|
165
|
+
}
|
|
172
166
|
}
|
|
173
167
|
});
|
|
174
168
|
api.modifyEnvironmentConfig((config)=>{
|
|
@@ -180,7 +174,7 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
180
174
|
];
|
|
181
175
|
return config;
|
|
182
176
|
});
|
|
183
|
-
const
|
|
177
|
+
const exposedFederationApi = {
|
|
184
178
|
options: {
|
|
185
179
|
nodePlugin: undefined,
|
|
186
180
|
browserPlugin: undefined,
|
|
@@ -190,65 +184,84 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
190
184
|
nodeEnvironmentName: undefined
|
|
191
185
|
},
|
|
192
186
|
assetResources: {},
|
|
187
|
+
getOptions: ()=>moduleFederationOptions,
|
|
188
|
+
registerNodeTargetConfigOwner: ()=>{
|
|
189
|
+
hasNodeTargetConfigOwner = true;
|
|
190
|
+
},
|
|
191
|
+
registerOptionsTransformer: (transformer)=>{
|
|
192
|
+
moduleFederationOptions = transformer(moduleFederationOptions);
|
|
193
|
+
},
|
|
193
194
|
isSSRConfig: isSSRConfig,
|
|
194
195
|
isRspressSSGConfig: isRspressSSGConfig
|
|
195
196
|
};
|
|
196
|
-
api.expose(RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
|
|
197
|
+
api.expose(RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, exposedFederationApi);
|
|
197
198
|
const defaultBrowserEnvironmentName = environment;
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
199
|
+
api.processAssets({
|
|
200
|
+
stage: 'report'
|
|
201
|
+
}, ({ assets, environment: envContext })=>{
|
|
202
|
+
if (moduleFederationOptions.manifest === false) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const assetFileNames = getManifestFileName(moduleFederationOptions.manifest);
|
|
206
|
+
const expectedBrowserEnv = exposedFederationApi.options.browserEnvironmentName ?? defaultBrowserEnvironmentName;
|
|
207
|
+
const expectedNodeEnv = exposedFederationApi.options.nodeEnvironmentName ?? SSR_ENV_NAME;
|
|
208
|
+
const envName = envContext.name;
|
|
209
|
+
if (envName !== expectedBrowserEnv && envName !== expectedNodeEnv) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
const assetResources = exposedFederationApi.assetResources;
|
|
213
|
+
const targetResources = assetResources[envName] || (assetResources[envName] = {});
|
|
214
|
+
const statsAsset = assets[assetFileNames.statsFileName];
|
|
215
|
+
if (statsAsset) {
|
|
216
|
+
try {
|
|
217
|
+
const raw = statsAsset.source();
|
|
218
|
+
const content = typeof raw === 'string' ? raw : raw.toString();
|
|
219
|
+
targetResources.stats = {
|
|
220
|
+
data: JSON.parse(content),
|
|
221
|
+
filename: assetFileNames.statsFileName
|
|
222
|
+
};
|
|
223
|
+
} catch (err) {
|
|
224
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
225
|
+
logger.error(`Failed to parse stats asset "${assetFileNames.statsFileName}" for environment "${envName}": ${message} `);
|
|
224
226
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
}
|
|
227
|
+
}
|
|
228
|
+
const manifestAsset = assets[assetFileNames.manifestFileName];
|
|
229
|
+
if (manifestAsset) {
|
|
230
|
+
try {
|
|
231
|
+
const raw = manifestAsset.source();
|
|
232
|
+
const content = typeof raw === 'string' ? raw : raw.toString();
|
|
233
|
+
targetResources.manifest = {
|
|
234
|
+
data: JSON.parse(content),
|
|
235
|
+
filename: assetFileNames.manifestFileName
|
|
236
|
+
};
|
|
237
|
+
} catch (err) {
|
|
238
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
239
|
+
logger.error(`Failed to parse manifest asset "${assetFileNames.manifestFileName}" for environment "${envName}": ${message} `);
|
|
238
240
|
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
+
}
|
|
242
|
+
});
|
|
241
243
|
api.onBeforeCreateCompiler(({ bundlerConfigs })=>{
|
|
242
244
|
if (!bundlerConfigs) {
|
|
243
245
|
throw new Error('Can not get bundlerConfigs!');
|
|
244
246
|
}
|
|
247
|
+
const sharedOptions = parseOptions(moduleFederationOptions.shared || [], (item, key)=>{
|
|
248
|
+
if (typeof item !== 'string') throw new Error('Unexpected array in shared');
|
|
249
|
+
const config = item === key || !isRequiredVersion(item) ? {
|
|
250
|
+
import: item
|
|
251
|
+
} : {
|
|
252
|
+
import: key,
|
|
253
|
+
requiredVersion: item
|
|
254
|
+
};
|
|
255
|
+
return config;
|
|
256
|
+
}, (item)=>item);
|
|
257
|
+
const shared = sharedOptions.map(([name])=>name.endsWith('/') ? name.slice(0, -1) : name);
|
|
245
258
|
bundlerConfigs.forEach((bundlerConfig)=>{
|
|
246
259
|
const bundlerConfigName = bundlerConfig.name || '';
|
|
247
260
|
const isConfiguredEnvironmentConfig = bundlerConfigName === environment;
|
|
248
261
|
const isNodeTargetEnvironmentConfig = target === 'node' && bundlerConfigName === environment;
|
|
249
262
|
const isRspressSSGEnvironmentConfig = isRspressSSGConfig(bundlerConfig.name);
|
|
250
263
|
const isActiveRspressSSGEnvironmentConfig = isRspress && isRspressSSGEnvironmentConfig;
|
|
251
|
-
const shouldUseSSRPluginConfig = isSSRConfig(bundlerConfig.name) || isNodeTargetEnvironmentConfig;
|
|
264
|
+
const shouldUseSSRPluginConfig = isSSRConfig(bundlerConfig.name) || isNodeTargetEnvironmentConfig && !isRstestCompanionActive();
|
|
252
265
|
if (target === 'node' && !isNodeTargetEnvironmentConfig && !isActiveRspressSSGEnvironmentConfig) {
|
|
253
266
|
return;
|
|
254
267
|
}
|
|
@@ -267,7 +280,12 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
267
280
|
autoDeleteSplitChunkCacheGroups(moduleFederationOptions, bundlerConfig?.optimization?.splitChunks);
|
|
268
281
|
addDataFetchExposes(moduleFederationOptions.exposes, shouldUseSSRPluginConfig);
|
|
269
282
|
const ssrModuleFederationOptions = shouldUseSSRPluginConfig ? createSSRMFConfig(moduleFederationOptions) : undefined;
|
|
270
|
-
|
|
283
|
+
if (!isRstestCompanionActive()) {
|
|
284
|
+
if (bundlerConfig.optimization?.runtimeChunk) {
|
|
285
|
+
logger.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.');
|
|
286
|
+
}
|
|
287
|
+
delete bundlerConfig.optimization?.runtimeChunk;
|
|
288
|
+
}
|
|
271
289
|
const externals = bundlerConfig.externals;
|
|
272
290
|
if (Array.isArray(externals)) {
|
|
273
291
|
const sharedModules = new Set();
|
|
@@ -311,7 +329,7 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
311
329
|
if (!bundlerConfig.output?.chunkLoadingGlobal && !shouldUseSSRPluginConfig && !isActiveRspressSSGEnvironmentConfig && target !== 'node') {
|
|
312
330
|
bundlerConfig.output.chunkLoadingGlobal = `chunk_${moduleFederationOptions.name}`;
|
|
313
331
|
}
|
|
314
|
-
if (isNodeTargetEnvironmentConfig) {
|
|
332
|
+
if (isNodeTargetEnvironmentConfig && !isRstestCompanionActive()) {
|
|
315
333
|
patchNodeConfig(bundlerConfig, ssrModuleFederationOptions ?? moduleFederationOptions);
|
|
316
334
|
}
|
|
317
335
|
// `uniqueName` is required for react refresh to work
|
|
@@ -324,11 +342,11 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
324
342
|
bundlerConfig.output.publicPath = 'auto';
|
|
325
343
|
}
|
|
326
344
|
if (!bundlerConfig.plugins.find((p)=>p && p.name === PLUGIN_NAME)) {
|
|
327
|
-
if (shouldUseSSRPluginConfig) {
|
|
328
|
-
const ssrMFConfig = ssrModuleFederationOptions ?? createSSRMFConfig(moduleFederationOptions);
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
bundlerConfig.plugins.push(
|
|
345
|
+
if (shouldUseSSRPluginConfig || isNodeTargetEnvironmentConfig) {
|
|
346
|
+
const ssrMFConfig = ssrModuleFederationOptions ?? (isRstestCompanionActive() ? moduleFederationOptions : createSSRMFConfig(moduleFederationOptions));
|
|
347
|
+
exposedFederationApi.options.nodePlugin = new ModuleFederationPlugin(ssrMFConfig);
|
|
348
|
+
exposedFederationApi.options.nodeEnvironmentName = bundlerConfig.name || SSR_ENV_NAME;
|
|
349
|
+
bundlerConfig.plugins.push(exposedFederationApi.options.nodePlugin);
|
|
332
350
|
return;
|
|
333
351
|
} else if (isActiveRspressSSGEnvironmentConfig) {
|
|
334
352
|
const mfConfig = {
|
|
@@ -345,25 +363,25 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
345
363
|
bundlerConfig.output.asyncChunks = undefined;
|
|
346
364
|
const p = new ModuleFederationPlugin(mfConfig);
|
|
347
365
|
if (bundlerConfig.name === RSPRESS_BUNDLER_CONFIG_NAME) {
|
|
348
|
-
|
|
366
|
+
exposedFederationApi.options.rspressSSGPlugin = p;
|
|
349
367
|
}
|
|
350
368
|
bundlerConfig.plugins.push(p);
|
|
351
369
|
return;
|
|
352
370
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
bundlerConfig.plugins.push(
|
|
371
|
+
exposedFederationApi.options.browserPlugin = new ModuleFederationPlugin(moduleFederationOptions);
|
|
372
|
+
exposedFederationApi.options.distOutputDir = bundlerConfig.output?.path || '';
|
|
373
|
+
exposedFederationApi.options.browserEnvironmentName = bundlerConfig.name || defaultBrowserEnvironmentName;
|
|
374
|
+
bundlerConfig.plugins.push(exposedFederationApi.options.browserPlugin);
|
|
357
375
|
}
|
|
358
376
|
}
|
|
359
377
|
});
|
|
360
378
|
});
|
|
361
379
|
const generateMergedStatsAndManifest = ()=>{
|
|
362
|
-
const { distOutputDir, browserEnvironmentName, nodeEnvironmentName } =
|
|
380
|
+
const { distOutputDir, browserEnvironmentName, nodeEnvironmentName } = exposedFederationApi.options;
|
|
363
381
|
if (!distOutputDir || !browserEnvironmentName || !nodeEnvironmentName) {
|
|
364
382
|
return;
|
|
365
383
|
}
|
|
366
|
-
const assetResources =
|
|
384
|
+
const assetResources = exposedFederationApi.assetResources;
|
|
367
385
|
const browserAssets = assetResources[browserEnvironmentName];
|
|
368
386
|
const nodeAssets = assetResources[nodeEnvironmentName];
|
|
369
387
|
if (!browserAssets || !nodeAssets) {
|
|
@@ -375,12 +393,8 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
375
393
|
logger.error(err);
|
|
376
394
|
}
|
|
377
395
|
};
|
|
378
|
-
api.onDevCompileDone(
|
|
379
|
-
|
|
380
|
-
});
|
|
381
|
-
api.onAfterBuild(()=>{
|
|
382
|
-
generateMergedStatsAndManifest();
|
|
383
|
-
});
|
|
396
|
+
api.onDevCompileDone(generateMergedStatsAndManifest);
|
|
397
|
+
api.onAfterBuild(generateMergedStatsAndManifest);
|
|
384
398
|
}
|
|
385
399
|
});
|
|
386
400
|
|
package/dist/utils/ssr.js
CHANGED
|
@@ -212,7 +212,15 @@ function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerN
|
|
|
212
212
|
};
|
|
213
213
|
return ssrEnvConfig;
|
|
214
214
|
}
|
|
215
|
-
|
|
215
|
+
/**
|
|
216
|
+
* Node-target MF defaults for Rsbuild SSR builds.
|
|
217
|
+
*
|
|
218
|
+
* `@module-federation/rstest` has a sibling helper (`withNodeDefaults` in
|
|
219
|
+
* packages/rstest/src/node-defaults.ts). Both default remotes to script
|
|
220
|
+
* transport and container output to CommonJS. Rstest additionally forces
|
|
221
|
+
* `library.name` to the container name so test workers can resolve it; Rsbuild
|
|
222
|
+
* keeps the bundler-derived name.
|
|
223
|
+
*/ function patchNodeMFConfig(mfConfig) {
|
|
216
224
|
if (mfConfig.remotes) {
|
|
217
225
|
mfConfig.remoteType = 'script';
|
|
218
226
|
}
|
|
@@ -221,7 +229,6 @@ function patchNodeMFConfig(mfConfig) {
|
|
|
221
229
|
};
|
|
222
230
|
mfConfig.library = {
|
|
223
231
|
...mfConfig.library,
|
|
224
|
-
name: mfConfig.name,
|
|
225
232
|
type: mfConfig.library?.type ?? 'commonjs-module'
|
|
226
233
|
};
|
|
227
234
|
mfConfig.runtimePlugins = [
|
package/dist/utils/ssr.mjs
CHANGED
|
@@ -142,7 +142,15 @@ function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerN
|
|
|
142
142
|
};
|
|
143
143
|
return ssrEnvConfig;
|
|
144
144
|
}
|
|
145
|
-
|
|
145
|
+
/**
|
|
146
|
+
* Node-target MF defaults for Rsbuild SSR builds.
|
|
147
|
+
*
|
|
148
|
+
* `@module-federation/rstest` has a sibling helper (`withNodeDefaults` in
|
|
149
|
+
* packages/rstest/src/node-defaults.ts). Both default remotes to script
|
|
150
|
+
* transport and container output to CommonJS. Rstest additionally forces
|
|
151
|
+
* `library.name` to the container name so test workers can resolve it; Rsbuild
|
|
152
|
+
* keeps the bundler-derived name.
|
|
153
|
+
*/ function patchNodeMFConfig(mfConfig) {
|
|
146
154
|
if (mfConfig.remotes) {
|
|
147
155
|
mfConfig.remoteType = 'script';
|
|
148
156
|
}
|
|
@@ -151,7 +159,6 @@ function patchNodeMFConfig(mfConfig) {
|
|
|
151
159
|
};
|
|
152
160
|
mfConfig.library = {
|
|
153
161
|
...mfConfig.library,
|
|
154
|
-
name: mfConfig.name,
|
|
155
162
|
type: mfConfig.library?.type ?? 'commonjs-module'
|
|
156
163
|
};
|
|
157
164
|
mfConfig.runtimePlugins = [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/rsbuild-plugin",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.9.0",
|
|
4
4
|
"description": "Module Federation plugin for Rsbuild",
|
|
5
5
|
"homepage": "https://module-federation.io",
|
|
6
6
|
"bugs": {
|
|
@@ -49,9 +49,9 @@
|
|
|
49
49
|
"dist"
|
|
50
50
|
],
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@module-federation/
|
|
53
|
-
"@module-federation/
|
|
54
|
-
"@module-federation/node": "2.7.
|
|
52
|
+
"@module-federation/enhanced": "2.9.0",
|
|
53
|
+
"@module-federation/sdk": "2.9.0",
|
|
54
|
+
"@module-federation/node": "2.7.50"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@rslib/core": "^0.23.2",
|