@module-federation/rsbuild-plugin 0.0.0-next-20241224035007 → 0.0.0-next-20241224092514

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/index.cjs.js CHANGED
@@ -3,11 +3,8 @@
3
3
  var enhanced = require('@module-federation/enhanced');
4
4
  var rspack = require('@module-federation/enhanced/rspack');
5
5
  var sdk = require('@module-federation/sdk');
6
- var util = require('util');
7
-
8
- function isRegExp(target) {
9
- return util.types.isRegExp(target);
10
- }
6
+ var utils = require('./utils.cjs.js');
7
+ require('util');
11
8
 
12
9
  var name = "@module-federation/rsbuild-plugin";
13
10
  var version = "0.8.5";
@@ -24,9 +21,14 @@ var repository = {
24
21
  var license = "MIT";
25
22
  var exports$1 = {
26
23
  ".": {
24
+ types: "./dist/index.cjs.d.ts",
27
25
  "import": "./dist/index.esm.js",
28
- require: "./dist/index.cjs.js",
29
- types: "./dist/index.cjs.d.ts"
26
+ require: "./dist/index.cjs.js"
27
+ },
28
+ "./utils": {
29
+ types: "./dist/utils.cjs.d.ts",
30
+ "import": "./dist/utils.esm.js",
31
+ require: "./dist/utils.cjs.js"
30
32
  }
31
33
  };
32
34
  var main = "./dist/index.cjs.js";
@@ -35,6 +37,9 @@ var typesVersions = {
35
37
  "*": {
36
38
  ".": [
37
39
  "./dist/index.cjs.d.ts"
40
+ ],
41
+ utils: [
42
+ "./dist/utils.cjs.d.ts"
38
43
  ]
39
44
  }
40
45
  };
@@ -89,91 +94,19 @@ function _array_like_to_array(arr, len) {
89
94
  function _array_without_holes(arr) {
90
95
  if (Array.isArray(arr)) return _array_like_to_array(arr);
91
96
  }
92
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
93
- try {
94
- var info = gen[key](arg);
95
- var value = info.value;
96
- } catch (error) {
97
- reject(error);
98
- return;
99
- }
100
- if (info.done) {
101
- resolve(value);
102
- } else {
103
- Promise.resolve(value).then(_next, _throw);
104
- }
105
- }
106
- function _async_to_generator(fn) {
107
- return function() {
108
- var self = this, args = arguments;
109
- return new Promise(function(resolve, reject) {
110
- var gen = fn.apply(self, args);
111
- function _next(value) {
112
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
113
- }
114
- function _throw(err) {
115
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
116
- }
117
- _next(undefined);
118
- });
119
- };
120
- }
121
- function _define_property(obj, key, value) {
122
- if (key in obj) {
123
- Object.defineProperty(obj, key, {
124
- value: value,
125
- enumerable: true,
126
- configurable: true,
127
- writable: true
128
- });
129
- } else {
130
- obj[key] = value;
131
- }
132
- return obj;
133
- }
134
97
  function _iterable_to_array(iter) {
135
98
  if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
136
99
  }
137
100
  function _non_iterable_spread() {
138
101
  throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
139
102
  }
140
- function _object_spread(target) {
141
- for(var i = 1; i < arguments.length; i++){
142
- var source = arguments[i] != null ? arguments[i] : {};
143
- var ownKeys = Object.keys(source);
144
- if (typeof Object.getOwnPropertySymbols === "function") {
145
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
146
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
147
- }));
148
- }
149
- ownKeys.forEach(function(key) {
150
- _define_property(target, key, source[key]);
151
- });
152
- }
153
- return target;
154
- }
155
- function ownKeys(object, enumerableOnly) {
156
- var keys = Object.keys(object);
157
- if (Object.getOwnPropertySymbols) {
158
- var symbols = Object.getOwnPropertySymbols(object);
159
- keys.push.apply(keys, symbols);
160
- }
161
- return keys;
162
- }
163
- function _object_spread_props(target, source) {
164
- source = source != null ? source : {};
165
- if (Object.getOwnPropertyDescriptors) {
166
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
167
- } else {
168
- ownKeys(Object(source)).forEach(function(key) {
169
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
170
- });
171
- }
172
- return target;
173
- }
174
103
  function _to_consumable_array(arr) {
175
104
  return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
176
105
  }
