@module-federation/modern-js 0.21.5 → 0.22.0

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