@module-federation/rsbuild-plugin 0.0.0-next-20241009081924

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present Bytedance, Inc. and its affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Example
2
+
3
+ ### Rsbuild App
4
+ import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
5
+ import { defineConfig } from '@rsbuild/core';
6
+ import { pluginReact } from '@rsbuild/plugin-react';
7
+
8
+ export default defineConfig({
9
+ server: {
10
+ port: 2000,
11
+ },
12
+ plugins: [
13
+ pluginReact(),
14
+ pluginModuleFederation({
15
+ name: 'federation_consumer',
16
+ remotes: {
17
+ remote1: 'remote1@http://localhost:2001/mf-manifest.json',
18
+ },
19
+ shared: ['react', 'react-dom'],
20
+ }),
21
+ ],
22
+ });
23
+
24
+
25
+ ### Rslib Module
26
+ ```
27
+ import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
28
+ import { defineConfig } from '@rslib/core';
29
+
30
+ export default defineConfig({
31
+ lib: [
32
+ // ...
33
+ {
34
+ format: 'mf',
35
+ output: {
36
+ distPath: {
37
+ root: './dist/mf',
38
+ },
39
+ assetPrefix: 'http://localhost:3001/mf',
40
+ },
41
+ plugins: [
42
+ // ...
43
+ pluginModuleFederation({
44
+ name: 'rslib_provider',
45
+ exposes: {
46
+ '.': './src/index.tsx',
47
+ },
48
+ shared: {
49
+ react: {
50
+ singleton: true,
51
+ },
52
+ 'react-dom': {
53
+ singleton: true,
54
+ },
55
+ },
56
+ }),
57
+ ],
58
+ },
59
+ ],
60
+ });
61
+
62
+ ```
package/dist/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present Bytedance, Inc. and its affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ export * from "./src/cli/index";
@@ -0,0 +1,330 @@
1
+ 'use strict';
2
+
3
+ var enhanced = require('@module-federation/enhanced');
4
+ var rspack = require('@module-federation/enhanced/rspack');
5
+
6
+ // RegExp for version string
7
+ var VERSION_PATTERN_REGEXP = /^([\d^=v<>~]|[*xX]$)/;
8
+ /**
9
+ * @param {string} str maybe required version
10
+ * @returns {boolean} true, if it looks like a version
11
+ */ function isRequiredVersion(str) {
12
+ return VERSION_PATTERN_REGEXP.test(str);
13
+ }
14
+ function isRegExp(target) {
15
+ return Object.prototype.toString.call(target) === '[object RegExp]';
16
+ }
17
+
18
+ function _array_like_to_array(arr, len) {
19
+ if (len == null || len > arr.length) len = arr.length;
20
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
21
+ return arr2;
22
+ }
23
+ function _array_without_holes(arr) {
24
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
25
+ }
26
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
27
+ try {
28
+ var info = gen[key](arg);
29
+ var value = info.value;
30
+ } catch (error) {
31
+ reject(error);
32
+ return;
33
+ }
34
+ if (info.done) {
35
+ resolve(value);
36
+ } else {
37
+ Promise.resolve(value).then(_next, _throw);
38
+ }
39
+ }
40
+ function _async_to_generator(fn) {
41
+ return function() {
42
+ var self = this, args = arguments;
43
+ return new Promise(function(resolve, reject) {
44
+ var gen = fn.apply(self, args);
45
+ function _next(value) {
46
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
47
+ }
48
+ function _throw(err) {
49
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
50
+ }
51
+ _next(undefined);
52
+ });
53
+ };
54
+ }
55
+ function _define_property(obj, key, value) {
56
+ if (key in obj) {
57
+ Object.defineProperty(obj, key, {
58
+ value: value,
59
+ enumerable: true,
60
+ configurable: true,
61
+ writable: true
62
+ });
63
+ } else {
64
+ obj[key] = value;
65
+ }
66
+ return obj;
67
+ }
68
+ function _iterable_to_array(iter) {
69
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
70
+ }
71
+ function _non_iterable_spread() {
72
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
73
+ }
74
+ function _object_spread(target) {
75
+ for(var i = 1; i < arguments.length; i++){
76
+ var source = arguments[i] != null ? arguments[i] : {};
77
+ var ownKeys = Object.keys(source);
78
+ if (typeof Object.getOwnPropertySymbols === "function") {
79
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
80
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
81
+ }));
82
+ }
83
+ ownKeys.forEach(function(key) {
84
+ _define_property(target, key, source[key]);
85
+ });
86
+ }
87
+ return target;
88
+ }
89
+ function ownKeys(object, enumerableOnly) {
90
+ var keys = Object.keys(object);
91
+ if (Object.getOwnPropertySymbols) {
92
+ var symbols = Object.getOwnPropertySymbols(object);
93
+ keys.push.apply(keys, symbols);
94
+ }
95
+ return keys;
96
+ }
97
+ function _object_spread_props(target, source) {
98
+ source = source != null ? source : {};
99
+ if (Object.getOwnPropertyDescriptors) {
100
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
101
+ } else {
102
+ ownKeys(Object(source)).forEach(function(key) {
103
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
104
+ });
105
+ }
106
+ return target;
107
+ }
108
+ function _to_consumable_array(arr) {
109
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
110
+ }
111
+ function _unsupported_iterable_to_array(o, minLen) {
112
+ if (!o) return;
113
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
114
+ var n = Object.prototype.toString.call(o).slice(8, -1);
115
+ if (n === "Object" && o.constructor) n = o.constructor.name;
116
+ if (n === "Map" || n === "Set") return Array.from(n);
117
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
118
+ }
119
+ function _ts_generator(thisArg, body) {
120
+ var f, y, t, g, _ = {
121
+ label: 0,
122
+ sent: function() {
123
+ if (t[0] & 1) throw t[1];
124
+ return t[1];
125
+ },
126
+ trys: [],
127
+ ops: []
128
+ };
129
+ return g = {
130
+ next: verb(0),
131
+ "throw": verb(1),
132
+ "return": verb(2)
133
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
134
+ return this;
135
+ }), g;
136
+ function verb(n) {
137
+ return function(v) {
138
+ return step([
139
+ n,
140
+ v
141
+ ]);
142
+ };
143
+ }
144
+ function step(op) {
145
+ if (f) throw new TypeError("Generator is already executing.");
146
+ while(_)try {
147
+ 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;
148
+ if (y = 0, t) op = [
149
+ op[0] & 2,
150
+ t.value
151
+ ];
152
+ switch(op[0]){
153
+ case 0:
154
+ case 1:
155
+ t = op;
156
+ break;
157
+ case 4:
158
+ _.label++;
159
+ return {
160
+ value: op[1],
161
+ done: false
162
+ };
163
+ case 5:
164
+ _.label++;
165
+ y = op[1];
166
+ op = [
167
+ 0
168
+ ];
169
+ continue;
170
+ case 7:
171
+ op = _.ops.pop();
172
+ _.trys.pop();
173
+ continue;
174
+ default:
175
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
176
+ _ = 0;
177
+ continue;
178
+ }
179
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
180
+ _.label = op[1];
181
+ break;
182
+ }
183
+ if (op[0] === 6 && _.label < t[1]) {
184
+ _.label = t[1];
185
+ t = op;
186
+ break;
187
+ }
188
+ if (t && _.label < t[2]) {
189
+ _.label = t[2];
190
+ _.ops.push(op);
191
+ break;
192
+ }
193
+ if (t[2]) _.ops.pop();
194
+ _.trys.pop();
195
+ continue;
196
+ }
197
+ op = body.call(thisArg, _);
198
+ } catch (e) {
199
+ op = [
200
+ 6,
201
+ e
202
+ ];
203
+ y = 0;
204
+ } finally{
205
+ f = t = 0;
206
+ }
207
+ if (op[0] & 5) throw op[1];
208
+ return {
209
+ value: op[0] ? op[1] : void 0,
210
+ done: true
211
+ };
212
+ }
213
+ }
214
+ var PLUGIN_MODULE_FEDERATION_NAME = 'rsbuild:module-federation-enhanced';
215
+ var pkgName = '@rsbuild/plugin-module-federation';
216
+ var pluginModuleFederation = function(moduleFederationOptions) {
217
+ return {
218
+ name: PLUGIN_MODULE_FEDERATION_NAME,
219
+ setup: function(api) {
220
+ api.modifyRsbuildConfig(function(config) {
221
+ var _config_server, _config_dev_client, _config_dev;
222
+ if (moduleFederationOptions.exposes && ((_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.port) && !((_config_dev = config.dev) === null || _config_dev === void 0 ? void 0 : (_config_dev_client = _config_dev.client) === null || _config_dev_client === void 0 ? void 0 : _config_dev_client.port)) {
223
+ var _config, _config_dev1;
224
+ (_config = config).dev || (_config.dev = {});
225
+ (_config_dev1 = config.dev).client || (_config_dev1.client = {});
226
+ config.dev.client.port = config.server.port;
227
+ }
228
+ });
229
+ api.modifyEnvironmentConfig(function(config) {
230
+ var _config_moduleFederation, _config_performance_chunkSplit, _config_performance;
231
+ if (!((_config_moduleFederation = config.moduleFederation) === null || _config_moduleFederation === void 0 ? void 0 : _config_moduleFederation.options)) {
232
+ return;
233
+ }
234
+ /**
235
+ * Currently, splitChunks will take precedence over module federation shared modules.
236
+ * So we need to disable the default split chunks rules to make shared modules to work properly.
237
+ * @see https://github.com/module-federation/module-federation-examples/issues/3161
238
+ */ 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') {
239
+ config.performance.chunkSplit = _object_spread_props(_object_spread({}, config.performance.chunkSplit), {
240
+ strategy: 'custom'
241
+ });
242
+ }
243
+ // Module Federation runtime uses ES6+ syntax,
244
+ // adding to include and let SWC transform it
245
+ config.source.include = _to_consumable_array(config.source.include || []).concat([
246
+ /@module-federation[\\/]sdk/,
247
+ /@module-federation[\\/]runtime/
248
+ ]);
249
+ });
250
+ var sharedOptions = enhanced.parseOptions(moduleFederationOptions.shared || [], function(item, key) {
251
+ if (typeof item !== 'string') throw new Error('Unexpected array in shared');
252
+ var config = item === key || !isRequiredVersion(item) ? {
253
+ import: item
254
+ } : {
255
+ import: key,
256
+ requiredVersion: item
257
+ };
258
+ return config;
259
+ }, function(item) {
260
+ return item;
261
+ });
262
+ // shared[0] is the shared name
263
+ var shared = sharedOptions.map(function(shared) {
264
+ return shared[0].endsWith('/') ? shared[0].slice(0, -1) : shared[0];
265
+ });
266
+ api.modifyEnvironmentConfig(function(config) {
267
+ var externals = config.output.externals;
268
+ if (Array.isArray(externals)) {
269
+ config.output.externals = externals.filter(function(ext) {
270
+ var sharedModule;
271
+ if (isRegExp(ext)) {
272
+ var match = shared.some(function(dep) {
273
+ if (ext.test(dep) || ext.test(pkgName)) {
274
+ sharedModule = dep;
275
+ return true;
276
+ }
277
+ return false;
278
+ });
279
+ if (match) {
280
+ console.log("[Module Federation Rsbuild Plugin]: ".concat(sharedModule, " is removed from externals because it is a shared module."));
281
+ }
282
+ return !match;
283
+ }
284
+ if (typeof ext === 'string') {
285
+ if (ext === pkgName) {
286
+ return false;
287
+ }
288
+ var match1 = shared.some(function(dep) {
289
+ if (dep === ext) {
290
+ sharedModule = dep;
291
+ }
292
+ return dep === ext;
293
+ });
294
+ if (match1) {
295
+ console.log("[Module Federation Rsbuild Plugin]: ".concat(sharedModule, " is removed from externals because it is a shared module."));
296
+ return false;
297
+ }
298
+ return true;
299
+ }
300
+ return true;
301
+ });
302
+ }
303
+ return config;
304
+ });
305
+ api.modifyBundlerChain(/*#__PURE__*/ function() {
306
+ var _ref = _async_to_generator(function(chain) {
307
+ return _ts_generator(this, function(_state) {
308
+ chain.plugin('module-federation').use(rspack.ModuleFederationPlugin, [
309
+ moduleFederationOptions
310
+ ]);
311
+ // `uniqueName` is required for react refresh to work
312
+ if (!chain.output.get('uniqueName')) {
313
+ chain.output.set('uniqueName', moduleFederationOptions.name);
314
+ }
315
+ // use jsonp mode to load chunk
316
+ chain.output.set('chunkLoading', 'jsonp');
317
+ return [
318
+ 2
319
+ ];
320
+ });
321
+ });
322
+ return function(chain) {
323
+ return _ref.apply(this, arguments);
324
+ };
325
+ }());
326
+ }
327
+ };
328
+ };
329
+
330
+ exports.pluginModuleFederation = pluginModuleFederation;
@@ -0,0 +1 @@
1
+ export * from "./src/cli/index";
@@ -0,0 +1,328 @@
1
+ import { parseOptions } from '@module-federation/enhanced';
2
+ import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack';
3
+
4
+ // RegExp for version string
5
+ var VERSION_PATTERN_REGEXP = /^([\d^=v<>~]|[*xX]$)/;
6
+ /**
7
+ * @param {string} str maybe required version
8
+ * @returns {boolean} true, if it looks like a version
9
+ */ function isRequiredVersion(str) {
10
+ return VERSION_PATTERN_REGEXP.test(str);
11
+ }
12
+ function isRegExp(target) {
13
+ return Object.prototype.toString.call(target) === '[object RegExp]';
14
+ }
15
+
16
+ function _array_like_to_array(arr, len) {
17
+ if (len == null || len > arr.length) len = arr.length;
18
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
19
+ return arr2;
20
+ }
21
+ function _array_without_holes(arr) {
22
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
23
+ }
24
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
25
+ try {
26
+ var info = gen[key](arg);
27
+ var value = info.value;
28
+ } catch (error) {
29
+ reject(error);
30
+ return;
31
+ }
32
+ if (info.done) {
33
+ resolve(value);
34
+ } else {
35
+ Promise.resolve(value).then(_next, _throw);
36
+ }
37
+ }
38
+ function _async_to_generator(fn) {
39
+ return function() {
40
+ var self = this, args = arguments;
41
+ return new Promise(function(resolve, reject) {
42
+ var gen = fn.apply(self, args);
43
+ function _next(value) {
44
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
45
+ }
46
+ function _throw(err) {
47
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
48
+ }
49
+ _next(undefined);
50
+ });
51
+ };
52
+ }
53
+ function _define_property(obj, key, value) {
54
+ if (key in obj) {
55
+ Object.defineProperty(obj, key, {
56
+ value: value,
57
+ enumerable: true,
58
+ configurable: true,
59
+ writable: true
60
+ });
61
+ } else {
62
+ obj[key] = value;
63
+ }
64
+ return obj;
65
+ }
66
+ function _iterable_to_array(iter) {
67
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
68
+ }
69
+ function _non_iterable_spread() {
70
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
71
+ }
72
+ function _object_spread(target) {
73
+ for(var i = 1; i < arguments.length; i++){
74
+ var source = arguments[i] != null ? arguments[i] : {};
75
+ var ownKeys = Object.keys(source);
76
+ if (typeof Object.getOwnPropertySymbols === "function") {
77
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
78
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
79
+ }));
80
+ }
81
+ ownKeys.forEach(function(key) {
82
+ _define_property(target, key, source[key]);
83
+ });
84
+ }
85
+ return target;
86
+ }
87
+ function ownKeys(object, enumerableOnly) {
88
+ var keys = Object.keys(object);
89
+ if (Object.getOwnPropertySymbols) {
90
+ var symbols = Object.getOwnPropertySymbols(object);
91
+ keys.push.apply(keys, symbols);
92
+ }
93
+ return keys;
94
+ }
95
+ function _object_spread_props(target, source) {
96
+ source = source != null ? source : {};
97
+ if (Object.getOwnPropertyDescriptors) {
98
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
99
+ } else {
100
+ ownKeys(Object(source)).forEach(function(key) {
101
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
102
+ });
103
+ }
104
+ return target;
105
+ }
106
+ function _to_consumable_array(arr) {
107
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
108
+ }
109
+ function _unsupported_iterable_to_array(o, minLen) {
110
+ if (!o) return;
111
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
112
+ var n = Object.prototype.toString.call(o).slice(8, -1);
113
+ if (n === "Object" && o.constructor) n = o.constructor.name;
114
+ if (n === "Map" || n === "Set") return Array.from(n);
115
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
116
+ }
117
+ function _ts_generator(thisArg, body) {
118
+ var f, y, t, g, _ = {
119
+ label: 0,
120
+ sent: function() {
121
+ if (t[0] & 1) throw t[1];
122
+ return t[1];
123
+ },
124
+ trys: [],
125
+ ops: []
126
+ };
127
+ return g = {
128
+ next: verb(0),
129
+ "throw": verb(1),
130
+ "return": verb(2)
131
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
132
+ return this;
133
+ }), g;
134
+ function verb(n) {
135
+ return function(v) {
136
+ return step([
137
+ n,
138
+ v
139
+ ]);
140
+ };
141
+ }
142
+ function step(op) {
143
+ if (f) throw new TypeError("Generator is already executing.");
144
+ while(_)try {
145
+ 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;
146
+ if (y = 0, t) op = [
147
+ op[0] & 2,
148
+ t.value
149
+ ];
150
+ switch(op[0]){
151
+ case 0:
152
+ case 1:
153
+ t = op;
154
+ break;
155
+ case 4:
156
+ _.label++;
157
+ return {
158
+ value: op[1],
159
+ done: false
160
+ };
161
+ case 5:
162
+ _.label++;
163
+ y = op[1];
164
+ op = [
165
+ 0
166
+ ];
167
+ continue;
168
+ case 7:
169
+ op = _.ops.pop();
170
+ _.trys.pop();
171
+ continue;
172
+ default:
173
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
174
+ _ = 0;
175
+ continue;
176
+ }
177
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
178
+ _.label = op[1];
179
+ break;
180
+ }
181
+ if (op[0] === 6 && _.label < t[1]) {
182
+ _.label = t[1];
183
+ t = op;
184
+ break;
185
+ }
186
+ if (t && _.label < t[2]) {
187
+ _.label = t[2];
188
+ _.ops.push(op);
189
+ break;
190
+ }
191
+ if (t[2]) _.ops.pop();
192
+ _.trys.pop();
193
+ continue;
194
+ }
195
+ op = body.call(thisArg, _);
196
+ } catch (e) {
197
+ op = [
198
+ 6,
199
+ e
200
+ ];
201
+ y = 0;
202
+ } finally{
203
+ f = t = 0;
204
+ }
205
+ if (op[0] & 5) throw op[1];
206
+ return {
207
+ value: op[0] ? op[1] : void 0,
208
+ done: true
209
+ };
210
+ }
211
+ }
212
+ var PLUGIN_MODULE_FEDERATION_NAME = 'rsbuild:module-federation-enhanced';
213
+ var pkgName = '@rsbuild/plugin-module-federation';
214
+ var pluginModuleFederation = function(moduleFederationOptions) {
215
+ return {
216
+ name: PLUGIN_MODULE_FEDERATION_NAME,
217
+ setup: function(api) {
218
+ api.modifyRsbuildConfig(function(config) {
219
+ var _config_server, _config_dev_client, _config_dev;
220
+ if (moduleFederationOptions.exposes && ((_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.port) && !((_config_dev = config.dev) === null || _config_dev === void 0 ? void 0 : (_config_dev_client = _config_dev.client) === null || _config_dev_client === void 0 ? void 0 : _config_dev_client.port)) {
221
+ var _config, _config_dev1;
222
+ (_config = config).dev || (_config.dev = {});
223
+ (_config_dev1 = config.dev).client || (_config_dev1.client = {});
224
+ config.dev.client.port = config.server.port;
225
+ }
226
+ });
227
+ api.modifyEnvironmentConfig(function(config) {
228
+ var _config_moduleFederation, _config_performance_chunkSplit, _config_performance;
229
+ if (!((_config_moduleFederation = config.moduleFederation) === null || _config_moduleFederation === void 0 ? void 0 : _config_moduleFederation.options)) {
230
+ return;
231
+ }
232
+ /**
233
+ * Currently, splitChunks will take precedence over module federation shared modules.
234
+ * So we need to disable the default split chunks rules to make shared modules to work properly.
235
+ * @see https://github.com/module-federation/module-federation-examples/issues/3161
236
+ */ 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') {
237
+ config.performance.chunkSplit = _object_spread_props(_object_spread({}, config.performance.chunkSplit), {
238
+ strategy: 'custom'
239
+ });
240
+ }
241
+ // Module Federation runtime uses ES6+ syntax,
242
+ // adding to include and let SWC transform it
243
+ config.source.include = _to_consumable_array(config.source.include || []).concat([
244
+ /@module-federation[\\/]sdk/,
245
+ /@module-federation[\\/]runtime/
246
+ ]);
247
+ });
248
+ var sharedOptions = parseOptions(moduleFederationOptions.shared || [], function(item, key) {
249
+ if (typeof item !== 'string') throw new Error('Unexpected array in shared');
250
+ var config = item === key || !isRequiredVersion(item) ? {
251
+ import: item
252
+ } : {
253
+ import: key,
254
+ requiredVersion: item
255
+ };
256
+ return config;
257
+ }, function(item) {
258
+ return item;
259
+ });
260
+ // shared[0] is the shared name
261
+ var shared = sharedOptions.map(function(shared) {
262
+ return shared[0].endsWith('/') ? shared[0].slice(0, -1) : shared[0];
263
+ });
264
+ api.modifyEnvironmentConfig(function(config) {
265
+ var externals = config.output.externals;
266
+ if (Array.isArray(externals)) {
267
+ config.output.externals = externals.filter(function(ext) {
268
+ var sharedModule;
269
+ if (isRegExp(ext)) {
270
+ var match = shared.some(function(dep) {
271
+ if (ext.test(dep) || ext.test(pkgName)) {
272
+ sharedModule = dep;
273
+ return true;
274
+ }
275
+ return false;
276
+ });
277
+ if (match) {
278
+ console.log("[Module Federation Rsbuild Plugin]: ".concat(sharedModule, " is removed from externals because it is a shared module."));
279
+ }
280
+ return !match;
281
+ }
282
+ if (typeof ext === 'string') {
283
+ if (ext === pkgName) {
284
+ return false;
285
+ }
286
+ var match1 = shared.some(function(dep) {
287
+ if (dep === ext) {
288
+ sharedModule = dep;
289
+ }
290
+ return dep === ext;
291
+ });
292
+ if (match1) {
293
+ console.log("[Module Federation Rsbuild Plugin]: ".concat(sharedModule, " is removed from externals because it is a shared module."));
294
+ return false;
295
+ }
296
+ return true;
297
+ }
298
+ return true;
299
+ });
300
+ }
301
+ return config;
302
+ });
303
+ api.modifyBundlerChain(/*#__PURE__*/ function() {
304
+ var _ref = _async_to_generator(function(chain) {
305
+ return _ts_generator(this, function(_state) {
306
+ chain.plugin('module-federation').use(ModuleFederationPlugin, [
307
+ moduleFederationOptions
308
+ ]);
309
+ // `uniqueName` is required for react refresh to work
310
+ if (!chain.output.get('uniqueName')) {
311
+ chain.output.set('uniqueName', moduleFederationOptions.name);
312
+ }
313
+ // use jsonp mode to load chunk
314
+ chain.output.set('chunkLoading', 'jsonp');
315
+ return [
316
+ 2
317
+ ];
318
+ });
319
+ });
320
+ return function(chain) {
321
+ return _ref.apply(this, arguments);
322
+ };
323
+ }());
324
+ }
325
+ };
326
+ };
327
+
328
+ export { pluginModuleFederation };
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@module-federation/rsbuild-plugin",
3
+ "version": "0.0.1",
4
+ "description": "Module Federation plugin for Rsbuild",
5
+ "homepage": "https://module-federation.io",
6
+ "bugs": {
7
+ "url": "https://github.com/module-federation/core/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/module-federation/core",
12
+ "directory": "packages/plugin-federation"
13
+ },
14
+ "license": "MIT",
15
+ "exports": {
16
+ ".": {
17
+ "import": "./dist/index.esm.js",
18
+ "require": "./dist/index.cjs.js",
19
+ "types": "./dist/index.cjs.d.ts"
20
+ },
21
+ "./runtime": {
22
+ "import": "./dist/runtime.esm.js",
23
+ "require": "./dist/runtime.cjs.js",
24
+ "types": "./dist/runtime.cjs.d.ts"
25
+ }
26
+ },
27
+ "main": "./index.cjs.js",
28
+ "module": "./index.esm.js",
29
+ "typesVersions": {
30
+ "*": {
31
+ ".": [
32
+ "./dist/index.cjs.d.ts"
33
+ ],
34
+ "runtime": [
35
+ "./dist/runtime.cjs.d.ts"
36
+ ]
37
+ }
38
+ },
39
+ "files": [
40
+ "dist"
41
+ ],
42
+ "dependencies": {
43
+ "@module-federation/enhanced": "workspace:*",
44
+ "@module-federation/sdk": "workspace:*"
45
+ },
46
+ "devDependencies": {
47
+ "@rsbuild/core": "1.0.7"
48
+ },
49
+ "peerDependencies": {
50
+ "@rsbuild/core": "1.0.7"
51
+ },
52
+ "engines": {
53
+ "node": ">=16.0.0"
54
+ },
55
+ "publishConfig": {
56
+ "access": "public",
57
+ "provenance": true,
58
+ "registry": "https://registry.npmjs.org/"
59
+ },
60
+ "types": "./index.esm.d.ts"
61
+ }
@@ -0,0 +1 @@
1
+ export * from "./src/runtime/index";
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ var runtime = require('@module-federation/enhanced/runtime');
4
+
5
+
6
+
7
+ Object.keys(runtime).forEach(function (k) {
8
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
9
+ enumerable: true,
10
+ get: function () { return runtime[k]; }
11
+ });
12
+ });
@@ -0,0 +1 @@
1
+ export * from "./src/runtime/index";
@@ -0,0 +1 @@
1
+ export * from '@module-federation/enhanced/runtime';
@@ -0,0 +1,5 @@
1
+ import type { moduleFederationPlugin } from '@module-federation/sdk';
2
+ import type { RsbuildPlugin } from '@rsbuild/core';
3
+ type ModuleFederationOptions = moduleFederationPlugin.ModuleFederationPluginOptions;
4
+ export declare const pluginModuleFederation: (moduleFederationOptions: ModuleFederationOptions) => RsbuildPlugin;
5
+ export {};
@@ -0,0 +1 @@
1
+ export * from '@module-federation/enhanced/runtime';
@@ -0,0 +1,2 @@
1
+ export declare function isRequiredVersion(str: string): boolean;
2
+ export declare function isRegExp(target: any): boolean;
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@module-federation/rsbuild-plugin",
3
+ "version": "0.0.0-next-20241009081924",
4
+ "description": "Module Federation plugin for Rsbuild",
5
+ "homepage": "https://module-federation.io",
6
+ "bugs": {
7
+ "url": "https://github.com/module-federation/core/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/module-federation/core",
12
+ "directory": "packages/plugin-federation"
13
+ },
14
+ "license": "MIT",
15
+ "exports": {
16
+ ".": {
17
+ "import": "./dist/index.esm.js",
18
+ "require": "./dist/index.cjs.js",
19
+ "types": "./dist/index.cjs.d.ts"
20
+ },
21
+ "./runtime": {
22
+ "import": "./dist/runtime.esm.js",
23
+ "require": "./dist/runtime.cjs.js",
24
+ "types": "./dist/runtime.cjs.d.ts"
25
+ }
26
+ },
27
+ "main": "./dist/index.cjs.js",
28
+ "module": "./dist/index.esm.js",
29
+ "typesVersions": {
30
+ "*": {
31
+ ".": [
32
+ "./dist/index.cjs.d.ts"
33
+ ],
34
+ "runtime": [
35
+ "./dist/runtime.cjs.d.ts"
36
+ ]
37
+ }
38
+ },
39
+ "files": [
40
+ "dist"
41
+ ],
42
+ "dependencies": {
43
+ "@module-federation/enhanced": "0.0.0-next-20241009081924",
44
+ "@module-federation/sdk": "0.0.0-next-20241009081924"
45
+ },
46
+ "devDependencies": {
47
+ "@rsbuild/core": "1.0.7"
48
+ },
49
+ "peerDependencies": {
50
+ "@rsbuild/core": "1.0.7"
51
+ },
52
+ "engines": {
53
+ "node": ">=16.0.0"
54
+ },
55
+ "publishConfig": {
56
+ "access": "public",
57
+ "provenance": true,
58
+ "registry": "https://registry.npmjs.org/"
59
+ }
60
+ }