@module-federation/rspack 0.24.1 → 2.0.1

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/{src/ModuleFederationPlugin.d.ts → ModuleFederationPlugin.d.ts} +1 -1
  2. package/dist/ModuleFederationPlugin.js +362 -0
  3. package/dist/ModuleFederationPlugin.mjs +299 -0
  4. package/dist/RemoteEntryPlugin.js +131 -0
  5. package/dist/{RemoteEntryPlugin.esm.mjs → RemoteEntryPlugin.mjs} +23 -21
  6. package/dist/TreeShakingSharedPlugin.js +63 -0
  7. package/dist/TreeShakingSharedPlugin.mjs +15 -0
  8. package/dist/index.d.ts +3 -1
  9. package/dist/index.js +69 -0
  10. package/dist/index.mjs +14 -0
  11. package/dist/logger.js +62 -0
  12. package/dist/logger.mjs +12 -0
  13. package/dist/plugin.d.ts +1 -1
  14. package/dist/plugin.js +95 -0
  15. package/dist/plugin.mjs +5 -0
  16. package/dist/remote-entry-plugin.d.ts +1 -1
  17. package/dist/remote-entry-plugin.js +95 -0
  18. package/dist/remote-entry-plugin.mjs +5 -0
  19. package/package.json +34 -19
  20. package/dist/RemoteEntryPlugin.cjs.js +0 -70
  21. package/dist/RemoteEntryPlugin.cjs.js.map +0 -1
  22. package/dist/RemoteEntryPlugin.esm.mjs.map +0 -1
  23. package/dist/index.cjs.js +0 -13
  24. package/dist/index.cjs.js.map +0 -1
  25. package/dist/index.esm.d.ts +0 -1
  26. package/dist/index.esm.mjs +0 -8
  27. package/dist/index.esm.mjs.map +0 -1
  28. package/dist/plugin.cjs.js +0 -284
  29. package/dist/plugin.cjs.js.map +0 -1
  30. package/dist/plugin.esm.d.ts +0 -1
  31. package/dist/plugin.esm.mjs +0 -279
  32. package/dist/plugin.esm.mjs.map +0 -1
  33. package/dist/remote-entry-plugin.cjs.js +0 -10
  34. package/dist/remote-entry-plugin.cjs.js.map +0 -1
  35. package/dist/remote-entry-plugin.esm.d.ts +0 -1
  36. package/dist/remote-entry-plugin.esm.mjs +0 -4
  37. package/dist/remote-entry-plugin.esm.mjs.map +0 -1
  38. package/dist/src/index.d.ts +0 -3
  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
@@ -13,4 +13,4 @@ export declare class ModuleFederationPlugin implements RspackPluginInstance {
13
13
  private _patchChunkSplit;
14
14
  }
15
15
  export declare const GetPublicPathPlugin: typeof RemoteEntryPlugin;
