@module-federation/rspack 2.0.0 → 2.1.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.
Files changed (41) hide show
  1. package/dist/ModuleFederationPlugin.js +365 -0
  2. package/dist/ModuleFederationPlugin.mjs +302 -0
  3. package/dist/RemoteEntryPlugin.js +131 -0
  4. package/dist/{RemoteEntryPlugin.esm.mjs → RemoteEntryPlugin.mjs} +23 -21
  5. package/dist/TreeShakingSharedPlugin.js +63 -0
  6. package/dist/TreeShakingSharedPlugin.mjs +15 -0
  7. package/dist/index.d.ts +3 -1
  8. package/dist/index.js +69 -0
  9. package/dist/index.mjs +14 -0
  10. package/dist/logger.js +62 -0
  11. package/dist/logger.mjs +12 -0
  12. package/dist/plugin.d.ts +1 -1
  13. package/dist/plugin.js +95 -0
  14. package/dist/plugin.mjs +5 -0
  15. package/dist/remote-entry-plugin.d.ts +1 -1
  16. package/dist/remote-entry-plugin.js +95 -0
  17. package/dist/remote-entry-plugin.mjs +5 -0
  18. package/package.json +34 -19
  19. package/dist/RemoteEntryPlugin.cjs.js +0 -70
  20. package/dist/RemoteEntryPlugin.cjs.js.map +0 -1
  21. package/dist/RemoteEntryPlugin.esm.mjs.map +0 -1
  22. package/dist/index.cjs.js +0 -13
  23. package/dist/index.cjs.js.map +0 -1
  24. package/dist/index.esm.d.ts +0 -1
  25. package/dist/index.esm.mjs +0 -8
  26. package/dist/index.esm.mjs.map +0 -1
  27. package/dist/plugin.cjs.js +0 -284
  28. package/dist/plugin.cjs.js.map +0 -1
  29. package/dist/plugin.esm.d.ts +0 -1
  30. package/dist/plugin.esm.mjs +0 -279
  31. package/dist/plugin.esm.mjs.map +0 -1
  32. package/dist/remote-entry-plugin.cjs.js +0 -10
  33. package/dist/remote-entry-plugin.cjs.js.map +0 -1
  34. package/dist/remote-entry-plugin.esm.d.ts +0 -1
  35. package/dist/remote-entry-plugin.esm.mjs +0 -4
  36. package/dist/remote-entry-plugin.esm.mjs.map +0 -1
  37. package/dist/src/index.d.ts +0 -3
  38. /package/dist/{src/ModuleFederationPlugin.d.ts → ModuleFederationPlugin.d.ts} +0 -0
  39. /package/dist/{src/RemoteEntryPlugin.d.ts → RemoteEntryPlugin.d.ts} +0 -0
  40. /package/dist/{src/TreeShakingSharedPlugin.d.ts → TreeShakingSharedPlugin.d.ts} +0 -0
  41. /package/dist/{src/logger.d.ts → logger.d.ts} +0 -0
