@module-federation/rsbuild-plugin 0.22.0 → 0.23.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 +1 -0
- package/dist/index.js +97 -49
- package/dist/index.mjs +97 -49
- package/dist/utils/ssr.d.ts +12 -13
- package/dist/utils.js +71 -35
- package/dist/utils.mjs +71 -35
- package/package.json +4 -4
package/dist/cli/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ 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;
|
package/dist/index.js
CHANGED
|
@@ -160,22 +160,14 @@ const ssr_require = (0, external_node_module_namespaceObject.createRequire)(__rs
|
|
|
160
160
|
const resolve = ssr_require.resolve;
|
|
161
161
|
const SSR_DIR = 'ssr';
|
|
162
162
|
const SSR_ENV_NAME = 'mf-ssr';
|
|
163
|
+
const ENV_NAME = 'mf';
|
|
163
164
|
function setSSREnv() {
|
|
164
165
|
process.env['MF_SSR_PRJ'] = 'true';
|
|
165
166
|
}
|
|
166
167
|
const isDev = ()=>'development' === process.env['NODE_ENV'];
|
|
167
|
-
function
|
|
168
|
+
function patchNodeConfig(config, mfConfig) {
|
|
168
169
|
var _config_output;
|
|
169
170
|
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
171
|
config.target = 'async-node';
|
|
180
172
|
const UniverseEntryChunkTrackerPlugin = ssr_require('@module-federation/node/universe-entry-chunk-tracker-plugin').default;
|
|
181
173
|
config.plugins ||= [];
|
|
@@ -186,19 +178,60 @@ function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry =
|
|
|
186
178
|
const suffix = `${(0, sdk_namespaceObject.encodeName)(uniqueName)}-[contenthash].js`;
|
|
187
179
|
config.output.chunkFilename = chunkFileName.replace('.js', suffix);
|
|
188
180
|
}
|
|
181
|
+
}
|
|
182
|
+
function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry = true, modifyPublicPath = true) {
|
|
183
|
+
config.output ||= {};
|
|
184
|
+
if (modifyPublicPath) {
|
|
185
|
+
var _config_output;
|
|
186
|
+
if ('string' != typeof (null == (_config_output = config.output) ? void 0 : _config_output.publicPath)) throw new Error('publicPath must be string!');
|
|
187
|
+
const publicPath = config.output.publicPath;
|
|
188
|
+
if ('auto' === publicPath) throw new Error('publicPath can not be "auto"!');
|
|
189
|
+
const publicPathWithSSRDir = `${publicPath}${ssrDir}/`;
|
|
190
|
+
config.output.publicPath = publicPathWithSSRDir;
|
|
191
|
+
}
|
|
192
|
+
if (callerName === CALL_NAME_MAP.RSPRESS && resetEntry) config.entry = 'data:application/node;base64,';
|
|
193
|
+
patchNodeConfig(config, mfConfig);
|
|
189
194
|
return config;
|
|
190
195
|
}
|
|
196
|
+
function patchToolsTspack(envConfig, fn) {
|
|
197
|
+
var _envConfig_tools;
|
|
198
|
+
const rspackArr = [];
|
|
199
|
+
if (null == (_envConfig_tools = envConfig.tools) ? void 0 : _envConfig_tools.rspack) {
|
|
200
|
+
var _envConfig_tools1, _envConfig_tools2;
|
|
201
|
+
if (Array.isArray(null == (_envConfig_tools1 = envConfig.tools) ? void 0 : _envConfig_tools1.rspack)) {
|
|
202
|
+
var _envConfig_tools3;
|
|
203
|
+
rspackArr.push(...null == (_envConfig_tools3 = envConfig.tools) ? void 0 : _envConfig_tools3.rspack);
|
|
204
|
+
} else if ('function' == typeof (null == (_envConfig_tools2 = envConfig.tools) ? void 0 : _envConfig_tools2.rspack)) {
|
|
205
|
+
var _envConfig_tools4;
|
|
206
|
+
rspackArr.push(null == (_envConfig_tools4 = envConfig.tools) ? void 0 : _envConfig_tools4.rspack);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
envConfig.tools ||= {};
|
|
210
|
+
envConfig.tools.rspack = [
|
|
211
|
+
...rspackArr,
|
|
212
|
+
fn
|
|
213
|
+
];
|
|
214
|
+
}
|
|
191
215
|
function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerName) {
|
|
192
|
-
var _ssrEnvConfig_output, _ssrEnvConfig_output_distPath, _ssrEnvConfig_output1, _rsbuildConfig_output_distPath, _rsbuildConfig_output;
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
216
|
+
var _envConfig_tools, _ssrEnvConfig_output, _ssrEnvConfig_output_distPath, _ssrEnvConfig_output1, _rsbuildConfig_output_distPath, _rsbuildConfig_output;
|
|
217
|
+
const rspackArr = [];
|
|
218
|
+
if (null == (_envConfig_tools = envConfig.tools) ? void 0 : _envConfig_tools.rspack) {
|
|
219
|
+
var _envConfig_tools1, _envConfig_tools2;
|
|
220
|
+
if (Array.isArray(null == (_envConfig_tools1 = envConfig.tools) ? void 0 : _envConfig_tools1.rspack)) {
|
|
221
|
+
var _envConfig_tools3;
|
|
222
|
+
rspackArr.push(...null == (_envConfig_tools3 = envConfig.tools) ? void 0 : _envConfig_tools3.rspack);
|
|
223
|
+
} else if ('function' == typeof (null == (_envConfig_tools2 = envConfig.tools) ? void 0 : _envConfig_tools2.rspack)) {
|
|
224
|
+
var _envConfig_tools4;
|
|
225
|
+
rspackArr.push(null == (_envConfig_tools4 = envConfig.tools) ? void 0 : _envConfig_tools4.rspack);
|
|
200
226
|
}
|
|
227
|
+
}
|
|
228
|
+
const ssrEnvConfig = {
|
|
229
|
+
...envConfig
|
|
201
230
|
};
|
|
231
|
+
patchToolsTspack(ssrEnvConfig, (config, { environment })=>{
|
|
232
|
+
if (environment.name !== SSR_ENV_NAME) return;
|
|
233
|
+
patchSSRRspackConfig(config, mfConfig, ssrDir, callerName);
|
|
234
|
+
});
|
|
202
235
|
ssrEnvConfig.output = {
|
|
203
236
|
...ssrEnvConfig.output,
|
|
204
237
|
target: 'node',
|
|
@@ -210,26 +243,30 @@ function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerN
|
|
|
210
243
|
};
|
|
211
244
|
return ssrEnvConfig;
|
|
212
245
|
}
|
|
213
|
-
function
|
|
246
|
+
function patchNodeMFConfig(mfConfig) {
|
|
214
247
|
var _mfConfig_library;
|
|
248
|
+
if (mfConfig.remotes) mfConfig.remoteType = "script";
|
|
249
|
+
mfConfig.exposes = {
|
|
250
|
+
...mfConfig.exposes
|
|
251
|
+
};
|
|
252
|
+
mfConfig.library = {
|
|
253
|
+
...mfConfig.library,
|
|
254
|
+
name: mfConfig.name,
|
|
255
|
+
type: (null == (_mfConfig_library = mfConfig.library) ? void 0 : _mfConfig_library.type) ?? 'commonjs-module'
|
|
256
|
+
};
|
|
257
|
+
mfConfig.runtimePlugins = [
|
|
258
|
+
...mfConfig.runtimePlugins || []
|
|
259
|
+
];
|
|
260
|
+
mfConfig.runtimePlugins.push(resolve('@module-federation/node/runtimePlugin'));
|
|
261
|
+
if (isDev()) mfConfig.runtimePlugins.push(resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'));
|
|
262
|
+
}
|
|
263
|
+
function createSSRMFConfig(mfConfig) {
|
|
215
264
|
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
|
-
]
|
|
265
|
+
...mfConfig
|
|
230
266
|
};
|
|
231
|
-
ssrMFConfig
|
|
232
|
-
|
|
267
|
+
patchNodeMFConfig(ssrMFConfig);
|
|
268
|
+
ssrMFConfig.dts = false;
|
|
269
|
+
ssrMFConfig.dev = false;
|
|
233
270
|
return ssrMFConfig;
|
|
234
271
|
}
|
|
235
272
|
function isRegExp(target) {
|
|
@@ -258,18 +295,19 @@ const isRspressSSGConfig = (bundlerConfigName)=>"node" === bundlerConfigName;
|
|
|
258
295
|
const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
259
296
|
name: RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
|
|
260
297
|
setup: (api)=>{
|
|
261
|
-
const { ssr, ssrDir = SSR_DIR, environment = DEFAULT_MF_ENVIRONMENT_NAME } = rsbuildOptions || {};
|
|
298
|
+
const { target = 'web', ssr, ssrDir = SSR_DIR, environment = DEFAULT_MF_ENVIRONMENT_NAME } = rsbuildOptions || {};
|
|
299
|
+
if (ssr) throw new Error("The `ssr` option is deprecated. If you want to enable SSR, please use `target: 'dual'` instead.");
|
|
262
300
|
const { callerName } = api.context;
|
|
263
301
|
const originalRsbuildConfig = api.getRsbuildConfig();
|
|
264
302
|
if (!callerName) throw new Error('`callerName` is undefined. Please ensure the @rsbuild/core version is higher than 1.3.21 .');
|
|
265
303
|
const isRslib = callerName === CALL_NAME_MAP.RSLIB;
|
|
266
304
|
const isRspress = callerName === CALL_NAME_MAP.RSPRESS;
|
|
267
|
-
const isSSR =
|
|
305
|
+
const isSSR = 'dual' === target;
|
|
268
306
|
if (isSSR && !isStoryBook(originalRsbuildConfig)) {
|
|
269
307
|
var _rsbuildConfig_environments;
|
|
270
|
-
if (!isRslib && !isRspress) throw new Error("'
|
|
308
|
+
if (!isRslib && !isRspress) throw new Error("'target' option is only supported in Rslib.");
|
|
271
309
|
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 '
|
|
310
|
+
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
311
|
setSSREnv();
|
|
274
312
|
}
|
|
275
313
|
const sharedOptions = (0, enhanced_namespaceObject.parseOptions)(moduleFederationOptions.shared || [], (item, key)=>{
|
|
@@ -281,7 +319,7 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
281
319
|
import: item
|
|
282
320
|
};
|
|
283
321
|
return config;
|
|
284
|
-
}, (item
|
|
322
|
+
}, (item)=>item);
|
|
285
323
|
const shared = sharedOptions.map((shared)=>shared[0].endsWith('/') ? shared[0].slice(0, -1) : shared[0]);
|
|
286
324
|
api.modifyRsbuildConfig((config)=>{
|
|
287
325
|
if (isStoryBook(config)) return;
|
|
@@ -308,8 +346,13 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
308
346
|
}
|
|
309
347
|
if (isSSR) {
|
|
310
348
|
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.
|
|
349
|
+
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
350
|
config.environments[SSR_ENV_NAME] = createSSRREnvConfig(null == (_config_environments1 = config.environments) ? void 0 : _config_environments1[environment], moduleFederationOptions, ssrDir, config, callerName);
|
|
351
|
+
} else if ('node' === target) {
|
|
352
|
+
const mfEnv = config.environments[ENV_NAME];
|
|
353
|
+
patchToolsTspack(mfEnv, (config, { environment })=>{
|
|
354
|
+
config.target = 'async-node';
|
|
355
|
+
});
|
|
313
356
|
}
|
|
314
357
|
});
|
|
315
358
|
api.modifyEnvironmentConfig((config)=>{
|
|
@@ -354,7 +397,7 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
354
397
|
};
|
|
355
398
|
} catch (err) {
|
|
356
399
|
const message = err instanceof Error ? err.message : String(err);
|
|
357
|
-
src_logger.error(`Failed to parse stats asset "${assetFileNames.statsFileName}" for environment "${envName}": ${message}`);
|
|
400
|
+
src_logger.error(`Failed to parse stats asset "${assetFileNames.statsFileName}" for environment "${envName}": ${message} `);
|
|
358
401
|
}
|
|
359
402
|
const manifestAsset = assets[assetFileNames.manifestFileName];
|
|
360
403
|
if (manifestAsset) try {
|
|
@@ -366,16 +409,16 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
366
409
|
};
|
|
367
410
|
} catch (err) {
|
|
368
411
|
const message = err instanceof Error ? err.message : String(err);
|
|
369
|
-
src_logger.error(`Failed to parse manifest asset "${assetFileNames.manifestFileName}" for environment "${envName}": ${message}`);
|
|
412
|
+
src_logger.error(`Failed to parse manifest asset "${assetFileNames.manifestFileName}" for environment "${envName}": ${message} `);
|
|
370
413
|
}
|
|
371
414
|
});
|
|
372
415
|
api.onBeforeCreateCompiler(({ bundlerConfigs })=>{
|
|
373
416
|
if (!bundlerConfigs) throw new Error('Can not get bundlerConfigs!');
|
|
374
417
|
bundlerConfigs.forEach((bundlerConfig)=>{
|
|
375
418
|
if (!isMFFormat(bundlerConfig) && !isRspress) return;
|
|
376
|
-
if (isStoryBook(originalRsbuildConfig)) bundlerConfig.output.uniqueName = `${moduleFederationOptions.name}-storybook-host`;
|
|
419
|
+
if (isStoryBook(originalRsbuildConfig)) bundlerConfig.output.uniqueName = `${moduleFederationOptions.name} -storybook - host`;
|
|
377
420
|
else {
|
|
378
|
-
var _bundlerConfig_optimization, _bundlerConfig_optimization1, _bundlerConfig_output, _bundlerConfig_output1;
|
|
421
|
+
var _bundlerConfig_optimization, _bundlerConfig_optimization1, _bundlerConfig_output, _bundlerConfig_output1, _bundlerConfig_output2;
|
|
379
422
|
autoDeleteSplitChunkCacheGroups(moduleFederationOptions, null == bundlerConfig ? void 0 : null == (_bundlerConfig_optimization = bundlerConfig.optimization) ? void 0 : _bundlerConfig_optimization.splitChunks);
|
|
380
423
|
addDataFetchExposes(moduleFederationOptions.exposes, isSSRConfig(bundlerConfig.name));
|
|
381
424
|
null == (_bundlerConfig_optimization1 = bundlerConfig.optimization) || delete _bundlerConfig_optimization1.runtimeChunk;
|
|
@@ -410,13 +453,18 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
410
453
|
});
|
|
411
454
|
if (sharedModules.size > 0) for (const sharedModule of sharedModules)src_logger.log(`${sharedModule} is removed from externals because it is a shared module.`);
|
|
412
455
|
}
|
|
413
|
-
if (!(null == (_bundlerConfig_output = bundlerConfig.output) ? void 0 : _bundlerConfig_output.chunkLoadingGlobal) && !isSSRConfig(bundlerConfig.name) && !isRspressSSGConfig(bundlerConfig.name)) {
|
|
456
|
+
if (!(null == (_bundlerConfig_output = bundlerConfig.output) ? void 0 : _bundlerConfig_output.chunkLoadingGlobal) && !isSSRConfig(bundlerConfig.name) && !isRspressSSGConfig(bundlerConfig.name) && 'node' !== target) {
|
|
414
457
|
bundlerConfig.output.chunkLoading = 'jsonp';
|
|
415
|
-
bundlerConfig.output.chunkLoadingGlobal = `chunk_${moduleFederationOptions.name}`;
|
|
458
|
+
bundlerConfig.output.chunkLoadingGlobal = `chunk_${moduleFederationOptions.name} `;
|
|
459
|
+
}
|
|
460
|
+
if ('node' === target && isMFFormat(bundlerConfig)) {
|
|
461
|
+
patchNodeConfig(bundlerConfig, moduleFederationOptions);
|
|
462
|
+
patchNodeMFConfig(moduleFederationOptions);
|
|
416
463
|
}
|
|
417
464
|
if (!(null == (_bundlerConfig_output1 = bundlerConfig.output) ? void 0 : _bundlerConfig_output1.uniqueName)) bundlerConfig.output.uniqueName = moduleFederationOptions.name;
|
|
465
|
+
if ((null == (_bundlerConfig_output2 = bundlerConfig.output) ? void 0 : _bundlerConfig_output2.publicPath) === void 0 && !isSSRConfig(bundlerConfig.name) && !isRspressSSGConfig(bundlerConfig.name)) bundlerConfig.output.publicPath = 'auto';
|
|
418
466
|
if (!bundlerConfig.plugins.find((p)=>p && p.name === rspack_namespaceObject.PLUGIN_NAME)) {
|
|
419
|
-
var
|
|
467
|
+
var _bundlerConfig_output3;
|
|
420
468
|
if (isSSRConfig(bundlerConfig.name)) {
|
|
421
469
|
generateMergedStatsAndManifestOptions.options.nodePlugin = new rspack_namespaceObject.ModuleFederationPlugin(createSSRMFConfig(moduleFederationOptions));
|
|
422
470
|
generateMergedStatsAndManifestOptions.options.nodeEnvironmentName = bundlerConfig.name || SSR_ENV_NAME;
|
|
@@ -439,7 +487,7 @@ const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
|
439
487
|
return;
|
|
440
488
|
}
|
|
441
489
|
generateMergedStatsAndManifestOptions.options.browserPlugin = new rspack_namespaceObject.ModuleFederationPlugin(moduleFederationOptions);
|
|
442
|
-
generateMergedStatsAndManifestOptions.options.distOutputDir = (null == (
|
|
490
|
+
generateMergedStatsAndManifestOptions.options.distOutputDir = (null == (_bundlerConfig_output3 = bundlerConfig.output) ? void 0 : _bundlerConfig_output3.path) || '';
|
|
443
491
|
generateMergedStatsAndManifestOptions.options.browserEnvironmentName = bundlerConfig.name || defaultBrowserEnvironmentName;
|
|
444
492
|
bundlerConfig.plugins.push(generateMergedStatsAndManifestOptions.options.browserPlugin);
|
|
445
493
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -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
|
|
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
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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
|
|
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
|
|
184
|
-
|
|
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 =
|
|
257
|
+
const isSSR = 'dual' === target;
|
|
220
258
|
if (isSSR && !isStoryBook(originalRsbuildConfig)) {
|
|
221
259
|
var _rsbuildConfig_environments;
|
|
222
|
-
if (!isRslib && !isRspress) throw new Error("'
|
|
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 '
|
|
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
|
|
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.
|
|
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
|
|
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 == (
|
|
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
|
}
|
package/dist/utils/ssr.d.ts
CHANGED
|
@@ -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?: {
|
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
|
|
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
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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
|
|
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
|
|
232
|
-
|
|
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
|
|
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
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
|
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
|
|
179
|
-
|
|
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.
|
|
3
|
+
"version": "0.23.0",
|
|
4
4
|
"description": "Module Federation plugin for Rsbuild",
|
|
5
5
|
"homepage": "https://module-federation.io",
|
|
6
6
|
"bugs": {
|
|
@@ -50,9 +50,9 @@
|
|
|
50
50
|
],
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"fs-extra": "11.3.0",
|
|
53
|
-
"@module-federation/
|
|
54
|
-
"@module-federation/node": "2.7.
|
|
55
|
-
"@module-federation/
|
|
53
|
+
"@module-federation/enhanced": "0.23.0",
|
|
54
|
+
"@module-federation/node": "2.7.28",
|
|
55
|
+
"@module-federation/sdk": "0.23.0"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@rslib/core": "^0.12.4",
|