@module-federation/modern-js 0.0.0-docs-remove-invalid-lark-link-20251205062649
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/LICENSE +21 -0
- package/dist/cjs/cli/configPlugin.js +325 -0
- package/dist/cjs/cli/index.js +105 -0
- package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +54 -0
- package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +80 -0
- package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +53 -0
- package/dist/cjs/cli/server/data-fetch-server-plugin.js +59 -0
- package/dist/cjs/cli/ssrPlugin.js +287 -0
- package/dist/cjs/cli/utils.js +93 -0
- package/dist/cjs/constant.js +40 -0
- package/dist/cjs/interfaces/bundler.js +18 -0
- package/dist/cjs/logger.js +39 -0
- package/dist/cjs/react/index.js +60 -0
- package/dist/cjs/react/plugin.js +67 -0
- package/dist/cjs/react/v18.js +60 -0
- package/dist/cjs/react/v19.js +60 -0
- package/dist/cjs/runtime/index.js +60 -0
- package/dist/cjs/server/fileCache.js +84 -0
- package/dist/cjs/server/index.js +71 -0
- package/dist/cjs/server/staticMiddleware.js +90 -0
- package/dist/cjs/ssr-runtime/SSRLiveReload.js +48 -0
- package/dist/cjs/ssr-runtime/devPlugin.js +59 -0
- package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +48 -0
- package/dist/cjs/types/index.js +18 -0
- package/dist/esm/cli/configPlugin.mjs +298 -0
- package/dist/esm/cli/index.mjs +82 -0
- package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.mjs +12 -0
- package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.mjs +58 -0
- package/dist/esm/cli/mfRuntimePlugins/shared-strategy.mjs +21 -0
- package/dist/esm/cli/server/data-fetch-server-plugin.mjs +17 -0
- package/dist/esm/cli/ssrPlugin.mjs +257 -0
- package/dist/esm/cli/utils.mjs +40 -0
- package/dist/esm/constant.mjs +3 -0
- package/dist/esm/interfaces/bundler.mjs +0 -0
- package/dist/esm/logger.mjs +5 -0
- package/dist/esm/react/index.mjs +1 -0
- package/dist/esm/react/plugin.mjs +3 -0
- package/dist/esm/react/v18.mjs +1 -0
- package/dist/esm/react/v19.mjs +1 -0
- package/dist/esm/runtime/index.mjs +1 -0
- package/dist/esm/server/fileCache.mjs +90 -0
- package/dist/esm/server/index.mjs +36 -0
- package/dist/esm/server/staticMiddleware.mjs +87 -0
- package/dist/esm/ssr-runtime/SSRLiveReload.mjs +21 -0
- package/dist/esm/ssr-runtime/devPlugin.mjs +63 -0
- package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.mjs +34 -0
- package/dist/esm/types/index.mjs +0 -0
- package/dist/esm-node/cli/configPlugin.mjs +256 -0
- package/dist/esm-node/cli/index.mjs +65 -0
- package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.mjs +10 -0
- package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.mjs +46 -0
- package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.mjs +19 -0
- package/dist/esm-node/cli/server/data-fetch-server-plugin.mjs +15 -0
- package/dist/esm-node/cli/ssrPlugin.mjs +231 -0
- package/dist/esm-node/cli/utils.mjs +40 -0
- package/dist/esm-node/constant.mjs +3 -0
- package/dist/esm-node/interfaces/bundler.mjs +0 -0
- package/dist/esm-node/logger.mjs +5 -0
- package/dist/esm-node/react/index.mjs +1 -0
- package/dist/esm-node/react/plugin.mjs +3 -0
- package/dist/esm-node/react/v18.mjs +1 -0
- package/dist/esm-node/react/v19.mjs +1 -0
- package/dist/esm-node/runtime/index.mjs +1 -0
- package/dist/esm-node/server/fileCache.mjs +37 -0
- package/dist/esm-node/server/index.mjs +34 -0
- package/dist/esm-node/server/staticMiddleware.mjs +42 -0
- package/dist/esm-node/ssr-runtime/SSRLiveReload.mjs +14 -0
- package/dist/esm-node/ssr-runtime/devPlugin.mjs +25 -0
- package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.mjs +14 -0
- package/dist/esm-node/types/index.mjs +0 -0
- package/dist/types/cli/configPlugin.d.ts +21 -0
- package/dist/types/cli/configPlugin.spec.d.ts +1 -0
- package/dist/types/cli/index.d.ts +6 -0
- package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +3 -0
- package/dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts +3 -0
- package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +3 -0
- package/dist/types/cli/server/data-fetch-server-plugin.d.ts +3 -0
- package/dist/types/cli/ssrPlugin.d.ts +6 -0
- package/dist/types/cli/utils.d.ts +6 -0
- package/dist/types/constant.d.ts +2 -0
- package/dist/types/interfaces/bundler.d.ts +18 -0
- package/dist/types/logger.d.ts +2 -0
- package/dist/types/react/index.d.ts +1 -0
- package/dist/types/react/plugin.d.ts +2 -0
- package/dist/types/react/v18.d.ts +1 -0
- package/dist/types/react/v19.d.ts +1 -0
- package/dist/types/runtime/index.d.ts +1 -0
- package/dist/types/server/fileCache.d.ts +14 -0
- package/dist/types/server/fileCache.spec.d.ts +1 -0
- package/dist/types/server/index.d.ts +4 -0
- package/dist/types/server/staticMiddleware.d.ts +7 -0
- package/dist/types/server/staticMiddleware.spec.d.ts +1 -0
- package/dist/types/ssr-runtime/SSRLiveReload.d.ts +1 -0
- package/dist/types/ssr-runtime/devPlugin.d.ts +2 -0
- package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +4 -0
- package/dist/types/types/index.d.ts +35 -0
- package/package.json +189 -0
- package/types.d.ts +6 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { getIPV4, isDev, isWebTarget, skipByTarget } from "./utils.mjs";
|
|
3
|
+
import { encodeName } from "@module-federation/sdk";
|
|
4
|
+
import { bundle } from "@modern-js/node-bundle-require";
|
|
5
|
+
import { LOCALHOST, PLUGIN_IDENTIFIER } from "../constant.mjs";
|
|
6
|
+
import { addDataFetchExposes, autoDeleteSplitChunkCacheGroups } from "@module-federation/rsbuild-plugin/utils";
|
|
7
|
+
import logger from "../logger.mjs";
|
|
8
|
+
const defaultPath = path.resolve(process.cwd(), 'module-federation.config.ts');
|
|
9
|
+
function setEnv(enableSSR) {
|
|
10
|
+
if (enableSSR) process.env['MF_SSR_PRJ'] = 'true';
|
|
11
|
+
}
|
|
12
|
+
const getMFConfig = async (userConfig)=>{
|
|
13
|
+
const { config, configPath } = userConfig;
|
|
14
|
+
if (config) return config;
|
|
15
|
+
const mfConfigPath = configPath ? configPath : defaultPath;
|
|
16
|
+
const preBundlePath = await bundle(mfConfigPath);
|
|
17
|
+
const mfConfig = (await import(preBundlePath)).default;
|
|
18
|
+
return mfConfig;
|
|
19
|
+
};
|
|
20
|
+
const injectRuntimePlugins = (runtimePlugin, runtimePlugins)=>{
|
|
21
|
+
const pluginName = 'string' == typeof runtimePlugin ? runtimePlugin : runtimePlugin[0];
|
|
22
|
+
const hasPlugin = runtimePlugins.some((existingPlugin)=>{
|
|
23
|
+
if ('string' == typeof existingPlugin) return existingPlugin === pluginName;
|
|
24
|
+
return existingPlugin[0] === pluginName;
|
|
25
|
+
});
|
|
26
|
+
if (!hasPlugin) runtimePlugins.push(runtimePlugin);
|
|
27
|
+
};
|
|
28
|
+
const replaceRemoteUrl = (mfConfig, remoteIpStrategy)=>{
|
|
29
|
+
if (remoteIpStrategy && 'inherit' === remoteIpStrategy) return;
|
|
30
|
+
if (!mfConfig.remotes) return;
|
|
31
|
+
const ipv4 = getIPV4();
|
|
32
|
+
const handleRemoteObject = (remoteObject)=>{
|
|
33
|
+
Object.keys(remoteObject).forEach((remoteKey)=>{
|
|
34
|
+
const remote = remoteObject[remoteKey];
|
|
35
|
+
if (Array.isArray(remote)) return;
|
|
36
|
+
if ('string' == typeof remote && remote.includes(LOCALHOST)) remoteObject[remoteKey] = remote.replace(LOCALHOST, ipv4);
|
|
37
|
+
if ('object' == typeof remote && !Array.isArray(remote.external) && remote.external.includes(LOCALHOST)) remote.external = remote.external.replace(LOCALHOST, ipv4);
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
if (Array.isArray(mfConfig.remotes)) mfConfig.remotes.forEach((remoteObject)=>{
|
|
41
|
+
if ('string' == typeof remoteObject) return;
|
|
42
|
+
handleRemoteObject(remoteObject);
|
|
43
|
+
});
|
|
44
|
+
else if ('string' != typeof mfConfig.remotes) handleRemoteObject(mfConfig.remotes);
|
|
45
|
+
};
|
|
46
|
+
const patchDTSConfig = (mfConfig, isServer)=>{
|
|
47
|
+
if (isServer) return;
|
|
48
|
+
const ModernJSRuntime = '@module-federation/modern-js/runtime';
|
|
49
|
+
if (false !== mfConfig.dts) {
|
|
50
|
+
var _mfConfig_dts, _mfConfig_dts1;
|
|
51
|
+
if ('boolean' == typeof mfConfig.dts || void 0 === mfConfig.dts) mfConfig.dts = {
|
|
52
|
+
consumeTypes: {
|
|
53
|
+
runtimePkgs: [
|
|
54
|
+
ModernJSRuntime
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
else if ((null == (_mfConfig_dts = mfConfig.dts) ? void 0 : _mfConfig_dts.consumeTypes) || (null == (_mfConfig_dts1 = mfConfig.dts) ? void 0 : _mfConfig_dts1.consumeTypes) === void 0) {
|
|
59
|
+
var _mfConfig_dts2;
|
|
60
|
+
if ('boolean' == typeof mfConfig.dts.consumeTypes || (null == (_mfConfig_dts2 = mfConfig.dts) ? void 0 : _mfConfig_dts2.consumeTypes) === void 0) mfConfig.dts.consumeTypes = {
|
|
61
|
+
runtimePkgs: [
|
|
62
|
+
ModernJSRuntime
|
|
63
|
+
]
|
|
64
|
+
};
|
|
65
|
+
else {
|
|
66
|
+
mfConfig.dts.consumeTypes.runtimePkgs = mfConfig.dts.consumeTypes.runtimePkgs || [];
|
|
67
|
+
if (!mfConfig.dts.consumeTypes.runtimePkgs.includes(ModernJSRuntime)) mfConfig.dts.consumeTypes.runtimePkgs.push(ModernJSRuntime);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const patchMFConfig = (mfConfig, isServer, remoteIpStrategy, enableSSR)=>{
|
|
73
|
+
replaceRemoteUrl(mfConfig, remoteIpStrategy);
|
|
74
|
+
addDataFetchExposes(mfConfig.exposes, isServer);
|
|
75
|
+
if (void 0 === mfConfig.remoteType) mfConfig.remoteType = "script";
|
|
76
|
+
if (!mfConfig.name) throw new Error(`${PLUGIN_IDENTIFIER} mfConfig.name can not be empty!`);
|
|
77
|
+
const runtimePlugins = [
|
|
78
|
+
...mfConfig.runtimePlugins || []
|
|
79
|
+
];
|
|
80
|
+
patchDTSConfig(mfConfig, isServer);
|
|
81
|
+
injectRuntimePlugins(require.resolve('@module-federation/modern-js/shared-strategy'), runtimePlugins);
|
|
82
|
+
if (enableSSR && isDev()) injectRuntimePlugins(require.resolve('@module-federation/modern-js/resolve-entry-ipv4'), runtimePlugins);
|
|
83
|
+
if (isServer) {
|
|
84
|
+
injectRuntimePlugins(require.resolve('@module-federation/node/runtimePlugin'), runtimePlugins);
|
|
85
|
+
if (isDev()) injectRuntimePlugins(require.resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'), runtimePlugins);
|
|
86
|
+
injectRuntimePlugins(require.resolve('@module-federation/modern-js/inject-node-fetch'), runtimePlugins);
|
|
87
|
+
if (mfConfig.library) {
|
|
88
|
+
if (!mfConfig.library.type) mfConfig.library.type = 'commonjs-module';
|
|
89
|
+
if (!mfConfig.library.name) mfConfig.library.name = mfConfig.name;
|
|
90
|
+
} else mfConfig.library = {
|
|
91
|
+
type: 'commonjs-module',
|
|
92
|
+
name: mfConfig.name
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
mfConfig.runtimePlugins = runtimePlugins;
|
|
96
|
+
if (!isServer) {
|
|
97
|
+
var _mfConfig_library;
|
|
98
|
+
if ((null == (_mfConfig_library = mfConfig.library) ? void 0 : _mfConfig_library.type) === 'commonjs-module') mfConfig.library.type = 'global';
|
|
99
|
+
return mfConfig;
|
|
100
|
+
}
|
|
101
|
+
mfConfig.dts = false;
|
|
102
|
+
mfConfig.dev = false;
|
|
103
|
+
return mfConfig;
|
|
104
|
+
};
|
|
105
|
+
function patchIgnoreWarning(chain) {
|
|
106
|
+
const ignoreWarnings = chain.get('ignoreWarnings') || [];
|
|
107
|
+
const ignoredMsgs = [
|
|
108
|
+
"external script",
|
|
109
|
+
'process.env.WS_NO_BUFFER_UTIL',
|
|
110
|
+
"Can't resolve 'utf-8-validate"
|
|
111
|
+
];
|
|
112
|
+
ignoreWarnings.push((warning)=>{
|
|
113
|
+
if (ignoredMsgs.some((msg)=>warning.message.includes(msg))) return true;
|
|
114
|
+
return false;
|
|
115
|
+
});
|
|
116
|
+
chain.ignoreWarnings(ignoreWarnings);
|
|
117
|
+
}
|
|
118
|
+
function addMyTypes2Ignored(chain, mfConfig) {
|
|
119
|
+
const watchOptions = chain.get('watchOptions');
|
|
120
|
+
if (!watchOptions || !watchOptions.ignored) return void chain.watchOptions({
|
|
121
|
+
ignored: /[\\/](?:\.git|node_modules|@mf-types)[\\/]/
|
|
122
|
+
});
|
|
123
|
+
const ignored = watchOptions.ignored;
|
|
124
|
+
const DEFAULT_IGNORED_GLOB = '**/@mf-types/**';
|
|
125
|
+
if (Array.isArray(ignored)) {
|
|
126
|
+
if (false !== mfConfig.dts && 'object' == typeof mfConfig.dts && 'object' == typeof mfConfig.dts.consumeTypes && mfConfig.dts.consumeTypes.remoteTypesFolder) chain.watchOptions({
|
|
127
|
+
...watchOptions,
|
|
128
|
+
ignored: ignored.concat(`**/${mfConfig.dts.consumeTypes.remoteTypesFolder}/**`)
|
|
129
|
+
});
|
|
130
|
+
else chain.watchOptions({
|
|
131
|
+
...watchOptions,
|
|
132
|
+
ignored: ignored.concat(DEFAULT_IGNORED_GLOB)
|
|
133
|
+
});
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if ('string' != typeof ignored) return void chain.watchOptions({
|
|
137
|
+
...watchOptions,
|
|
138
|
+
ignored: /[\\/](?:\.git|node_modules|@mf-types)[\\/]/
|
|
139
|
+
});
|
|
140
|
+
chain.watchOptions({
|
|
141
|
+
...watchOptions,
|
|
142
|
+
ignored: ignored.concat(DEFAULT_IGNORED_GLOB)
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
function patchBundlerConfig(options) {
|
|
146
|
+
var _modernjsConfig_deploy;
|
|
147
|
+
const { chain, modernjsConfig, isServer, mfConfig, enableSSR } = options;
|
|
148
|
+
chain.optimization.delete('runtimeChunk');
|
|
149
|
+
patchIgnoreWarning(chain);
|
|
150
|
+
if (!chain.output.get('chunkLoadingGlobal')) chain.output.chunkLoadingGlobal(`chunk_${mfConfig.name}`);
|
|
151
|
+
if (!chain.output.get('uniqueName')) chain.output.uniqueName(mfConfig.name);
|
|
152
|
+
const splitChunkConfig = chain.optimization.splitChunks.entries();
|
|
153
|
+
if (!isServer) autoDeleteSplitChunkCacheGroups(mfConfig, splitChunkConfig);
|
|
154
|
+
if (!isServer && enableSSR && splitChunkConfig && 'object' == typeof splitChunkConfig && splitChunkConfig.cacheGroups) {
|
|
155
|
+
splitChunkConfig.chunks = 'async';
|
|
156
|
+
logger.warn('splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"');
|
|
157
|
+
}
|
|
158
|
+
if (isDev() && 'auto' === chain.output.get('publicPath')) {
|
|
159
|
+
var _modernjsConfig_dev, _modernjsConfig_server;
|
|
160
|
+
const port = (null == (_modernjsConfig_dev = modernjsConfig.dev) ? void 0 : _modernjsConfig_dev.port) || (null == (_modernjsConfig_server = modernjsConfig.server) ? void 0 : _modernjsConfig_server.port) || 8080;
|
|
161
|
+
const publicPath = `http://localhost:${port}/`;
|
|
162
|
+
chain.output.publicPath(publicPath);
|
|
163
|
+
}
|
|
164
|
+
if (isServer && enableSSR) {
|
|
165
|
+
const uniqueName = mfConfig.name || chain.output.get('uniqueName');
|
|
166
|
+
const chunkFileName = chain.output.get('chunkFilename');
|
|
167
|
+
if ('string' == typeof chunkFileName && uniqueName && !chunkFileName.includes(uniqueName)) {
|
|
168
|
+
const suffix = `${encodeName(uniqueName)}-[contenthash].js`;
|
|
169
|
+
chain.output.chunkFilename(chunkFileName.replace('.js', suffix));
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (isDev() && enableSSR && !isServer) chain.resolve.fallback.set('crypto', false).set('stream', false).set('vm', false);
|
|
173
|
+
if ((null == (_modernjsConfig_deploy = modernjsConfig.deploy) ? void 0 : _modernjsConfig_deploy.microFrontend) && Object.keys(mfConfig.exposes || {}).length) chain.optimization.usedExports(false);
|
|
174
|
+
}
|
|
175
|
+
const moduleFederationConfigPlugin = (userConfig)=>({
|
|
176
|
+
name: '@modern-js/plugin-module-federation-config',
|
|
177
|
+
pre: [
|
|
178
|
+
'@modern-js/plugin-initialize'
|
|
179
|
+
],
|
|
180
|
+
post: [
|
|
181
|
+
'@modern-js/plugin-module-federation'
|
|
182
|
+
],
|
|
183
|
+
setup: async (api)=>{
|
|
184
|
+
var _userConfig_userConfig, _modernjsConfig_server;
|
|
185
|
+
const modernjsConfig = api.getConfig();
|
|
186
|
+
const mfConfig = await getMFConfig(userConfig.originPluginOptions);
|
|
187
|
+
const csrConfig = userConfig.csrConfig || JSON.parse(JSON.stringify(mfConfig));
|
|
188
|
+
const ssrConfig = userConfig.ssrConfig || JSON.parse(JSON.stringify(mfConfig));
|
|
189
|
+
userConfig.ssrConfig = ssrConfig;
|
|
190
|
+
userConfig.csrConfig = csrConfig;
|
|
191
|
+
var _userConfig_userConfig_ssr;
|
|
192
|
+
const enableSSR = Boolean(null != (_userConfig_userConfig_ssr = null == (_userConfig_userConfig = userConfig.userConfig) ? void 0 : _userConfig_userConfig.ssr) ? _userConfig_userConfig_ssr : Boolean(null == modernjsConfig ? void 0 : null == (_modernjsConfig_server = modernjsConfig.server) ? void 0 : _modernjsConfig_server.ssr));
|
|
193
|
+
api.modifyBundlerChain((chain)=>{
|
|
194
|
+
const target = chain.get('target');
|
|
195
|
+
if (skipByTarget(target)) return;
|
|
196
|
+
const isWeb = isWebTarget(target);
|
|
197
|
+
addMyTypes2Ignored(chain, isWeb ? csrConfig : ssrConfig);
|
|
198
|
+
const targetMFConfig = isWeb ? csrConfig : ssrConfig;
|
|
199
|
+
patchMFConfig(targetMFConfig, !isWeb, userConfig.remoteIpStrategy || 'ipv4', enableSSR);
|
|
200
|
+
patchBundlerConfig({
|
|
201
|
+
chain,
|
|
202
|
+
isServer: !isWeb,
|
|
203
|
+
modernjsConfig,
|
|
204
|
+
mfConfig,
|
|
205
|
+
enableSSR
|
|
206
|
+
});
|
|
207
|
+
userConfig.distOutputDir = chain.output.get('path') || path.resolve(process.cwd(), 'dist');
|
|
208
|
+
});
|
|
209
|
+
api.config(()=>{
|
|
210
|
+
var _modernjsConfig_tools, _userConfig_csrConfig, _modernjsConfig_source, _modernjsConfig_source1, _modernjsConfig_dev;
|
|
211
|
+
const bundlerType = 'rspack' === api.getAppContext().bundlerType ? 'rspack' : 'webpack';
|
|
212
|
+
const ipv4 = getIPV4();
|
|
213
|
+
if (void 0 === userConfig.remoteIpStrategy) if (enableSSR) userConfig.remoteIpStrategy = 'ipv4';
|
|
214
|
+
else userConfig.remoteIpStrategy = 'inherit';
|
|
215
|
+
const devServerConfig = null == (_modernjsConfig_tools = modernjsConfig.tools) ? void 0 : _modernjsConfig_tools.devServer;
|
|
216
|
+
const corsWarnMsgs = [
|
|
217
|
+
'View https://module-federation.io/guide/troubleshooting/other.html#cors-warn for more details.'
|
|
218
|
+
];
|
|
219
|
+
if ('object' != typeof devServerConfig || !('headers' in devServerConfig)) corsWarnMsgs.unshift('Detect devServer.headers is empty, mf modern plugin will add default cors header: devServer.headers["Access-Control-Allow-Headers"] = "*". It is recommended to specify an allowlist of trusted origins instead.');
|
|
220
|
+
const exposes = null == (_userConfig_csrConfig = userConfig.csrConfig) ? void 0 : _userConfig_csrConfig.exposes;
|
|
221
|
+
const hasExposes = exposes && Array.isArray(exposes) ? exposes.length : Object.keys(null != exposes ? exposes : {}).length;
|
|
222
|
+
if (corsWarnMsgs.length > 1 && hasExposes) logger.warn(corsWarnMsgs.join('\n'));
|
|
223
|
+
const corsHeaders = hasExposes ? {
|
|
224
|
+
'Access-Control-Allow-Origin': '*',
|
|
225
|
+
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
|
|
226
|
+
'Access-Control-Allow-Headers': '*'
|
|
227
|
+
} : void 0;
|
|
228
|
+
const defineConfig = {
|
|
229
|
+
REMOTE_IP_STRATEGY: JSON.stringify(userConfig.remoteIpStrategy)
|
|
230
|
+
};
|
|
231
|
+
if (enableSSR && isDev()) defineConfig['FEDERATION_IPV4'] = JSON.stringify(ipv4);
|
|
232
|
+
var _modernjsConfig_source_enableAsyncEntry;
|
|
233
|
+
return {
|
|
234
|
+
tools: {
|
|
235
|
+
devServer: {
|
|
236
|
+
headers: corsHeaders
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
resolve: {
|
|
240
|
+
alias: {
|
|
241
|
+
'@modern-js/runtime/mf': require.resolve('@module-federation/modern-js/runtime')
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
source: {
|
|
245
|
+
define: defineConfig,
|
|
246
|
+
enableAsyncEntry: 'rspack' === bundlerType ? null != (_modernjsConfig_source_enableAsyncEntry = null == (_modernjsConfig_source = modernjsConfig.source) ? void 0 : _modernjsConfig_source.enableAsyncEntry) ? _modernjsConfig_source_enableAsyncEntry : true : null == (_modernjsConfig_source1 = modernjsConfig.source) ? void 0 : _modernjsConfig_source1.enableAsyncEntry
|
|
247
|
+
},
|
|
248
|
+
dev: {
|
|
249
|
+
assetPrefix: (null == modernjsConfig ? void 0 : null == (_modernjsConfig_dev = modernjsConfig.dev) ? void 0 : _modernjsConfig_dev.assetPrefix) ? modernjsConfig.dev.assetPrefix : true
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
const configPlugin = moduleFederationConfigPlugin;
|
|
256
|
+
export { addMyTypes2Ignored, configPlugin as default, getMFConfig, isWebTarget, moduleFederationConfigPlugin, patchBundlerConfig, patchMFConfig, setEnv, skipByTarget };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { AsyncBoundaryPlugin, ModuleFederationPlugin, createModuleFederationConfig } from "@module-federation/enhanced";
|
|
2
|
+
import { ModuleFederationPlugin as rspack_ModuleFederationPlugin } from "@module-federation/enhanced/rspack";
|
|
3
|
+
import { moduleFederationConfigPlugin } from "./configPlugin.mjs";
|
|
4
|
+
import { moduleFederationSSRPlugin } from "./ssrPlugin.mjs";
|
|
5
|
+
import { isWebTarget } from "./utils.mjs";
|
|
6
|
+
const moduleFederationPlugin = (userConfig = {})=>{
|
|
7
|
+
var _userConfig_fetchServerQuery;
|
|
8
|
+
const internalModernPluginOptions = {
|
|
9
|
+
csrConfig: void 0,
|
|
10
|
+
ssrConfig: void 0,
|
|
11
|
+
browserPlugin: void 0,
|
|
12
|
+
nodePlugin: void 0,
|
|
13
|
+
assetResources: {},
|
|
14
|
+
distOutputDir: '',
|
|
15
|
+
originPluginOptions: userConfig,
|
|
16
|
+
remoteIpStrategy: null == userConfig ? void 0 : userConfig.remoteIpStrategy,
|
|
17
|
+
userConfig: userConfig || {},
|
|
18
|
+
assetFileNames: {},
|
|
19
|
+
fetchServerQuery: null != (_userConfig_fetchServerQuery = userConfig.fetchServerQuery) ? _userConfig_fetchServerQuery : void 0
|
|
20
|
+
};
|
|
21
|
+
return {
|
|
22
|
+
name: '@modern-js/plugin-module-federation',
|
|
23
|
+
setup: async (api)=>{
|
|
24
|
+
const modernjsConfig = api.getConfig();
|
|
25
|
+
api.modifyBundlerChain((chain)=>{
|
|
26
|
+
const bundlerType = 'rspack' === api.getAppContext().bundlerType ? 'rspack' : 'webpack';
|
|
27
|
+
const browserPluginOptions = internalModernPluginOptions.csrConfig;
|
|
28
|
+
const MFPlugin = 'webpack' === bundlerType ? ModuleFederationPlugin : rspack_ModuleFederationPlugin;
|
|
29
|
+
if (isWebTarget(chain.get('target'))) chain.plugin('plugin-module-federation').use(MFPlugin, [
|
|
30
|
+
browserPluginOptions
|
|
31
|
+
]).init((Plugin, args)=>{
|
|
32
|
+
internalModernPluginOptions.browserPlugin = new Plugin(args[0]);
|
|
33
|
+
return internalModernPluginOptions.browserPlugin;
|
|
34
|
+
});
|
|
35
|
+
if ('webpack' === bundlerType) {
|
|
36
|
+
var _modernjsConfig_source;
|
|
37
|
+
const enableAsyncEntry = null == (_modernjsConfig_source = modernjsConfig.source) ? void 0 : _modernjsConfig_source.enableAsyncEntry;
|
|
38
|
+
if (!enableAsyncEntry && false !== browserPluginOptions.async) {
|
|
39
|
+
const asyncBoundaryPluginOptions = 'object' == typeof browserPluginOptions.async ? browserPluginOptions.async : {
|
|
40
|
+
eager: (module)=>module && /\.federation/.test((null == module ? void 0 : module.request) || ''),
|
|
41
|
+
excludeChunk: (chunk)=>chunk.name === browserPluginOptions.name
|
|
42
|
+
};
|
|
43
|
+
chain.plugin('async-boundary-plugin').use(AsyncBoundaryPlugin, [
|
|
44
|
+
asyncBoundaryPluginOptions
|
|
45
|
+
]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
api._internalServerPlugins(({ plugins })=>{
|
|
50
|
+
plugins.push({
|
|
51
|
+
name: '@module-federation/modern-js/server'
|
|
52
|
+
});
|
|
53
|
+
return {
|
|
54
|
+
plugins
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
usePlugins: [
|
|
59
|
+
moduleFederationConfigPlugin(internalModernPluginOptions),
|
|
60
|
+
moduleFederationSSRPlugin(internalModernPluginOptions)
|
|
61
|
+
]
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
const cli = moduleFederationPlugin;
|
|
65
|
+
export { createModuleFederationConfig, cli as default, moduleFederationPlugin };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import node_fetch from "node-fetch";
|
|
2
|
+
const injectNodeFetchPlugin = ()=>({
|
|
3
|
+
name: 'inject-node-fetch-plugin',
|
|
4
|
+
beforeInit (args) {
|
|
5
|
+
if (!globalThis.fetch) globalThis.fetch = node_fetch;
|
|
6
|
+
return args;
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
const inject_node_fetch = injectNodeFetchPlugin;
|
|
10
|
+
export { inject_node_fetch as default };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { LOCALHOST } from "../../constant.mjs";
|
|
2
|
+
const ipv4 = 'undefined' != typeof FEDERATION_IPV4 ? FEDERATION_IPV4 : '127.0.0.1';
|
|
3
|
+
const remoteIpStrategy = 'undefined' != typeof REMOTE_IP_STRATEGY ? REMOTE_IP_STRATEGY : 'inherit';
|
|
4
|
+
function replaceObjectLocalhost(key, obj) {
|
|
5
|
+
if ('ipv4' !== remoteIpStrategy) return;
|
|
6
|
+
if (!(key in obj)) return;
|
|
7
|
+
const remote = obj[key];
|
|
8
|
+
if (remote && 'string' == typeof remote && remote.includes(LOCALHOST)) obj[key] = replaceLocalhost(remote);
|
|
9
|
+
}
|
|
10
|
+
function replaceLocalhost(url) {
|
|
11
|
+
return url.replace(LOCALHOST, ipv4);
|
|
12
|
+
}
|
|
13
|
+
const resolveEntryIpv4Plugin = ()=>({
|
|
14
|
+
name: 'resolve-entry-ipv4',
|
|
15
|
+
beforeRegisterRemote (args) {
|
|
16
|
+
const { remote } = args;
|
|
17
|
+
replaceObjectLocalhost('entry', remote);
|
|
18
|
+
return args;
|
|
19
|
+
},
|
|
20
|
+
async afterResolve (args) {
|
|
21
|
+
const { remoteInfo } = args;
|
|
22
|
+
replaceObjectLocalhost('entry', remoteInfo);
|
|
23
|
+
return args;
|
|
24
|
+
},
|
|
25
|
+
beforeLoadRemoteSnapshot (args) {
|
|
26
|
+
const { moduleInfo } = args;
|
|
27
|
+
if ('entry' in moduleInfo) {
|
|
28
|
+
replaceObjectLocalhost('entry', moduleInfo);
|
|
29
|
+
return args;
|
|
30
|
+
}
|
|
31
|
+
if ('version' in moduleInfo) replaceObjectLocalhost('version', moduleInfo);
|
|
32
|
+
return args;
|
|
33
|
+
},
|
|
34
|
+
loadRemoteSnapshot (args) {
|
|
35
|
+
const { remoteSnapshot } = args;
|
|
36
|
+
if ('publicPath' in remoteSnapshot) replaceObjectLocalhost('publicPath', remoteSnapshot);
|
|
37
|
+
if ('getPublicPath' in remoteSnapshot) replaceObjectLocalhost('getPublicPath', remoteSnapshot);
|
|
38
|
+
if (remoteSnapshot.remotesInfo) Object.keys(remoteSnapshot.remotesInfo).forEach((key)=>{
|
|
39
|
+
const remoteInfo = remoteSnapshot.remotesInfo[key];
|
|
40
|
+
replaceObjectLocalhost('matchedVersion', remoteInfo);
|
|
41
|
+
});
|
|
42
|
+
return args;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
const resolve_entry_ipv4 = resolveEntryIpv4Plugin;
|
|
46
|
+
export { resolve_entry_ipv4 as default };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const sharedStrategy = ()=>({
|
|
2
|
+
name: 'shared-strategy-plugin',
|
|
3
|
+
beforeInit (args) {
|
|
4
|
+
const { userOptions } = args;
|
|
5
|
+
const shared = userOptions.shared;
|
|
6
|
+
if (shared) Object.keys(shared).forEach((sharedKey)=>{
|
|
7
|
+
const sharedConfigs = shared[sharedKey];
|
|
8
|
+
const arraySharedConfigs = Array.isArray(sharedConfigs) ? sharedConfigs : [
|
|
9
|
+
sharedConfigs
|
|
10
|
+
];
|
|
11
|
+
arraySharedConfigs.forEach((s)=>{
|
|
12
|
+
s.strategy = 'loaded-first';
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
return args;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
const shared_strategy = sharedStrategy;
|
|
19
|
+
export { shared_strategy as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import data_fetch_server_middleware from "@module-federation/bridge-react/data-fetch-server-middleware";
|
|
2
|
+
const dataFetchServePlugin = ()=>({
|
|
3
|
+
name: 'mf-data-fetch-server-plugin',
|
|
4
|
+
setup: (api)=>{
|
|
5
|
+
api.onPrepare(()=>{
|
|
6
|
+
const { middlewares } = api.getServerContext();
|
|
7
|
+
middlewares.push({
|
|
8
|
+
name: 'module-federation-serve-manifest',
|
|
9
|
+
handler: data_fetch_server_middleware
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
const data_fetch_server_plugin = dataFetchServePlugin;
|
|
15
|
+
export { data_fetch_server_plugin as default };
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import fs_extra from "fs-extra";
|
|
3
|
+
import { ModuleFederationPlugin } from "@module-federation/enhanced/webpack";
|
|
4
|
+
import { ModuleFederationPlugin as rspack_ModuleFederationPlugin } from "@module-federation/enhanced/rspack";
|
|
5
|
+
import universe_entry_chunk_tracker_plugin from "@module-federation/node/universe-entry-chunk-tracker-plugin";
|
|
6
|
+
import logger from "../logger.mjs";
|
|
7
|
+
import { isDev, isWebTarget, skipByTarget } from "./utils.mjs";
|
|
8
|
+
import { updateStatsAndManifest } from "@module-federation/rsbuild-plugin/utils";
|
|
9
|
+
import { ManifestFileName, StatsFileName, simpleJoinRemoteEntry } from "@module-federation/sdk";
|
|
10
|
+
function setEnv() {
|
|
11
|
+
process.env['MF_SSR_PRJ'] = 'true';
|
|
12
|
+
}
|
|
13
|
+
const CHAIN_MF_PLUGIN_ID = 'plugin-module-federation-server';
|
|
14
|
+
function getManifestAssetFileNames(manifestOption) {
|
|
15
|
+
if (!manifestOption) return {
|
|
16
|
+
statsFileName: StatsFileName,
|
|
17
|
+
manifestFileName: ManifestFileName
|
|
18
|
+
};
|
|
19
|
+
const JSON_EXT = '.json';
|
|
20
|
+
const filePath = 'boolean' == typeof manifestOption ? '' : manifestOption.filePath || '';
|
|
21
|
+
const baseFileName = 'boolean' == typeof manifestOption ? '' : manifestOption.fileName || '';
|
|
22
|
+
const ensureExt = (name)=>name.endsWith(JSON_EXT) ? name : `${name}${JSON_EXT}`;
|
|
23
|
+
const withSuffix = (name, suffix)=>name.replace(JSON_EXT, `${suffix}${JSON_EXT}`);
|
|
24
|
+
const manifestFileName = baseFileName ? ensureExt(baseFileName) : ManifestFileName;
|
|
25
|
+
const statsFileName = baseFileName ? withSuffix(manifestFileName, '-stats') : StatsFileName;
|
|
26
|
+
return {
|
|
27
|
+
statsFileName: simpleJoinRemoteEntry(filePath, statsFileName),
|
|
28
|
+
manifestFileName: simpleJoinRemoteEntry(filePath, manifestFileName)
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const mfSSRRsbuildPlugin = (pluginOptions)=>({
|
|
32
|
+
name: '@modern-js/plugin-mf-post-config',
|
|
33
|
+
pre: [
|
|
34
|
+
'@modern-js/builder-plugin-ssr'
|
|
35
|
+
],
|
|
36
|
+
setup (api) {
|
|
37
|
+
var _pluginOptions_csrConfig, _pluginOptions_assetFileNames, _pluginOptions_ssrConfig;
|
|
38
|
+
if (pluginOptions.csrConfig.getPublicPath) return;
|
|
39
|
+
let csrOutputPath = '';
|
|
40
|
+
let ssrOutputPath = '';
|
|
41
|
+
let ssrEnv = '';
|
|
42
|
+
let csrEnv = '';
|
|
43
|
+
const browserAssetFileNames = pluginOptions.assetFileNames.browser || getManifestAssetFileNames(null == (_pluginOptions_csrConfig = pluginOptions.csrConfig) ? void 0 : _pluginOptions_csrConfig.manifest);
|
|
44
|
+
const nodeAssetFileNames = (null == (_pluginOptions_assetFileNames = pluginOptions.assetFileNames) ? void 0 : _pluginOptions_assetFileNames.node) || getManifestAssetFileNames(null == (_pluginOptions_ssrConfig = pluginOptions.ssrConfig) ? void 0 : _pluginOptions_ssrConfig.manifest);
|
|
45
|
+
const collectAssets = (assets, fileNames, tag)=>{
|
|
46
|
+
const statsAsset = assets[fileNames.statsFileName];
|
|
47
|
+
const manifestAsset = assets[fileNames.manifestFileName];
|
|
48
|
+
if (!statsAsset || !manifestAsset) return;
|
|
49
|
+
try {
|
|
50
|
+
const statsRaw = statsAsset.source();
|
|
51
|
+
const manifestRaw = manifestAsset.source();
|
|
52
|
+
const statsContent = 'string' == typeof statsRaw ? statsRaw : statsRaw.toString();
|
|
53
|
+
const manifestContent = 'string' == typeof manifestRaw ? manifestRaw : manifestRaw.toString();
|
|
54
|
+
return {
|
|
55
|
+
stats: {
|
|
56
|
+
data: JSON.parse(statsContent),
|
|
57
|
+
filename: fileNames.statsFileName
|
|
58
|
+
},
|
|
59
|
+
manifest: {
|
|
60
|
+
data: JSON.parse(manifestContent),
|
|
61
|
+
filename: fileNames.manifestFileName
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
} catch (err) {
|
|
65
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
66
|
+
logger.error(`Failed to parse ${tag} manifest assets: ${message}`);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
api.modifyEnvironmentConfig((config, { name })=>{
|
|
71
|
+
const target = config.output.target;
|
|
72
|
+
if (skipByTarget(target)) return config;
|
|
73
|
+
if (isWebTarget(target)) {
|
|
74
|
+
csrOutputPath = config.output.distPath.root;
|
|
75
|
+
csrEnv = name;
|
|
76
|
+
} else {
|
|
77
|
+
ssrOutputPath = config.output.distPath.root;
|
|
78
|
+
ssrEnv = name;
|
|
79
|
+
}
|
|
80
|
+
return config;
|
|
81
|
+
});
|
|
82
|
+
const modifySSRPublicPath = (config, utils)=>{
|
|
83
|
+
if (ssrEnv !== utils.environment.name) return config;
|
|
84
|
+
const userSSRConfig = pluginOptions.userConfig.ssr ? 'object' == typeof pluginOptions.userConfig.ssr ? pluginOptions.userConfig.ssr : {} : {};
|
|
85
|
+
if (userSSRConfig.distOutputDir) return;
|
|
86
|
+
config.output.publicPath = `${config.output.publicPath}${path.relative(csrOutputPath, ssrOutputPath)}/`;
|
|
87
|
+
return config;
|
|
88
|
+
};
|
|
89
|
+
api.modifyWebpackConfig((config, utils)=>{
|
|
90
|
+
modifySSRPublicPath(config, utils);
|
|
91
|
+
return config;
|
|
92
|
+
});
|
|
93
|
+
api.modifyRspackConfig((config, utils)=>{
|
|
94
|
+
modifySSRPublicPath(config, utils);
|
|
95
|
+
return config;
|
|
96
|
+
});
|
|
97
|
+
api.processAssets({
|
|
98
|
+
stage: 'report'
|
|
99
|
+
}, ({ assets, environment: envContext })=>{
|
|
100
|
+
var _pluginOptions_csrConfig, _pluginOptions_ssrConfig;
|
|
101
|
+
const envName = envContext.name;
|
|
102
|
+
if ((null == (_pluginOptions_csrConfig = pluginOptions.csrConfig) ? void 0 : _pluginOptions_csrConfig.manifest) !== false && csrEnv && envName === csrEnv) {
|
|
103
|
+
const browserAssets = collectAssets(assets, browserAssetFileNames, 'browser');
|
|
104
|
+
if (browserAssets) pluginOptions.assetResources.browser = browserAssets;
|
|
105
|
+
}
|
|
106
|
+
if ((null == (_pluginOptions_ssrConfig = pluginOptions.ssrConfig) ? void 0 : _pluginOptions_ssrConfig.manifest) !== false && ssrEnv && envName === ssrEnv) {
|
|
107
|
+
const nodeAssets = collectAssets(assets, nodeAssetFileNames, 'node');
|
|
108
|
+
if (nodeAssets) pluginOptions.assetResources.node = nodeAssets;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
const moduleFederationSSRPlugin = (pluginOptions)=>({
|
|
114
|
+
name: '@modern-js/plugin-module-federation-ssr',
|
|
115
|
+
pre: [
|
|
116
|
+
'@modern-js/plugin-module-federation-config',
|
|
117
|
+
'@modern-js/plugin-module-federation'
|
|
118
|
+
],
|
|
119
|
+
setup: async (api)=>{
|
|
120
|
+
var _pluginOptions_userConfig, _modernjsConfig_server;
|
|
121
|
+
const modernjsConfig = api.getConfig();
|
|
122
|
+
var _pluginOptions_userConfig_ssr;
|
|
123
|
+
const enableSSR = null != (_pluginOptions_userConfig_ssr = null == (_pluginOptions_userConfig = pluginOptions.userConfig) ? void 0 : _pluginOptions_userConfig.ssr) ? _pluginOptions_userConfig_ssr : Boolean(null == modernjsConfig ? void 0 : null == (_modernjsConfig_server = modernjsConfig.server) ? void 0 : _modernjsConfig_server.ssr);
|
|
124
|
+
if (!enableSSR) return;
|
|
125
|
+
setEnv();
|
|
126
|
+
api._internalRuntimePlugins(({ entrypoint, plugins })=>{
|
|
127
|
+
const { fetchServerQuery } = pluginOptions;
|
|
128
|
+
plugins.push({
|
|
129
|
+
name: 'injectDataFetchFunction',
|
|
130
|
+
path: '@module-federation/modern-js/ssr-inject-data-fetch-function-plugin',
|
|
131
|
+
config: {
|
|
132
|
+
fetchServerQuery
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
if (!isDev()) return {
|
|
136
|
+
entrypoint,
|
|
137
|
+
plugins
|
|
138
|
+
};
|
|
139
|
+
plugins.push({
|
|
140
|
+
name: 'mfSSRDev',
|
|
141
|
+
path: '@module-federation/modern-js/ssr-dev-plugin',
|
|
142
|
+
config: {}
|
|
143
|
+
});
|
|
144
|
+
return {
|
|
145
|
+
entrypoint,
|
|
146
|
+
plugins
|
|
147
|
+
};
|
|
148
|
+
});
|
|
149
|
+
if (pluginOptions.ssrConfig.remotes) api._internalServerPlugins(({ plugins })=>{
|
|
150
|
+
plugins.push({
|
|
151
|
+
name: '@module-federation/modern-js/data-fetch-server-plugin',
|
|
152
|
+
options: {}
|
|
153
|
+
});
|
|
154
|
+
return {
|
|
155
|
+
plugins
|
|
156
|
+
};
|
|
157
|
+
});
|
|
158
|
+
api.modifyBundlerChain((chain)=>{
|
|
159
|
+
const target = chain.get('target');
|
|
160
|
+
if (skipByTarget(target)) return;
|
|
161
|
+
const bundlerType = 'rspack' === api.getAppContext().bundlerType ? 'rspack' : 'webpack';
|
|
162
|
+
const MFPlugin = 'webpack' === bundlerType ? ModuleFederationPlugin : rspack_ModuleFederationPlugin;
|
|
163
|
+
const isWeb = isWebTarget(target);
|
|
164
|
+
if (!isWeb) {
|
|
165
|
+
if (!chain.plugins.has(CHAIN_MF_PLUGIN_ID)) chain.plugin(CHAIN_MF_PLUGIN_ID).use(MFPlugin, [
|
|
166
|
+
pluginOptions.ssrConfig
|
|
167
|
+
]).init((Plugin, args)=>{
|
|
168
|
+
pluginOptions.nodePlugin = new Plugin(args[0]);
|
|
169
|
+
return pluginOptions.nodePlugin;
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
if (!isWeb) {
|
|
173
|
+
chain.target('async-node');
|
|
174
|
+
if (isDev()) chain.plugin('UniverseEntryChunkTrackerPlugin').use(universe_entry_chunk_tracker_plugin);
|
|
175
|
+
const userSSRConfig = pluginOptions.userConfig.ssr ? 'object' == typeof pluginOptions.userConfig.ssr ? pluginOptions.userConfig.ssr : {} : {};
|
|
176
|
+
const publicPath = chain.output.get('publicPath');
|
|
177
|
+
if (userSSRConfig.distOutputDir && publicPath) chain.output.publicPath(`${publicPath}${userSSRConfig.distOutputDir}/`);
|
|
178
|
+
}
|
|
179
|
+
if (isDev() && isWeb) chain.externals({
|
|
180
|
+
'@module-federation/node/utils': 'NOT_USED_IN_BROWSER'
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
api.config(()=>({
|
|
184
|
+
builderPlugins: [
|
|
185
|
+
mfSSRRsbuildPlugin(pluginOptions)
|
|
186
|
+
],
|
|
187
|
+
tools: {
|
|
188
|
+
devServer: {
|
|
189
|
+
before: [
|
|
190
|
+
(req, res, next)=>{
|
|
191
|
+
if (!enableSSR) return void next();
|
|
192
|
+
try {
|
|
193
|
+
var _req_url, _req_url1;
|
|
194
|
+
if ((null == (_req_url = req.url) ? void 0 : _req_url.includes('.json')) && !(null == (_req_url1 = req.url) ? void 0 : _req_url1.includes('hot-update'))) {
|
|
195
|
+
const filepath = path.join(process.cwd(), `dist${req.url}`);
|
|
196
|
+
fs_extra.statSync(filepath);
|
|
197
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
198
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
|
|
199
|
+
res.setHeader('Access-Control-Allow-Headers', '*');
|
|
200
|
+
fs_extra.createReadStream(filepath).pipe(res);
|
|
201
|
+
} else next();
|
|
202
|
+
} catch (err) {
|
|
203
|
+
logger.debug(err);
|
|
204
|
+
next();
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
]
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}));
|
|
211
|
+
const writeMergedManifest = ()=>{
|
|
212
|
+
const { distOutputDir, assetResources } = pluginOptions;
|
|
213
|
+
const browserAssets = assetResources.browser;
|
|
214
|
+
const nodeAssets = assetResources.node;
|
|
215
|
+
if (!distOutputDir || !browserAssets || !nodeAssets) return;
|
|
216
|
+
try {
|
|
217
|
+
updateStatsAndManifest(nodeAssets, browserAssets, distOutputDir);
|
|
218
|
+
} catch (err) {
|
|
219
|
+
logger.error(err);
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
api.onAfterBuild(()=>{
|
|
223
|
+
writeMergedManifest();
|
|
224
|
+
});
|
|
225
|
+
api.onDevCompileDone(()=>{
|
|
226
|
+
writeMergedManifest();
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
const ssrPlugin = moduleFederationSSRPlugin;
|
|
231
|
+
export { CHAIN_MF_PLUGIN_ID, ssrPlugin as default, moduleFederationSSRPlugin, setEnv };
|