@module-federation/rsbuild-plugin 0.14.3 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/utils.cjs.js CHANGED
@@ -1,9 +1,13 @@
1
1
  'use strict';
2
2
 
3
3
  var util = require('util');
4
+ var constant = require('./constant.cjs.js');
5
+ var fs = require('fs-extra');
6
+ var path = require('path');
7
+ var sdk = require('@module-federation/sdk');
8
+ var node_module = require('node:module');
4
9
 
5
- var DEFAULT_ASSET_PREFIX = '/';
6
-
10
+ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
7
11
  // lib-polyfill.js: include core-js,@babel/runtime,@swc/helpers,tslib.
8
12
  // lib-react.js: include react,react-dom.
9
13
  // lib-router.js: include react-router,react-router-dom,history,@remix-run/router.
@@ -71,10 +75,268 @@ function autoDeleteSplitChunkCacheGroups(mfConfig, splitChunks) {
71
75
  return splitChunks;
72
76
  }
73
77
 
78
+ function _type_of(obj) {
79
+ "@swc/helpers - typeof";
80
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
81
+ }
82
+ var addDataFetchExpose = function(exposes, key, filepath) {
83
+ var suffix = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : '';
84
+ if (!fs.existsSync(filepath)) {
85
+ return false;
86
+ }
87
+ var dataFetchKey = key === '.' ? "./".concat(constant.DATA_FETCH_IDENTIFIER).concat(suffix) : "".concat(key, ".").concat(constant.DATA_FETCH_IDENTIFIER).concat(suffix);
88
+ if (exposes[dataFetchKey] && exposes[dataFetchKey] !== filepath) {
89
+ throw new Error("data fetch key ".concat(dataFetchKey, " already exists, please modify this expose key, do not end with '").concat(constant.DATA_FETCH_IDENTIFIER, "' or '").concat(constant.DATA_FETCH_CLIENT_SUFFIX, "'"));
90
+ }
91
+ exposes[dataFetchKey] = filepath;
92
+ return dataFetchKey;
93
+ };
94
+ var addExcludeDtsSuffix = function(filepath) {
95
+ return "".concat(filepath, "?exclude-mf-dts=true");
96
+ };
97
+ function addDataFetchExposes(exposes, isServer) {
98
+ if ((typeof exposes === "undefined" ? "undefined" : _type_of(exposes)) !== 'object' || Array.isArray(exposes)) {
99
+ return;
100
+ }
101
+ if (Object.keys(exposes).length === 0) {
102
+ return;
103
+ }
104
+ var tempDataFetchFilepath = path.resolve(process.cwd(), "node_modules/".concat(sdk.TEMP_DIR, "/data-fetch-fallback.ts"));
105
+ var content = "export const fetchData=()=>{throw new Error('should not be called')};";
106
+ fs.ensureDirSync(path.dirname(tempDataFetchFilepath));
107
+ fs.writeFileSync(tempDataFetchFilepath, content);
108
+ Object.keys(exposes).forEach(function(key) {
109
+ var expose = exposes[key];
110
+ if (typeof expose !== 'string') {
111
+ return;
112
+ }
113
+ var absPath = path.resolve(process.cwd(), expose);
114
+ var dataFetchPath = "".concat(absPath.replace(path.extname(absPath), ''), ".").concat(constant.DATA_FETCH_IDENTIFIER, ".ts");
115
+ var dataFetchClientPath = "".concat(absPath.replace(path.extname(absPath), ''), ".").concat(constant.DATA_FETCH_IDENTIFIER, ".client.ts");
116
+ var dateFetchClientKey = addDataFetchExpose(exposes, key, dataFetchClientPath, constant.DATA_FETCH_CLIENT_SUFFIX);
117
+ if (!isServer && dateFetchClientKey) {
118
+ exposes[dateFetchClientKey.replace(constant.DATA_FETCH_CLIENT_SUFFIX, '')] = addExcludeDtsSuffix(tempDataFetchFilepath);
119
+ return;
120
+ }
121
+ var dataFetchKey = addDataFetchExpose(exposes, key, dataFetchPath);
122
+ if (dataFetchKey && fs.existsSync(dataFetchClientPath)) {
123
+ exposes["".concat(dataFetchKey).concat(constant.DATA_FETCH_CLIENT_SUFFIX)] = addExcludeDtsSuffix(tempDataFetchFilepath);
124
+ }
125
+ });
126
+ }
127
+
128
+ function mergeStats(browserStats, nodeStats) {
129
+ var ssrRemoteEntry = nodeStats.metaData.remoteEntry;
130
+ browserStats.metaData.ssrRemoteEntry = ssrRemoteEntry;
131
+ if ('publicPath' in browserStats.metaData) {
132
+ // @ts-ignore nodeStats has the same structure with browserStats
133
+ browserStats.metaData.ssrPublicPath = nodeStats.metaData.publicPath;
134
+ }
135
+ return browserStats;
136
+ }
137
+ function mergeManifest(browserManifest, nodeManifest) {
138
+ var ssrRemoteEntry = nodeManifest.metaData.remoteEntry;
139
+ browserManifest.metaData.ssrRemoteEntry = ssrRemoteEntry;
140
+ if ('publicPath' in browserManifest.metaData) {
141
+ // @ts-ignore nodeStats has the same structure with browserStats
142
+ browserManifest.metaData.ssrPublicPath = nodeManifest.metaData.publicPath;
143
+ }
144
+ return browserManifest;
145
+ }
146
+ function mergeStatsAndManifest(nodePlugin, browserPlugin) {
147
+ var nodeResourceInfo = nodePlugin.statsResourceInfo;
148
+ var browserResourceInfo = browserPlugin.statsResourceInfo;
149
+ if (!browserResourceInfo || !nodeResourceInfo || !browserResourceInfo.stats || !nodeResourceInfo.stats || !browserResourceInfo.manifest || !nodeResourceInfo.manifest) {
150
+ throw new Error('can not get browserResourceInfo or nodeResourceInfo');
151
+ }
152
+ var mergedStats = mergeStats(browserResourceInfo.stats.stats, nodeResourceInfo.stats.stats);
153
+ var mergedManifest = mergeManifest(browserResourceInfo.manifest.manifest, nodeResourceInfo.manifest.manifest);
154
+ return {
155
+ mergedStats: mergedStats,
156
+ mergedStatsFilePath: browserResourceInfo.stats.filename,
157
+ mergedManifest: mergedManifest,
158
+ mergedManifestFilePath: browserResourceInfo.manifest.filename
159
+ };
160
+ }
161
+ function updateStatsAndManifest(nodePlugin, browserPlugin, outputDir) {
162
+ var _mergeStatsAndManifest = mergeStatsAndManifest(nodePlugin, browserPlugin), mergedStats = _mergeStatsAndManifest.mergedStats, mergedStatsFilePath = _mergeStatsAndManifest.mergedStatsFilePath, mergedManifest = _mergeStatsAndManifest.mergedManifest, mergedManifestFilePath = _mergeStatsAndManifest.mergedManifestFilePath;
163
+ fs.writeFileSync(path.resolve(outputDir, mergedStatsFilePath), JSON.stringify(mergedStats, null, 2));
164
+ fs.writeFileSync(path.resolve(outputDir, mergedManifestFilePath), JSON.stringify(mergedManifest, null, 2));
165
+ }
166
+
167
+ function _array_like_to_array(arr, len) {
168
+ if (len == null || len > arr.length) len = arr.length;
169
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
170
+ return arr2;
171
+ }
172
+ function _array_without_holes(arr) {
173
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
174
+ }
175
+ function _define_property(obj, key, value) {
176
+ if (key in obj) {
177
+ Object.defineProperty(obj, key, {
178
+ value: value,
179
+ enumerable: true,
180
+ configurable: true,
181
+ writable: true
182
+ });
183
+ } else {
184
+ obj[key] = value;
185
+ }
186
+ return obj;
187
+ }
188
+ function _iterable_to_array(iter) {
189
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
190
+ }
191
+ function _non_iterable_spread() {
192
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
193
+ }
194
+ function _object_spread(target) {
195
+ for(var i = 1; i < arguments.length; i++){
196
+ var source = arguments[i] != null ? arguments[i] : {};
197
+ var ownKeys = Object.keys(source);
198
+ if (typeof Object.getOwnPropertySymbols === "function") {
199
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
200
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
201
+ }));
202
+ }
203
+ ownKeys.forEach(function(key) {
204
+ _define_property(target, key, source[key]);
205
+ });
206
+ }
207
+ return target;
208
+ }
209
+ function ownKeys(object, enumerableOnly) {
210
+ var keys = Object.keys(object);
211
+ if (Object.getOwnPropertySymbols) {
212
+ var symbols = Object.getOwnPropertySymbols(object);
213
+ keys.push.apply(keys, symbols);
214
+ }
215
+ return keys;
216
+ }
217
+ function _object_spread_props(target, source) {
218
+ source = source != null ? source : {};
219
+ if (Object.getOwnPropertyDescriptors) {
220
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
221
+ } else {
222
+ ownKeys(Object(source)).forEach(function(key) {
223
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
224
+ });
225
+ }
226
+ return target;
227
+ }
228
+ function _to_consumable_array(arr) {
229
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
230
+ }
231
+ function _unsupported_iterable_to_array(o, minLen) {
232
+ if (!o) return;
233
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
234
+ var n = Object.prototype.toString.call(o).slice(8, -1);
235
+ if (n === "Object" && o.constructor) n = o.constructor.name;
236
+ if (n === "Map" || n === "Set") return Array.from(n);
237
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
238
+ }
239
+ var require$1 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('utils.cjs.js', document.baseURI).href)));
240
+ var resolve = require$1.resolve;
241
+ var SSR_DIR = 'ssr';
242
+ var SSR_ENV_NAME = 'mf-ssr';
243
+ function setSSREnv() {
244
+ process.env['MF_DISABLE_EMIT_STATS'] = 'true';
245
+ process.env['MF_SSR_PRJ'] = 'true';
246
+ }
247
+ var isDev = function() {
248
+ return process.env.NODE_ENV === 'development';
249
+ };
250
+ function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName) {
251
+ var resetEntry = arguments.length > 4 && arguments[4] !== void 0 ? arguments[4] : true, modifyPublicPath = arguments.length > 5 && arguments[5] !== void 0 ? arguments[5] : true;
252
+ var _config_output;
253
+ var _config, _config1;
254
+ (_config = config).output || (_config.output = {});
255
+ if (modifyPublicPath) {
256
+ var _config_output1;
257
+ if (typeof ((_config_output1 = config.output) === null || _config_output1 === void 0 ? void 0 : _config_output1.publicPath) !== 'string') {
258
+ throw new Error('publicPath must be string!');
259
+ }
260
+ var publicPath = config.output.publicPath;
261
+ if (publicPath === 'auto') {
262
+ throw new Error('publicPath can not be "auto"!');
263
+ }
264
+ var publicPathWithSSRDir = "".concat(publicPath).concat(ssrDir, "/");
265
+ config.output.publicPath = publicPathWithSSRDir;
266
+ }
267
+ if (callerName === constant.CALL_NAME_MAP.RSPRESS && resetEntry) {
268
+ // set virtue entry, only need mf entry
269
+ config.entry = 'data:application/node;base64,';
270
+ }
271
+ config.target = 'async-node';
272
+ // @module-federation/node/universe-entry-chunk-tracker-plugin only export cjs
273
+ var UniverseEntryChunkTrackerPlugin = require$1('@module-federation/node/universe-entry-chunk-tracker-plugin').default;
274
+ (_config1 = config).plugins || (_config1.plugins = []);
275
+ isDev() && config.plugins.push(new UniverseEntryChunkTrackerPlugin());
276
+ var uniqueName = mfConfig.name || ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.uniqueName);
277
+ var chunkFileName = config.output.chunkFilename;
278
+ if (typeof chunkFileName === 'string' && uniqueName && !chunkFileName.includes(uniqueName)) {
279
+ var suffix = "".concat(sdk.encodeName(uniqueName), "-[contenthash].js");
280
+ config.output.chunkFilename = chunkFileName.replace('.js', suffix);
281
+ }
282
+ return config;
283
+ }
284
+ function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerName) {
285
+ var _ssrEnvConfig_output, _ssrEnvConfig_output_distPath, _ssrEnvConfig_output1, _rsbuildConfig_output_distPath, _rsbuildConfig_output;
286
+ var ssrEnvConfig = _object_spread_props(_object_spread({}, envConfig), {
287
+ tools: {
288
+ rspack: function(config, param) {
289
+ var environment = param.environment;
290
+ if (environment.name !== SSR_ENV_NAME) {
291
+ return;
292
+ }
293
+ patchSSRRspackConfig(config, mfConfig, ssrDir, callerName);
294
+ }
295
+ }
296
+ });
297
+ ssrEnvConfig.output = _object_spread_props(_object_spread({}, ssrEnvConfig.output), {
298
+ // https://rsbuild.rs/config/output/target#other-targets
299
+ // Rsbuild not support all rspack targets, so modify to async-node in modifyRspackConfig
300
+ target: 'node',
301
+ distPath: _object_spread_props(_object_spread({}, (_ssrEnvConfig_output = ssrEnvConfig.output) === null || _ssrEnvConfig_output === void 0 ? void 0 : _ssrEnvConfig_output.distPath), {
302
+ root: path.join(((_ssrEnvConfig_output1 = ssrEnvConfig.output) === null || _ssrEnvConfig_output1 === void 0 ? void 0 : (_ssrEnvConfig_output_distPath = _ssrEnvConfig_output1.distPath) === null || _ssrEnvConfig_output_distPath === void 0 ? void 0 : _ssrEnvConfig_output_distPath.root) || ((_rsbuildConfig_output = rsbuildConfig.output) === null || _rsbuildConfig_output === void 0 ? void 0 : (_rsbuildConfig_output_distPath = _rsbuildConfig_output.distPath) === null || _rsbuildConfig_output_distPath === void 0 ? void 0 : _rsbuildConfig_output_distPath.root) || '', ssrDir)
303
+ })
304
+ });
305
+ return ssrEnvConfig;
306
+ }
307
+ function createSSRMFConfig(mfConfig) {
308
+ var _mfConfig_library;
309
+ var _mfConfig_library_type;
310
+ var ssrMFConfig = _object_spread_props(_object_spread({}, mfConfig), {
311
+ exposes: _object_spread({}, mfConfig.exposes),
312
+ library: _object_spread_props(_object_spread({}, mfConfig.library), {
313
+ name: mfConfig.name,
314
+ type: (_mfConfig_library_type = (_mfConfig_library = mfConfig.library) === null || _mfConfig_library === void 0 ? void 0 : _mfConfig_library.type) !== null && _mfConfig_library_type !== void 0 ? _mfConfig_library_type : 'commonjs-module'
315
+ }),
316
+ dts: false,
317
+ dev: false,
318
+ runtimePlugins: _to_consumable_array(mfConfig.runtimePlugins || [])
319
+ });
320
+ ssrMFConfig.runtimePlugins.push(resolve('@module-federation/node/runtimePlugin'));
321
+ if (isDev()) {
322
+ ssrMFConfig.runtimePlugins.push(// @ts-ignore
323
+ resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'));
324
+ }
325
+ return ssrMFConfig;
326
+ }
327
+
74
328
  function isRegExp(target) {
75
329
  return util.types.isRegExp(target);
76
330
  }
77
331
 
78
- exports.DEFAULT_ASSET_PREFIX = DEFAULT_ASSET_PREFIX;
332
+ exports.DEFAULT_ASSET_PREFIX = constant.DEFAULT_ASSET_PREFIX;
333
+ exports.SSR_DIR = SSR_DIR;
334
+ exports.SSR_ENV_NAME = SSR_ENV_NAME;
335
+ exports.addDataFetchExposes = addDataFetchExposes;
79
336
  exports.autoDeleteSplitChunkCacheGroups = autoDeleteSplitChunkCacheGroups;
337
+ exports.createSSRMFConfig = createSSRMFConfig;
338
+ exports.createSSRREnvConfig = createSSRREnvConfig;
80
339
  exports.isRegExp = isRegExp;
340
+ exports.patchSSRRspackConfig = patchSSRRspackConfig;
341
+ exports.setSSREnv = setSSREnv;
342
+ exports.updateStatsAndManifest = updateStatsAndManifest;
@@ -0,0 +1,330 @@
1
+ import util from 'util';
2
+ import { DATA_FETCH_IDENTIFIER, DATA_FETCH_CLIENT_SUFFIX, CALL_NAME_MAP } from './constant.esm.mjs';
3
+ export { DEFAULT_ASSET_PREFIX } from './constant.esm.mjs';
4
+ import fs from 'fs-extra';
5
+ import path from 'path';
6
+ import { TEMP_DIR, encodeName } from '@module-federation/sdk';
7
+ import { createRequire } from 'node:module';
8
+
9
+ // lib-polyfill.js: include core-js,@babel/runtime,@swc/helpers,tslib.
10
+ // lib-react.js: include react,react-dom.
11
+ // lib-router.js: include react-router,react-router-dom,history,@remix-run/router.
12
+ // lib-lodash.js: include lodash,lodash-es.
13
+ // lib-antd.js: include antd.
14
+ // lib-arco.js: include @arco-design/web-react.
15
+ // lib-semi.js: include @douyinfe/semi-ui.
16
+ // lib-axios.js: include axios.
17
+ var SPLIT_CHUNK_MAP = {
18
+ REACT: 'react',
19
+ ROUTER: 'router',
20
+ LODASH: 'lib-lodash',
21
+ ANTD: 'lib-antd',
22
+ ARCO: 'lib-arco',
23
+ SEMI: 'lib-semi',
24
+ AXIOS: 'lib-axios'
25
+ };
26
+ var SHARED_SPLIT_CHUNK_MAP = {
27
+ react: SPLIT_CHUNK_MAP.REACT,
28
+ 'react-dom': SPLIT_CHUNK_MAP.REACT,
29
+ 'react-router': SPLIT_CHUNK_MAP.ROUTER,
30
+ 'react-router-dom': SPLIT_CHUNK_MAP.ROUTER,
31
+ '@remix-run/router': SPLIT_CHUNK_MAP.ROUTER,
32
+ lodash: SPLIT_CHUNK_MAP.LODASH,
33
+ 'lodash-es': SPLIT_CHUNK_MAP.LODASH,
34
+ antd: SPLIT_CHUNK_MAP.ANTD,
35
+ '@arco-design/web-react': SPLIT_CHUNK_MAP.ARCO,
36
+ '@douyinfe/semi-ui': SPLIT_CHUNK_MAP.SEMI,
37
+ axios: SPLIT_CHUNK_MAP.AXIOS
38
+ };
39
+ function autoDeleteSplitChunkCacheGroups(mfConfig, splitChunks) {
40
+ if (!mfConfig.shared) {
41
+ return;
42
+ }
43
+ if (!splitChunks || !(splitChunks === null || splitChunks === void 0 ? void 0 : splitChunks.cacheGroups)) {
44
+ return;
45
+ }
46
+ var arrayShared = Array.isArray(mfConfig.shared) ? mfConfig.shared : Object.keys(mfConfig.shared);
47
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
48
+ try {
49
+ for(var _iterator = arrayShared[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
50
+ var shared = _step.value;
51
+ var splitChunkKey = SHARED_SPLIT_CHUNK_MAP[shared];
52
+ if (!splitChunkKey) {
53
+ continue;
54
+ }
55
+ if (splitChunks.cacheGroups[splitChunkKey]) {
56
+ delete splitChunks.cacheGroups[splitChunkKey];
57
+ }
58
+ }
59
+ } catch (err) {
60
+ _didIteratorError = true;
61
+ _iteratorError = err;
62
+ } finally{
63
+ try {
64
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
65
+ _iterator.return();
66
+ }
67
+ } finally{
68
+ if (_didIteratorError) {
69
+ throw _iteratorError;
70
+ }
71
+ }
72
+ }
73
+ return splitChunks;
74
+ }
75
+
76
+ function _type_of(obj) {
77
+ "@swc/helpers - typeof";
78
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
79
+ }
80
+ var addDataFetchExpose = function(exposes, key, filepath) {
81
+ var suffix = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : '';
82
+ if (!fs.existsSync(filepath)) {
83
+ return false;
84
+ }
85
+ var dataFetchKey = key === '.' ? "./".concat(DATA_FETCH_IDENTIFIER).concat(suffix) : "".concat(key, ".").concat(DATA_FETCH_IDENTIFIER).concat(suffix);
86
+ if (exposes[dataFetchKey] && exposes[dataFetchKey] !== filepath) {
87
+ throw new Error("data fetch key ".concat(dataFetchKey, " already exists, please modify this expose key, do not end with '").concat(DATA_FETCH_IDENTIFIER, "' or '").concat(DATA_FETCH_CLIENT_SUFFIX, "'"));
88
+ }
89
+ exposes[dataFetchKey] = filepath;
90
+ return dataFetchKey;
91
+ };
92
+ var addExcludeDtsSuffix = function(filepath) {
93
+ return "".concat(filepath, "?exclude-mf-dts=true");
94
+ };
95
+ function addDataFetchExposes(exposes, isServer) {
96
+ if ((typeof exposes === "undefined" ? "undefined" : _type_of(exposes)) !== 'object' || Array.isArray(exposes)) {
97
+ return;
98
+ }
99
+ if (Object.keys(exposes).length === 0) {
100
+ return;
101
+ }
102
+ var tempDataFetchFilepath = path.resolve(process.cwd(), "node_modules/".concat(TEMP_DIR, "/data-fetch-fallback.ts"));
103
+ var content = "export const fetchData=()=>{throw new Error('should not be called')};";
104
+ fs.ensureDirSync(path.dirname(tempDataFetchFilepath));
105
+ fs.writeFileSync(tempDataFetchFilepath, content);
106
+ Object.keys(exposes).forEach(function(key) {
107
+ var expose = exposes[key];
108
+ if (typeof expose !== 'string') {
109
+ return;
110
+ }
111
+ var absPath = path.resolve(process.cwd(), expose);
112
+ var dataFetchPath = "".concat(absPath.replace(path.extname(absPath), ''), ".").concat(DATA_FETCH_IDENTIFIER, ".ts");
113
+ var dataFetchClientPath = "".concat(absPath.replace(path.extname(absPath), ''), ".").concat(DATA_FETCH_IDENTIFIER, ".client.ts");
114
+ var dateFetchClientKey = addDataFetchExpose(exposes, key, dataFetchClientPath, DATA_FETCH_CLIENT_SUFFIX);
115
+ if (!isServer && dateFetchClientKey) {
116
+ exposes[dateFetchClientKey.replace(DATA_FETCH_CLIENT_SUFFIX, '')] = addExcludeDtsSuffix(tempDataFetchFilepath);
117
+ return;
118
+ }
119
+ var dataFetchKey = addDataFetchExpose(exposes, key, dataFetchPath);
120
+ if (dataFetchKey && fs.existsSync(dataFetchClientPath)) {
121
+ exposes["".concat(dataFetchKey).concat(DATA_FETCH_CLIENT_SUFFIX)] = addExcludeDtsSuffix(tempDataFetchFilepath);
122
+ }
123
+ });
124
+ }
125
+
126
+ function mergeStats(browserStats, nodeStats) {
127
+ var ssrRemoteEntry = nodeStats.metaData.remoteEntry;
128
+ browserStats.metaData.ssrRemoteEntry = ssrRemoteEntry;
129
+ if ('publicPath' in browserStats.metaData) {
130
+ // @ts-ignore nodeStats has the same structure with browserStats
131
+ browserStats.metaData.ssrPublicPath = nodeStats.metaData.publicPath;
132
+ }
133
+ return browserStats;
134
+ }
135
+ function mergeManifest(browserManifest, nodeManifest) {
136
+ var ssrRemoteEntry = nodeManifest.metaData.remoteEntry;
137
+ browserManifest.metaData.ssrRemoteEntry = ssrRemoteEntry;
138
+ if ('publicPath' in browserManifest.metaData) {
139
+ // @ts-ignore nodeStats has the same structure with browserStats
140
+ browserManifest.metaData.ssrPublicPath = nodeManifest.metaData.publicPath;
141
+ }
142
+ return browserManifest;
143
+ }
144
+ function mergeStatsAndManifest(nodePlugin, browserPlugin) {
145
+ var nodeResourceInfo = nodePlugin.statsResourceInfo;
146
+ var browserResourceInfo = browserPlugin.statsResourceInfo;
147
+ if (!browserResourceInfo || !nodeResourceInfo || !browserResourceInfo.stats || !nodeResourceInfo.stats || !browserResourceInfo.manifest || !nodeResourceInfo.manifest) {
148
+ throw new Error('can not get browserResourceInfo or nodeResourceInfo');
149
+ }
150
+ var mergedStats = mergeStats(browserResourceInfo.stats.stats, nodeResourceInfo.stats.stats);
151
+ var mergedManifest = mergeManifest(browserResourceInfo.manifest.manifest, nodeResourceInfo.manifest.manifest);
152
+ return {
153
+ mergedStats: mergedStats,
154
+ mergedStatsFilePath: browserResourceInfo.stats.filename,
155
+ mergedManifest: mergedManifest,
156
+ mergedManifestFilePath: browserResourceInfo.manifest.filename
157
+ };
158
+ }
159
+ function updateStatsAndManifest(nodePlugin, browserPlugin, outputDir) {
160
+ var _mergeStatsAndManifest = mergeStatsAndManifest(nodePlugin, browserPlugin), mergedStats = _mergeStatsAndManifest.mergedStats, mergedStatsFilePath = _mergeStatsAndManifest.mergedStatsFilePath, mergedManifest = _mergeStatsAndManifest.mergedManifest, mergedManifestFilePath = _mergeStatsAndManifest.mergedManifestFilePath;
161
+ fs.writeFileSync(path.resolve(outputDir, mergedStatsFilePath), JSON.stringify(mergedStats, null, 2));
162
+ fs.writeFileSync(path.resolve(outputDir, mergedManifestFilePath), JSON.stringify(mergedManifest, null, 2));
163
+ }
164
+
165
+ function _array_like_to_array(arr, len) {
166
+ if (len == null || len > arr.length) len = arr.length;
167
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
168
+ return arr2;
169
+ }
170
+ function _array_without_holes(arr) {
171
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
172
+ }
173
+ function _define_property(obj, key, value) {
174
+ if (key in obj) {
175
+ Object.defineProperty(obj, key, {
176
+ value: value,
177
+ enumerable: true,
178
+ configurable: true,
179
+ writable: true
180
+ });
181
+ } else {
182
+ obj[key] = value;
183
+ }
184
+ return obj;
185
+ }
186
+ function _iterable_to_array(iter) {
187
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
188
+ }
189
+ function _non_iterable_spread() {
190
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
191
+ }
192
+ function _object_spread(target) {
193
+ for(var i = 1; i < arguments.length; i++){
194
+ var source = arguments[i] != null ? arguments[i] : {};
195
+ var ownKeys = Object.keys(source);
196
+ if (typeof Object.getOwnPropertySymbols === "function") {
197
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
198
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
199
+ }));
200
+ }
201
+ ownKeys.forEach(function(key) {
202
+ _define_property(target, key, source[key]);
203
+ });
204
+ }
205
+ return target;
206
+ }
207
+ function ownKeys(object, enumerableOnly) {
208
+ var keys = Object.keys(object);
209
+ if (Object.getOwnPropertySymbols) {
210
+ var symbols = Object.getOwnPropertySymbols(object);
211
+ keys.push.apply(keys, symbols);
212
+ }
213
+ return keys;
214
+ }
215
+ function _object_spread_props(target, source) {
216
+ source = source != null ? source : {};
217
+ if (Object.getOwnPropertyDescriptors) {
218
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
219
+ } else {
220
+ ownKeys(Object(source)).forEach(function(key) {
221
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
222
+ });
223
+ }
224
+ return target;
225
+ }
226
+ function _to_consumable_array(arr) {
227
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
228
+ }
229
+ function _unsupported_iterable_to_array(o, minLen) {
230
+ if (!o) return;
231
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
232
+ var n = Object.prototype.toString.call(o).slice(8, -1);
233
+ if (n === "Object" && o.constructor) n = o.constructor.name;
234
+ if (n === "Map" || n === "Set") return Array.from(n);
235
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
236
+ }
237
+ var require = createRequire(import.meta.url);
238
+ var resolve = require.resolve;
239
+ var SSR_DIR = 'ssr';
240
+ var SSR_ENV_NAME = 'mf-ssr';
241
+ function setSSREnv() {
242
+ process.env['MF_DISABLE_EMIT_STATS'] = 'true';
243
+ process.env['MF_SSR_PRJ'] = 'true';
244
+ }
245
+ var isDev = function() {
246
+ return process.env.NODE_ENV === 'development';
247
+ };
248
+ function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName) {
249
+ var resetEntry = arguments.length > 4 && arguments[4] !== void 0 ? arguments[4] : true, modifyPublicPath = arguments.length > 5 && arguments[5] !== void 0 ? arguments[5] : true;
250
+ var _config_output;
251
+ var _config, _config1;
252
+ (_config = config).output || (_config.output = {});
253
+ if (modifyPublicPath) {
254
+ var _config_output1;
255
+ if (typeof ((_config_output1 = config.output) === null || _config_output1 === void 0 ? void 0 : _config_output1.publicPath) !== 'string') {
256
+ throw new Error('publicPath must be string!');
257
+ }
258
+ var publicPath = config.output.publicPath;
259
+ if (publicPath === 'auto') {
260
+ throw new Error('publicPath can not be "auto"!');
261
+ }
262
+ var publicPathWithSSRDir = "".concat(publicPath).concat(ssrDir, "/");
263
+ config.output.publicPath = publicPathWithSSRDir;
264
+ }
265
+ if (callerName === CALL_NAME_MAP.RSPRESS && resetEntry) {
266
+ // set virtue entry, only need mf entry
267
+ config.entry = 'data:application/node;base64,';
268
+ }
269
+ config.target = 'async-node';
270
+ // @module-federation/node/universe-entry-chunk-tracker-plugin only export cjs
271
+ var UniverseEntryChunkTrackerPlugin = require('@module-federation/node/universe-entry-chunk-tracker-plugin').default;
272
+ (_config1 = config).plugins || (_config1.plugins = []);
273
+ isDev() && config.plugins.push(new UniverseEntryChunkTrackerPlugin());
274
+ var uniqueName = mfConfig.name || ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.uniqueName);
275
+ var chunkFileName = config.output.chunkFilename;
276
+ if (typeof chunkFileName === 'string' && uniqueName && !chunkFileName.includes(uniqueName)) {
277
+ var suffix = "".concat(encodeName(uniqueName), "-[contenthash].js");
278
+ config.output.chunkFilename = chunkFileName.replace('.js', suffix);
279
+ }
280
+ return config;
281
+ }
282
+ function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerName) {
283
+ var _ssrEnvConfig_output, _ssrEnvConfig_output_distPath, _ssrEnvConfig_output1, _rsbuildConfig_output_distPath, _rsbuildConfig_output;
284
+ var ssrEnvConfig = _object_spread_props(_object_spread({}, envConfig), {
285
+ tools: {
286
+ rspack: function(config, param) {
287
+ var environment = param.environment;
288
+ if (environment.name !== SSR_ENV_NAME) {
289
+ return;
290
+ }
291
+ patchSSRRspackConfig(config, mfConfig, ssrDir, callerName);
292
+ }
293
+ }
294
+ });
295
+ ssrEnvConfig.output = _object_spread_props(_object_spread({}, ssrEnvConfig.output), {
296
+ // https://rsbuild.rs/config/output/target#other-targets
297
+ // Rsbuild not support all rspack targets, so modify to async-node in modifyRspackConfig
298
+ target: 'node',
299
+ distPath: _object_spread_props(_object_spread({}, (_ssrEnvConfig_output = ssrEnvConfig.output) === null || _ssrEnvConfig_output === void 0 ? void 0 : _ssrEnvConfig_output.distPath), {
300
+ root: path.join(((_ssrEnvConfig_output1 = ssrEnvConfig.output) === null || _ssrEnvConfig_output1 === void 0 ? void 0 : (_ssrEnvConfig_output_distPath = _ssrEnvConfig_output1.distPath) === null || _ssrEnvConfig_output_distPath === void 0 ? void 0 : _ssrEnvConfig_output_distPath.root) || ((_rsbuildConfig_output = rsbuildConfig.output) === null || _rsbuildConfig_output === void 0 ? void 0 : (_rsbuildConfig_output_distPath = _rsbuildConfig_output.distPath) === null || _rsbuildConfig_output_distPath === void 0 ? void 0 : _rsbuildConfig_output_distPath.root) || '', ssrDir)
301
+ })
302
+ });
303
+ return ssrEnvConfig;
304
+ }
305
+ function createSSRMFConfig(mfConfig) {
306
+ var _mfConfig_library;
307
+ var _mfConfig_library_type;
308
+ var ssrMFConfig = _object_spread_props(_object_spread({}, mfConfig), {
309
+ exposes: _object_spread({}, mfConfig.exposes),
310
+ library: _object_spread_props(_object_spread({}, mfConfig.library), {
311
+ name: mfConfig.name,
312
+ type: (_mfConfig_library_type = (_mfConfig_library = mfConfig.library) === null || _mfConfig_library === void 0 ? void 0 : _mfConfig_library.type) !== null && _mfConfig_library_type !== void 0 ? _mfConfig_library_type : 'commonjs-module'
313
+ }),
314
+ dts: false,
315
+ dev: false,
316
+ runtimePlugins: _to_consumable_array(mfConfig.runtimePlugins || [])
317
+ });
318
+ ssrMFConfig.runtimePlugins.push(resolve('@module-federation/node/runtimePlugin'));
319
+ if (isDev()) {
320
+ ssrMFConfig.runtimePlugins.push(// @ts-ignore
321
+ resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'));
322
+ }
323
+ return ssrMFConfig;
324
+ }
325
+
326
+ function isRegExp(target) {
327
+ return util.types.isRegExp(target);
328
+ }
329
+
330
+ export { SSR_DIR, SSR_ENV_NAME, addDataFetchExposes, autoDeleteSplitChunkCacheGroups, createSSRMFConfig, createSSRREnvConfig, isRegExp, patchSSRRspackConfig, setSSREnv, updateStatsAndManifest };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/rsbuild-plugin",
3
- "version": "0.14.3",
3
+ "version": "0.16.0",
4
4
  "description": "Module Federation plugin for Rsbuild",
5
5
  "homepage": "https://module-federation.io",
6
6
  "bugs": {
@@ -15,17 +15,22 @@
15
15
  "exports": {
16
16
  ".": {
17
17
  "types": "./dist/index.cjs.d.ts",
18
- "import": "./dist/index.esm.js",
18
+ "import": "./dist/index.esm.mjs",
19
19
  "require": "./dist/index.cjs.js"
20
20
  },
21
21
  "./utils": {
22
22
  "types": "./dist/utils.cjs.d.ts",
23
- "import": "./dist/utils.esm.js",
23
+ "import": "./dist/utils.esm.mjs",
24
24
  "require": "./dist/utils.cjs.js"
25
+ },
26
+ "./constant": {
27
+ "types": "./dist/constant.cjs.d.ts",
28
+ "import": "./dist/constant.esm.mjs",
29
+ "require": "./dist/constant.cjs.js"
25
30
  }
26
31
  },
27
32
  "main": "./dist/index.cjs.js",
28
- "module": "./dist/index.esm.js",
33
+ "module": "./dist/index.esm.mjs",
29
34
  "typesVersions": {
30
35
  "*": {
31
36
  ".": [
@@ -33,6 +38,9 @@
33
38
  ],
34
39
  "utils": [
35
40
  "./dist/utils.cjs.d.ts"
41
+ ],
42
+ "constant": [
43
+ "./dist/constant.cjs.d.ts"
36
44
  ]
37
45
  }
38
46
  },
@@ -40,14 +48,16 @@
40
48
  "dist"
41
49
  ],
42
50
  "dependencies": {
43
- "@module-federation/sdk": "0.14.3",
44
- "@module-federation/enhanced": "0.14.3"
51
+ "fs-extra": "11.3.0",
52
+ "@module-federation/sdk": "0.16.0",
53
+ "@module-federation/enhanced": "0.16.0",
54
+ "@module-federation/node": "2.7.8"
45
55
  },
46
56
  "devDependencies": {
47
- "@rsbuild/core": "^1.3.17"
57
+ "@rsbuild/core": "^1.3.21"
48
58
  },
49
59
  "peerDependencies": {
50
- "@rsbuild/core": "1.x"
60
+ "@rsbuild/core": "^1.3.21"
51
61
  },
52
62
  "peerDependenciesMeta": {
53
63
  "@rsbuild/core": {