@@ -0,0 +1,365 @@
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
+ /************************************************************************/
13
+ // webpack/runtime/compat_get_default_export
14
+ (() => {
15
+ // getDefaultExport function for compatibility with non-ESM modules
16
+ __webpack_require__.n = (module) => {
17
+ var getter = module && module.__esModule ?
18
+ () => (module['default']) :
19
+ () => (module);
20
+ __webpack_require__.d(getter, { a: getter });
21
+ return getter;
22
+ };
23
+
24
+ })();
25
+ // webpack/runtime/define_property_getters
26
+ (() => {
27
+ __webpack_require__.d = (exports, definition) => {
28
+ for(var key in definition) {
29
+ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
30
+ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
31
+ }
32
+ }
33
+ };
34
+ })();
35
+ // webpack/runtime/has_own_property
36
+ (() => {
37
+ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
38
+ })();
39
+ // webpack/runtime/make_namespace_object
40
+ (() => {
41
+ // define __esModule on exports
42
+ __webpack_require__.r = (exports) => {
43
+ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
44
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
45
+ }
46
+ Object.defineProperty(exports, '__esModule', { value: true });
47
+ };
48
+ })();
49
+ /************************************************************************/
50
+ var __webpack_exports__ = {};
51
+ // ESM COMPAT FLAG
52
+ __webpack_require__.r(__webpack_exports__);
53
+
54
+ // EXPORTS
55
+ __webpack_require__.d(__webpack_exports__, {
56
+ ModuleFederationPlugin: () => (/* binding */ ModuleFederationPlugin),
57
+ GetPublicPathPlugin: () => (/* binding */ GetPublicPathPlugin),
58
+ PLUGIN_NAME: () => (/* binding */ PLUGIN_NAME),
59
+ TreeShakingSharedPlugin: () => (/* reexport */ external_TreeShakingSharedPlugin_js_namespaceObject.TreeShakingSharedPlugin)
60
+ });
61
+
62
+ ;// CONCATENATED MODULE: external "@module-federation/sdk"
63
+ const sdk_namespaceObject = require("@module-federation/sdk");
64
+ ;// CONCATENATED MODULE: external "@module-federation/manifest"
65
+ const manifest_namespaceObject = require("@module-federation/manifest");
66
+ ;// CONCATENATED MODULE: external "@module-federation/managers"
67
+ const managers_namespaceObject = require("@module-federation/managers");
68
+ ;// CONCATENATED MODULE: external "@module-federation/dts-plugin"
69
+ const dts_plugin_namespaceObject = require("@module-federation/dts-plugin");
70
+ ;// CONCATENATED MODULE: external "@module-federation/bridge-react-webpack-plugin"
71
+ const bridge_react_webpack_plugin_namespaceObject = require("@module-federation/bridge-react-webpack-plugin");
72
+ var bridge_react_webpack_plugin_default = /*#__PURE__*/__webpack_require__.n(bridge_react_webpack_plugin_namespaceObject);
73
+ ;// CONCATENATED MODULE: external "node:path"
74
+ const external_node_path_namespaceObject = require("node:path");
75
+ var external_node_path_default = /*#__PURE__*/__webpack_require__.n(external_node_path_namespaceObject);
76
+ ;// CONCATENATED MODULE: external "node:fs"
77
+ const external_node_fs_namespaceObject = require("node:fs");
78
+ var external_node_fs_default = /*#__PURE__*/__webpack_require__.n(external_node_fs_namespaceObject);
79
+ ;// CONCATENATED MODULE: external "./RemoteEntryPlugin.js"
80
+ const external_RemoteEntryPlugin_js_namespaceObject = require("./RemoteEntryPlugin.js");
81
+ ;// CONCATENATED MODULE: external "./logger.js"
82
+ const external_logger_js_namespaceObject = require("./logger.js");
83
+ var external_logger_js_default = /*#__PURE__*/__webpack_require__.n(external_logger_js_namespaceObject);
84
+ ;// CONCATENATED MODULE: external "./TreeShakingSharedPlugin.js"
85
+ const external_TreeShakingSharedPlugin_js_namespaceObject = require("./TreeShakingSharedPlugin.js");
86
+ ;// CONCATENATED MODULE: ./src/ModuleFederationPlugin.ts
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+ const RuntimeToolsPath = require.resolve('@module-federation/runtime-tools');
97
+ const PLUGIN_NAME = 'RspackModuleFederationPlugin';
98
+ class ModuleFederationPlugin {
99
+ _patchBundlerConfig(compiler) {
100
+ var _experiments_optimization;
101
+ const { name, experiments } = this._options;
102
+ const definePluginOptions = {};
103
+ if (name) {
104
+ definePluginOptions['FEDERATION_BUILD_IDENTIFIER'] = JSON.stringify((0,sdk_namespaceObject.composeKeyWithSeparator)(name, managers_namespaceObject.utils.getBuildVersion()));
105
+ }
106
+ // Add FEDERATION_OPTIMIZE_NO_SNAPSHOT_PLUGIN
107
+ const disableSnapshot = (experiments === null || experiments === void 0 ? void 0 : (_experiments_optimization = experiments.optimization) === null || _experiments_optimization === void 0 ? void 0 : _experiments_optimization.disableSnapshot) ?? false;
108
+ definePluginOptions['FEDERATION_OPTIMIZE_NO_SNAPSHOT_PLUGIN'] = disableSnapshot;
109
+ // Determine ENV_TARGET: only if manually specified in experiments.optimization.target
110
+ if ((experiments === null || experiments === void 0 ? void 0 : experiments.optimization) && typeof experiments.optimization === 'object' && experiments.optimization !== null && 'target' in experiments.optimization) {
111
+ const manualTarget = experiments.optimization.target;
112
+ // Ensure the target is one of the expected values before setting
113
+ if (manualTarget === 'web' || manualTarget === 'node') {
114
+ definePluginOptions['ENV_TARGET'] = JSON.stringify(manualTarget);
115
+ }
116
+ }
117
+ // No inference for ENV_TARGET. If not manually set and valid, it's not defined.
118
+ new compiler.webpack.DefinePlugin(definePluginOptions).apply(compiler);
119
+ }
120
+ _checkSingleton(compiler) {
121
+ let count = 0;
122
+ compiler.options.plugins.forEach((p)=>{
123
+ if (typeof p !== 'object' || !p) {
124
+ return;
125
+ }
126
+ if (p['name'] === this.name) {
127
+ count++;
128
+ if (count > 1) {
129
+ throw new Error(`Detect duplicate register ${this.name},please ensure ${this.name} is singleton!`);
130
+ }
131
+ }
132
+ });
133
+ }
134
+ apply(compiler) {
135
+ var _options_experiments, _options_experiments1;
136
+ (0,sdk_namespaceObject.bindLoggerToCompiler)((external_logger_js_default()), compiler, PLUGIN_NAME);
137
+ const { _options: options } = this;
138
+ if (!options.name) {
139
+ throw new Error('[ ModuleFederationPlugin ]: name is required');
140
+ }
141
+ this._checkSingleton(compiler);
142
+ this._patchBundlerConfig(compiler);
143
+ const containerManager = new managers_namespaceObject.ContainerManager();
144
+ containerManager.init(options);
145
+ if (containerManager.enable) {
146
+ this._patchChunkSplit(compiler, options.name);
147
+ }
148
+ // must before ModuleFederationPlugin
149
+ new external_RemoteEntryPlugin_js_namespaceObject.RemoteEntryPlugin(options).apply(compiler);
150
+ if ((_options_experiments = options.experiments) === null || _options_experiments === void 0 ? void 0 : _options_experiments.provideExternalRuntime) {
151
+ if (options.exposes) {
152
+ throw new Error('You can only set provideExternalRuntime: true in pure consumer which not expose modules.');
153
+ }
154
+ const runtimePlugins = options.runtimePlugins || [];
155
+ options.runtimePlugins = runtimePlugins.concat(require.resolve('@module-federation/inject-external-runtime-core-plugin'));
156
+ }
157
+ if (((_options_experiments1 = options.experiments) === null || _options_experiments1 === void 0 ? void 0 : _options_experiments1.externalRuntime) === true) {
158
+ const Externals = compiler.webpack.ExternalsPlugin;
159
+ new Externals(compiler.options.externalsType || 'global', {
160
+ '@module-federation/runtime-core': '_FEDERATION_RUNTIME_CORE'
161
+ }).apply(compiler);
162
+ }
163
+ const implementationPath = options.implementation || RuntimeToolsPath;
164
+ options.implementation = implementationPath;
165
+ let disableManifest = options.manifest === false;
166
+ let disableDts = options.dts === false;
167
+ if (!disableDts) {
168
+ const dtsPlugin = new dts_plugin_namespaceObject.DtsPlugin(options);
169
+ // @ts-ignore
170
+ dtsPlugin.apply(compiler);
171
+ dtsPlugin.addRuntimePlugins();
172
+ }
173
+ if (!disableManifest && options.exposes) {
174
+ try {
175
+ options.exposes = containerManager.containerPluginExposesOptions;
176
+ } catch (err) {
177
+ if (err instanceof Error) {
178
+ err.message = `[ ModuleFederationPlugin ]: Manifest will not generate, because: ${err.message}`;
179
+ }
180
+ external_logger_js_default().warn(err);
181
+ disableManifest = true;
182
+ }
183
+ }
184
+ new compiler.webpack.container.ModuleFederationPlugin(options).apply(compiler);
185
+ const resolveRuntimePath = (candidates)=>{
186
+ for (const candidate of candidates){
187
+ try {
188
+ return require.resolve(candidate, {
189
+ paths: [
190
+ implementationPath
191
+ ]
192
+ });
193
+ } catch {}
194
+ }
195
+ throw new Error(`[ ModuleFederationPlugin ]: Unable to resolve runtime entry from ${candidates.join(', ')}`);
196
+ };
197
+ const runtimeESMPath = resolveRuntimePath([
198
+ '@module-federation/runtime/dist/index.js',
199
+ '@module-federation/runtime/dist/index.esm.js',
200
+ '@module-federation/runtime/dist/index.cjs',
201
+ '@module-federation/runtime/dist/index.cjs.cjs',
202
+ '@module-federation/runtime'
203
+ ]);
204
+ compiler.hooks.afterPlugins.tap('PatchAliasWebpackPlugin', ()=>{
205
+ compiler.options.resolve.alias = {
206
+ ...compiler.options.resolve.alias,
207
+ '@module-federation/runtime$': runtimeESMPath
208
+ };
209
+ });
210
+ if (!disableManifest) {
211
+ this._statsPlugin = new manifest_namespaceObject.StatsPlugin(options, {
212
+ pluginVersion: "2.1.0",
213
+ bundler: 'rspack'
214
+ });
215
+ // @ts-ignore
216
+ this._statsPlugin.apply(compiler);
217
+ }
218
+ const checkBridgeReactInstalled = ()=>{
219
+ try {
220
+ const userPackageJsonPath = external_node_path_default().resolve(compiler.context, 'package.json');
221
+ if (external_node_fs_default().existsSync(userPackageJsonPath)) {
222
+ const userPackageJson = JSON.parse(external_node_fs_default().readFileSync(userPackageJsonPath, 'utf-8'));
223
+ const userDependencies = {
224
+ ...userPackageJson.dependencies,
225
+ ...userPackageJson.devDependencies
226
+ };
227
+ return !!userDependencies['@module-federation/bridge-react'];
228
+ }
229
+ return false;
230
+ } catch (error) {
231
+ return false;
232
+ }
233
+ };
234
+ const hasBridgeReact = checkBridgeReactInstalled();
235
+ // react bridge plugin
236
+ const shouldEnableBridgePlugin = ()=>{
237
+ var _options_bridge, _options_bridge1, _options_bridge2;
238
+ // Priority 1: Explicit enableBridgeRouter configuration
239
+ if ((options === null || options === void 0 ? void 0 : (_options_bridge = options.bridge) === null || _options_bridge === void 0 ? void 0 : _options_bridge.enableBridgeRouter) === true) {
240
+ return true;
241
+ }
242
+ // Priority 2: Explicit disable via enableBridgeRouter:false or disableAlias:true
243
+ if ((options === null || options === void 0 ? void 0 : (_options_bridge1 = options.bridge) === null || _options_bridge1 === void 0 ? void 0 : _options_bridge1.enableBridgeRouter) === false || (options === null || options === void 0 ? void 0 : (_options_bridge2 = options.bridge) === null || _options_bridge2 === void 0 ? void 0 : _options_bridge2.disableAlias) === true) {
244
+ var _options_bridge3;
245
+ if ((options === null || options === void 0 ? void 0 : (_options_bridge3 = options.bridge) === null || _options_bridge3 === void 0 ? void 0 : _options_bridge3.disableAlias) === true) {
246
+ external_logger_js_default().warn("\u26A0\uFE0F [ModuleFederationPlugin] The `disableAlias` option is deprecated and will be removed in a future version.\n" + ' Please use `enableBridgeRouter: false` instead:\n' + ' {\n' + ' bridge: {\n' + ' enableBridgeRouter: false // Use this instead of disableAlias: true\n' + ' }\n' + ' }');
247
+ }
248
+ return false;
249
+ }
250
+ // Priority 3: Automatic detection based on bridge-react installation
251
+ if (hasBridgeReact) {
252
+ external_logger_js_default().info("\uD83D\uDCA1 [ModuleFederationPlugin] Detected @module-federation/bridge-react in your dependencies.\n" + ' For better control and to avoid future breaking changes, please explicitly set:\n' + ' {\n' + ' bridge: {\n' + ' enableBridgeRouter: true // Explicitly enable bridge router\n' + ' }\n' + ' }');
253
+ return true;
254
+ }
255
+ return false;
256
+ };
257
+ const enableBridgePlugin = shouldEnableBridgePlugin();
258
+ // When bridge plugin is disabled (router disabled), alias to /base entry
259
+ if (!enableBridgePlugin && hasBridgeReact) {
260
+ compiler.hooks.afterPlugins.tap('BridgeReactBaseAliasPlugin', ()=>{
261
+ try {
262
+ const bridgeReactBasePath = external_node_path_default().resolve(compiler.context, 'node_modules/@module-federation/bridge-react/dist/base.es.js');
263
+ if (!external_node_fs_default().existsSync(bridgeReactBasePath)) {
264
+ external_logger_js_default().warn("\u26A0\uFE0F [ModuleFederationPlugin] bridge-react /base entry not found, falling back to default entry");
265
+ return;
266
+ }
267
+ compiler.options.resolve.alias = {
268
+ ...compiler.options.resolve.alias,
269
+ '@module-federation/bridge-react$': bridgeReactBasePath
270
+ };
271
+ external_logger_js_default().info("\u2705 [ModuleFederationPlugin] Router disabled - using /base entry (no react-router-dom)");
272
+ } catch (error) {
273
+ external_logger_js_default().warn("\u26A0\uFE0F [ModuleFederationPlugin] Failed to set /base alias, using default entry");
274
+ }
275
+ });
276
+ }
277
+ if (enableBridgePlugin) {
278
+ new (bridge_react_webpack_plugin_default())({
279
+ moduleFederationOptions: this._options
280
+ }).apply(compiler);
281
+ }
282
+ }
283
+ _patchChunkSplit(compiler, name) {
284
+ const { splitChunks } = compiler.options.optimization;
285
+ const patchChunkSplit = (cacheGroup)=>{
286
+ switch(typeof cacheGroup){
287
+ case 'boolean':
288
+ case 'string':
289
+ case 'function':
290
+ break;
291
+ // cacheGroup.chunks will inherit splitChunks.chunks, so you only need to modify the chunks that are set separately
292
+ case 'object':
293
+ {
294
+ if (cacheGroup instanceof RegExp) {
295
+ break;
296
+ }
297
+ if (!cacheGroup.chunks) {
298
+ break;
299
+ }
300
+ if (typeof cacheGroup.chunks === 'function') {
301
+ const prevChunks = cacheGroup.chunks;
302
+ cacheGroup.chunks = (chunk)=>{
303
+ if (chunk.name && (chunk.name === name || chunk.name === name + '_partial')) {
304
+ return false;
305
+ }
306
+ return prevChunks(chunk);
307
+ };
308
+ break;
309
+ }
310
+ if (cacheGroup.chunks === 'all') {
311
+ cacheGroup.chunks = (chunk)=>{
312
+ if (chunk.name && (chunk.name === name || chunk.name === name + '_partial')) {
313
+ return false;
314
+ }
315
+ return true;
316
+ };
317
+ break;
318
+ }
319
+ if (cacheGroup.chunks === 'initial') {
320
+ cacheGroup.chunks = (chunk)=>{
321
+ if (chunk.name && (chunk.name === name || chunk.name === name + '_partial')) {
322
+ return false;
323
+ }
324
+ return chunk.isOnlyInitial();
325
+ };
326
+ break;
327
+ }
328
+ break;
329
+ }
330
+ default:
331
+ break;
332
+ }
333
+ };
334
+ if (!splitChunks) {
335
+ return;
336
+ }
337
+ // 修改 splitChunk.chunks
338
+ patchChunkSplit(splitChunks);
339
+ const { cacheGroups } = splitChunks;
340
+ if (!cacheGroups) {
341
+ return;
342
+ }
343
+ // 修改 splitChunk.cacheGroups[key].chunks
344
+ Object.keys(cacheGroups).forEach((cacheGroupKey)=>{
345
+ patchChunkSplit(cacheGroups[cacheGroupKey]);
346
+ });
347
+ }
348
+ constructor(options){
349
+ this.name = PLUGIN_NAME;
350
+ this._options = options;
351
+ }
352
+ }
353
+ const GetPublicPathPlugin = external_RemoteEntryPlugin_js_namespaceObject.RemoteEntryPlugin;
354
+
355
+
356
+ exports.GetPublicPathPlugin = __webpack_exports__.GetPublicPathPlugin;
357
+ exports.ModuleFederationPlugin = __webpack_exports__.ModuleFederationPlugin;
358
+ exports.PLUGIN_NAME = __webpack_exports__.PLUGIN_NAME;
359
+ exports.TreeShakingSharedPlugin = __webpack_exports__.TreeShakingSharedPlugin;
360
+ for(var __webpack_i__ in __webpack_exports__) {
361
+ if(["GetPublicPathPlugin","ModuleFederationPlugin","PLUGIN_NAME","TreeShakingSharedPlugin"].indexOf(__webpack_i__) === -1) {
362
+ exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
363
+ }
364
+ }
365
+ Object.defineProperty(exports, '__esModule', { value: true });
@@ -0,0 +1,302 @@
1
+ import { bindLoggerToCompiler, composeKeyWithSeparator } from "@module-federation/sdk";
2
+ import { StatsPlugin } from "@module-federation/manifest";
3
+ import { ContainerManager, utils } from "@module-federation/managers";
4
+ import { DtsPlugin } from "@module-federation/dts-plugin";
5
+ import bridge_react_webpack_plugin from "@module-federation/bridge-react-webpack-plugin";
6
+ import node_path from "node:path";
7
+ import node_fs from "node:fs";
8
+ import { RemoteEntryPlugin } from "./RemoteEntryPlugin.mjs";
9
+ import logger from "./logger.mjs";
10
+ import { TreeShakingSharedPlugin } from "./TreeShakingSharedPlugin.mjs";
11
+
12
+ ;// CONCATENATED MODULE: external "@module-federation/sdk"
13
+
14
+ ;// CONCATENATED MODULE: external "@module-federation/manifest"
15
+
16
+ ;// CONCATENATED MODULE: external "@module-federation/managers"
17
+
18
+ ;// CONCATENATED MODULE: external "@module-federation/dts-plugin"
19
+
20
+ ;// CONCATENATED MODULE: external "@module-federation/bridge-react-webpack-plugin"
21
+
22
+ ;// CONCATENATED MODULE: external "node:path"
23
+
24
+ ;// CONCATENATED MODULE: external "node:fs"
25
+
26
+ ;// CONCATENATED MODULE: external "./RemoteEntryPlugin.mjs"
27
+
28
+ ;// CONCATENATED MODULE: external "./logger.mjs"
29
+
30
+ ;// CONCATENATED MODULE: external "./TreeShakingSharedPlugin.mjs"
31
+
32
+ ;// CONCATENATED MODULE: ./src/ModuleFederationPlugin.ts
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+
42
+ const RuntimeToolsPath = require.resolve('@module-federation/runtime-tools');
43
+ const PLUGIN_NAME = 'RspackModuleFederationPlugin';
44
+ class ModuleFederationPlugin {
45
+ _patchBundlerConfig(compiler) {
46
+ var _experiments_optimization;
47
+ const { name, experiments } = this._options;
48
+ const definePluginOptions = {};
49
+ if (name) {
50
+ definePluginOptions['FEDERATION_BUILD_IDENTIFIER'] = JSON.stringify(composeKeyWithSeparator(name, utils.getBuildVersion()));
51
+ }
52
+ // Add FEDERATION_OPTIMIZE_NO_SNAPSHOT_PLUGIN
53
+ const disableSnapshot = (experiments === null || experiments === void 0 ? void 0 : (_experiments_optimization = experiments.optimization) === null || _experiments_optimization === void 0 ? void 0 : _experiments_optimization.disableSnapshot) ?? false;
54
+ definePluginOptions['FEDERATION_OPTIMIZE_NO_SNAPSHOT_PLUGIN'] = disableSnapshot;
55
+ // Determine ENV_TARGET: only if manually specified in experiments.optimization.target
56
+ if ((experiments === null || experiments === void 0 ? void 0 : experiments.optimization) && typeof experiments.optimization === 'object' && experiments.optimization !== null && 'target' in experiments.optimization) {
57
+ const manualTarget = experiments.optimization.target;
58
+ // Ensure the target is one of the expected values before setting
59
+ if (manualTarget === 'web' || manualTarget === 'node') {
60
+ definePluginOptions['ENV_TARGET'] = JSON.stringify(manualTarget);
61
+ }
62
+ }
63
+ // No inference for ENV_TARGET. If not manually set and valid, it's not defined.
64
+ new compiler.webpack.DefinePlugin(definePluginOptions).apply(compiler);
65
+ }
66
+ _checkSingleton(compiler) {
67
+ let count = 0;
68
+ compiler.options.plugins.forEach((p)=>{
69
+ if (typeof p !== 'object' || !p) {
70
+ return;
71
+ }
72
+ if (p['name'] === this.name) {
73
+ count++;
74
+ if (count > 1) {
75
+ throw new Error(`Detect duplicate register ${this.name},please ensure ${this.name} is singleton!`);
76
+ }
77
+ }
78
+ });
79
+ }
80
+ apply(compiler) {
81
+ var _options_experiments, _options_experiments1;
82
+ bindLoggerToCompiler(logger, compiler, PLUGIN_NAME);
83
+ const { _options: options } = this;
84
+ if (!options.name) {
85
+ throw new Error('[ ModuleFederationPlugin ]: name is required');
86
+ }
87
+ this._checkSingleton(compiler);
88
+ this._patchBundlerConfig(compiler);
89
+ const containerManager = new ContainerManager();
90
+ containerManager.init(options);
91
+ if (containerManager.enable) {
92
+ this._patchChunkSplit(compiler, options.name);
93
+ }
94
+ // must before ModuleFederationPlugin
95
+ new RemoteEntryPlugin(options).apply(compiler);
96
+ if ((_options_experiments = options.experiments) === null || _options_experiments === void 0 ? void 0 : _options_experiments.provideExternalRuntime) {
97
+ if (options.exposes) {
98
+ throw new Error('You can only set provideExternalRuntime: true in pure consumer which not expose modules.');
99
+ }
100
+ const runtimePlugins = options.runtimePlugins || [];
101
+ options.runtimePlugins = runtimePlugins.concat(require.resolve('@module-federation/inject-external-runtime-core-plugin'));
102
+ }
103
+ if (((_options_experiments1 = options.experiments) === null || _options_experiments1 === void 0 ? void 0 : _options_experiments1.externalRuntime) === true) {
104
+ const Externals = compiler.webpack.ExternalsPlugin;
105
+ new Externals(compiler.options.externalsType || 'global', {
106
+ '@module-federation/runtime-core': '_FEDERATION_RUNTIME_CORE'
107
+ }).apply(compiler);
108
+ }
109
+ const implementationPath = options.implementation || RuntimeToolsPath;
110
+ options.implementation = implementationPath;
111
+ let disableManifest = options.manifest === false;
112
+ let disableDts = options.dts === false;
113
+ if (!disableDts) {
114
+ const dtsPlugin = new DtsPlugin(options);
115
+ // @ts-ignore
116
+ dtsPlugin.apply(compiler);
117
+ dtsPlugin.addRuntimePlugins();
118
+ }
119
+ if (!disableManifest && options.exposes) {
120
+ try {
121
+ options.exposes = containerManager.containerPluginExposesOptions;
122
+ } catch (err) {
123
+ if (err instanceof Error) {
124
+ err.message = `[ ModuleFederationPlugin ]: Manifest will not generate, because: ${err.message}`;
125
+ }
126
+ logger.warn(err);
127
+ disableManifest = true;
128
+ }
129
+ }
130
+ new compiler.webpack.container.ModuleFederationPlugin(options).apply(compiler);
131
+ const resolveRuntimePath = (candidates)=>{
132
+ for (const candidate of candidates){
133
+ try {
134
+ return require.resolve(candidate, {
135
+ paths: [
136
+ implementationPath
137
+ ]
138
+ });
139
+ } catch {}
140
+ }
141
+ throw new Error(`[ ModuleFederationPlugin ]: Unable to resolve runtime entry from ${candidates.join(', ')}`);
142
+ };
143
+ const runtimeESMPath = resolveRuntimePath([
144
+ '@module-federation/runtime/dist/index.js',
145
+ '@module-federation/runtime/dist/index.esm.js',
146
+ '@module-federation/runtime/dist/index.cjs',
147
+ '@module-federation/runtime/dist/index.cjs.cjs',
148
+ '@module-federation/runtime'
149
+ ]);
150
+ compiler.hooks.afterPlugins.tap('PatchAliasWebpackPlugin', ()=>{
151
+ compiler.options.resolve.alias = {
152
+ ...compiler.options.resolve.alias,
153
+ '@module-federation/runtime$': runtimeESMPath
154
+ };
155
+ });
156
+ if (!disableManifest) {
157
+ this._statsPlugin = new StatsPlugin(options, {
158
+ pluginVersion: "2.1.0",
159
+ bundler: 'rspack'
160
+ });
161
+ // @ts-ignore
162
+ this._statsPlugin.apply(compiler);
163
+ }
164
+ const checkBridgeReactInstalled = ()=>{
165
+ try {
166
+ const userPackageJsonPath = node_path.resolve(compiler.context, 'package.json');
167
+ if (node_fs.existsSync(userPackageJsonPath)) {
168
+ const userPackageJson = JSON.parse(node_fs.readFileSync(userPackageJsonPath, 'utf-8'));
169
+ const userDependencies = {
170
+ ...userPackageJson.dependencies,
171
+ ...userPackageJson.devDependencies
172
+ };
173
+ return !!userDependencies['@module-federation/bridge-react'];
174
+ }
175
+ return false;
176
+ } catch (error) {
177
+ return false;
178
+ }
179
+ };
180
+ const hasBridgeReact = checkBridgeReactInstalled();
181
+ // react bridge plugin
182
+ const shouldEnableBridgePlugin = ()=>{
183
+ var _options_bridge, _options_bridge1, _options_bridge2;
184
+ // Priority 1: Explicit enableBridgeRouter configuration
185
+ if ((options === null || options === void 0 ? void 0 : (_options_bridge = options.bridge) === null || _options_bridge === void 0 ? void 0 : _options_bridge.enableBridgeRouter) === true) {
186
+ return true;
187
+ }
188
+ // Priority 2: Explicit disable via enableBridgeRouter:false or disableAlias:true
189
+ if ((options === null || options === void 0 ? void 0 : (_options_bridge1 = options.bridge) === null || _options_bridge1 === void 0 ? void 0 : _options_bridge1.enableBridgeRouter) === false || (options === null || options === void 0 ? void 0 : (_options_bridge2 = options.bridge) === null || _options_bridge2 === void 0 ? void 0 : _options_bridge2.disableAlias) === true) {
190
+ var _options_bridge3;
191
+ if ((options === null || options === void 0 ? void 0 : (_options_bridge3 = options.bridge) === null || _options_bridge3 === void 0 ? void 0 : _options_bridge3.disableAlias) === true) {
192
+ logger.warn("\u26A0\uFE0F [ModuleFederationPlugin] The `disableAlias` option is deprecated and will be removed in a future version.\n" + ' Please use `enableBridgeRouter: false` instead:\n' + ' {\n' + ' bridge: {\n' + ' enableBridgeRouter: false // Use this instead of disableAlias: true\n' + ' }\n' + ' }');
193
+ }
194
+ return false;
195
+ }
196
+ // Priority 3: Automatic detection based on bridge-react installation
197
+ if (hasBridgeReact) {
198
+ logger.info("\uD83D\uDCA1 [ModuleFederationPlugin] Detected @module-federation/bridge-react in your dependencies.\n" + ' For better control and to avoid future breaking changes, please explicitly set:\n' + ' {\n' + ' bridge: {\n' + ' enableBridgeRouter: true // Explicitly enable bridge router\n' + ' }\n' + ' }');
199
+ return true;
200
+ }
201
+ return false;
202
+ };
203
+ const enableBridgePlugin = shouldEnableBridgePlugin();
204
+ // When bridge plugin is disabled (router disabled), alias to /base entry
205
+ if (!enableBridgePlugin && hasBridgeReact) {
206
+ compiler.hooks.afterPlugins.tap('BridgeReactBaseAliasPlugin', ()=>{
207
+ try {
208
+ const bridgeReactBasePath = node_path.resolve(compiler.context, 'node_modules/@module-federation/bridge-react/dist/base.es.js');
209
+ if (!node_fs.existsSync(bridgeReactBasePath)) {
210
+ logger.warn("\u26A0\uFE0F [ModuleFederationPlugin] bridge-react /base entry not found, falling back to default entry");
211
+ return;
212
+ }
213
+ compiler.options.resolve.alias = {
214
+ ...compiler.options.resolve.alias,
215
+ '@module-federation/bridge-react$': bridgeReactBasePath
216
+ };
217
+ logger.info("\u2705 [ModuleFederationPlugin] Router disabled - using /base entry (no react-router-dom)");
218
+ } catch (error) {
219
+ logger.warn("\u26A0\uFE0F [ModuleFederationPlugin] Failed to set /base alias, using default entry");
220
+ }
221
+ });
222
+ }
223
+ if (enableBridgePlugin) {
224
+ new bridge_react_webpack_plugin({
225
+ moduleFederationOptions: this._options
226
+ }).apply(compiler);
227
+ }
228
+ }
229
+ _patchChunkSplit(compiler, name) {
230
+ const { splitChunks } = compiler.options.optimization;
231
+ const patchChunkSplit = (cacheGroup)=>{
232
+ switch(typeof cacheGroup){
233
+ case 'boolean':
234
+ case 'string':
235
+ case 'function':
236
+ break;
237
+ // cacheGroup.chunks will inherit splitChunks.chunks, so you only need to modify the chunks that are set separately
238
+ case 'object':
239
+ {
240
+ if (cacheGroup instanceof RegExp) {
241
+ break;
242
+ }
243
+ if (!cacheGroup.chunks) {
244
+ break;
245
+ }
246
+ if (typeof cacheGroup.chunks === 'function') {
247
+ const prevChunks = cacheGroup.chunks;
248
+ cacheGroup.chunks = (chunk)=>{
249
+ if (chunk.name && (chunk.name === name || chunk.name === name + '_partial')) {
250
+ return false;
251
+ }
252
+ return prevChunks(chunk);
253
+ };
254
+ break;
255
+ }
256
+ if (cacheGroup.chunks === 'all') {
257
+ cacheGroup.chunks = (chunk)=>{
258
+ if (chunk.name && (chunk.name === name || chunk.name === name + '_partial')) {
259
+ return false;
260
+ }
261
+ return true;
262
+ };
263
+ break;
264
+ }
265
+ if (cacheGroup.chunks === 'initial') {
266
+ cacheGroup.chunks = (chunk)=>{
267
+ if (chunk.name && (chunk.name === name || chunk.name === name + '_partial')) {
268
+ return false;
269
+ }
270
+ return chunk.isOnlyInitial();
271
+ };
272
+ break;
273
+ }
274
+ break;
275
+ }
276
+ default:
277
+ break;
278
+ }
279
+ };
280
+ if (!splitChunks) {
281
+ return;
282
+ }
283
+ // 修改 splitChunk.chunks
284
+ patchChunkSplit(splitChunks);
285
+ const { cacheGroups } = splitChunks;
286
+ if (!cacheGroups) {
287
+ return;
288
+ }
289
+ // 修改 splitChunk.cacheGroups[key].chunks
290
+ Object.keys(cacheGroups).forEach((cacheGroupKey)=>{
291
+ patchChunkSplit(cacheGroups[cacheGroupKey]);
292
+ });
293
+ }
294
+ constructor(options){
295
+ this.name = PLUGIN_NAME;
296
+ this._options = options;
297
+ }
298
+ }
299
+ const GetPublicPathPlugin = RemoteEntryPlugin;
300
+
301
+
302
+ export { GetPublicPathPlugin, ModuleFederationPlugin, PLUGIN_NAME, TreeShakingSharedPlugin };