@module-federation/modern-js 0.0.0-next-20250522124810 → 0.0.0-next-20250522130707

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