16
- export { TreeShakingSharedPlugin } from './TreeShakingSharedPlugin';
16
+ export { TreeShakingSharedPlugin, type TreeShakingSharedPluginOptions, } from './TreeShakingSharedPlugin';
@@ -0,0 +1,362 @@
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
+ ]);
201
+ compiler.hooks.afterPlugins.tap('PatchAliasWebpackPlugin', ()=>{
202
+ compiler.options.resolve.alias = {
203
+ ...compiler.options.resolve.alias,
204
+ '@module-federation/runtime$': runtimeESMPath
205
+ };
206
+ });
207
+ if (!disableManifest) {
208
+ this._statsPlugin = new manifest_namespaceObject.StatsPlugin(options, {
209
+ pluginVersion: "2.0.1",
210
+ bundler: 'rspack'
211
+ });
212
+ // @ts-ignore
213
+ this._statsPlugin.apply(compiler);
214
+ }
215
+ const checkBridgeReactInstalled = ()=>{
216
+ try {
217
+ const userPackageJsonPath = external_node_path_default().resolve(compiler.context, 'package.json');
218
+ if (external_node_fs_default().existsSync(userPackageJsonPath)) {
219
+ const userPackageJson = JSON.parse(external_node_fs_default().readFileSync(userPackageJsonPath, 'utf-8'));
220
+ const userDependencies = {
221
+ ...userPackageJson.dependencies,
222
+ ...userPackageJson.devDependencies
223
+ };
224
+ return !!userDependencies['@module-federation/bridge-react'];
225
+ }
226
+ return false;
227
+ } catch (error) {
228
+ return false;
229
+ }
230
+ };
231
+ const hasBridgeReact = checkBridgeReactInstalled();
232
+ // react bridge plugin
233
+ const shouldEnableBridgePlugin = ()=>{
234
+ var _options_bridge, _options_bridge1, _options_bridge2;
235
+ // Priority 1: Explicit enableBridgeRouter configuration
236
+ if ((options === null || options === void 0 ? void 0 : (_options_bridge = options.bridge) === null || _options_bridge === void 0 ? void 0 : _options_bridge.enableBridgeRouter) === true) {
237
+ return true;
238
+ }
239
+ // Priority 2: Explicit disable via enableBridgeRouter:false or disableAlias:true
240
+ 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) {
241
+ var _options_bridge3;
242
+ if ((options === null || options === void 0 ? void 0 : (_options_bridge3 = options.bridge) === null || _options_bridge3 === void 0 ? void 0 : _options_bridge3.disableAlias) === true) {
243
+ 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' + ' }');
244
+ }
245
+ return false;
246
+ }
247
+ // Priority 3: Automatic detection based on bridge-react installation
248
+ if (hasBridgeReact) {
249
+ 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' + ' }');
250
+ return true;
251
+ }
252
+ return false;
253
+ };
254
+ const enableBridgePlugin = shouldEnableBridgePlugin();
255
+ // When bridge plugin is disabled (router disabled), alias to /base entry
256
+ if (!enableBridgePlugin && hasBridgeReact) {
257
+ compiler.hooks.afterPlugins.tap('BridgeReactBaseAliasPlugin', ()=>{
258
+ try {
259
+ const bridgeReactBasePath = external_node_path_default().resolve(compiler.context, 'node_modules/@module-federation/bridge-react/dist/base.es.js');
260
+ if (!external_node_fs_default().existsSync(bridgeReactBasePath)) {
261
+ external_logger_js_default().warn("\u26A0\uFE0F [ModuleFederationPlugin] bridge-react /base entry not found, falling back to default entry");
262
+ return;
263
+ }
264
+ compiler.options.resolve.alias = {
265
+ ...compiler.options.resolve.alias,
266
+ '@module-federation/bridge-react$': bridgeReactBasePath
267
+ };
268
+ external_logger_js_default().info("\u2705 [ModuleFederationPlugin] Router disabled - using /base entry (no react-router-dom)");
269
+ } catch (error) {
270
+ external_logger_js_default().warn("\u26A0\uFE0F [ModuleFederationPlugin] Failed to set /base alias, using default entry");
271
+ }
272
+ });
273
+ }
274
+ if (enableBridgePlugin) {
275
+ new (bridge_react_webpack_plugin_default())({
276
+ moduleFederationOptions: this._options
277
+ }).apply(compiler);
278
+ }
279
+ }
280
+ _patchChunkSplit(compiler, name) {
281
+ const { splitChunks } = compiler.options.optimization;
282
+ const patchChunkSplit = (cacheGroup)=>{
283
+ switch(typeof cacheGroup){
284
+ case 'boolean':
285
+ case 'string':
286
+ case 'function':
287
+ break;
288
+ // cacheGroup.chunks will inherit splitChunks.chunks, so you only need to modify the chunks that are set separately
289
+ case 'object':
290
+ {
291
+ if (cacheGroup instanceof RegExp) {
292
+ break;
293
+ }
294
+ if (!cacheGroup.chunks) {
295
+ break;
296
+ }
297
+ if (typeof cacheGroup.chunks === 'function') {
298
+ const prevChunks = cacheGroup.chunks;
299
+ cacheGroup.chunks = (chunk)=>{
300
+ if (chunk.name && (chunk.name === name || chunk.name === name + '_partial')) {
301
+ return false;
302
+ }
303
+ return prevChunks(chunk);
304
+ };
305
+ break;
306
+ }
307
+ if (cacheGroup.chunks === 'all') {
308
+ cacheGroup.chunks = (chunk)=>{
309
+ if (chunk.name && (chunk.name === name || chunk.name === name + '_partial')) {
310
+ return false;
311
+ }
312
+ return true;
313
+ };
314
+ break;
315
+ }
316
+ if (cacheGroup.chunks === 'initial') {
317
+ cacheGroup.chunks = (chunk)=>{
318
+ if (chunk.name && (chunk.name === name || chunk.name === name + '_partial')) {
319
+ return false;
320
+ }
321
+ return chunk.isOnlyInitial();
322
+ };
323
+ break;
324
+ }
325
+ break;
326
+ }
327
+ default:
328
+ break;
329
+ }
330
+ };
331
+ if (!splitChunks) {
332
+ return;
333
+ }
334
+ // 修改 splitChunk.chunks
335
+ patchChunkSplit(splitChunks);
336
+ const { cacheGroups } = splitChunks;
337
+ if (!cacheGroups) {
338
+ return;
339
+ }
340
+ // 修改 splitChunk.cacheGroups[key].chunks
341
+ Object.keys(cacheGroups).forEach((cacheGroupKey)=>{
342
+ patchChunkSplit(cacheGroups[cacheGroupKey]);
343
+ });
344
+ }
345
+ constructor(options){
346
+ this.name = PLUGIN_NAME;
347
+ this._options = options;
348
+ }
349
+ }
350
+ const GetPublicPathPlugin = external_RemoteEntryPlugin_js_namespaceObject.RemoteEntryPlugin;
351
+
352
+
353
+ exports.GetPublicPathPlugin = __webpack_exports__.GetPublicPathPlugin;
354
+ exports.ModuleFederationPlugin = __webpack_exports__.ModuleFederationPlugin;
355
+ exports.PLUGIN_NAME = __webpack_exports__.PLUGIN_NAME;
356
+ exports.TreeShakingSharedPlugin = __webpack_exports__.TreeShakingSharedPlugin;
357
+ for(var __webpack_i__ in __webpack_exports__) {
358
+ if(["GetPublicPathPlugin","ModuleFederationPlugin","PLUGIN_NAME","TreeShakingSharedPlugin"].indexOf(__webpack_i__) === -1) {
359
+ exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
360
+ }
361
+ }
362
+ Object.defineProperty(exports, '__esModule', { value: true });
@@ -0,0 +1,299 @@
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
+ ]);
147
+ compiler.hooks.afterPlugins.tap('PatchAliasWebpackPlugin', ()=>{
148
+ compiler.options.resolve.alias = {
149
+ ...compiler.options.resolve.alias,
150
+ '@module-federation/runtime$': runtimeESMPath
151
+ };
152
+ });
153
+ if (!disableManifest) {
154
+ this._statsPlugin = new StatsPlugin(options, {
155
+ pluginVersion: "2.0.1",
156
+ bundler: 'rspack'
157
+ });
158
+ // @ts-ignore
159
+ this._statsPlugin.apply(compiler);
160
+ }
161
+ const checkBridgeReactInstalled = ()=>{
162
+ try {
163
+ const userPackageJsonPath = node_path.resolve(compiler.context, 'package.json');
164
+ if (node_fs.existsSync(userPackageJsonPath)) {
165
+ const userPackageJson = JSON.parse(node_fs.readFileSync(userPackageJsonPath, 'utf-8'));
166
+ const userDependencies = {
167
+ ...userPackageJson.dependencies,
168
+ ...userPackageJson.devDependencies
169
+ };
170
+ return !!userDependencies['@module-federation/bridge-react'];
171
+ }
172
+ return false;
173
+ } catch (error) {
174
+ return false;
175
+ }
176
+ };
177
+ const hasBridgeReact = checkBridgeReactInstalled();
178
+ // react bridge plugin
179
+ const shouldEnableBridgePlugin = ()=>{
180
+ var _options_bridge, _options_bridge1, _options_bridge2;
181
+ // Priority 1: Explicit enableBridgeRouter configuration
182
+ if ((options === null || options === void 0 ? void 0 : (_options_bridge = options.bridge) === null || _options_bridge === void 0 ? void 0 : _options_bridge.enableBridgeRouter) === true) {
183
+ return true;
184
+ }
185
+ // Priority 2: Explicit disable via enableBridgeRouter:false or disableAlias:true
186
+ 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) {
187
+ var _options_bridge3;
188
+ if ((options === null || options === void 0 ? void 0 : (_options_bridge3 = options.bridge) === null || _options_bridge3 === void 0 ? void 0 : _options_bridge3.disableAlias) === true) {
189
+ 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' + ' }');
190
+ }
191
+ return false;
192
+ }
193
+ // Priority 3: Automatic detection based on bridge-react installation
194
+ if (hasBridgeReact) {
195
+ 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' + ' }');
196
+ return true;
197
+ }
198
+ return false;
199
+ };
200
+ const enableBridgePlugin = shouldEnableBridgePlugin();
201
+ // When bridge plugin is disabled (router disabled), alias to /base entry
202
+ if (!enableBridgePlugin && hasBridgeReact) {
203
+ compiler.hooks.afterPlugins.tap('BridgeReactBaseAliasPlugin', ()=>{
204
+ try {
205
+ const bridgeReactBasePath = node_path.resolve(compiler.context, 'node_modules/@module-federation/bridge-react/dist/base.es.js');
206
+ if (!node_fs.existsSync(bridgeReactBasePath)) {
207
+ logger.warn("\u26A0\uFE0F [ModuleFederationPlugin] bridge-react /base entry not found, falling back to default entry");
208
+ return;
209
+ }
210
+ compiler.options.resolve.alias = {
211
+ ...compiler.options.resolve.alias,
212
+ '@module-federation/bridge-react$': bridgeReactBasePath
213
+ };
214
+ logger.info("\u2705 [ModuleFederationPlugin] Router disabled - using /base entry (no react-router-dom)");
215
+ } catch (error) {
216
+ logger.warn("\u26A0\uFE0F [ModuleFederationPlugin] Failed to set /base alias, using default entry");
217
+ }
218
+ });
219
+ }
220
+ if (enableBridgePlugin) {
221
+ new bridge_react_webpack_plugin({
222
+ moduleFederationOptions: this._options
223
+ }).apply(compiler);
224
+ }
225
+ }
226
+ _patchChunkSplit(compiler, name) {
227
+ const { splitChunks } = compiler.options.optimization;
228
+ const patchChunkSplit = (cacheGroup)=>{
229
+ switch(typeof cacheGroup){
230
+ case 'boolean':
231
+ case 'string':
232
+ case 'function':
233
+ break;
234
+ // cacheGroup.chunks will inherit splitChunks.chunks, so you only need to modify the chunks that are set separately
235
+ case 'object':
236
+ {
237
+ if (cacheGroup instanceof RegExp) {
238
+ break;
239
+ }
240
+ if (!cacheGroup.chunks) {
241
+ break;
242
+ }
243
+ if (typeof cacheGroup.chunks === 'function') {
244
+ const prevChunks = cacheGroup.chunks;
245
+ cacheGroup.chunks = (chunk)=>{
246
+ if (chunk.name && (chunk.name === name || chunk.name === name + '_partial')) {
247
+ return false;
248
+ }
249
+ return prevChunks(chunk);
250
+ };
251
+ break;
252
+ }
253
+ if (cacheGroup.chunks === 'all') {
254
+ cacheGroup.chunks = (chunk)=>{
255
+ if (chunk.name && (chunk.name === name || chunk.name === name + '_partial')) {
256
+ return false;
257
+ }
258
+ return true;
259
+ };
260
+ break;
261
+ }
262
+ if (cacheGroup.chunks === 'initial') {
263
+ cacheGroup.chunks = (chunk)=>{
264
+ if (chunk.name && (chunk.name === name || chunk.name === name + '_partial')) {
265
+ return false;
266
+ }
267
+ return chunk.isOnlyInitial();
268
+ };
269
+ break;
270
+ }
271
+ break;
272
+ }
273
+ default:
274
+ break;
275
+ }
276
+ };
277
+ if (!splitChunks) {
278
+ return;
279
+ }
280
+ // 修改 splitChunk.chunks
281
+ patchChunkSplit(splitChunks);
282
+ const { cacheGroups } = splitChunks;
283
+ if (!cacheGroups) {
284
+ return;
285
+ }
286
+ // 修改 splitChunk.cacheGroups[key].chunks
287
+ Object.keys(cacheGroups).forEach((cacheGroupKey)=>{
288
+ patchChunkSplit(cacheGroups[cacheGroupKey]);
289
+ });
290
+ }
291
+ constructor(options){
292
+ this.name = PLUGIN_NAME;
293
+ this._options = options;
294
+ }
295
+ }
296
+ const GetPublicPathPlugin = RemoteEntryPlugin;
297
+
298
+
299
+ export { GetPublicPathPlugin, ModuleFederationPlugin, PLUGIN_NAME, TreeShakingSharedPlugin };