@module-federation/rsbuild-plugin 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -2
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.js +429 -0
- package/dist/cli/index.mjs +360 -0
- package/dist/constant.d.ts +1 -0
- package/dist/constant.js +48 -35
- package/dist/constant.mjs +5 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +86 -528
- package/dist/index.mjs +5 -470
- package/dist/logger.js +60 -0
- package/dist/logger.mjs +10 -0
- package/dist/utils/addDataFetchExposes.js +126 -0
- package/dist/utils/addDataFetchExposes.mjs +65 -0
- package/dist/utils/autoDeleteSplitChunkCacheGroups.js +99 -0
- package/dist/utils/autoDeleteSplitChunkCacheGroups.mjs +53 -0
- package/dist/utils/index.js +109 -0
- package/dist/utils/index.mjs +31 -0
- package/dist/utils/manifest.js +113 -0
- package/dist/utils/manifest.mjs +50 -0
- package/dist/utils/ssr.js +230 -0
- package/dist/utils/ssr.mjs +152 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +86 -291
- package/dist/utils.mjs +5 -222
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -1,470 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
const ENV_NAME = 'mf';
|
|
116
|
-
function setSSREnv() {
|
|
117
|
-
process.env['MF_SSR_PRJ'] = 'true';
|
|
118
|
-
}
|
|
119
|
-
const isDev = ()=>'development' === process.env['NODE_ENV'];
|
|
120
|
-
function patchNodeConfig(config, mfConfig) {
|
|
121
|
-
var _config_output;
|
|
122
|
-
config.output ||= {};
|
|
123
|
-
config.target = 'async-node';
|
|
124
|
-
const UniverseEntryChunkTrackerPlugin = ssr_require('@module-federation/node/universe-entry-chunk-tracker-plugin').default;
|
|
125
|
-
config.plugins ||= [];
|
|
126
|
-
isDev() && config.plugins.push(new UniverseEntryChunkTrackerPlugin());
|
|
127
|
-
const uniqueName = mfConfig.name || (null == (_config_output = config.output) ? void 0 : _config_output.uniqueName);
|
|
128
|
-
const chunkFileName = config.output.chunkFilename;
|
|
129
|
-
if ('string' == typeof chunkFileName && uniqueName && !chunkFileName.includes(uniqueName)) {
|
|
130
|
-
const suffix = `${encodeName(uniqueName)}-[contenthash].js`;
|
|
131
|
-
config.output.chunkFilename = chunkFileName.replace('.js', suffix);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
function patchSSRRspackConfig(config, mfConfig, ssrDir, callerName, resetEntry = true, modifyPublicPath = true) {
|
|
135
|
-
config.output ||= {};
|
|
136
|
-
if (modifyPublicPath) {
|
|
137
|
-
var _config_output;
|
|
138
|
-
if ('string' != typeof (null == (_config_output = config.output) ? void 0 : _config_output.publicPath)) throw new Error('publicPath must be string!');
|
|
139
|
-
const publicPath = config.output.publicPath;
|
|
140
|
-
if ('auto' === publicPath) throw new Error('publicPath can not be "auto"!');
|
|
141
|
-
const publicPathWithSSRDir = `${publicPath}${ssrDir}/`;
|
|
142
|
-
config.output.publicPath = publicPathWithSSRDir;
|
|
143
|
-
}
|
|
144
|
-
if (callerName === CALL_NAME_MAP.RSPRESS && resetEntry) config.entry = 'data:application/node;base64,';
|
|
145
|
-
patchNodeConfig(config, mfConfig);
|
|
146
|
-
return config;
|
|
147
|
-
}
|
|
148
|
-
function patchToolsTspack(envConfig, fn) {
|
|
149
|
-
var _envConfig_tools;
|
|
150
|
-
const rspackArr = [];
|
|
151
|
-
if (null == (_envConfig_tools = envConfig.tools) ? void 0 : _envConfig_tools.rspack) {
|
|
152
|
-
var _envConfig_tools1, _envConfig_tools2;
|
|
153
|
-
if (Array.isArray(null == (_envConfig_tools1 = envConfig.tools) ? void 0 : _envConfig_tools1.rspack)) {
|
|
154
|
-
var _envConfig_tools3;
|
|
155
|
-
rspackArr.push(...null == (_envConfig_tools3 = envConfig.tools) ? void 0 : _envConfig_tools3.rspack);
|
|
156
|
-
} else if ('function' == typeof (null == (_envConfig_tools2 = envConfig.tools) ? void 0 : _envConfig_tools2.rspack)) {
|
|
157
|
-
var _envConfig_tools4;
|
|
158
|
-
rspackArr.push(null == (_envConfig_tools4 = envConfig.tools) ? void 0 : _envConfig_tools4.rspack);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
envConfig.tools ||= {};
|
|
162
|
-
envConfig.tools.rspack = [
|
|
163
|
-
...rspackArr,
|
|
164
|
-
fn
|
|
165
|
-
];
|
|
166
|
-
}
|
|
167
|
-
function createSSRREnvConfig(envConfig, mfConfig, ssrDir, rsbuildConfig, callerName) {
|
|
168
|
-
var _envConfig_tools, _ssrEnvConfig_output, _ssrEnvConfig_output_distPath, _ssrEnvConfig_output1, _rsbuildConfig_output_distPath, _rsbuildConfig_output;
|
|
169
|
-
const rspackArr = [];
|
|
170
|
-
if (null == (_envConfig_tools = envConfig.tools) ? void 0 : _envConfig_tools.rspack) {
|
|
171
|
-
var _envConfig_tools1, _envConfig_tools2;
|
|
172
|
-
if (Array.isArray(null == (_envConfig_tools1 = envConfig.tools) ? void 0 : _envConfig_tools1.rspack)) {
|
|
173
|
-
var _envConfig_tools3;
|
|
174
|
-
rspackArr.push(...null == (_envConfig_tools3 = envConfig.tools) ? void 0 : _envConfig_tools3.rspack);
|
|
175
|
-
} else if ('function' == typeof (null == (_envConfig_tools2 = envConfig.tools) ? void 0 : _envConfig_tools2.rspack)) {
|
|
176
|
-
var _envConfig_tools4;
|
|
177
|
-
rspackArr.push(null == (_envConfig_tools4 = envConfig.tools) ? void 0 : _envConfig_tools4.rspack);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
const ssrEnvConfig = {
|
|
181
|
-
...envConfig
|
|
182
|
-
};
|
|
183
|
-
patchToolsTspack(ssrEnvConfig, (config, { environment })=>{
|
|
184
|
-
if (environment.name !== SSR_ENV_NAME) return;
|
|
185
|
-
patchSSRRspackConfig(config, mfConfig, ssrDir, callerName);
|
|
186
|
-
});
|
|
187
|
-
ssrEnvConfig.output = {
|
|
188
|
-
...ssrEnvConfig.output,
|
|
189
|
-
target: 'node',
|
|
190
|
-
distPath: {
|
|
191
|
-
...null == (_ssrEnvConfig_output = ssrEnvConfig.output) ? void 0 : _ssrEnvConfig_output.distPath,
|
|
192
|
-
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)
|
|
193
|
-
},
|
|
194
|
-
emitAssets: true
|
|
195
|
-
};
|
|
196
|
-
return ssrEnvConfig;
|
|
197
|
-
}
|
|
198
|
-
function patchNodeMFConfig(mfConfig) {
|
|
199
|
-
var _mfConfig_library;
|
|
200
|
-
if (mfConfig.remotes) mfConfig.remoteType = "script";
|
|
201
|
-
mfConfig.exposes = {
|
|
202
|
-
...mfConfig.exposes
|
|
203
|
-
};
|
|
204
|
-
mfConfig.library = {
|
|
205
|
-
...mfConfig.library,
|
|
206
|
-
name: mfConfig.name,
|
|
207
|
-
type: (null == (_mfConfig_library = mfConfig.library) ? void 0 : _mfConfig_library.type) ?? 'commonjs-module'
|
|
208
|
-
};
|
|
209
|
-
mfConfig.runtimePlugins = [
|
|
210
|
-
...mfConfig.runtimePlugins || []
|
|
211
|
-
];
|
|
212
|
-
mfConfig.runtimePlugins.push(resolve('@module-federation/node/runtimePlugin'));
|
|
213
|
-
if (isDev()) mfConfig.runtimePlugins.push(resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'));
|
|
214
|
-
}
|
|
215
|
-
function createSSRMFConfig(mfConfig) {
|
|
216
|
-
const ssrMFConfig = {
|
|
217
|
-
...mfConfig
|
|
218
|
-
};
|
|
219
|
-
patchNodeMFConfig(ssrMFConfig);
|
|
220
|
-
ssrMFConfig.dts = false;
|
|
221
|
-
ssrMFConfig.dev = false;
|
|
222
|
-
return ssrMFConfig;
|
|
223
|
-
}
|
|
224
|
-
function isRegExp(target) {
|
|
225
|
-
return util.types.isRegExp(target);
|
|
226
|
-
}
|
|
227
|
-
const RSBUILD_PLUGIN_MODULE_FEDERATION_NAME = 'rsbuild:module-federation-enhanced';
|
|
228
|
-
const RSBUILD_PLUGIN_NAME = '@module-federation/rsbuild-plugin';
|
|
229
|
-
const LIB_FORMAT = [
|
|
230
|
-
'umd',
|
|
231
|
-
'modern-module'
|
|
232
|
-
];
|
|
233
|
-
const DEFAULT_MF_ENVIRONMENT_NAME = 'mf';
|
|
234
|
-
function isStoryBook(rsbuildConfig) {
|
|
235
|
-
var _rsbuildConfig_plugins;
|
|
236
|
-
if (null == (_rsbuildConfig_plugins = rsbuildConfig.plugins) ? void 0 : _rsbuildConfig_plugins.find((p)=>p && 'name' in p && 'module-federation-storybook-plugin' === p.name)) return true;
|
|
237
|
-
return false;
|
|
238
|
-
}
|
|
239
|
-
function isMFFormat(bundlerConfig) {
|
|
240
|
-
var _bundlerConfig_output;
|
|
241
|
-
const library = null == (_bundlerConfig_output = bundlerConfig.output) ? void 0 : _bundlerConfig_output.library;
|
|
242
|
-
if (bundlerConfig.name === SSR_ENV_NAME) return true;
|
|
243
|
-
return !('object' == typeof library && !Array.isArray(library) && 'type' in library && (LIB_FORMAT.includes(library.type) || /commonjs/.test(library.type)));
|
|
244
|
-
}
|
|
245
|
-
const isSSRConfig = (bundlerConfigName)=>Boolean(bundlerConfigName === SSR_ENV_NAME);
|
|
246
|
-
const isRspressSSGConfig = (bundlerConfigName)=>"node" === bundlerConfigName;
|
|
247
|
-
const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
248
|
-
name: RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
|
|
249
|
-
setup: (api)=>{
|
|
250
|
-
const { target = 'web', ssr, ssrDir = SSR_DIR, environment = DEFAULT_MF_ENVIRONMENT_NAME } = rsbuildOptions || {};
|
|
251
|
-
if (ssr) throw new Error("The `ssr` option is deprecated. If you want to enable SSR, please use `target: 'dual'` instead.");
|
|
252
|
-
const { callerName } = api.context;
|
|
253
|
-
const originalRsbuildConfig = api.getRsbuildConfig();
|
|
254
|
-
if (!callerName) throw new Error('`callerName` is undefined. Please ensure the @rsbuild/core version is higher than 1.3.21 .');
|
|
255
|
-
const isRslib = callerName === CALL_NAME_MAP.RSLIB;
|
|
256
|
-
const isRspress = callerName === CALL_NAME_MAP.RSPRESS;
|
|
257
|
-
const isSSR = 'dual' === target;
|
|
258
|
-
if (isSSR && !isStoryBook(originalRsbuildConfig)) {
|
|
259
|
-
var _rsbuildConfig_environments;
|
|
260
|
-
if (!isRslib && !isRspress) throw new Error("'target' option is only supported in Rslib.");
|
|
261
|
-
const rsbuildConfig = api.getRsbuildConfig();
|
|
262
|
-
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 'target: "dual"'.`);
|
|
263
|
-
setSSREnv();
|
|
264
|
-
}
|
|
265
|
-
const sharedOptions = parseOptions(moduleFederationOptions.shared || [], (item, key)=>{
|
|
266
|
-
if ('string' != typeof item) throw new Error('Unexpected array in shared');
|
|
267
|
-
const config = item !== key && isRequiredVersion(item) ? {
|
|
268
|
-
import: key,
|
|
269
|
-
requiredVersion: item
|
|
270
|
-
} : {
|
|
271
|
-
import: item
|
|
272
|
-
};
|
|
273
|
-
return config;
|
|
274
|
-
}, (item)=>item);
|
|
275
|
-
const shared = sharedOptions.map((shared)=>shared[0].endsWith('/') ? shared[0].slice(0, -1) : shared[0]);
|
|
276
|
-
api.modifyRsbuildConfig((config)=>{
|
|
277
|
-
if (isStoryBook(config)) return;
|
|
278
|
-
if (moduleFederationOptions.exposes) {
|
|
279
|
-
var _userConfig_server, _config_server, _config_dev_client, _originalConfig_dev, _config_server1;
|
|
280
|
-
config.dev ||= {};
|
|
281
|
-
config.server ||= {};
|
|
282
|
-
const userConfig = api.getRsbuildConfig('original');
|
|
283
|
-
config.server.headers ||= {};
|
|
284
|
-
if (!config.server.headers['Access-Control-Allow-Origin'] && !('object' == typeof (null == (_userConfig_server = userConfig.server) ? void 0 : _userConfig_server.cors) && userConfig.server.cors.origin)) {
|
|
285
|
-
const corsWarnMsgs = [
|
|
286
|
-
'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.',
|
|
287
|
-
'View https://module-federation.io/guide/troubleshooting/other.html#cors-warn for more details.'
|
|
288
|
-
];
|
|
289
|
-
isRslib || isRspress || src_logger.warn(corsWarnMsgs.join('\n'));
|
|
290
|
-
config.server.headers['Access-Control-Allow-Origin'] = '*';
|
|
291
|
-
}
|
|
292
|
-
if ((null == (_config_server = config.server) ? void 0 : _config_server.port) && !(null == (_config_dev_client = config.dev.client) ? void 0 : _config_dev_client.port)) {
|
|
293
|
-
config.dev.client ||= {};
|
|
294
|
-
config.dev.client.port = config.server.port;
|
|
295
|
-
}
|
|
296
|
-
const originalConfig = api.getRsbuildConfig('original');
|
|
297
|
-
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;
|
|
298
|
-
}
|
|
299
|
-
if (isSSR) {
|
|
300
|
-
var _config_environments, _config_environments1;
|
|
301
|
-
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.`);
|
|
302
|
-
config.environments[SSR_ENV_NAME] = createSSRREnvConfig(null == (_config_environments1 = config.environments) ? void 0 : _config_environments1[environment], moduleFederationOptions, ssrDir, config, callerName);
|
|
303
|
-
} else if ('node' === target) {
|
|
304
|
-
const mfEnv = config.environments[ENV_NAME];
|
|
305
|
-
patchToolsTspack(mfEnv, (config, { environment })=>{
|
|
306
|
-
config.target = 'async-node';
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
api.modifyEnvironmentConfig((config)=>{
|
|
311
|
-
config.source.include = [
|
|
312
|
-
...config.source.include || [],
|
|
313
|
-
/@module-federation[\\/]/
|
|
314
|
-
];
|
|
315
|
-
return config;
|
|
316
|
-
});
|
|
317
|
-
const generateMergedStatsAndManifestOptions = {
|
|
318
|
-
options: {
|
|
319
|
-
nodePlugin: void 0,
|
|
320
|
-
browserPlugin: void 0,
|
|
321
|
-
rspressSSGPlugin: void 0,
|
|
322
|
-
distOutputDir: void 0,
|
|
323
|
-
browserEnvironmentName: void 0,
|
|
324
|
-
nodeEnvironmentName: void 0
|
|
325
|
-
},
|
|
326
|
-
assetResources: {},
|
|
327
|
-
isSSRConfig,
|
|
328
|
-
isRspressSSGConfig
|
|
329
|
-
};
|
|
330
|
-
api.expose(RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, generateMergedStatsAndManifestOptions);
|
|
331
|
-
const defaultBrowserEnvironmentName = environment;
|
|
332
|
-
const assetFileNames = getManifestFileName(moduleFederationOptions.manifest);
|
|
333
|
-
if (false !== moduleFederationOptions.manifest) api.processAssets({
|
|
334
|
-
stage: 'report'
|
|
335
|
-
}, ({ assets, environment: envContext })=>{
|
|
336
|
-
const expectedBrowserEnv = generateMergedStatsAndManifestOptions.options.browserEnvironmentName ?? defaultBrowserEnvironmentName;
|
|
337
|
-
const expectedNodeEnv = generateMergedStatsAndManifestOptions.options.nodeEnvironmentName ?? SSR_ENV_NAME;
|
|
338
|
-
const envName = envContext.name;
|
|
339
|
-
if (envName !== expectedBrowserEnv && envName !== expectedNodeEnv) return;
|
|
340
|
-
const assetResources = generateMergedStatsAndManifestOptions.assetResources;
|
|
341
|
-
const targetResources = assetResources[envName] || (assetResources[envName] = {});
|
|
342
|
-
const statsAsset = assets[assetFileNames.statsFileName];
|
|
343
|
-
if (statsAsset) try {
|
|
344
|
-
const raw = statsAsset.source();
|
|
345
|
-
const content = 'string' == typeof raw ? raw : raw.toString();
|
|
346
|
-
targetResources.stats = {
|
|
347
|
-
data: JSON.parse(content),
|
|
348
|
-
filename: assetFileNames.statsFileName
|
|
349
|
-
};
|
|
350
|
-
} catch (err) {
|
|
351
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
352
|
-
src_logger.error(`Failed to parse stats asset "${assetFileNames.statsFileName}" for environment "${envName}": ${message} `);
|
|
353
|
-
}
|
|
354
|
-
const manifestAsset = assets[assetFileNames.manifestFileName];
|
|
355
|
-
if (manifestAsset) try {
|
|
356
|
-
const raw = manifestAsset.source();
|
|
357
|
-
const content = 'string' == typeof raw ? raw : raw.toString();
|
|
358
|
-
targetResources.manifest = {
|
|
359
|
-
data: JSON.parse(content),
|
|
360
|
-
filename: assetFileNames.manifestFileName
|
|
361
|
-
};
|
|
362
|
-
} catch (err) {
|
|
363
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
364
|
-
src_logger.error(`Failed to parse manifest asset "${assetFileNames.manifestFileName}" for environment "${envName}": ${message} `);
|
|
365
|
-
}
|
|
366
|
-
});
|
|
367
|
-
api.onBeforeCreateCompiler(({ bundlerConfigs })=>{
|
|
368
|
-
if (!bundlerConfigs) throw new Error('Can not get bundlerConfigs!');
|
|
369
|
-
bundlerConfigs.forEach((bundlerConfig)=>{
|
|
370
|
-
if (!isMFFormat(bundlerConfig) && !isRspress) return;
|
|
371
|
-
if (isStoryBook(originalRsbuildConfig)) bundlerConfig.output.uniqueName = `${moduleFederationOptions.name} -storybook - host`;
|
|
372
|
-
else {
|
|
373
|
-
var _bundlerConfig_optimization, _bundlerConfig_optimization1, _bundlerConfig_output, _bundlerConfig_output1, _bundlerConfig_output2;
|
|
374
|
-
autoDeleteSplitChunkCacheGroups(moduleFederationOptions, null == bundlerConfig ? void 0 : null == (_bundlerConfig_optimization = bundlerConfig.optimization) ? void 0 : _bundlerConfig_optimization.splitChunks);
|
|
375
|
-
addDataFetchExposes(moduleFederationOptions.exposes, isSSRConfig(bundlerConfig.name));
|
|
376
|
-
null == (_bundlerConfig_optimization1 = bundlerConfig.optimization) || delete _bundlerConfig_optimization1.runtimeChunk;
|
|
377
|
-
const externals = bundlerConfig.externals;
|
|
378
|
-
if (Array.isArray(externals)) {
|
|
379
|
-
const sharedModules = new Set();
|
|
380
|
-
bundlerConfig.externals = externals.filter((ext)=>{
|
|
381
|
-
let sharedModule;
|
|
382
|
-
if (isRegExp(ext)) {
|
|
383
|
-
const match = shared.some((dep)=>{
|
|
384
|
-
if (ext.test(dep) || ext.test(package_namespaceObject.UU)) {
|
|
385
|
-
sharedModule = dep;
|
|
386
|
-
return true;
|
|
387
|
-
}
|
|
388
|
-
return false;
|
|
389
|
-
});
|
|
390
|
-
match && sharedModule && sharedModules.add(sharedModule);
|
|
391
|
-
return !match;
|
|
392
|
-
}
|
|
393
|
-
if ('string' == typeof ext) {
|
|
394
|
-
if (ext === package_namespaceObject.UU) return false;
|
|
395
|
-
const match = shared.some((dep)=>{
|
|
396
|
-
if (dep === ext) sharedModule = dep;
|
|
397
|
-
return dep === ext;
|
|
398
|
-
});
|
|
399
|
-
if (match) {
|
|
400
|
-
sharedModule && sharedModules.add(sharedModule);
|
|
401
|
-
return false;
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
return true;
|
|
405
|
-
});
|
|
406
|
-
if (sharedModules.size > 0) for (const sharedModule of sharedModules)src_logger.log(`${sharedModule} is removed from externals because it is a shared module.`);
|
|
407
|
-
}
|
|
408
|
-
if (!(null == (_bundlerConfig_output = bundlerConfig.output) ? void 0 : _bundlerConfig_output.chunkLoadingGlobal) && !isSSRConfig(bundlerConfig.name) && !isRspressSSGConfig(bundlerConfig.name) && 'node' !== target) {
|
|
409
|
-
bundlerConfig.output.chunkLoading = 'jsonp';
|
|
410
|
-
bundlerConfig.output.chunkLoadingGlobal = `chunk_${moduleFederationOptions.name} `;
|
|
411
|
-
}
|
|
412
|
-
if ('node' === target && isMFFormat(bundlerConfig)) {
|
|
413
|
-
patchNodeConfig(bundlerConfig, moduleFederationOptions);
|
|
414
|
-
patchNodeMFConfig(moduleFederationOptions);
|
|
415
|
-
}
|
|
416
|
-
if (!(null == (_bundlerConfig_output1 = bundlerConfig.output) ? void 0 : _bundlerConfig_output1.uniqueName)) bundlerConfig.output.uniqueName = moduleFederationOptions.name;
|
|
417
|
-
if ((null == (_bundlerConfig_output2 = bundlerConfig.output) ? void 0 : _bundlerConfig_output2.publicPath) === void 0 && !isSSRConfig(bundlerConfig.name) && !isRspressSSGConfig(bundlerConfig.name)) bundlerConfig.output.publicPath = 'auto';
|
|
418
|
-
if (!bundlerConfig.plugins.find((p)=>p && p.name === PLUGIN_NAME)) {
|
|
419
|
-
var _bundlerConfig_output3;
|
|
420
|
-
if (isSSRConfig(bundlerConfig.name)) {
|
|
421
|
-
generateMergedStatsAndManifestOptions.options.nodePlugin = new ModuleFederationPlugin(createSSRMFConfig(moduleFederationOptions));
|
|
422
|
-
generateMergedStatsAndManifestOptions.options.nodeEnvironmentName = bundlerConfig.name || SSR_ENV_NAME;
|
|
423
|
-
bundlerConfig.plugins.push(generateMergedStatsAndManifestOptions.options.nodePlugin);
|
|
424
|
-
return;
|
|
425
|
-
}
|
|
426
|
-
if (isRspressSSGConfig(bundlerConfig.name)) {
|
|
427
|
-
const mfConfig = {
|
|
428
|
-
...createSSRMFConfig(moduleFederationOptions),
|
|
429
|
-
exposes: {},
|
|
430
|
-
manifest: false,
|
|
431
|
-
library: void 0
|
|
432
|
-
};
|
|
433
|
-
patchSSRRspackConfig(bundlerConfig, mfConfig, "ssr", callerName, false, false);
|
|
434
|
-
bundlerConfig.output ||= {};
|
|
435
|
-
bundlerConfig.output.publicPath = '/';
|
|
436
|
-
bundlerConfig.output.asyncChunks = void 0;
|
|
437
|
-
generateMergedStatsAndManifestOptions.options.rspressSSGPlugin = new ModuleFederationPlugin(mfConfig);
|
|
438
|
-
bundlerConfig.plugins.push(generateMergedStatsAndManifestOptions.options.rspressSSGPlugin);
|
|
439
|
-
return;
|
|
440
|
-
}
|
|
441
|
-
generateMergedStatsAndManifestOptions.options.browserPlugin = new ModuleFederationPlugin(moduleFederationOptions);
|
|
442
|
-
generateMergedStatsAndManifestOptions.options.distOutputDir = (null == (_bundlerConfig_output3 = bundlerConfig.output) ? void 0 : _bundlerConfig_output3.path) || '';
|
|
443
|
-
generateMergedStatsAndManifestOptions.options.browserEnvironmentName = bundlerConfig.name || defaultBrowserEnvironmentName;
|
|
444
|
-
bundlerConfig.plugins.push(generateMergedStatsAndManifestOptions.options.browserPlugin);
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
});
|
|
448
|
-
});
|
|
449
|
-
const generateMergedStatsAndManifest = ()=>{
|
|
450
|
-
const { distOutputDir, browserEnvironmentName, nodeEnvironmentName } = generateMergedStatsAndManifestOptions.options;
|
|
451
|
-
if (!distOutputDir || !browserEnvironmentName || !nodeEnvironmentName) return;
|
|
452
|
-
const assetResources = generateMergedStatsAndManifestOptions.assetResources;
|
|
453
|
-
const browserAssets = assetResources[browserEnvironmentName];
|
|
454
|
-
const nodeAssets = assetResources[nodeEnvironmentName];
|
|
455
|
-
if (!browserAssets || !nodeAssets) return;
|
|
456
|
-
try {
|
|
457
|
-
updateStatsAndManifest(nodeAssets, browserAssets, distOutputDir);
|
|
458
|
-
} catch (err) {
|
|
459
|
-
src_logger.error(err);
|
|
460
|
-
}
|
|
461
|
-
};
|
|
462
|
-
api.onDevCompileDone(()=>{
|
|
463
|
-
generateMergedStatsAndManifest();
|
|
464
|
-
});
|
|
465
|
-
api.onAfterBuild(()=>{
|
|
466
|
-
generateMergedStatsAndManifest();
|
|
467
|
-
});
|
|
468
|
-
}
|
|
469
|
-
});
|
|
470
|
-
export { PLUGIN_NAME, RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, SSR_DIR, TreeShakingSharedPlugin, createModuleFederationConfig, isMFFormat, pluginModuleFederation };
|
|
1
|
+
export * from "./cli/index.mjs";
|
|
2
|
+
|
|
3
|
+
;// CONCATENATED MODULE: ./src/index.ts
|
|
4
|
+
|
|
5
|
+
|
package/dist/logger.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
|
|
3
|
+
return typeof document === 'undefined'
|
|
4
|
+
? new (require('url'.replace('', '')).URL)('file:' + __filename).href
|
|
5
|
+
: (document.currentScript && document.currentScript.src) ||
|
|
6
|
+
new URL('main.js', document.baseURI).href;
|
|
7
|
+
})();
|
|
8
|
+
;
|
|
9
|
+
// The require scope
|
|
10
|
+
var __webpack_require__ = {};
|
|
11
|
+
|
|
12
|
+
/************************************************************************/
|
|
13
|
+
// webpack/runtime/define_property_getters
|
|
14
|
+
(() => {
|
|
15
|
+
__webpack_require__.d = (exports, definition) => {
|
|
16
|
+
for(var key in definition) {
|
|
17
|
+
if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
|
|
18
|
+
Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
})();
|
|
23
|
+
// webpack/runtime/has_own_property
|
|
24
|
+
(() => {
|
|
25
|
+
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
26
|
+
})();
|
|
27
|
+
// webpack/runtime/make_namespace_object
|
|
28
|
+
(() => {
|
|
29
|
+
// define __esModule on exports
|
|
30
|
+
__webpack_require__.r = (exports) => {
|
|
31
|
+
if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
32
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
33
|
+
}
|
|
34
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
35
|
+
};
|
|
36
|
+
})();
|
|
37
|
+
/************************************************************************/
|
|
38
|
+
var __webpack_exports__ = {};
|
|
39
|
+
// ESM COMPAT FLAG
|
|
40
|
+
__webpack_require__.r(__webpack_exports__);
|
|
41
|
+
|
|
42
|
+
// EXPORTS
|
|
43
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
44
|
+
"default": () => (/* binding */ src_logger)
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
;// CONCATENATED MODULE: external "@module-federation/sdk"
|
|
48
|
+
const sdk_namespaceObject = require("@module-federation/sdk");
|
|
49
|
+
;// CONCATENATED MODULE: ./src/logger.ts
|
|
50
|
+
|
|
51
|
+
const logger = (0,sdk_namespaceObject.createLogger)('[ Module Federation Rsbuild Plugin ]');
|
|
52
|
+
/* ESM default export */ const src_logger = (logger);
|
|
53
|
+
|
|
54
|
+
exports["default"] = __webpack_exports__["default"];
|
|
55
|
+
for(var __webpack_i__ in __webpack_exports__) {
|
|
56
|
+
if(["default"].indexOf(__webpack_i__) === -1) {
|
|
57
|
+
exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
package/dist/logger.mjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createLogger } from "@module-federation/sdk";
|
|
2
|
+
|
|
3
|
+
;// CONCATENATED MODULE: external "@module-federation/sdk"
|
|
4
|
+
|
|
5
|
+
;// CONCATENATED MODULE: ./src/logger.ts
|
|
6
|
+
|
|
7
|
+
const logger = createLogger('[ Module Federation Rsbuild Plugin ]');
|
|
8
|
+
/* ESM default export */ const src_logger = (logger);
|
|
9
|
+
|
|
10
|
+
export { src_logger as default };
|