@module-federation/modern-js 0.13.1 → 0.14.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/package.json +22 -14
- package/dist/LICENSE +0 -21
- package/dist/cjs/cli/configPlugin.js +0 -377
- package/dist/cjs/cli/configPlugin.spec.js +0 -112
- package/dist/cjs/cli/constant.js +0 -28
- package/dist/cjs/cli/index.js +0 -85
- package/dist/cjs/cli/logger.js +0 -27
- package/dist/cjs/cli/manifest.js +0 -75
- package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +0 -44
- package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +0 -82
- package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +0 -43
- package/dist/cjs/cli/ssrPlugin.js +0 -217
- package/dist/cjs/cli/utils.js +0 -85
- package/dist/cjs/constant.js +0 -31
- package/dist/cjs/interfaces/bundler.js +0 -16
- package/dist/cjs/runtime/createRemoteSSRComponent.js +0 -201
- package/dist/cjs/runtime/index.js +0 -38
- package/dist/cjs/ssr-runtime/SSRLiveReload.js +0 -43
- package/dist/cjs/ssr-runtime/index.js +0 -22
- package/dist/cjs/ssr-runtime/plugin.js +0 -68
- package/dist/cjs/types/index.js +0 -16
- package/dist/esm/cli/configPlugin.js +0 -384
- package/dist/esm/cli/configPlugin.spec.js +0 -108
- package/dist/esm/cli/constant.js +0 -4
- package/dist/esm/cli/index.js +0 -79
- package/dist/esm/cli/logger.js +0 -7
- package/dist/esm/cli/manifest.js +0 -41
- package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +0 -16
- package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +0 -74
- package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +0 -25
- package/dist/esm/cli/ssrPlugin.js +0 -199
- package/dist/esm/cli/utils.js +0 -49
- package/dist/esm/constant.js +0 -6
- package/dist/esm/interfaces/bundler.js +0 -0
- package/dist/esm/runtime/createRemoteSSRComponent.js +0 -191
- package/dist/esm/runtime/index.js +0 -12
- package/dist/esm/ssr-runtime/SSRLiveReload.js +0 -26
- package/dist/esm/ssr-runtime/index.js +0 -1
- package/dist/esm/ssr-runtime/plugin.js +0 -70
- package/dist/esm/types/index.js +0 -0
- package/dist/esm-node/cli/configPlugin.js +0 -336
- package/dist/esm-node/cli/configPlugin.spec.js +0 -89
- package/dist/esm-node/cli/constant.js +0 -4
- package/dist/esm-node/cli/index.js +0 -60
- package/dist/esm-node/cli/logger.js +0 -7
- package/dist/esm-node/cli/manifest.js +0 -41
- package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +0 -14
- package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +0 -62
- package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +0 -23
- package/dist/esm-node/cli/ssrPlugin.js +0 -181
- package/dist/esm-node/cli/utils.js +0 -49
- package/dist/esm-node/constant.js +0 -6
- package/dist/esm-node/interfaces/bundler.js +0 -0
- package/dist/esm-node/runtime/createRemoteSSRComponent.js +0 -166
- package/dist/esm-node/runtime/index.js +0 -12
- package/dist/esm-node/ssr-runtime/SSRLiveReload.js +0 -19
- package/dist/esm-node/ssr-runtime/index.js +0 -1
- package/dist/esm-node/ssr-runtime/plugin.js +0 -34
- package/dist/esm-node/types/index.js +0 -0
- package/dist/types/cli/configPlugin.d.ts +0 -21
- package/dist/types/cli/configPlugin.spec.d.ts +0 -1
- package/dist/types/cli/constant.d.ts +0 -1
- package/dist/types/cli/index.d.ts +0 -5
- package/dist/types/cli/logger.d.ts +0 -2
- package/dist/types/cli/manifest.d.ts +0 -2
- package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +0 -3
- package/dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts +0 -3
- package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +0 -3
- package/dist/types/cli/ssrPlugin.d.ts +0 -6
- package/dist/types/cli/utils.d.ts +0 -5
- package/dist/types/constant.d.ts +0 -2
- package/dist/types/interfaces/bundler.d.ts +0 -18
- package/dist/types/runtime/createRemoteSSRComponent.d.ts +0 -18
- package/dist/types/runtime/index.d.ts +0 -5
- package/dist/types/ssr-runtime/SSRLiveReload.d.ts +0 -1
- package/dist/types/ssr-runtime/index.d.ts +0 -1
- package/dist/types/ssr-runtime/plugin.d.ts +0 -2
- package/dist/types/types/index.d.ts +0 -22
|
@@ -1,336 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { getIPV4, isWebTarget, skipByTarget } from "./utils";
|
|
3
|
-
import { encodeName } from "@module-federation/sdk";
|
|
4
|
-
import { bundle } from "@modern-js/node-bundle-require";
|
|
5
|
-
import { LOCALHOST, PLUGIN_IDENTIFIER } from "../constant";
|
|
6
|
-
import { autoDeleteSplitChunkCacheGroups } from "@module-federation/rsbuild-plugin/utils";
|
|
7
|
-
import logger from "./logger";
|
|
8
|
-
const defaultPath = path.resolve(process.cwd(), "module-federation.config.ts");
|
|
9
|
-
const isDev = process.env.NODE_ENV === "development";
|
|
10
|
-
function setEnv(enableSSR) {
|
|
11
|
-
if (enableSSR) {
|
|
12
|
-
process.env["MF_DISABLE_EMIT_STATS"] = "true";
|
|
13
|
-
process.env["MF_SSR_PRJ"] = "true";
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
const getMFConfig = async (userConfig) => {
|
|
17
|
-
const { config, configPath } = userConfig;
|
|
18
|
-
if (config) {
|
|
19
|
-
return config;
|
|
20
|
-
}
|
|
21
|
-
const mfConfigPath = configPath ? configPath : defaultPath;
|
|
22
|
-
const preBundlePath = await bundle(mfConfigPath);
|
|
23
|
-
const mfConfig = (await import(preBundlePath)).default;
|
|
24
|
-
return mfConfig;
|
|
25
|
-
};
|
|
26
|
-
const injectRuntimePlugins = (runtimePlugin, runtimePlugins) => {
|
|
27
|
-
if (!runtimePlugins.includes(runtimePlugin)) {
|
|
28
|
-
runtimePlugins.push(runtimePlugin);
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
const replaceRemoteUrl = (mfConfig, remoteIpStrategy) => {
|
|
32
|
-
if (remoteIpStrategy && remoteIpStrategy === "inherit") {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
if (!mfConfig.remotes) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
const ipv4 = getIPV4();
|
|
39
|
-
const handleRemoteObject = (remoteObject) => {
|
|
40
|
-
Object.keys(remoteObject).forEach((remoteKey) => {
|
|
41
|
-
const remote = remoteObject[remoteKey];
|
|
42
|
-
if (Array.isArray(remote)) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
if (typeof remote === "string" && remote.includes(LOCALHOST)) {
|
|
46
|
-
remoteObject[remoteKey] = remote.replace(LOCALHOST, ipv4);
|
|
47
|
-
}
|
|
48
|
-
if (typeof remote === "object" && !Array.isArray(remote.external) && remote.external.includes(LOCALHOST)) {
|
|
49
|
-
remote.external = remote.external.replace(LOCALHOST, ipv4);
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
};
|
|
53
|
-
if (Array.isArray(mfConfig.remotes)) {
|
|
54
|
-
mfConfig.remotes.forEach((remoteObject) => {
|
|
55
|
-
if (typeof remoteObject === "string") {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
handleRemoteObject(remoteObject);
|
|
59
|
-
});
|
|
60
|
-
} else if (typeof mfConfig.remotes !== "string") {
|
|
61
|
-
handleRemoteObject(mfConfig.remotes);
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
const patchDTSConfig = (mfConfig, isServer) => {
|
|
65
|
-
if (isServer) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
const ModernJSRuntime = "@module-federation/modern-js/runtime";
|
|
69
|
-
if (mfConfig.dts !== false) {
|
|
70
|
-
var _mfConfig_dts, _mfConfig_dts1;
|
|
71
|
-
if (typeof mfConfig.dts === "boolean" || mfConfig.dts === void 0) {
|
|
72
|
-
mfConfig.dts = {
|
|
73
|
-
consumeTypes: {
|
|
74
|
-
runtimePkgs: [
|
|
75
|
-
ModernJSRuntime
|
|
76
|
-
]
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
} else if (((_mfConfig_dts = mfConfig.dts) === null || _mfConfig_dts === void 0 ? void 0 : _mfConfig_dts.consumeTypes) || ((_mfConfig_dts1 = mfConfig.dts) === null || _mfConfig_dts1 === void 0 ? void 0 : _mfConfig_dts1.consumeTypes) === void 0) {
|
|
80
|
-
var _mfConfig_dts2;
|
|
81
|
-
if (typeof mfConfig.dts.consumeTypes === "boolean" || ((_mfConfig_dts2 = mfConfig.dts) === null || _mfConfig_dts2 === void 0 ? void 0 : _mfConfig_dts2.consumeTypes) === void 0) {
|
|
82
|
-
mfConfig.dts.consumeTypes = {
|
|
83
|
-
runtimePkgs: [
|
|
84
|
-
ModernJSRuntime
|
|
85
|
-
]
|
|
86
|
-
};
|
|
87
|
-
} else {
|
|
88
|
-
mfConfig.dts.consumeTypes.runtimePkgs = mfConfig.dts.consumeTypes.runtimePkgs || [];
|
|
89
|
-
if (!mfConfig.dts.consumeTypes.runtimePkgs.includes(ModernJSRuntime)) {
|
|
90
|
-
mfConfig.dts.consumeTypes.runtimePkgs.push(ModernJSRuntime);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
const patchMFConfig = (mfConfig, isServer, remoteIpStrategy) => {
|
|
97
|
-
replaceRemoteUrl(mfConfig, remoteIpStrategy);
|
|
98
|
-
if (mfConfig.remoteType === void 0) {
|
|
99
|
-
mfConfig.remoteType = "script";
|
|
100
|
-
}
|
|
101
|
-
if (!mfConfig.name) {
|
|
102
|
-
throw new Error(`${PLUGIN_IDENTIFIER} mfConfig.name can not be empty!`);
|
|
103
|
-
}
|
|
104
|
-
const runtimePlugins = [
|
|
105
|
-
...mfConfig.runtimePlugins || []
|
|
106
|
-
];
|
|
107
|
-
patchDTSConfig(mfConfig, isServer);
|
|
108
|
-
injectRuntimePlugins(require.resolve("@module-federation/modern-js/shared-strategy"), runtimePlugins);
|
|
109
|
-
if (isDev) {
|
|
110
|
-
injectRuntimePlugins(require.resolve("@module-federation/modern-js/resolve-entry-ipv4"), runtimePlugins);
|
|
111
|
-
}
|
|
112
|
-
if (isServer) {
|
|
113
|
-
injectRuntimePlugins(require.resolve("@module-federation/node/runtimePlugin"), runtimePlugins);
|
|
114
|
-
if (isDev) {
|
|
115
|
-
injectRuntimePlugins(require.resolve("@module-federation/node/record-dynamic-remote-entry-hash-plugin"), runtimePlugins);
|
|
116
|
-
}
|
|
117
|
-
injectRuntimePlugins(require.resolve("@module-federation/modern-js/inject-node-fetch"), runtimePlugins);
|
|
118
|
-
if (!mfConfig.library) {
|
|
119
|
-
mfConfig.library = {
|
|
120
|
-
type: "commonjs-module",
|
|
121
|
-
name: mfConfig.name
|
|
122
|
-
};
|
|
123
|
-
} else {
|
|
124
|
-
if (!mfConfig.library.type) {
|
|
125
|
-
mfConfig.library.type = "commonjs-module";
|
|
126
|
-
}
|
|
127
|
-
if (!mfConfig.library.name) {
|
|
128
|
-
mfConfig.library.name = mfConfig.name;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
mfConfig.runtimePlugins = runtimePlugins;
|
|
133
|
-
if (!isServer) {
|
|
134
|
-
var _mfConfig_library;
|
|
135
|
-
if (((_mfConfig_library = mfConfig.library) === null || _mfConfig_library === void 0 ? void 0 : _mfConfig_library.type) === "commonjs-module") {
|
|
136
|
-
mfConfig.library.type = "global";
|
|
137
|
-
}
|
|
138
|
-
return mfConfig;
|
|
139
|
-
}
|
|
140
|
-
mfConfig.dts = false;
|
|
141
|
-
mfConfig.dev = false;
|
|
142
|
-
return mfConfig;
|
|
143
|
-
};
|
|
144
|
-
function patchIgnoreWarning(chain) {
|
|
145
|
-
const ignoreWarnings = chain.get("ignoreWarnings") || [];
|
|
146
|
-
const ignoredMsgs = [
|
|
147
|
-
"external script",
|
|
148
|
-
"process.env.WS_NO_BUFFER_UTIL",
|
|
149
|
-
`Can't resolve 'utf-8-validate`
|
|
150
|
-
];
|
|
151
|
-
ignoreWarnings.push((warning) => {
|
|
152
|
-
if (ignoredMsgs.some((msg) => warning.message.includes(msg))) {
|
|
153
|
-
return true;
|
|
154
|
-
}
|
|
155
|
-
return false;
|
|
156
|
-
});
|
|
157
|
-
chain.ignoreWarnings(ignoreWarnings);
|
|
158
|
-
}
|
|
159
|
-
function addMyTypes2Ignored(chain, mfConfig) {
|
|
160
|
-
const watchOptions = chain.get("watchOptions");
|
|
161
|
-
if (!watchOptions || !watchOptions.ignored) {
|
|
162
|
-
chain.watchOptions({
|
|
163
|
-
ignored: /[\\/](?:\.git|node_modules|@mf-types)[\\/]/
|
|
164
|
-
});
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
const ignored = watchOptions.ignored;
|
|
168
|
-
const DEFAULT_IGNORED_GLOB = "**/@mf-types/**";
|
|
169
|
-
if (Array.isArray(ignored)) {
|
|
170
|
-
if (mfConfig.dts !== false && typeof mfConfig.dts === "object" && typeof mfConfig.dts.consumeTypes === "object" && mfConfig.dts.consumeTypes.remoteTypesFolder) {
|
|
171
|
-
chain.watchOptions({
|
|
172
|
-
...watchOptions,
|
|
173
|
-
ignored: ignored.concat(`**/${mfConfig.dts.consumeTypes.remoteTypesFolder}/**`)
|
|
174
|
-
});
|
|
175
|
-
} else {
|
|
176
|
-
chain.watchOptions({
|
|
177
|
-
...watchOptions,
|
|
178
|
-
ignored: ignored.concat(DEFAULT_IGNORED_GLOB)
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
if (typeof ignored !== "string") {
|
|
184
|
-
chain.watchOptions({
|
|
185
|
-
...watchOptions,
|
|
186
|
-
ignored: /[\\/](?:\.git|node_modules|@mf-types)[\\/]/
|
|
187
|
-
});
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
chain.watchOptions({
|
|
191
|
-
...watchOptions,
|
|
192
|
-
ignored: ignored.concat(DEFAULT_IGNORED_GLOB)
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
function patchBundlerConfig(options) {
|
|
196
|
-
var _modernjsConfig_deploy;
|
|
197
|
-
const { chain, modernjsConfig, isServer, mfConfig, enableSSR } = options;
|
|
198
|
-
chain.optimization.delete("runtimeChunk");
|
|
199
|
-
patchIgnoreWarning(chain);
|
|
200
|
-
if (!chain.output.get("chunkLoadingGlobal")) {
|
|
201
|
-
chain.output.chunkLoadingGlobal(`chunk_${mfConfig.name}`);
|
|
202
|
-
}
|
|
203
|
-
if (!chain.output.get("uniqueName")) {
|
|
204
|
-
chain.output.uniqueName(mfConfig.name);
|
|
205
|
-
}
|
|
206
|
-
const splitChunkConfig = chain.optimization.splitChunks.entries();
|
|
207
|
-
if (!isServer) {
|
|
208
|
-
autoDeleteSplitChunkCacheGroups(mfConfig, splitChunkConfig);
|
|
209
|
-
}
|
|
210
|
-
if (!isServer && enableSSR && splitChunkConfig && typeof splitChunkConfig === "object" && splitChunkConfig.cacheGroups) {
|
|
211
|
-
splitChunkConfig.chunks = "async";
|
|
212
|
-
logger.warn(`splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"`);
|
|
213
|
-
}
|
|
214
|
-
if (isDev && chain.output.get("publicPath") === "auto") {
|
|
215
|
-
var _modernjsConfig_dev, _modernjsConfig_server;
|
|
216
|
-
const port = ((_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.port) || ((_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.port) || 8080;
|
|
217
|
-
const publicPath = `http://localhost:${port}/`;
|
|
218
|
-
chain.output.publicPath(publicPath);
|
|
219
|
-
}
|
|
220
|
-
if (isServer && enableSSR) {
|
|
221
|
-
const uniqueName = mfConfig.name || chain.output.get("uniqueName");
|
|
222
|
-
const chunkFileName = chain.output.get("chunkFilename");
|
|
223
|
-
if (typeof chunkFileName === "string" && uniqueName && !chunkFileName.includes(uniqueName)) {
|
|
224
|
-
const suffix = `${encodeName(uniqueName)}-[chunkhash].js`;
|
|
225
|
-
chain.output.chunkFilename(chunkFileName.replace(".js", suffix));
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
if (isDev && enableSSR && !isServer) {
|
|
229
|
-
chain.resolve.fallback.set("crypto", false).set("stream", false).set("vm", false);
|
|
230
|
-
}
|
|
231
|
-
if (((_modernjsConfig_deploy = modernjsConfig.deploy) === null || _modernjsConfig_deploy === void 0 ? void 0 : _modernjsConfig_deploy.microFrontend) && Object.keys(mfConfig.exposes || {}).length) {
|
|
232
|
-
chain.optimization.usedExports(false);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
const moduleFederationConfigPlugin = (userConfig) => ({
|
|
236
|
-
name: "@modern-js/plugin-module-federation-config",
|
|
237
|
-
pre: [
|
|
238
|
-
"@modern-js/plugin-initialize"
|
|
239
|
-
],
|
|
240
|
-
post: [
|
|
241
|
-
"@modern-js/plugin-module-federation"
|
|
242
|
-
],
|
|
243
|
-
setup: async (api) => {
|
|
244
|
-
var _userConfig_userConfig, _modernjsConfig_server;
|
|
245
|
-
const modernjsConfig = api.getConfig();
|
|
246
|
-
const mfConfig = await getMFConfig(userConfig.originPluginOptions);
|
|
247
|
-
const csrConfig = userConfig.csrConfig || JSON.parse(JSON.stringify(mfConfig));
|
|
248
|
-
const ssrConfig = userConfig.ssrConfig || JSON.parse(JSON.stringify(mfConfig));
|
|
249
|
-
userConfig.ssrConfig = ssrConfig;
|
|
250
|
-
userConfig.csrConfig = csrConfig;
|
|
251
|
-
var _userConfig_userConfig_ssr;
|
|
252
|
-
const enableSSR = Boolean((_userConfig_userConfig_ssr = (_userConfig_userConfig = userConfig.userConfig) === null || _userConfig_userConfig === void 0 ? void 0 : _userConfig_userConfig.ssr) !== null && _userConfig_userConfig_ssr !== void 0 ? _userConfig_userConfig_ssr : Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr));
|
|
253
|
-
api.modifyBundlerChain((chain) => {
|
|
254
|
-
const target = chain.get("target");
|
|
255
|
-
if (skipByTarget(target)) {
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
const isWeb = isWebTarget(target);
|
|
259
|
-
addMyTypes2Ignored(chain, !isWeb ? ssrConfig : csrConfig);
|
|
260
|
-
const targetMFConfig = !isWeb ? ssrConfig : csrConfig;
|
|
261
|
-
patchMFConfig(targetMFConfig, !isWeb, userConfig.remoteIpStrategy || "ipv4");
|
|
262
|
-
patchBundlerConfig({
|
|
263
|
-
// @ts-expect-error chain type is not correct
|
|
264
|
-
chain,
|
|
265
|
-
isServer: !isWeb,
|
|
266
|
-
modernjsConfig,
|
|
267
|
-
mfConfig,
|
|
268
|
-
enableSSR
|
|
269
|
-
});
|
|
270
|
-
userConfig.distOutputDir = chain.output.get("path") || path.resolve(process.cwd(), "dist");
|
|
271
|
-
});
|
|
272
|
-
api.config(() => {
|
|
273
|
-
var _modernjsConfig_tools, _userConfig_csrConfig, _modernjsConfig_source, _modernjsConfig_source1, _modernjsConfig_dev;
|
|
274
|
-
const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
|
|
275
|
-
const ipv4 = getIPV4();
|
|
276
|
-
if (userConfig.remoteIpStrategy === void 0) {
|
|
277
|
-
if (!enableSSR) {
|
|
278
|
-
userConfig.remoteIpStrategy = "inherit";
|
|
279
|
-
} else {
|
|
280
|
-
userConfig.remoteIpStrategy = "ipv4";
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
const devServerConfig = (_modernjsConfig_tools = modernjsConfig.tools) === null || _modernjsConfig_tools === void 0 ? void 0 : _modernjsConfig_tools.devServer;
|
|
284
|
-
const corsWarnMsgs = [
|
|
285
|
-
"View https://module-federation.io/guide/troubleshooting/other.html#cors-warn for more details."
|
|
286
|
-
];
|
|
287
|
-
if (typeof devServerConfig !== "object" || !("headers" in devServerConfig)) {
|
|
288
|
-
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.');
|
|
289
|
-
}
|
|
290
|
-
const exposes = (_userConfig_csrConfig = userConfig.csrConfig) === null || _userConfig_csrConfig === void 0 ? void 0 : _userConfig_csrConfig.exposes;
|
|
291
|
-
const hasExposes = exposes && Array.isArray(exposes) ? exposes.length : Object.keys(exposes !== null && exposes !== void 0 ? exposes : {}).length;
|
|
292
|
-
if (corsWarnMsgs.length > 1 && hasExposes) {
|
|
293
|
-
logger.warn(corsWarnMsgs.join("\n"));
|
|
294
|
-
}
|
|
295
|
-
const corsHeaders = hasExposes ? {
|
|
296
|
-
"Access-Control-Allow-Origin": "*",
|
|
297
|
-
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
|
|
298
|
-
"Access-Control-Allow-Headers": "*"
|
|
299
|
-
} : void 0;
|
|
300
|
-
var _modernjsConfig_source_enableAsyncEntry;
|
|
301
|
-
return {
|
|
302
|
-
tools: {
|
|
303
|
-
devServer: {
|
|
304
|
-
headers: corsHeaders
|
|
305
|
-
}
|
|
306
|
-
},
|
|
307
|
-
source: {
|
|
308
|
-
alias: {
|
|
309
|
-
// TODO: deprecated
|
|
310
|
-
"@modern-js/runtime/mf": require.resolve("@module-federation/modern-js/runtime")
|
|
311
|
-
},
|
|
312
|
-
define: {
|
|
313
|
-
FEDERATION_IPV4: JSON.stringify(ipv4),
|
|
314
|
-
REMOTE_IP_STRATEGY: JSON.stringify(userConfig.remoteIpStrategy)
|
|
315
|
-
},
|
|
316
|
-
enableAsyncEntry: bundlerType === "rspack" ? (_modernjsConfig_source_enableAsyncEntry = (_modernjsConfig_source = modernjsConfig.source) === null || _modernjsConfig_source === void 0 ? void 0 : _modernjsConfig_source.enableAsyncEntry) !== null && _modernjsConfig_source_enableAsyncEntry !== void 0 ? _modernjsConfig_source_enableAsyncEntry : true : (_modernjsConfig_source1 = modernjsConfig.source) === null || _modernjsConfig_source1 === void 0 ? void 0 : _modernjsConfig_source1.enableAsyncEntry
|
|
317
|
-
},
|
|
318
|
-
dev: {
|
|
319
|
-
assetPrefix: (modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.assetPrefix) ? modernjsConfig.dev.assetPrefix : true
|
|
320
|
-
}
|
|
321
|
-
};
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
});
|
|
325
|
-
var configPlugin_default = moduleFederationConfigPlugin;
|
|
326
|
-
export {
|
|
327
|
-
addMyTypes2Ignored,
|
|
328
|
-
configPlugin_default as default,
|
|
329
|
-
getMFConfig,
|
|
330
|
-
isWebTarget,
|
|
331
|
-
moduleFederationConfigPlugin,
|
|
332
|
-
patchBundlerConfig,
|
|
333
|
-
patchMFConfig,
|
|
334
|
-
setEnv,
|
|
335
|
-
skipByTarget
|
|
336
|
-
};
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { it, expect, describe } from "vitest";
|
|
2
|
-
import { patchMFConfig } from "./configPlugin";
|
|
3
|
-
import { getIPV4 } from "./utils";
|
|
4
|
-
const mfConfig = {
|
|
5
|
-
name: "host",
|
|
6
|
-
filename: "remoteEntry.js",
|
|
7
|
-
remotes: {
|
|
8
|
-
remote: "http://localhost:3000/remoteEntry.js"
|
|
9
|
-
},
|
|
10
|
-
shared: {
|
|
11
|
-
react: {
|
|
12
|
-
singleton: true,
|
|
13
|
-
eager: true
|
|
14
|
-
},
|
|
15
|
-
"react-dom": {
|
|
16
|
-
singleton: true,
|
|
17
|
-
eager: true
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
describe("patchMFConfig", async () => {
|
|
22
|
-
it("patchMFConfig: server", async () => {
|
|
23
|
-
const patchedConfig = JSON.parse(JSON.stringify(mfConfig));
|
|
24
|
-
patchMFConfig(patchedConfig, true);
|
|
25
|
-
const ipv4 = getIPV4();
|
|
26
|
-
expect(patchedConfig).toStrictEqual({
|
|
27
|
-
dev: false,
|
|
28
|
-
dts: false,
|
|
29
|
-
filename: "remoteEntry.js",
|
|
30
|
-
library: {
|
|
31
|
-
name: "host",
|
|
32
|
-
type: "commonjs-module"
|
|
33
|
-
},
|
|
34
|
-
name: "host",
|
|
35
|
-
remotes: {
|
|
36
|
-
remote: `http://${ipv4}:3000/remoteEntry.js`
|
|
37
|
-
},
|
|
38
|
-
remoteType: "script",
|
|
39
|
-
runtimePlugins: [
|
|
40
|
-
require.resolve("@module-federation/modern-js/shared-strategy"),
|
|
41
|
-
require.resolve("@module-federation/node/runtimePlugin"),
|
|
42
|
-
require.resolve("@module-federation/modern-js/inject-node-fetch")
|
|
43
|
-
],
|
|
44
|
-
shared: {
|
|
45
|
-
react: {
|
|
46
|
-
eager: true,
|
|
47
|
-
singleton: true
|
|
48
|
-
},
|
|
49
|
-
"react-dom": {
|
|
50
|
-
eager: true,
|
|
51
|
-
singleton: true
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
it("patchMFConfig: client", async () => {
|
|
57
|
-
const patchedConfig = JSON.parse(JSON.stringify(mfConfig));
|
|
58
|
-
patchMFConfig(patchedConfig, false);
|
|
59
|
-
const ipv4 = getIPV4();
|
|
60
|
-
expect(patchedConfig).toStrictEqual({
|
|
61
|
-
filename: "remoteEntry.js",
|
|
62
|
-
name: "host",
|
|
63
|
-
remotes: {
|
|
64
|
-
remote: `http://${ipv4}:3000/remoteEntry.js`
|
|
65
|
-
},
|
|
66
|
-
remoteType: "script",
|
|
67
|
-
runtimePlugins: [
|
|
68
|
-
require.resolve("@module-federation/modern-js/shared-strategy")
|
|
69
|
-
],
|
|
70
|
-
shared: {
|
|
71
|
-
react: {
|
|
72
|
-
eager: true,
|
|
73
|
-
singleton: true
|
|
74
|
-
},
|
|
75
|
-
"react-dom": {
|
|
76
|
-
eager: true,
|
|
77
|
-
singleton: true
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
dts: {
|
|
81
|
-
consumeTypes: {
|
|
82
|
-
runtimePkgs: [
|
|
83
|
-
"@module-federation/modern-js/runtime"
|
|
84
|
-
]
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
});
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { ModuleFederationPlugin as WebpackModuleFederationPlugin, AsyncBoundaryPlugin } from "@module-federation/enhanced";
|
|
2
|
-
import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
|
|
3
|
-
import { moduleFederationConfigPlugin } from "./configPlugin";
|
|
4
|
-
import { moduleFederationSSRPlugin } from "./ssrPlugin";
|
|
5
|
-
import { isWebTarget } from "./utils";
|
|
6
|
-
const moduleFederationPlugin = (userConfig = {}) => {
|
|
7
|
-
const internalModernPluginOptions = {
|
|
8
|
-
csrConfig: void 0,
|
|
9
|
-
ssrConfig: void 0,
|
|
10
|
-
browserPlugin: void 0,
|
|
11
|
-
nodePlugin: void 0,
|
|
12
|
-
distOutputDir: "",
|
|
13
|
-
originPluginOptions: userConfig,
|
|
14
|
-
remoteIpStrategy: userConfig === null || userConfig === void 0 ? void 0 : userConfig.remoteIpStrategy,
|
|
15
|
-
userConfig: userConfig || {}
|
|
16
|
-
};
|
|
17
|
-
return {
|
|
18
|
-
name: "@modern-js/plugin-module-federation",
|
|
19
|
-
setup: async (api) => {
|
|
20
|
-
const modernjsConfig = api.getConfig();
|
|
21
|
-
api.modifyBundlerChain((chain) => {
|
|
22
|
-
const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
|
|
23
|
-
const browserPluginOptions = internalModernPluginOptions.csrConfig;
|
|
24
|
-
const MFPlugin = bundlerType === "webpack" ? WebpackModuleFederationPlugin : RspackModuleFederationPlugin;
|
|
25
|
-
if (isWebTarget(chain.get("target"))) {
|
|
26
|
-
chain.plugin("plugin-module-federation").use(MFPlugin, [
|
|
27
|
-
browserPluginOptions
|
|
28
|
-
]).init((Plugin, args) => {
|
|
29
|
-
internalModernPluginOptions.browserPlugin = new Plugin(args[0]);
|
|
30
|
-
return internalModernPluginOptions.browserPlugin;
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
if (bundlerType === "webpack") {
|
|
34
|
-
var _modernjsConfig_source;
|
|
35
|
-
const enableAsyncEntry = (_modernjsConfig_source = modernjsConfig.source) === null || _modernjsConfig_source === void 0 ? void 0 : _modernjsConfig_source.enableAsyncEntry;
|
|
36
|
-
if (!enableAsyncEntry && browserPluginOptions.async !== false) {
|
|
37
|
-
const asyncBoundaryPluginOptions = typeof browserPluginOptions.async === "object" ? browserPluginOptions.async : {
|
|
38
|
-
eager: (module) => module && /\.federation/.test((module === null || module === void 0 ? void 0 : module.request) || ""),
|
|
39
|
-
excludeChunk: (chunk) => chunk.name === browserPluginOptions.name
|
|
40
|
-
};
|
|
41
|
-
chain.plugin("async-boundary-plugin").use(AsyncBoundaryPlugin, [
|
|
42
|
-
asyncBoundaryPluginOptions
|
|
43
|
-
]);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
},
|
|
48
|
-
usePlugins: [
|
|
49
|
-
moduleFederationConfigPlugin(internalModernPluginOptions),
|
|
50
|
-
moduleFederationSSRPlugin(internalModernPluginOptions)
|
|
51
|
-
]
|
|
52
|
-
};
|
|
53
|
-
};
|
|
54
|
-
var cli_default = moduleFederationPlugin;
|
|
55
|
-
import { createModuleFederationConfig } from "@module-federation/enhanced";
|
|
56
|
-
export {
|
|
57
|
-
createModuleFederationConfig,
|
|
58
|
-
cli_default as default,
|
|
59
|
-
moduleFederationPlugin
|
|
60
|
-
};
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { fs } from "@modern-js/utils";
|
|
3
|
-
function mergeStats(browserStats, nodeStats) {
|
|
4
|
-
const ssrRemoteEntry = nodeStats.metaData.remoteEntry;
|
|
5
|
-
browserStats.metaData.ssrRemoteEntry = ssrRemoteEntry;
|
|
6
|
-
if ("publicPath" in browserStats.metaData) {
|
|
7
|
-
browserStats.metaData.ssrPublicPath = nodeStats.metaData.publicPath;
|
|
8
|
-
}
|
|
9
|
-
return browserStats;
|
|
10
|
-
}
|
|
11
|
-
function mergeManifest(browserManifest, nodeManifest) {
|
|
12
|
-
const ssrRemoteEntry = nodeManifest.metaData.remoteEntry;
|
|
13
|
-
browserManifest.metaData.ssrRemoteEntry = ssrRemoteEntry;
|
|
14
|
-
if ("publicPath" in browserManifest.metaData) {
|
|
15
|
-
browserManifest.metaData.ssrPublicPath = nodeManifest.metaData.publicPath;
|
|
16
|
-
}
|
|
17
|
-
return browserManifest;
|
|
18
|
-
}
|
|
19
|
-
function mergeStatsAndManifest(nodePlugin, browserPlugin) {
|
|
20
|
-
const nodeResourceInfo = nodePlugin.statsResourceInfo;
|
|
21
|
-
const browserResourceInfo = browserPlugin.statsResourceInfo;
|
|
22
|
-
if (!browserResourceInfo || !nodeResourceInfo || !browserResourceInfo.stats || !nodeResourceInfo.stats || !browserResourceInfo.manifest || !nodeResourceInfo.manifest) {
|
|
23
|
-
throw new Error("can not get browserResourceInfo or nodeResourceInfo");
|
|
24
|
-
}
|
|
25
|
-
const mergedStats = mergeStats(browserResourceInfo.stats.stats, nodeResourceInfo.stats.stats);
|
|
26
|
-
const mergedManifest = mergeManifest(browserResourceInfo.manifest.manifest, nodeResourceInfo.manifest.manifest);
|
|
27
|
-
return {
|
|
28
|
-
mergedStats,
|
|
29
|
-
mergedStatsFilePath: browserResourceInfo.stats.filename,
|
|
30
|
-
mergedManifest,
|
|
31
|
-
mergedManifestFilePath: browserResourceInfo.manifest.filename
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
function updateStatsAndManifest(nodePlugin, browserPlugin, outputDir) {
|
|
35
|
-
const { mergedStats, mergedStatsFilePath, mergedManifest, mergedManifestFilePath } = mergeStatsAndManifest(nodePlugin, browserPlugin);
|
|
36
|
-
fs.writeFileSync(path.resolve(outputDir, mergedStatsFilePath), JSON.stringify(mergedStats, null, 2));
|
|
37
|
-
fs.writeFileSync(path.resolve(outputDir, mergedManifestFilePath), JSON.stringify(mergedManifest, null, 2));
|
|
38
|
-
}
|
|
39
|
-
export {
|
|
40
|
-
updateStatsAndManifest
|
|
41
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import nodeFetch from "node-fetch";
|
|
2
|
-
const injectNodeFetchPlugin = () => ({
|
|
3
|
-
name: "inject-node-fetch-plugin",
|
|
4
|
-
beforeInit(args) {
|
|
5
|
-
if (!globalThis.fetch) {
|
|
6
|
-
globalThis.fetch = nodeFetch;
|
|
7
|
-
}
|
|
8
|
-
return args;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
var inject_node_fetch_default = injectNodeFetchPlugin;
|
|
12
|
-
export {
|
|
13
|
-
inject_node_fetch_default as default
|
|
14
|
-
};
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { LOCALHOST } from "../../constant";
|
|
2
|
-
const ipv4 = typeof FEDERATION_IPV4 !== "undefined" ? FEDERATION_IPV4 : "127.0.0.1";
|
|
3
|
-
const remoteIpStrategy = typeof REMOTE_IP_STRATEGY !== "undefined" ? REMOTE_IP_STRATEGY : "inherit";
|
|
4
|
-
function replaceObjectLocalhost(key, obj) {
|
|
5
|
-
if (remoteIpStrategy !== "ipv4") {
|
|
6
|
-
return;
|
|
7
|
-
}
|
|
8
|
-
if (!(key in obj)) {
|
|
9
|
-
return;
|
|
10
|
-
}
|
|
11
|
-
const remote = obj[key];
|
|
12
|
-
if (remote && typeof remote === "string" && remote.includes(LOCALHOST)) {
|
|
13
|
-
obj[key] = replaceLocalhost(remote);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
function replaceLocalhost(url) {
|
|
17
|
-
return url.replace(LOCALHOST, ipv4);
|
|
18
|
-
}
|
|
19
|
-
const resolveEntryIpv4Plugin = () => ({
|
|
20
|
-
name: "resolve-entry-ipv4",
|
|
21
|
-
beforeRegisterRemote(args) {
|
|
22
|
-
const { remote } = args;
|
|
23
|
-
replaceObjectLocalhost("entry", remote);
|
|
24
|
-
return args;
|
|
25
|
-
},
|
|
26
|
-
async afterResolve(args) {
|
|
27
|
-
const { remoteInfo } = args;
|
|
28
|
-
replaceObjectLocalhost("entry", remoteInfo);
|
|
29
|
-
return args;
|
|
30
|
-
},
|
|
31
|
-
beforeLoadRemoteSnapshot(args) {
|
|
32
|
-
const { moduleInfo } = args;
|
|
33
|
-
if ("entry" in moduleInfo) {
|
|
34
|
-
replaceObjectLocalhost("entry", moduleInfo);
|
|
35
|
-
return args;
|
|
36
|
-
}
|
|
37
|
-
if ("version" in moduleInfo) {
|
|
38
|
-
replaceObjectLocalhost("version", moduleInfo);
|
|
39
|
-
}
|
|
40
|
-
return args;
|
|
41
|
-
},
|
|
42
|
-
loadRemoteSnapshot(args) {
|
|
43
|
-
const { remoteSnapshot } = args;
|
|
44
|
-
if ("publicPath" in remoteSnapshot) {
|
|
45
|
-
replaceObjectLocalhost("publicPath", remoteSnapshot);
|
|
46
|
-
}
|
|
47
|
-
if ("getPublicPath" in remoteSnapshot) {
|
|
48
|
-
replaceObjectLocalhost("getPublicPath", remoteSnapshot);
|
|
49
|
-
}
|
|
50
|
-
if (remoteSnapshot.remotesInfo) {
|
|
51
|
-
Object.keys(remoteSnapshot.remotesInfo).forEach((key) => {
|
|
52
|
-
const remoteInfo = remoteSnapshot.remotesInfo[key];
|
|
53
|
-
replaceObjectLocalhost("matchedVersion", remoteInfo);
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
return args;
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
var resolve_entry_ipv4_default = resolveEntryIpv4Plugin;
|
|
60
|
-
export {
|
|
61
|
-
resolve_entry_ipv4_default as default
|
|
62
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
const sharedStrategy = () => ({
|
|
2
|
-
name: "shared-strategy-plugin",
|
|
3
|
-
beforeInit(args) {
|
|
4
|
-
const { userOptions } = args;
|
|
5
|
-
const shared = userOptions.shared;
|
|
6
|
-
if (shared) {
|
|
7
|
-
Object.keys(shared).forEach((sharedKey) => {
|
|
8
|
-
const sharedConfigs = shared[sharedKey];
|
|
9
|
-
const arraySharedConfigs = Array.isArray(sharedConfigs) ? sharedConfigs : [
|
|
10
|
-
sharedConfigs
|
|
11
|
-
];
|
|
12
|
-
arraySharedConfigs.forEach((s) => {
|
|
13
|
-
s.strategy = "loaded-first";
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
return args;
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
var shared_strategy_default = sharedStrategy;
|
|
21
|
-
export {
|
|
22
|
-
shared_strategy_default as default
|
|
23
|
-
};
|