@module-federation/rsbuild-plugin 0.0.0-next-20250924123930 → 0.0.0-perf-devtools-20260106124142
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/{src/cli → cli}/index.d.ts +4 -1
- package/dist/constant.d.ts +9 -1
- package/dist/constant.js +59 -0
- package/dist/constant.mjs +10 -0
- package/dist/index.js +486 -0
- package/dist/index.mjs +422 -0
- package/dist/{src/utils → utils}/index.d.ts +1 -0
- package/dist/utils/manifest.d.ts +11 -0
- package/dist/{src/utils → utils}/ssr.d.ts +2 -1
- package/dist/utils.js +264 -0
- package/dist/utils.mjs +186 -0
- package/package.json +18 -17
- package/dist/constant.cjs.js +0 -19
- package/dist/constant.cjs.js.map +0 -1
- package/dist/constant.esm.d.ts +0 -1
- package/dist/constant.esm.mjs +0 -12
- package/dist/constant.esm.mjs.map +0 -1
- package/dist/index.cjs.js +0 -360
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.esm.d.ts +0 -1
- package/dist/index.esm.mjs +0 -349
- package/dist/index.esm.mjs.map +0 -1
- package/dist/src/constant.d.ts +0 -9
- package/dist/src/utils/manifest.d.ts +0 -5
- package/dist/utils.cjs.js +0 -329
- package/dist/utils.cjs.js.map +0 -1
- package/dist/utils.d.ts +0 -1
- package/dist/utils.esm.d.ts +0 -1
- package/dist/utils.esm.mjs +0 -317
- package/dist/utils.esm.mjs.map +0 -1
- /package/dist/{src/logger.d.ts → logger.d.ts} +0 -0
- /package/dist/{src/utils → utils}/addDataFetchExposes.d.ts +0 -0
- /package/dist/{src/utils → utils}/autoDeleteSplitChunkCacheGroups.d.ts +0 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import { parseOptions } from "@module-federation/enhanced";
|
|
2
|
+
import { ModuleFederationPlugin, PLUGIN_NAME } from "@module-federation/enhanced/rspack";
|
|
3
|
+
import { TEMP_DIR, createLogger, createModuleFederationConfig, encodeName, getManifestFileName, isRequiredVersion } from "@module-federation/sdk";
|
|
4
|
+
import util from "util";
|
|
5
|
+
import fs_extra from "fs-extra";
|
|
6
|
+
import path from "path";
|
|
7
|
+
import { createRequire } from "node:module";
|
|
8
|
+
var package_namespaceObject = JSON.parse('{"UU":"@module-federation/rsbuild-plugin"}');
|
|
9
|
+
const logger = createLogger('[ Module Federation Rsbuild Plugin ]');
|
|
10
|
+
const src_logger = logger;
|
|
11
|
+
const DATA_FETCH_CLIENT_SUFFIX = '.client';
|
|
12
|
+
const CALL_NAME_MAP = {
|
|
13
|
+
RSPRESS: 'rspress',
|
|
14
|
+
RSLIB: 'rslib'
|
|
15
|
+
};
|
|
16
|
+
const SPLIT_CHUNK_MAP = {
|
|
17
|
+
REACT: 'react',
|
|
18
|
+
ROUTER: 'router',
|
|
19
|
+
LODASH: 'lib-lodash',
|
|
20
|
+
ANTD: 'lib-antd',
|
|
21
|
+
ARCO: 'lib-arco',
|
|
22
|
+
SEMI: 'lib-semi',
|
|
23
|
+
AXIOS: 'lib-axios'
|
|
24
|
+
};
|
|
25
|
+
const SHARED_SPLIT_CHUNK_MAP = {
|
|
26
|
+
react: SPLIT_CHUNK_MAP.REACT,
|
|
27
|
+
'react-dom': SPLIT_CHUNK_MAP.REACT,
|
|
28
|
+
'react-router': SPLIT_CHUNK_MAP.ROUTER,
|
|
29
|
+
'react-router-dom': SPLIT_CHUNK_MAP.ROUTER,
|
|
30
|
+
'@remix-run/router': SPLIT_CHUNK_MAP.ROUTER,
|
|
31
|
+
lodash: SPLIT_CHUNK_MAP.LODASH,
|
|
32
|
+
'lodash-es': SPLIT_CHUNK_MAP.LODASH,
|
|
33
|
+
antd: SPLIT_CHUNK_MAP.ANTD,
|
|
34
|
+
'@arco-design/web-react': SPLIT_CHUNK_MAP.ARCO,
|
|
35
|
+
'@douyinfe/semi-ui': SPLIT_CHUNK_MAP.SEMI,
|
|
36
|
+
axios: SPLIT_CHUNK_MAP.AXIOS
|
|
37
|
+
};
|
|
38
|
+
function autoDeleteSplitChunkCacheGroups(mfConfig, splitChunks) {
|
|
39
|
+
if (!mfConfig.shared) return;
|
|
40
|
+
if (!splitChunks || !(null == splitChunks ? void 0 : splitChunks.cacheGroups)) return;
|
|
41
|
+
const arrayShared = Array.isArray(mfConfig.shared) ? mfConfig.shared : Object.keys(mfConfig.shared);
|
|
42
|
+
for (const shared of arrayShared){
|
|
43
|
+
const splitChunkKey = SHARED_SPLIT_CHUNK_MAP[shared];
|
|
44
|
+
if (splitChunkKey) {
|
|
45
|
+
if (splitChunks.cacheGroups[splitChunkKey]) delete splitChunks.cacheGroups[splitChunkKey];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return splitChunks;
|
|
49
|
+
}
|
|
50
|
+
const addDataFetchExpose = (exposes, key, filepath, suffix = '')=>{
|
|
51
|
+
if (!fs_extra.existsSync(filepath)) return false;
|
|
52
|
+
const dataFetchKey = '.' === key ? `./data${suffix}` : `${key}.data${suffix}`;
|
|
53
|
+
if (exposes[dataFetchKey] && exposes[dataFetchKey] !== filepath) throw new Error(`data fetch key ${dataFetchKey} already exists, please modify this expose key, do not end with 'data' or '${DATA_FETCH_CLIENT_SUFFIX}'`);
|
|
54
|
+
exposes[dataFetchKey] = filepath;
|
|
55
|
+
return dataFetchKey;
|
|
56
|
+
};
|
|
57
|
+
const addExcludeDtsSuffix = (filepath)=>`${filepath}?exclude-mf-dts=true`;
|
|
58
|
+
function addDataFetchExposes(exposes, isServer) {
|
|
59
|
+
if ('object' != typeof exposes || Array.isArray(exposes)) return;
|
|
60
|
+
if (0 === Object.keys(exposes).length) return;
|
|
61
|
+
const tempDataFetch = path.resolve(process.cwd(), `node_modules/${TEMP_DIR}`);
|
|
62
|
+
const content = "export const fetchData=()=>{throw new Error('should not be called')};";
|
|
63
|
+
fs_extra.ensureDirSync(tempDataFetch);
|
|
64
|
+
Object.keys(exposes).forEach((key, index)=>{
|
|
65
|
+
const expose = exposes[key];
|
|
66
|
+
if ('string' != typeof expose) return;
|
|
67
|
+
const absPath = path.resolve(process.cwd(), expose);
|
|
68
|
+
const dataFetchPath = `${absPath.replace(path.extname(absPath), '')}.data.ts`;
|
|
69
|
+
const dataFetchClientPath = `${absPath.replace(path.extname(absPath), '')}.data.client.ts`;
|
|
70
|
+
const tempFile = path.join(tempDataFetch, `data-fetch-fallback${index}.ts`);
|
|
71
|
+
fs_extra.writeFileSync(tempFile, content);
|
|
72
|
+
const dateFetchClientKey = addDataFetchExpose(exposes, key, dataFetchClientPath, DATA_FETCH_CLIENT_SUFFIX);
|
|
73
|
+
if (!isServer && dateFetchClientKey) {
|
|
74
|
+
exposes[dateFetchClientKey.replace(DATA_FETCH_CLIENT_SUFFIX, '')] = addExcludeDtsSuffix(tempFile);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const dataFetchKey = addDataFetchExpose(exposes, key, dataFetchPath);
|
|
78
|
+
if (dataFetchKey && fs_extra.existsSync(dataFetchClientPath)) exposes[`${dataFetchKey}${DATA_FETCH_CLIENT_SUFFIX}`] = addExcludeDtsSuffix(tempFile);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function mergeStats(browserStats, nodeStats) {
|
|
82
|
+
const ssrRemoteEntry = nodeStats.metaData.remoteEntry;
|
|
83
|
+
browserStats.metaData.ssrRemoteEntry = ssrRemoteEntry;
|
|
84
|
+
if ('publicPath' in browserStats.metaData) browserStats.metaData.ssrPublicPath = nodeStats.metaData.publicPath;
|
|
85
|
+
return browserStats;
|
|
86
|
+
}
|
|
87
|
+
function mergeManifest(browserManifest, nodeManifest) {
|
|
88
|
+
const ssrRemoteEntry = nodeManifest.metaData.remoteEntry;
|
|
89
|
+
browserManifest.metaData.ssrRemoteEntry = ssrRemoteEntry;
|
|
90
|
+
if ('publicPath' in browserManifest.metaData) browserManifest.metaData.ssrPublicPath = nodeManifest.metaData.publicPath;
|
|
91
|
+
return browserManifest;
|
|
92
|
+
}
|
|
93
|
+
function mergeStatsAndManifest(nodeAssets, browserAssets) {
|
|
94
|
+
const { stats: browserStats, manifest: browserManifest } = browserAssets;
|
|
95
|
+
const { stats: nodeStats, manifest: nodeManifest } = nodeAssets;
|
|
96
|
+
if (!browserStats || !nodeStats || !browserManifest || !nodeManifest) throw new Error('Failed to read stats or manifest assets for merge');
|
|
97
|
+
const mergedStats = mergeStats(browserStats.data, nodeStats.data);
|
|
98
|
+
const mergedManifest = mergeManifest(browserManifest.data, nodeManifest.data);
|
|
99
|
+
return {
|
|
100
|
+
mergedStats: mergedStats,
|
|
101
|
+
mergedStatsFilePath: browserStats.filename,
|
|
102
|
+
mergedManifest: mergedManifest,
|
|
103
|
+
mergedManifestFilePath: browserManifest.filename
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function updateStatsAndManifest(nodeAssets, browserAssets, outputDir) {
|
|
107
|
+
const { mergedStats, mergedStatsFilePath, mergedManifest, mergedManifestFilePath } = mergeStatsAndManifest(nodeAssets, browserAssets);
|
|
108
|
+
fs_extra.writeFileSync(path.resolve(outputDir, mergedStatsFilePath), JSON.stringify(mergedStats, null, 2));
|
|
109
|
+
fs_extra.writeFileSync(path.resolve(outputDir, mergedManifestFilePath), JSON.stringify(mergedManifest, null, 2));
|
|
110
|
+
}
|
|
111
|
+
const ssr_require = createRequire(import.meta.url);
|
|
112
|
+
const resolve = ssr_require.resolve;
|
|
113
|
+
const SSR_DIR = 'ssr';
|
|
114
|
+
const SSR_ENV_NAME = 'mf-ssr';
|
|
115
|
+
function setSSREnv() {
|
|
116
|
+
process.env['MF_SSR_PRJ'] = 'true';
|
|
117
|
+
}
|
|
118
|
+
const isDev = ()=>'development' === process.env['NODE_ENV'];
|
|
119
|
+
function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry = true, modifyPublicPath = true) {
|
|
120
|
+
var _config_output;
|
|
121
|
+
config.output ||= {};
|
|
122
|
+
if (modifyPublicPath) {
|
|
123
|
+
var _config_output1;
|
|
124
|
+
if ('string' != typeof (null == (_config_output1 = config.output) ? void 0 : _config_output1.publicPath)) throw new Error('publicPath must be string!');
|
|
125
|
+
const publicPath = config.output.publicPath;
|
|
126
|
+
if ('auto' === publicPath) throw new Error('publicPath can not be "auto"!');
|
|
127
|
+
const publicPathWithSSRDir = `${publicPath}${ssrDir}/`;
|
|
128
|
+
config.output.publicPath = publicPathWithSSRDir;
|
|
129
|
+
}
|
|
130
|
+
if (callerName === CALL_NAME_MAP.RSPRESS && resetEntry) config.entry = 'data:application/node;base64,';
|
|
131
|
+
config.target = 'async-node';
|
|
132
|
+
const UniverseEntryChunkTrackerPlugin = ssr_require('@module-federation/node/universe-entry-chunk-tracker-plugin').default;
|
|
133
|
+
config.plugins ||= [];
|
|
134
|
+
isDev() && config.plugins.push(new UniverseEntryChunkTrackerPlugin());
|
|
135
|
+
const uniqueName = mfConfig.name || (null == (_config_output = config.output) ? void 0 : _config_output.uniqueName);
|
|
136
|
+
const chunkFileName = config.output.chunkFilename;
|
|
137
|
+
if ('string' == typeof chunkFileName && uniqueName && !chunkFileName.includes(uniqueName)) {
|
|
138
|
+
const suffix = `${encodeName(uniqueName)}-[contenthash].js`;
|
|
139
|
+
config.output.chunkFilename = chunkFileName.replace('.js', suffix);
|
|
140
|
+
}
|
|
141
|
+
return config;
|
|
142
|
+
}
|
|
143
|
+
function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerName) {
|
|
144
|
+
var _ssrEnvConfig_output, _ssrEnvConfig_output_distPath, _ssrEnvConfig_output1, _rsbuildConfig_output_distPath, _rsbuildConfig_output;
|
|
145
|
+
const ssrEnvConfig = {
|
|
146
|
+
...envConfig,
|
|
147
|
+
tools: {
|
|
148
|
+
rspack: (config, { environment })=>{
|
|
149
|
+
if (environment.name !== SSR_ENV_NAME) return;
|
|
150
|
+
patchSSRRspackConfig(config, mfConfig, ssrDir, callerName);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
ssrEnvConfig.output = {
|
|
155
|
+
...ssrEnvConfig.output,
|
|
156
|
+
target: 'node',
|
|
157
|
+
distPath: {
|
|
158
|
+
...null == (_ssrEnvConfig_output = ssrEnvConfig.output) ? void 0 : _ssrEnvConfig_output.distPath,
|
|
159
|
+
root: path.join((null == (_ssrEnvConfig_output1 = ssrEnvConfig.output) ? void 0 : null == (_ssrEnvConfig_output_distPath = _ssrEnvConfig_output1.distPath) ? void 0 : _ssrEnvConfig_output_distPath.root) || (null == (_rsbuildConfig_output = rsbuildConfig.output) ? void 0 : null == (_rsbuildConfig_output_distPath = _rsbuildConfig_output.distPath) ? void 0 : _rsbuildConfig_output_distPath.root) || '', ssrDir)
|
|
160
|
+
},
|
|
161
|
+
emitAssets: true
|
|
162
|
+
};
|
|
163
|
+
return ssrEnvConfig;
|
|
164
|
+
}
|
|
165
|
+
function createSSRMFConfig(mfConfig) {
|
|
166
|
+
var _mfConfig_library;
|
|
167
|
+
const ssrMFConfig = {
|
|
168
|
+
...mfConfig,
|
|
169
|
+
exposes: {
|
|
170
|
+
...mfConfig.exposes
|
|
171
|
+
},
|
|
172
|
+
library: {
|
|
173
|
+
...mfConfig.library,
|
|
174
|
+
name: mfConfig.name,
|
|
175
|
+
type: (null == (_mfConfig_library = mfConfig.library) ? void 0 : _mfConfig_library.type) ?? 'commonjs-module'
|
|
176
|
+
},
|
|
177
|
+
dts: false,
|
|
178
|
+
dev: false,
|
|
179
|
+
runtimePlugins: [
|
|
180
|
+
...mfConfig.runtimePlugins || []
|
|
181
|
+
]
|
|
182
|
+
};
|
|
183
|
+
ssrMFConfig.runtimePlugins.push(resolve('@module-federation/node/runtimePlugin'));
|
|
184
|
+
if (isDev()) ssrMFConfig.runtimePlugins.push(resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'));
|
|
185
|
+
return ssrMFConfig;
|
|
186
|
+
}
|
|
187
|
+
function isRegExp(target) {
|
|
188
|
+
return util.types.isRegExp(target);
|
|
189
|
+
}
|
|
190
|
+
const RSBUILD_PLUGIN_MODULE_FEDERATION_NAME = 'rsbuild:module-federation-enhanced';
|
|
191
|
+
const RSBUILD_PLUGIN_NAME = '@module-federation/rsbuild-plugin';
|
|
192
|
+
const LIB_FORMAT = [
|
|
193
|
+
'umd',
|
|
194
|
+
'modern-module'
|
|
195
|
+
];
|
|
196
|
+
const DEFAULT_MF_ENVIRONMENT_NAME = 'mf';
|
|
197
|
+
function isStoryBook(rsbuildConfig) {
|
|
198
|
+
var _rsbuildConfig_plugins;
|
|
199
|
+
if (null == (_rsbuildConfig_plugins = rsbuildConfig.plugins) ? void 0 : _rsbuildConfig_plugins.find((p)=>p && 'name' in p && 'module-federation-storybook-plugin' === p.name)) return true;
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
function isMFFormat(bundlerConfig) {
|
|
203
|
+
var _bundlerConfig_output;
|
|
204
|
+
const library = null == (_bundlerConfig_output = bundlerConfig.output) ? void 0 : _bundlerConfig_output.library;
|
|
205
|
+
if (bundlerConfig.name === SSR_ENV_NAME) return true;
|
|
206
|
+
return !('object' == typeof library && !Array.isArray(library) && 'type' in library && (LIB_FORMAT.includes(library.type) || /commonjs/.test(library.type)));
|
|
207
|
+
}
|
|
208
|
+
const isSSRConfig = (bundlerConfigName)=>Boolean(bundlerConfigName === SSR_ENV_NAME);
|
|
209
|
+
const isRspressSSGConfig = (bundlerConfigName)=>"node" === bundlerConfigName;
|
|
210
|
+
const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
211
|
+
name: RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
|
|
212
|
+
setup: (api)=>{
|
|
213
|
+
const { ssr, ssrDir = SSR_DIR, environment = DEFAULT_MF_ENVIRONMENT_NAME } = rsbuildOptions || {};
|
|
214
|
+
const { callerName } = api.context;
|
|
215
|
+
const originalRsbuildConfig = api.getRsbuildConfig();
|
|
216
|
+
if (!callerName) throw new Error('`callerName` is undefined. Please ensure the @rsbuild/core version is higher than 1.3.21 .');
|
|
217
|
+
const isRslib = callerName === CALL_NAME_MAP.RSLIB;
|
|
218
|
+
const isRspress = callerName === CALL_NAME_MAP.RSPRESS;
|
|
219
|
+
const isSSR = Boolean(ssr);
|
|
220
|
+
if (isSSR && !isStoryBook(originalRsbuildConfig)) {
|
|
221
|
+
var _rsbuildConfig_environments;
|
|
222
|
+
if (!isRslib && !isRspress) throw new Error("'ssr' option is only supported in rslib.");
|
|
223
|
+
const rsbuildConfig = api.getRsbuildConfig();
|
|
224
|
+
if (!(null == (_rsbuildConfig_environments = rsbuildConfig.environments) ? void 0 : _rsbuildConfig_environments[environment]) || Object.keys(rsbuildConfig.environments).some((key)=>key.startsWith(environment) && key !== environment)) throw new Error(`Please set ${RSBUILD_PLUGIN_NAME} as global plugin in rslib.config.ts if you set 'ssr:true' .`);
|
|
225
|
+
setSSREnv();
|
|
226
|
+
}
|
|
227
|
+
const sharedOptions = parseOptions(moduleFederationOptions.shared || [], (item, key)=>{
|
|
228
|
+
if ('string' != typeof item) throw new Error('Unexpected array in shared');
|
|
229
|
+
const config = item !== key && isRequiredVersion(item) ? {
|
|
230
|
+
import: key,
|
|
231
|
+
requiredVersion: item
|
|
232
|
+
} : {
|
|
233
|
+
import: item
|
|
234
|
+
};
|
|
235
|
+
return config;
|
|
236
|
+
}, (item, key)=>item);
|
|
237
|
+
const shared = sharedOptions.map((shared)=>shared[0].endsWith('/') ? shared[0].slice(0, -1) : shared[0]);
|
|
238
|
+
api.modifyRsbuildConfig((config)=>{
|
|
239
|
+
if (isStoryBook(config)) return;
|
|
240
|
+
if (moduleFederationOptions.exposes) {
|
|
241
|
+
var _userConfig_server, _config_server, _config_dev_client, _originalConfig_dev, _config_server1;
|
|
242
|
+
config.dev ||= {};
|
|
243
|
+
config.server ||= {};
|
|
244
|
+
const userConfig = api.getRsbuildConfig('original');
|
|
245
|
+
config.server.headers ||= {};
|
|
246
|
+
if (!config.server.headers['Access-Control-Allow-Origin'] && !('object' == typeof (null == (_userConfig_server = userConfig.server) ? void 0 : _userConfig_server.cors) && userConfig.server.cors.origin)) {
|
|
247
|
+
const corsWarnMsgs = [
|
|
248
|
+
'Detect that CORS options are not set, mf Rsbuild plugin will add default cors header: server.headers["Access-Control-Allow-Headers"] = "*". It is recommended to specify an allowlist of trusted origins in "server.cors" instead.',
|
|
249
|
+
'View https://module-federation.io/guide/troubleshooting/other.html#cors-warn for more details.'
|
|
250
|
+
];
|
|
251
|
+
isRslib || isRspress || src_logger.warn(corsWarnMsgs.join('\n'));
|
|
252
|
+
config.server.headers['Access-Control-Allow-Origin'] = '*';
|
|
253
|
+
}
|
|
254
|
+
if ((null == (_config_server = config.server) ? void 0 : _config_server.port) && !(null == (_config_dev_client = config.dev.client) ? void 0 : _config_dev_client.port)) {
|
|
255
|
+
config.dev.client ||= {};
|
|
256
|
+
config.dev.client.port = config.server.port;
|
|
257
|
+
}
|
|
258
|
+
const originalConfig = api.getRsbuildConfig('original');
|
|
259
|
+
if ((null == (_originalConfig_dev = originalConfig.dev) ? void 0 : _originalConfig_dev.assetPrefix) === void 0 && config.dev.assetPrefix === (null == (_config_server1 = config.server) ? void 0 : _config_server1.base)) config.dev.assetPrefix = true;
|
|
260
|
+
}
|
|
261
|
+
if (isSSR) {
|
|
262
|
+
var _config_environments, _config_environments1;
|
|
263
|
+
if (null == (_config_environments = config.environments) ? void 0 : _config_environments[SSR_ENV_NAME]) throw new Error(`'${SSR_ENV_NAME}' environment is already defined. Please use another name.`);
|
|
264
|
+
config.environments[SSR_ENV_NAME] = createSSRREnvConfig(null == (_config_environments1 = config.environments) ? void 0 : _config_environments1[environment], moduleFederationOptions, ssrDir, config, callerName);
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
api.modifyEnvironmentConfig((config)=>{
|
|
268
|
+
config.source.include = [
|
|
269
|
+
...config.source.include || [],
|
|
270
|
+
/@module-federation[\\/]/
|
|
271
|
+
];
|
|
272
|
+
return config;
|
|
273
|
+
});
|
|
274
|
+
const generateMergedStatsAndManifestOptions = {
|
|
275
|
+
options: {
|
|
276
|
+
nodePlugin: void 0,
|
|
277
|
+
browserPlugin: void 0,
|
|
278
|
+
rspressSSGPlugin: void 0,
|
|
279
|
+
distOutputDir: void 0,
|
|
280
|
+
browserEnvironmentName: void 0,
|
|
281
|
+
nodeEnvironmentName: void 0
|
|
282
|
+
},
|
|
283
|
+
assetResources: {},
|
|
284
|
+
isSSRConfig,
|
|
285
|
+
isRspressSSGConfig
|
|
286
|
+
};
|
|
287
|
+
api.expose(RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, generateMergedStatsAndManifestOptions);
|
|
288
|
+
const defaultBrowserEnvironmentName = environment;
|
|
289
|
+
const assetFileNames = getManifestFileName(moduleFederationOptions.manifest);
|
|
290
|
+
if (false !== moduleFederationOptions.manifest) api.processAssets({
|
|
291
|
+
stage: 'report'
|
|
292
|
+
}, ({ assets, environment: envContext })=>{
|
|
293
|
+
const expectedBrowserEnv = generateMergedStatsAndManifestOptions.options.browserEnvironmentName ?? defaultBrowserEnvironmentName;
|
|
294
|
+
const expectedNodeEnv = generateMergedStatsAndManifestOptions.options.nodeEnvironmentName ?? SSR_ENV_NAME;
|
|
295
|
+
const envName = envContext.name;
|
|
296
|
+
if (envName !== expectedBrowserEnv && envName !== expectedNodeEnv) return;
|
|
297
|
+
const assetResources = generateMergedStatsAndManifestOptions.assetResources;
|
|
298
|
+
const targetResources = assetResources[envName] || (assetResources[envName] = {});
|
|
299
|
+
const statsAsset = assets[assetFileNames.statsFileName];
|
|
300
|
+
if (statsAsset) try {
|
|
301
|
+
const raw = statsAsset.source();
|
|
302
|
+
const content = 'string' == typeof raw ? raw : raw.toString();
|
|
303
|
+
targetResources.stats = {
|
|
304
|
+
data: JSON.parse(content),
|
|
305
|
+
filename: assetFileNames.statsFileName
|
|
306
|
+
};
|
|
307
|
+
} catch (err) {
|
|
308
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
309
|
+
src_logger.error(`Failed to parse stats asset "${assetFileNames.statsFileName}" for environment "${envName}": ${message}`);
|
|
310
|
+
}
|
|
311
|
+
const manifestAsset = assets[assetFileNames.manifestFileName];
|
|
312
|
+
if (manifestAsset) try {
|
|
313
|
+
const raw = manifestAsset.source();
|
|
314
|
+
const content = 'string' == typeof raw ? raw : raw.toString();
|
|
315
|
+
targetResources.manifest = {
|
|
316
|
+
data: JSON.parse(content),
|
|
317
|
+
filename: assetFileNames.manifestFileName
|
|
318
|
+
};
|
|
319
|
+
} catch (err) {
|
|
320
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
321
|
+
src_logger.error(`Failed to parse manifest asset "${assetFileNames.manifestFileName}" for environment "${envName}": ${message}`);
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
api.onBeforeCreateCompiler(({ bundlerConfigs })=>{
|
|
325
|
+
if (!bundlerConfigs) throw new Error('Can not get bundlerConfigs!');
|
|
326
|
+
bundlerConfigs.forEach((bundlerConfig)=>{
|
|
327
|
+
if (!isMFFormat(bundlerConfig) && !isRspress) return;
|
|
328
|
+
if (isStoryBook(originalRsbuildConfig)) bundlerConfig.output.uniqueName = `${moduleFederationOptions.name}-storybook-host`;
|
|
329
|
+
else {
|
|
330
|
+
var _bundlerConfig_optimization, _bundlerConfig_optimization1, _bundlerConfig_output, _bundlerConfig_output1;
|
|
331
|
+
autoDeleteSplitChunkCacheGroups(moduleFederationOptions, null == bundlerConfig ? void 0 : null == (_bundlerConfig_optimization = bundlerConfig.optimization) ? void 0 : _bundlerConfig_optimization.splitChunks);
|
|
332
|
+
addDataFetchExposes(moduleFederationOptions.exposes, isSSRConfig(bundlerConfig.name));
|
|
333
|
+
null == (_bundlerConfig_optimization1 = bundlerConfig.optimization) || delete _bundlerConfig_optimization1.runtimeChunk;
|
|
334
|
+
const externals = bundlerConfig.externals;
|
|
335
|
+
if (Array.isArray(externals)) {
|
|
336
|
+
const sharedModules = new Set();
|
|
337
|
+
bundlerConfig.externals = externals.filter((ext)=>{
|
|
338
|
+
let sharedModule;
|
|
339
|
+
if (isRegExp(ext)) {
|
|
340
|
+
const match = shared.some((dep)=>{
|
|
341
|
+
if (ext.test(dep) || ext.test(package_namespaceObject.UU)) {
|
|
342
|
+
sharedModule = dep;
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
return false;
|
|
346
|
+
});
|
|
347
|
+
match && sharedModule && sharedModules.add(sharedModule);
|
|
348
|
+
return !match;
|
|
349
|
+
}
|
|
350
|
+
if ('string' == typeof ext) {
|
|
351
|
+
if (ext === package_namespaceObject.UU) return false;
|
|
352
|
+
const match = shared.some((dep)=>{
|
|
353
|
+
if (dep === ext) sharedModule = dep;
|
|
354
|
+
return dep === ext;
|
|
355
|
+
});
|
|
356
|
+
if (match) {
|
|
357
|
+
sharedModule && sharedModules.add(sharedModule);
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return true;
|
|
362
|
+
});
|
|
363
|
+
if (sharedModules.size > 0) for (const sharedModule of sharedModules)src_logger.log(`${sharedModule} is removed from externals because it is a shared module.`);
|
|
364
|
+
}
|
|
365
|
+
if (!(null == (_bundlerConfig_output = bundlerConfig.output) ? void 0 : _bundlerConfig_output.chunkLoadingGlobal) && !isSSRConfig(bundlerConfig.name) && !isRspressSSGConfig(bundlerConfig.name)) {
|
|
366
|
+
bundlerConfig.output.chunkLoading = 'jsonp';
|
|
367
|
+
bundlerConfig.output.chunkLoadingGlobal = `chunk_${moduleFederationOptions.name}`;
|
|
368
|
+
}
|
|
369
|
+
if (!(null == (_bundlerConfig_output1 = bundlerConfig.output) ? void 0 : _bundlerConfig_output1.uniqueName)) bundlerConfig.output.uniqueName = moduleFederationOptions.name;
|
|
370
|
+
if (!bundlerConfig.plugins.find((p)=>p && p.name === PLUGIN_NAME)) {
|
|
371
|
+
var _bundlerConfig_output2;
|
|
372
|
+
if (isSSRConfig(bundlerConfig.name)) {
|
|
373
|
+
generateMergedStatsAndManifestOptions.options.nodePlugin = new ModuleFederationPlugin(createSSRMFConfig(moduleFederationOptions));
|
|
374
|
+
generateMergedStatsAndManifestOptions.options.nodeEnvironmentName = bundlerConfig.name || SSR_ENV_NAME;
|
|
375
|
+
bundlerConfig.plugins.push(generateMergedStatsAndManifestOptions.options.nodePlugin);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
if (isRspressSSGConfig(bundlerConfig.name)) {
|
|
379
|
+
const mfConfig = {
|
|
380
|
+
...createSSRMFConfig(moduleFederationOptions),
|
|
381
|
+
exposes: {},
|
|
382
|
+
manifest: false,
|
|
383
|
+
library: void 0
|
|
384
|
+
};
|
|
385
|
+
patchSSRRspackConfig(bundlerConfig, mfConfig, "ssr", callerName, false, false);
|
|
386
|
+
bundlerConfig.output ||= {};
|
|
387
|
+
bundlerConfig.output.publicPath = '/';
|
|
388
|
+
bundlerConfig.output.asyncChunks = void 0;
|
|
389
|
+
generateMergedStatsAndManifestOptions.options.rspressSSGPlugin = new ModuleFederationPlugin(mfConfig);
|
|
390
|
+
bundlerConfig.plugins.push(generateMergedStatsAndManifestOptions.options.rspressSSGPlugin);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
generateMergedStatsAndManifestOptions.options.browserPlugin = new ModuleFederationPlugin(moduleFederationOptions);
|
|
394
|
+
generateMergedStatsAndManifestOptions.options.distOutputDir = (null == (_bundlerConfig_output2 = bundlerConfig.output) ? void 0 : _bundlerConfig_output2.path) || '';
|
|
395
|
+
generateMergedStatsAndManifestOptions.options.browserEnvironmentName = bundlerConfig.name || defaultBrowserEnvironmentName;
|
|
396
|
+
bundlerConfig.plugins.push(generateMergedStatsAndManifestOptions.options.browserPlugin);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
const generateMergedStatsAndManifest = ()=>{
|
|
402
|
+
const { distOutputDir, browserEnvironmentName, nodeEnvironmentName } = generateMergedStatsAndManifestOptions.options;
|
|
403
|
+
if (!distOutputDir || !browserEnvironmentName || !nodeEnvironmentName) return;
|
|
404
|
+
const assetResources = generateMergedStatsAndManifestOptions.assetResources;
|
|
405
|
+
const browserAssets = assetResources[browserEnvironmentName];
|
|
406
|
+
const nodeAssets = assetResources[nodeEnvironmentName];
|
|
407
|
+
if (!browserAssets || !nodeAssets) return;
|
|
408
|
+
try {
|
|
409
|
+
updateStatsAndManifest(nodeAssets, browserAssets, distOutputDir);
|
|
410
|
+
} catch (err) {
|
|
411
|
+
src_logger.error(err);
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
api.onDevCompileDone(()=>{
|
|
415
|
+
generateMergedStatsAndManifest();
|
|
416
|
+
});
|
|
417
|
+
api.onAfterBuild(()=>{
|
|
418
|
+
generateMergedStatsAndManifest();
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
export { PLUGIN_NAME, RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, SSR_DIR, createModuleFederationConfig, isMFFormat, pluginModuleFederation };
|
|
@@ -3,4 +3,5 @@ export { DEFAULT_ASSET_PREFIX } from '../constant';
|
|
|
3
3
|
export { autoDeleteSplitChunkCacheGroups } from './autoDeleteSplitChunkCacheGroups';
|
|
4
4
|
export { addDataFetchExposes } from './addDataFetchExposes';
|
|
5
5
|
export { updateStatsAndManifest } from './manifest';
|
|
6
|
+
export type { StatsAssetResource } from './manifest';
|
|
6
7
|
export { patchSSRRspackConfig, createSSRREnvConfig, createSSRMFConfig, setSSREnv, SSR_DIR, SSR_ENV_NAME, } from './ssr';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Stats, Manifest } from '@module-federation/sdk';
|
|
2
|
+
type AssetResource<T> = {
|
|
3
|
+
data: T;
|
|
4
|
+
filename: string;
|
|
5
|
+
};
|
|
6
|
+
export type StatsAssetResource = {
|
|
7
|
+
stats?: AssetResource<Stats>;
|
|
8
|
+
manifest?: AssetResource<Manifest>;
|
|
9
|
+
};
|
|
10
|
+
export declare function updateStatsAndManifest(nodeAssets: StatsAssetResource, browserAssets: StatsAssetResource, outputDir: string): void;
|
|
11
|
+
export {};
|
|
@@ -17,7 +17,7 @@ export declare function createSSRMFConfig(mfConfig: moduleFederationPlugin.Modul
|
|
|
17
17
|
};
|
|
18
18
|
dts: boolean;
|
|
19
19
|
dev: boolean;
|
|
20
|
-
runtimePlugins: string[];
|
|
20
|
+
runtimePlugins: (string | [string, Record<string, unknown>])[];
|
|
21
21
|
filename?: string;
|
|
22
22
|
name?: string;
|
|
23
23
|
remoteType?: moduleFederationPlugin.ExternalsType;
|
|
@@ -41,6 +41,7 @@ export declare function createSSRMFConfig(mfConfig: moduleFederationPlugin.Modul
|
|
|
41
41
|
};
|
|
42
42
|
};
|
|
43
43
|
bridge?: {
|
|
44
|
+
enableBridgeRouter?: boolean;
|
|
44
45
|
disableAlias?: boolean;
|
|
45
46
|
};
|
|
46
47
|
async?: boolean | moduleFederationPlugin.AsyncBoundaryOptions;
|