106
+ function _type_of(obj) {
107
+ "@swc/helpers - typeof";
108
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
109
+ }
177
110
  function _unsupported_iterable_to_array(o, minLen) {
178
111
  if (!o) return;
179
112
  if (typeof o === "string") return _array_like_to_array(o, minLen);
@@ -182,105 +115,16 @@ function _unsupported_iterable_to_array(o, minLen) {
182
115
  if (n === "Map" || n === "Set") return Array.from(n);
183
116
  if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
184
117
  }
185
- function _ts_generator(thisArg, body) {
186
- var f, y, t, g, _ = {
187
- label: 0,
188
- sent: function() {
189
- if (t[0] & 1) throw t[1];
190
- return t[1];
191
- },
192
- trys: [],
193
- ops: []
194
- };
195
- return g = {
196
- next: verb(0),
197
- "throw": verb(1),
198
- "return": verb(2)
199
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
200
- return this;
201
- }), g;
202
- function verb(n) {
203
- return function(v) {
204
- return step([
205
- n,
206
- v
207
- ]);
208
- };
209
- }
210
- function step(op) {
211
- if (f) throw new TypeError("Generator is already executing.");
212
- while(_)try {
213
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
214
- if (y = 0, t) op = [
215
- op[0] & 2,
216
- t.value
217
- ];
218
- switch(op[0]){
219
- case 0:
220
- case 1:
221
- t = op;
222
- break;
223
- case 4:
224
- _.label++;
225
- return {
226
- value: op[1],
227
- done: false
228
- };
229
- case 5:
230
- _.label++;
231
- y = op[1];
232
- op = [
233
- 0
234
- ];
235
- continue;
236
- case 7:
237
- op = _.ops.pop();
238
- _.trys.pop();
239
- continue;
240
- default:
241
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
242
- _ = 0;
243
- continue;
244
- }
245
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
246
- _.label = op[1];
247
- break;
248
- }
249
- if (op[0] === 6 && _.label < t[1]) {
250
- _.label = t[1];
251
- t = op;
252
- break;
253
- }
254
- if (t && _.label < t[2]) {
255
- _.label = t[2];
256
- _.ops.push(op);
257
- break;
258
- }
259
- if (t[2]) _.ops.pop();
260
- _.trys.pop();
261
- continue;
262
- }
263
- op = body.call(thisArg, _);
264
- } catch (e) {
265
- op = [
266
- 6,
267
- e
268
- ];
269
- y = 0;
270
- } finally{
271
- f = t = 0;
272
- }
273
- if (op[0] & 5) throw op[1];
274
- return {
275
- value: op[0] ? op[1] : void 0,
276
- done: true
277
- };
278
- }
279
- }
280
- var PLUGIN_MODULE_FEDERATION_NAME = 'rsbuild:module-federation-enhanced';
118
+ var RSBUILD_PLUGIN_MODULE_FEDERATION_NAME = 'rsbuild:module-federation-enhanced';
119
+ var RSPACK_PLUGIN_MODULE_FEDERATION_NAME = 'module-federation';
120
+ var LIB_FORMAT = [
121
+ 'commonjs',
122
+ 'umd',
123
+ 'modern-module'
124
+ ];
281
125
  var pluginModuleFederation = function(moduleFederationOptions) {
282
126
  return {
283
- name: PLUGIN_MODULE_FEDERATION_NAME,
127
+ name: RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
284
128
  setup: function(api) {
285
129
  var sharedOptions = enhanced.parseOptions(moduleFederationOptions.shared || [], function(item, key) {
286
130
  if (typeof item !== 'string') throw new Error('Unexpected array in shared');
@@ -298,6 +142,91 @@ var pluginModuleFederation = function(moduleFederationOptions) {
298
142
  var shared = sharedOptions.map(function(shared) {
299
143
  return shared[0].endsWith('/') ? shared[0].slice(0, -1) : shared[0];
300
144
  });
145
+ api.onBeforeCreateCompiler(function(param) {
146
+ var bundlerConfigs = param.bundlerConfigs;
147
+ bundlerConfigs.forEach(function(bundlerConfig) {
148
+ var _bundlerConfig_output;
149
+ var library = (_bundlerConfig_output = bundlerConfig.output) === null || _bundlerConfig_output === void 0 ? void 0 : _bundlerConfig_output.library;
150
+ // lib format
151
+ if ((typeof library === "undefined" ? "undefined" : _type_of(library)) === 'object' && !Array.isArray(library) && 'type' in library && LIB_FORMAT.includes(library.type)) {
152
+ return;
153
+ } else {
154
+ var _bundlerConfig_output1, _bundlerConfig_output2;
155
+ // mf
156
+ utils.autoDeleteSplitChunkCacheGroups(moduleFederationOptions, bundlerConfig);
157
+ var externals = bundlerConfig.externals;
158
+ if (Array.isArray(externals)) {
159
+ var sharedModules = new Set();
160
+ bundlerConfig.externals = externals.filter(function(ext) {
161
+ var sharedModule;
162
+ if (utils.isRegExp(ext)) {
163
+ var match = shared.some(function(dep) {
164
+ if (ext.test(dep) || ext.test(pkgJson.name)) {
165
+ sharedModule = dep;
166
+ return true;
167
+ }
168
+ return false;
169
+ });
170
+ match && sharedModule && sharedModules.add(sharedModule);
171
+ return !match;
172
+ }
173
+ if (typeof ext === 'string') {
174
+ if (ext === pkgJson.name) {
175
+ return false;
176
+ }
177
+ var match1 = shared.some(function(dep) {
178
+ if (dep === ext) {
179
+ sharedModule = dep;
180
+ }
181
+ return dep === ext;
182
+ });
183
+ if (match1) {
184
+ sharedModule && sharedModules.add(sharedModule);
185
+ return false;
186
+ }
187
+ return true;
188
+ }
189
+ return true;
190
+ });
191
+ if (sharedModules.size > 0) {
192
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
193
+ try {
194
+ for(var _iterator = sharedModules[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
195
+ var sharedModule = _step.value;
196
+ logger.log("".concat(sharedModule, " is removed from externals because it is a shared module."));
197
+ }
198
+ } catch (err) {
199
+ _didIteratorError = true;
200
+ _iteratorError = err;
201
+ } finally{
202
+ try {
203
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
204
+ _iterator.return();
205
+ }
206
+ } finally{
207
+ if (_didIteratorError) {
208
+ throw _iteratorError;
209
+ }
210
+ }
211
+ }
212
+ }
213
+ }
214
+ if (!((_bundlerConfig_output1 = bundlerConfig.output) === null || _bundlerConfig_output1 === void 0 ? void 0 : _bundlerConfig_output1.chunkLoadingGlobal)) {
215
+ bundlerConfig.output.chunkLoadingGlobal = 'jsonp';
216
+ }
217
+ // `uniqueName` is required for react refresh to work
218
+ if (!((_bundlerConfig_output2 = bundlerConfig.output) === null || _bundlerConfig_output2 === void 0 ? void 0 : _bundlerConfig_output2.uniqueName)) {
219
+ bundlerConfig.output.uniqueName = moduleFederationOptions.name;
220
+ }
221
+ if (!bundlerConfig.plugins.find(function(p) {
222
+ return p && p.name === rspack.PLUGIN_NAME;
223
+ })) {
224
+ bundlerConfig.plugins.push(new rspack.ModuleFederationPlugin(moduleFederationOptions));
225
+ }
226
+ }
227
+ });
228
+ });
229
+ // dev config only works on format: 'mf'
301
230
  api.modifyRsbuildConfig(function(config) {
302
231
  // Change some default configs for remote modules
303
232
  if (moduleFederationOptions.exposes) {
@@ -320,113 +249,24 @@ var pluginModuleFederation = function(moduleFederationOptions) {
320
249
  }
321
250
  });
322
251
  api.modifyEnvironmentConfig(function(config, param) {
323
- var mergeEnvironmentConfig = param.mergeEnvironmentConfig;
324
- var _config_performance_chunkSplit, _config_performance;
325
- /**
326
- * Currently, splitChunks will take precedence over module federation shared modules.
327
- * So we need to disable the default split chunks rules to make shared modules to work properly.
328
- * @see https://github.com/module-federation/module-federation-examples/issues/3161
329
- */ if (((_config_performance = config.performance) === null || _config_performance === void 0 ? void 0 : (_config_performance_chunkSplit = _config_performance.chunkSplit) === null || _config_performance_chunkSplit === void 0 ? void 0 : _config_performance_chunkSplit.strategy) === 'split-by-experience') {
330
- config.performance.chunkSplit = _object_spread_props(_object_spread({}, config.performance.chunkSplit), {
331
- strategy: 'custom'
332
- });
333
- }
252
+ param.mergeEnvironmentConfig;
334
253
  // Module Federation runtime uses ES6+ syntax,
335
254
  // adding to include and let SWC transform it
336
255
  config.source.include = _to_consumable_array(config.source.include || []).concat([
337
256
  /@module-federation[\\/]sdk/,
338
257
  /@module-federation[\\/]runtime/
339
258
  ]);
340
- // filter external with shared config,
341
- var externals = config.output.externals;
342
- if (Array.isArray(externals)) {
343
- var sharedModules = new Set();
344
- config.output.externals = externals.filter(function(ext) {
345
- var sharedModule;
346
- if (isRegExp(ext)) {
347
- var match = shared.some(function(dep) {
348
- if (ext.test(dep) || ext.test(pkgJson.name)) {
349
- sharedModule = dep;
350
- return true;
351
- }
352
- return false;
353
- });
354
- match && sharedModule && sharedModules.add(sharedModule);
355
- return !match;
356
- }
357
- if (typeof ext === 'string') {
358
- if (ext === pkgJson.name) {
359
- return false;
360
- }
361
- var match1 = shared.some(function(dep) {
362
- if (dep === ext) {
363
- sharedModule = dep;
364
- }
365
- return dep === ext;
366
- });
367
- if (match1) {
368
- sharedModule && sharedModules.add(sharedModule);
369
- return false;
370
- }
371
- return true;
372
- }
373
- return true;
374
- });
375
- if (sharedModules.size > 0) {
376
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
377
- try {
378
- for(var _iterator = sharedModules[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
379
- var sharedModule = _step.value;
380
- logger.log("".concat(sharedModule, " is removed from externals because it is a shared module."));
381
- }
382
- } catch (err) {
383
- _didIteratorError = true;
384
- _iteratorError = err;
385
- } finally{
386
- try {
387
- if (!_iteratorNormalCompletion && _iterator.return != null) {
388
- _iterator.return();
389
- }
390
- } finally{
391
- if (_didIteratorError) {
392
- throw _iteratorError;
393
- }
394
- }
395
- }
396
- }
397
- }
398
- var mfConfig = {
399
- tools: {
400
- rspack: {
401
- output: {
402
- chunkLoading: 'jsonp'
403
- }
404
- }
405
- }
406
- };
407
- return mergeEnvironmentConfig(config, mfConfig);
259
+ return config;
408
260
  });
409
- api.modifyBundlerChain(/*#__PURE__*/ function() {
410
- var _ref = _async_to_generator(function(chain) {
411
- return _ts_generator(this, function(_state) {
412
- chain.plugin('module-federation').use(rspack.ModuleFederationPlugin, [
413
- moduleFederationOptions
414
- ]);
415
- // `uniqueName` is required for react refresh to work
416
- if (!chain.output.get('uniqueName')) {
417
- chain.output.set('uniqueName', moduleFederationOptions.name);
418
- }
419
- return [
420
- 2
421
- ];
422
- });
423
- });
424
- return function(chain) {
425
- return _ref.apply(this, arguments);
426
- };
427
- }());
428
261
  }
429
262
  };
430
263
  };
431
264
 
265
+ Object.defineProperty(exports, "PLUGIN_NAME", {
266
+ enumerable: true,
267
+ get: function () { return rspack.PLUGIN_NAME; }
268
+ });
269
+ exports.LIB_FORMAT = LIB_FORMAT;
270
+ exports.RSBUILD_PLUGIN_MODULE_FEDERATION_NAME = RSBUILD_PLUGIN_MODULE_FEDERATION_NAME;
271
+ exports.RSPACK_PLUGIN_MODULE_FEDERATION_NAME = RSPACK_PLUGIN_MODULE_FEDERATION_NAME;
432
272
  exports.pluginModuleFederation = pluginModuleFederation;
package/dist/index.esm.js CHANGED
@@ -1,11 +1,9 @@
1
1
  import { parseOptions } from '@module-federation/enhanced';
2
- import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack';
2
+ import { PLUGIN_NAME, ModuleFederationPlugin } from '@module-federation/enhanced/rspack';
3
+ export { PLUGIN_NAME } from '@module-federation/enhanced/rspack';
3
4
  import { createLogger, isRequiredVersion } from '@module-federation/sdk';
4
- import util from 'util';
5
-
6
- function isRegExp(target) {
7
- return util.types.isRegExp(target);
8
- }
5
+ import { autoDeleteSplitChunkCacheGroups, isRegExp } from './utils.esm.js';
6
+ import 'util';
9
7
 
10
8
  var name = "@module-federation/rsbuild-plugin";
11
9
  var version = "0.8.5";
@@ -22,9 +20,14 @@ var repository = {
22
20
  var license = "MIT";
23
21
  var exports = {
24
22
  ".": {
23
+ types: "./dist/index.cjs.d.ts",
25
24
  "import": "./dist/index.esm.js",
26
- require: "./dist/index.cjs.js",
27
- types: "./dist/index.cjs.d.ts"
25
+ require: "./dist/index.cjs.js"
26
+ },
27
+ "./utils": {
28
+ types: "./dist/utils.cjs.d.ts",
29
+ "import": "./dist/utils.esm.js",
30
+ require: "./dist/utils.cjs.js"
28
31
  }
29
32
  };
30
33
  var main = "./dist/index.cjs.js";
@@ -33,6 +36,9 @@ var typesVersions = {
33
36
  "*": {
34
37
  ".": [
35
38
  "./dist/index.cjs.d.ts"
39
+ ],
40
+ utils: [
41
+ "./dist/utils.cjs.d.ts"
36
42
  ]
37
43
  }
38
44
  };
@@ -87,91 +93,19 @@ function _array_like_to_array(arr, len) {
87
93
  function _array_without_holes(arr) {
88
94
  if (Array.isArray(arr)) return _array_like_to_array(arr);
89
95
  }
90
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
91
- try {
92
- var info = gen[key](arg);
93
- var value = info.value;
94
- } catch (error) {
95
- reject(error);
96
- return;
97
- }
98
- if (info.done) {
99
- resolve(value);
100
- } else {
101
- Promise.resolve(value).then(_next, _throw);
102
- }
103
- }
104
- function _async_to_generator(fn) {
105
- return function() {
106
- var self = this, args = arguments;
107
- return new Promise(function(resolve, reject) {
108
- var gen = fn.apply(self, args);
109
- function _next(value) {
110
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
111
- }
112
- function _throw(err) {
113
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
114
- }
115
- _next(undefined);
116
- });
117
- };
118
- }
119
- function _define_property(obj, key, value) {
120
- if (key in obj) {
121
- Object.defineProperty(obj, key, {
122
- value: value,
123
- enumerable: true,
124
- configurable: true,
125
- writable: true
126
- });
127
- } else {
128
- obj[key] = value;
129
- }
130
- return obj;
131
- }
132
96
  function _iterable_to_array(iter) {
133
97
  if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
134
98
  }
135
99
  function _non_iterable_spread() {
136
100
  throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
137
101
  }
138
- function _object_spread(target) {
139
- for(var i = 1; i < arguments.length; i++){
140
- var source = arguments[i] != null ? arguments[i] : {};
141
- var ownKeys = Object.keys(source);
142
- if (typeof Object.getOwnPropertySymbols === "function") {
143
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
144
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
145
- }));
146
- }
147
- ownKeys.forEach(function(key) {
148
- _define_property(target, key, source[key]);
149
- });
150
- }
151
- return target;
152
- }
153
- function ownKeys(object, enumerableOnly) {
154
- var keys = Object.keys(object);
155
- if (Object.getOwnPropertySymbols) {
156
- var symbols = Object.getOwnPropertySymbols(object);
157
- keys.push.apply(keys, symbols);
158
- }
159
- return keys;
160
- }
161
- function _object_spread_props(target, source) {
162
- source = source != null ? source : {};
163
- if (Object.getOwnPropertyDescriptors) {
164
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
165
- } else {
166
- ownKeys(Object(source)).forEach(function(key) {
167
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
168
- });
169
- }
170
- return target;
171
- }
172
102
  function _to_consumable_array(arr) {
173
103
  return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
174
104
  }
105
+ function _type_of(obj) {
106
+ "@swc/helpers - typeof";
107
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
108
+ }
175
109
  function _unsupported_iterable_to_array(o, minLen) {
176
110
  if (!o) return;
177
111
  if (typeof o === "string") return _array_like_to_array(o, minLen);
@@ -180,105 +114,16 @@ function _unsupported_iterable_to_array(o, minLen) {
180
114
  if (n === "Map" || n === "Set") return Array.from(n);
181
115
  if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
182
116
  }
183
- function _ts_generator(thisArg, body) {
184
- var f, y, t, g, _ = {
185
- label: 0,
186
- sent: function() {
187
- if (t[0] & 1) throw t[1];
188
- return t[1];
189
- },
190
- trys: [],
191
- ops: []
192
- };
193
- return g = {
194
- next: verb(0),
195
- "throw": verb(1),
196
- "return": verb(2)
197
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
198
- return this;
199
- }), g;
200
- function verb(n) {
201
- return function(v) {
202
- return step([
203
- n,
204
- v
205
- ]);
206
- };
207
- }
208
- function step(op) {
209
- if (f) throw new TypeError("Generator is already executing.");
210
- while(_)try {
211
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
212
- if (y = 0, t) op = [
213
- op[0] & 2,
214
- t.value
215
- ];
216
- switch(op[0]){
217
- case 0:
218
- case 1:
219
- t = op;
220
- break;
221
- case 4:
222
- _.label++;
223
- return {
224
- value: op[1],
225
- done: false
226
- };
227
- case 5:
228
- _.label++;
229
- y = op[1];
230
- op = [
231
- 0
232
- ];
233
- continue;
234
- case 7:
235
- op = _.ops.pop();
236
- _.trys.pop();
237
- continue;
238
- default:
239
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
240
- _ = 0;
241
- continue;
242
- }
243
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
244
- _.label = op[1];
245
- break;
246
- }
247
- if (op[0] === 6 && _.label < t[1]) {
248
- _.label = t[1];
249
- t = op;
250
- break;
251
- }
252
- if (t && _.label < t[2]) {
253
- _.label = t[2];
254
- _.ops.push(op);
255
- break;
256
- }
257
- if (t[2]) _.ops.pop();
258
- _.trys.pop();
259
- continue;
260
- }
261
- op = body.call(thisArg, _);
262
- } catch (e) {
263
- op = [
264
- 6,
265
- e
266
- ];
267
- y = 0;
268
- } finally{
269
- f = t = 0;
270
- }
271
- if (op[0] & 5) throw op[1];
272
- return {
273
- value: op[0] ? op[1] : void 0,
274
- done: true
275
- };
276
- }
277
- }
278
- var PLUGIN_MODULE_FEDERATION_NAME = 'rsbuild:module-federation-enhanced';
117
+ var RSBUILD_PLUGIN_MODULE_FEDERATION_NAME = 'rsbuild:module-federation-enhanced';
118
+ var RSPACK_PLUGIN_MODULE_FEDERATION_NAME = 'module-federation';
119
+ var LIB_FORMAT = [
120
+ 'commonjs',
121
+ 'umd',
122
+ 'modern-module'
123
+ ];
279
124
  var pluginModuleFederation = function(moduleFederationOptions) {
280
125
  return {
281
- name: PLUGIN_MODULE_FEDERATION_NAME,
126
+ name: RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
282
127
  setup: function(api) {
283
128
  var sharedOptions = parseOptions(moduleFederationOptions.shared || [], function(item, key) {
284
129
  if (typeof item !== 'string') throw new Error('Unexpected array in shared');
@@ -296,6 +141,91 @@ var pluginModuleFederation = function(moduleFederationOptions) {
296
141
  var shared = sharedOptions.map(function(shared) {
297
142
  return shared[0].endsWith('/') ? shared[0].slice(0, -1) : shared[0];
298
143
  });
144
+ api.onBeforeCreateCompiler(function(param) {
145
+ var bundlerConfigs = param.bundlerConfigs;
146
+ bundlerConfigs.forEach(function(bundlerConfig) {
147
+ var _bundlerConfig_output;
148
+ var library = (_bundlerConfig_output = bundlerConfig.output) === null || _bundlerConfig_output === void 0 ? void 0 : _bundlerConfig_output.library;
149
+ // lib format
150
+ if ((typeof library === "undefined" ? "undefined" : _type_of(library)) === 'object' && !Array.isArray(library) && 'type' in library && LIB_FORMAT.includes(library.type)) {
151
+ return;
152
+ } else {
153
+ var _bundlerConfig_output1, _bundlerConfig_output2;
154
+ // mf
155
+ autoDeleteSplitChunkCacheGroups(moduleFederationOptions, bundlerConfig);
156
+ var externals = bundlerConfig.externals;
157
+ if (Array.isArray(externals)) {
158
+ var sharedModules = new Set();
159
+ bundlerConfig.externals = externals.filter(function(ext) {
160
+ var sharedModule;
161
+ if (isRegExp(ext)) {
162
+ var match = shared.some(function(dep) {
163
+ if (ext.test(dep) || ext.test(pkgJson.name)) {
164
+ sharedModule = dep;
165
+ return true;
166
+ }
167
+ return false;
168
+ });
169
+ match && sharedModule && sharedModules.add(sharedModule);
170
+ return !match;
171
+ }
172
+ if (typeof ext === 'string') {
173
+ if (ext === pkgJson.name) {
174
+ return false;
175
+ }
176
+ var match1 = shared.some(function(dep) {
177
+ if (dep === ext) {
178
+ sharedModule = dep;
179
+ }
180
+ return dep === ext;
181
+ });
182
+ if (match1) {
183
+ sharedModule && sharedModules.add(sharedModule);
184
+ return false;
185
+ }
186
+ return true;
187
+ }
188
+ return true;
189
+ });
190
+ if (sharedModules.size > 0) {
191
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
192
+ try {
193
+ for(var _iterator = sharedModules[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
194
+ var sharedModule = _step.value;
195
+ logger.log("".concat(sharedModule, " is removed from externals because it is a shared module."));
196
+ }
197
+ } catch (err) {
198
+ _didIteratorError = true;
199
+ _iteratorError = err;
200
+ } finally{
201
+ try {
202
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
203
+ _iterator.return();
204
+ }
205
+ } finally{
206
+ if (_didIteratorError) {
207
+ throw _iteratorError;
208
+ }
209
+ }
210
+ }
211
+ }
212
+ }
213
+ if (!((_bundlerConfig_output1 = bundlerConfig.output) === null || _bundlerConfig_output1 === void 0 ? void 0 : _bundlerConfig_output1.chunkLoadingGlobal)) {
214
+ bundlerConfig.output.chunkLoadingGlobal = 'jsonp';
215
+ }
216
+ // `uniqueName` is required for react refresh to work
217
+ if (!((_bundlerConfig_output2 = bundlerConfig.output) === null || _bundlerConfig_output2 === void 0 ? void 0 : _bundlerConfig_output2.uniqueName)) {
218
+ bundlerConfig.output.uniqueName = moduleFederationOptions.name;
219
+ }
220
+ if (!bundlerConfig.plugins.find(function(p) {
221
+ return p && p.name === PLUGIN_NAME;
222
+ })) {
223
+ bundlerConfig.plugins.push(new ModuleFederationPlugin(moduleFederationOptions));
224
+ }
225
+ }
226
+ });
227
+ });
228
+ // dev config only works on format: 'mf'
299
229
  api.modifyRsbuildConfig(function(config) {
300
230
  // Change some default configs for remote modules
301
231
  if (moduleFederationOptions.exposes) {
@@ -318,113 +248,17 @@ var pluginModuleFederation = function(moduleFederationOptions) {
318
248
  }
319
249
  });
320
250
  api.modifyEnvironmentConfig(function(config, param) {
321
- var mergeEnvironmentConfig = param.mergeEnvironmentConfig;
322
- var _config_performance_chunkSplit, _config_performance;
323
- /**
324
- * Currently, splitChunks will take precedence over module federation shared modules.
325
- * So we need to disable the default split chunks rules to make shared modules to work properly.
326
- * @see https://github.com/module-federation/module-federation-examples/issues/3161
327
- */ if (((_config_performance = config.performance) === null || _config_performance === void 0 ? void 0 : (_config_performance_chunkSplit = _config_performance.chunkSplit) === null || _config_performance_chunkSplit === void 0 ? void 0 : _config_performance_chunkSplit.strategy) === 'split-by-experience') {
328
- config.performance.chunkSplit = _object_spread_props(_object_spread({}, config.performance.chunkSplit), {
329
- strategy: 'custom'
330
- });
331
- }
251
+ param.mergeEnvironmentConfig;
332
252
  // Module Federation runtime uses ES6+ syntax,
333
253
  // adding to include and let SWC transform it
334
254
  config.source.include = _to_consumable_array(config.source.include || []).concat([
335
255
  /@module-federation[\\/]sdk/,
336
256
  /@module-federation[\\/]runtime/
337
257
  ]);
338
- // filter external with shared config,
339
- var externals = config.output.externals;
340
- if (Array.isArray(externals)) {
341
- var sharedModules = new Set();
342
- config.output.externals = externals.filter(function(ext) {
343
- var sharedModule;
344
- if (isRegExp(ext)) {
345
- var match = shared.some(function(dep) {
346
- if (ext.test(dep) || ext.test(pkgJson.name)) {
347
- sharedModule = dep;
348
- return true;
349
- }
350
- return false;
351
- });
352
- match && sharedModule && sharedModules.add(sharedModule);
353
- return !match;
354
- }
355
- if (typeof ext === 'string') {
356
- if (ext === pkgJson.name) {
357
- return false;
358
- }
359
- var match1 = shared.some(function(dep) {
360
- if (dep === ext) {
361
- sharedModule = dep;
362
- }
363
- return dep === ext;
364
- });
365
- if (match1) {
366
- sharedModule && sharedModules.add(sharedModule);
367
- return false;
368
- }
369
- return true;
370
- }
371
- return true;
372
- });
373
- if (sharedModules.size > 0) {
374
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
375
- try {
376
- for(var _iterator = sharedModules[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
377
- var sharedModule = _step.value;
378
- logger.log("".concat(sharedModule, " is removed from externals because it is a shared module."));
379
- }
380
- } catch (err) {
381
- _didIteratorError = true;
382
- _iteratorError = err;
383
- } finally{
384
- try {
385
- if (!_iteratorNormalCompletion && _iterator.return != null) {
386
- _iterator.return();
387
- }
388
- } finally{
389
- if (_didIteratorError) {
390
- throw _iteratorError;
391
- }
392
- }
393
- }
394
- }
395
- }
396
- var mfConfig = {
397
- tools: {
398
- rspack: {
399
- output: {
400
- chunkLoading: 'jsonp'
401
- }
402
- }
403
- }
404
- };
405
- return mergeEnvironmentConfig(config, mfConfig);
258
+ return config;
406
259
  });
407
- api.modifyBundlerChain(/*#__PURE__*/ function() {
408
- var _ref = _async_to_generator(function(chain) {
409
- return _ts_generator(this, function(_state) {
410
- chain.plugin('module-federation').use(ModuleFederationPlugin, [
411
- moduleFederationOptions
412
- ]);
413
- // `uniqueName` is required for react refresh to work
414
- if (!chain.output.get('uniqueName')) {
415
- chain.output.set('uniqueName', moduleFederationOptions.name);
416
- }
417
- return [
418
- 2
419
- ];
420
- });
421
- });
422
- return function(chain) {
423
- return _ref.apply(this, arguments);
424
- };
425
- }());
426
260
  }
427
261
  };
428
262
  };
429
263
 
430
- export { pluginModuleFederation };
264
+ export { LIB_FORMAT, RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, RSPACK_PLUGIN_MODULE_FEDERATION_NAME, pluginModuleFederation };
@@ -1,5 +1,9 @@
1
+ import { PLUGIN_NAME } from '@module-federation/enhanced/rspack';
1
2
  import type { moduleFederationPlugin } from '@module-federation/sdk';
2
3
  import type { RsbuildPlugin } from '@rsbuild/core';
3
4
  type ModuleFederationOptions = moduleFederationPlugin.ModuleFederationPluginOptions;
5
+ declare const RSBUILD_PLUGIN_MODULE_FEDERATION_NAME = "rsbuild:module-federation-enhanced";
6
+ declare const RSPACK_PLUGIN_MODULE_FEDERATION_NAME = "module-federation";
7
+ export { RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, RSPACK_PLUGIN_MODULE_FEDERATION_NAME, PLUGIN_NAME, };
8
+ export declare const LIB_FORMAT: string[];
4
9
  export declare const pluginModuleFederation: (moduleFederationOptions: ModuleFederationOptions) => RsbuildPlugin;
5
- export {};
@@ -0,0 +1,3 @@
1
+ import type { moduleFederationPlugin } from '@module-federation/sdk';
2
+ import type { Rspack } from '@rsbuild/core';
3
+ export declare function autoDeleteSplitChunkCacheGroups(mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions, bundlerConfig: Rspack.Configuration): void;
@@ -1,2 +1,3 @@
1
1
  export declare function isRegExp(target: any): target is RegExp;
2
2
  export * from './constant';
3
+ export * from './autoDeleteSplitChunkCacheGroups';
@@ -0,0 +1 @@
1
+ export * from "./src/utils/index";
@@ -0,0 +1,80 @@
1
+ 'use strict';
2
+
3
+ var util = require('util');
4
+
5
+ var DEFAULT_ASSET_PREFIX = '/';
6
+
7
+ // lib-polyfill.js: include core-js,@babel/runtime,@swc/helpers,tslib.
8
+ // lib-react.js: include react,react-dom.
9
+ // lib-router.js: include react-router,react-router-dom,history,@remix-run/router.
10
+ // lib-lodash.js: include lodash,lodash-es.
11
+ // lib-antd.js: include antd.
12
+ // lib-arco.js: include @arco-design/web-react.
13
+ // lib-semi.js: include @douyinfe/semi-ui.
14
+ // lib-axios.js: include axios.
15
+ var SPLIT_CHUNK_MAP = {
16
+ REACT: 'react',
17
+ ROUTER: 'router',
18
+ LODASH: 'lib-lodash',
19
+ ANTD: 'lib-antd',
20
+ ARCO: 'lib-arco',
21
+ SEMI: 'lib-semi',
22
+ AXIOS: 'lib-axios'
23
+ };
24
+ var SHARED_SPLIT_CHUNK_MAP = {
25
+ react: SPLIT_CHUNK_MAP.REACT,
26
+ 'react-dom': SPLIT_CHUNK_MAP.REACT,
27
+ 'react-router': SPLIT_CHUNK_MAP.ROUTER,
28
+ 'react-router-dom': SPLIT_CHUNK_MAP.ROUTER,
29
+ '@remix-run/router': SPLIT_CHUNK_MAP.ROUTER,
30
+ lodash: SPLIT_CHUNK_MAP.LODASH,
31
+ 'lodash-es': SPLIT_CHUNK_MAP.LODASH,
32
+ antd: SPLIT_CHUNK_MAP.ANTD,
33
+ '@arco-design/web-react': SPLIT_CHUNK_MAP.ARCO,
34
+ '@douyinfe/semi-ui': SPLIT_CHUNK_MAP.SEMI,
35
+ axios: SPLIT_CHUNK_MAP.AXIOS
36
+ };
37
+ function autoDeleteSplitChunkCacheGroups(mfConfig, bundlerConfig) {
38
+ var _bundlerConfig_optimization;
39
+ if (!mfConfig.shared) {
40
+ return;
41
+ }
42
+ if (!((_bundlerConfig_optimization = bundlerConfig.optimization) === null || _bundlerConfig_optimization === void 0 ? void 0 : _bundlerConfig_optimization.splitChunks) || !bundlerConfig.optimization.splitChunks.cacheGroups) {
43
+ return;
44
+ }
45
+ var arrayShared = Array.isArray(mfConfig.shared) ? mfConfig.shared : Object.keys(mfConfig.shared);
46
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
47
+ try {
48
+ for(var _iterator = arrayShared[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
49
+ var shared = _step.value;
50
+ var splitChunkKey = SHARED_SPLIT_CHUNK_MAP[shared];
51
+ if (!splitChunkKey) {
52
+ continue;
53
+ }
54
+ if (bundlerConfig.optimization.splitChunks.cacheGroups[splitChunkKey]) {
55
+ delete bundlerConfig.optimization.splitChunks.cacheGroups[splitChunkKey];
56
+ }
57
+ }
58
+ } catch (err) {
59
+ _didIteratorError = true;
60
+ _iteratorError = err;
61
+ } finally{
62
+ try {
63
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
64
+ _iterator.return();
65
+ }
66
+ } finally{
67
+ if (_didIteratorError) {
68
+ throw _iteratorError;
69
+ }
70
+ }
71
+ }
72
+ }
73
+
74
+ function isRegExp(target) {
75
+ return util.types.isRegExp(target);
76
+ }
77
+
78
+ exports.DEFAULT_ASSET_PREFIX = DEFAULT_ASSET_PREFIX;
79
+ exports.autoDeleteSplitChunkCacheGroups = autoDeleteSplitChunkCacheGroups;
80
+ exports.isRegExp = isRegExp;
@@ -0,0 +1 @@
1
+ export * from "./src/utils/index";
@@ -0,0 +1,76 @@
1
+ import util from 'util';
2
+
3
+ var DEFAULT_ASSET_PREFIX = '/';
4
+
5
+ // lib-polyfill.js: include core-js,@babel/runtime,@swc/helpers,tslib.
6
+ // lib-react.js: include react,react-dom.
7
+ // lib-router.js: include react-router,react-router-dom,history,@remix-run/router.
8
+ // lib-lodash.js: include lodash,lodash-es.
9
+ // lib-antd.js: include antd.
10
+ // lib-arco.js: include @arco-design/web-react.
11
+ // lib-semi.js: include @douyinfe/semi-ui.
12
+ // lib-axios.js: include axios.
13
+ var SPLIT_CHUNK_MAP = {
14
+ REACT: 'react',
15
+ ROUTER: 'router',
16
+ LODASH: 'lib-lodash',
17
+ ANTD: 'lib-antd',
18
+ ARCO: 'lib-arco',
19
+ SEMI: 'lib-semi',
20
+ AXIOS: 'lib-axios'
21
+ };
22
+ var SHARED_SPLIT_CHUNK_MAP = {
23
+ react: SPLIT_CHUNK_MAP.REACT,
24
+ 'react-dom': SPLIT_CHUNK_MAP.REACT,
25
+ 'react-router': SPLIT_CHUNK_MAP.ROUTER,
26
+ 'react-router-dom': SPLIT_CHUNK_MAP.ROUTER,
27
+ '@remix-run/router': SPLIT_CHUNK_MAP.ROUTER,
28
+ lodash: SPLIT_CHUNK_MAP.LODASH,
29
+ 'lodash-es': SPLIT_CHUNK_MAP.LODASH,
30
+ antd: SPLIT_CHUNK_MAP.ANTD,
31
+ '@arco-design/web-react': SPLIT_CHUNK_MAP.ARCO,
32
+ '@douyinfe/semi-ui': SPLIT_CHUNK_MAP.SEMI,
33
+ axios: SPLIT_CHUNK_MAP.AXIOS
34
+ };
35
+ function autoDeleteSplitChunkCacheGroups(mfConfig, bundlerConfig) {
36
+ var _bundlerConfig_optimization;
37
+ if (!mfConfig.shared) {
38
+ return;
39
+ }
40
+ if (!((_bundlerConfig_optimization = bundlerConfig.optimization) === null || _bundlerConfig_optimization === void 0 ? void 0 : _bundlerConfig_optimization.splitChunks) || !bundlerConfig.optimization.splitChunks.cacheGroups) {
41
+ return;
42
+ }
43
+ var arrayShared = Array.isArray(mfConfig.shared) ? mfConfig.shared : Object.keys(mfConfig.shared);
44
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
45
+ try {
46
+ for(var _iterator = arrayShared[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
47
+ var shared = _step.value;
48
+ var splitChunkKey = SHARED_SPLIT_CHUNK_MAP[shared];
49
+ if (!splitChunkKey) {
50
+ continue;
51
+ }
52
+ if (bundlerConfig.optimization.splitChunks.cacheGroups[splitChunkKey]) {
53
+ delete bundlerConfig.optimization.splitChunks.cacheGroups[splitChunkKey];
54
+ }
55
+ }
56
+ } catch (err) {
57
+ _didIteratorError = true;
58
+ _iteratorError = err;
59
+ } finally{
60
+ try {
61
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
62
+ _iterator.return();
63
+ }
64
+ } finally{
65
+ if (_didIteratorError) {
66
+ throw _iteratorError;
67
+ }
68
+ }
69
+ }
70
+ }
71
+
72
+ function isRegExp(target) {
73
+ return util.types.isRegExp(target);
74
+ }
75
+
76
+ export { DEFAULT_ASSET_PREFIX, autoDeleteSplitChunkCacheGroups, isRegExp };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/rsbuild-plugin",
3
- "version": "0.0.0-next-20241224035007",
3
+ "version": "0.0.0-next-20241224092514",
4
4
  "description": "Module Federation plugin for Rsbuild",
5
5
  "homepage": "https://module-federation.io",
6
6
  "bugs": {
@@ -14,9 +14,14 @@
14
14
  "license": "MIT",
15
15
  "exports": {
16
16
  ".": {
17
+ "types": "./dist/index.cjs.d.ts",
17
18
  "import": "./dist/index.esm.js",
18
- "require": "./dist/index.cjs.js",
19
- "types": "./dist/index.cjs.d.ts"
19
+ "require": "./dist/index.cjs.js"
20
+ },
21
+ "./utils": {
22
+ "types": "./dist/utils.cjs.d.ts",
23
+ "import": "./dist/utils.esm.js",
24
+ "require": "./dist/utils.cjs.js"
20
25
  }
21
26
  },
22
27
  "main": "./dist/index.cjs.js",
@@ -25,6 +30,9 @@
25
30
  "*": {
26
31
  ".": [
27
32
  "./dist/index.cjs.d.ts"
33
+ ],
34
+ "utils": [
35
+ "./dist/utils.cjs.d.ts"
28
36
  ]
29
37
  }
30
38
  },
@@ -32,14 +40,14 @@
32
40
  "dist"
33
41
  ],
34
42
  "dependencies": {
35
- "@module-federation/sdk": "0.0.0-next-20241224035007"
43
+ "@module-federation/sdk": "0.0.0-next-20241224092514"
36
44
  },
37
45
  "devDependencies": {
38
46
  "@rsbuild/core": "^1.0.19"
39
47
  },
40
48
  "peerDependencies": {
41
49
  "@rsbuild/core": "1.x",
42
- "@module-federation/enhanced": "0.0.0-next-20241224035007"
50
+ "@module-federation/enhanced": "0.0.0-next-20241224092514"
43
51
  },
44
52
  "engines": {
45
53
  "node": ">=16.0.0"