@module-federation/modern-js 0.14.1 → 0.14.3

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 (98) hide show
  1. package/dist/LICENSE +21 -0
  2. package/dist/cjs/cli/configPlugin.js +377 -0
  3. package/dist/cjs/cli/configPlugin.spec.js +112 -0
  4. package/dist/cjs/cli/constant.js +28 -0
  5. package/dist/cjs/cli/index.js +93 -0
  6. package/dist/cjs/cli/logger.js +27 -0
  7. package/dist/cjs/cli/manifest.js +75 -0
  8. package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +44 -0
  9. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +82 -0
  10. package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +43 -0
  11. package/dist/cjs/cli/ssrPlugin.js +217 -0
  12. package/dist/cjs/cli/utils.js +85 -0
  13. package/dist/cjs/constant.js +31 -0
  14. package/dist/cjs/interfaces/bundler.js +16 -0
  15. package/dist/cjs/runtime/createRemoteSSRComponent.js +201 -0
  16. package/dist/cjs/runtime/index.js +38 -0
  17. package/dist/cjs/server/fileCache.js +84 -0
  18. package/dist/cjs/server/fileCache.spec.js +28 -0
  19. package/dist/cjs/server/index.js +58 -0
  20. package/dist/cjs/server/staticMiddleware.js +77 -0
  21. package/dist/cjs/server/staticMiddleware.spec.js +185 -0
  22. package/dist/cjs/ssr-runtime/SSRLiveReload.js +43 -0
  23. package/dist/cjs/ssr-runtime/index.js +22 -0
  24. package/dist/cjs/ssr-runtime/plugin.js +68 -0
  25. package/dist/cjs/types/index.js +16 -0
  26. package/dist/esm/cli/configPlugin.js +384 -0
  27. package/dist/esm/cli/configPlugin.spec.js +108 -0
  28. package/dist/esm/cli/constant.js +4 -0
  29. package/dist/esm/cli/index.js +88 -0
  30. package/dist/esm/cli/logger.js +7 -0
  31. package/dist/esm/cli/manifest.js +41 -0
  32. package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +16 -0
  33. package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +74 -0
  34. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +25 -0
  35. package/dist/esm/cli/ssrPlugin.js +199 -0
  36. package/dist/esm/cli/utils.js +49 -0
  37. package/dist/esm/constant.js +6 -0
  38. package/dist/esm/interfaces/bundler.js +0 -0
  39. package/dist/esm/runtime/createRemoteSSRComponent.js +191 -0
  40. package/dist/esm/runtime/index.js +12 -0
  41. package/dist/esm/server/fileCache.js +98 -0
  42. package/dist/esm/server/fileCache.spec.js +50 -0
  43. package/dist/esm/server/index.js +36 -0
  44. package/dist/esm/server/staticMiddleware.js +81 -0
  45. package/dist/esm/server/staticMiddleware.spec.js +328 -0
  46. package/dist/esm/ssr-runtime/SSRLiveReload.js +26 -0
  47. package/dist/esm/ssr-runtime/index.js +1 -0
  48. package/dist/esm/ssr-runtime/plugin.js +70 -0
  49. package/dist/esm/types/index.js +0 -0
  50. package/dist/esm-node/cli/configPlugin.js +336 -0
  51. package/dist/esm-node/cli/configPlugin.spec.js +89 -0
  52. package/dist/esm-node/cli/constant.js +4 -0
  53. package/dist/esm-node/cli/index.js +68 -0
  54. package/dist/esm-node/cli/logger.js +7 -0
  55. package/dist/esm-node/cli/manifest.js +41 -0
  56. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +14 -0
  57. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +62 -0
  58. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +23 -0
  59. package/dist/esm-node/cli/ssrPlugin.js +181 -0
  60. package/dist/esm-node/cli/utils.js +49 -0
  61. package/dist/esm-node/constant.js +6 -0
  62. package/dist/esm-node/interfaces/bundler.js +0 -0
  63. package/dist/esm-node/runtime/createRemoteSSRComponent.js +166 -0
  64. package/dist/esm-node/runtime/index.js +12 -0
  65. package/dist/esm-node/server/fileCache.js +49 -0
  66. package/dist/esm-node/server/fileCache.spec.js +27 -0
  67. package/dist/esm-node/server/index.js +34 -0
  68. package/dist/esm-node/server/staticMiddleware.js +43 -0
  69. package/dist/esm-node/server/staticMiddleware.spec.js +162 -0
  70. package/dist/esm-node/ssr-runtime/SSRLiveReload.js +19 -0
  71. package/dist/esm-node/ssr-runtime/index.js +1 -0
  72. package/dist/esm-node/ssr-runtime/plugin.js +34 -0
  73. package/dist/esm-node/types/index.js +0 -0
  74. package/dist/types/cli/configPlugin.d.ts +21 -0
  75. package/dist/types/cli/configPlugin.spec.d.ts +1 -0
  76. package/dist/types/cli/constant.d.ts +1 -0
  77. package/dist/types/cli/index.d.ts +5 -0
  78. package/dist/types/cli/logger.d.ts +2 -0
  79. package/dist/types/cli/manifest.d.ts +2 -0
  80. package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +3 -0
  81. package/dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts +3 -0
  82. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +3 -0
  83. package/dist/types/cli/ssrPlugin.d.ts +6 -0
  84. package/dist/types/cli/utils.d.ts +5 -0
  85. package/dist/types/constant.d.ts +2 -0
  86. package/dist/types/interfaces/bundler.d.ts +18 -0
  87. package/dist/types/runtime/createRemoteSSRComponent.d.ts +18 -0
  88. package/dist/types/runtime/index.d.ts +5 -0
  89. package/dist/types/server/fileCache.d.ts +14 -0
  90. package/dist/types/server/fileCache.spec.d.ts +1 -0
  91. package/dist/types/server/index.d.ts +4 -0
  92. package/dist/types/server/staticMiddleware.d.ts +6 -0
  93. package/dist/types/server/staticMiddleware.spec.d.ts +1 -0
  94. package/dist/types/ssr-runtime/SSRLiveReload.d.ts +1 -0
  95. package/dist/types/ssr-runtime/index.d.ts +1 -0
  96. package/dist/types/ssr-runtime/plugin.d.ts +2 -0
  97. package/dist/types/types/index.d.ts +22 -0
  98. package/package.json +8 -8
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var SSRLiveReload_exports = {};
20
+ __export(SSRLiveReload_exports, {
21
+ SSRLiveReload: () => SSRLiveReload
22
+ });
23
+ module.exports = __toCommonJS(SSRLiveReload_exports);
24
+ var import_jsx_runtime = require("react/jsx-runtime");
25
+ function SSRLiveReload() {
26
+ if (process.env.NODE_ENV !== "development") {
27
+ return null;
28
+ }
29
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", {
30
+ suppressHydrationWarning: true,
31
+ dangerouslySetInnerHTML: {
32
+ __html: String.raw`
33
+ if(${globalThis.shouldUpdate}){
34
+ location.reload();
35
+ }
36
+ `
37
+ }
38
+ });
39
+ }
40
+ // Annotate the CommonJS export names for ESM import in node:
41
+ 0 && (module.exports = {
42
+ SSRLiveReload
43
+ });
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
+ var ssr_runtime_exports = {};
17
+ module.exports = __toCommonJS(ssr_runtime_exports);
18
+ __reExport(ssr_runtime_exports, require("./plugin"), module.exports);
19
+ // Annotate the CommonJS export names for ESM import in node:
20
+ 0 && (module.exports = {
21
+ ...require("./plugin")
22
+ });
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var plugin_exports = {};
30
+ __export(plugin_exports, {
31
+ mfSSRPlugin: () => mfSSRPlugin
32
+ });
33
+ module.exports = __toCommonJS(plugin_exports);
34
+ var import_jsx_runtime = require("react/jsx-runtime");
35
+ var import_SSRLiveReload = require("./SSRLiveReload");
36
+ const mfSSRPlugin = () => ({
37
+ name: "@module-federation/modern-js",
38
+ setup: (api) => {
39
+ api.onBeforeRender(async () => {
40
+ if (typeof window !== "undefined") {
41
+ return;
42
+ }
43
+ globalThis.shouldUpdate = false;
44
+ const nodeUtils = await Promise.resolve().then(() => __toESM(require("@module-federation/node/utils")));
45
+ const shouldUpdate = await nodeUtils.revalidate();
46
+ if (shouldUpdate) {
47
+ console.log("should RELOAD", shouldUpdate);
48
+ await nodeUtils.flushChunks();
49
+ globalThis.shouldUpdate = true;
50
+ }
51
+ });
52
+ api.wrapRoot((App) => {
53
+ const AppWrapper = (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
54
+ children: [
55
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_SSRLiveReload.SSRLiveReload, {}),
56
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(App, {
57
+ ...props
58
+ })
59
+ ]
60
+ });
61
+ return AppWrapper;
62
+ });
63
+ }
64
+ });
65
+ // Annotate the CommonJS export names for ESM import in node:
66
+ 0 && (module.exports = {
67
+ mfSSRPlugin
68
+ });
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+ var types_exports = {};
16
+ module.exports = __toCommonJS(types_exports);
@@ -0,0 +1,384 @@
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 } from "@module-federation/rsbuild-plugin/utils";
13
+ import logger from "./logger";
14
+ var defaultPath = path.resolve(process.cwd(), "module-federation.config.ts");
15
+ var isDev = process.env.NODE_ENV === "development";
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, remoteIpStrategy) {
130
+ replaceRemoteUrl(mfConfig, remoteIpStrategy);
131
+ if (mfConfig.remoteType === void 0) {
132
+ mfConfig.remoteType = "script";
133
+ }
134
+ if (!mfConfig.name) {
135
+ throw new Error("".concat(PLUGIN_IDENTIFIER, " mfConfig.name can not be empty!"));
136
+ }
137
+ var runtimePlugins = _to_consumable_array(mfConfig.runtimePlugins || []);
138
+ patchDTSConfig(mfConfig, isServer);
139
+ injectRuntimePlugins(require.resolve("@module-federation/modern-js/shared-strategy"), runtimePlugins);
140
+ if (isDev) {
141
+ injectRuntimePlugins(require.resolve("@module-federation/modern-js/resolve-entry-ipv4"), runtimePlugins);
142
+ }
143
+ if (isServer) {
144
+ injectRuntimePlugins(require.resolve("@module-federation/node/runtimePlugin"), runtimePlugins);
145
+ if (isDev) {
146
+ injectRuntimePlugins(require.resolve("@module-federation/node/record-dynamic-remote-entry-hash-plugin"), runtimePlugins);
147
+ }
148
+ injectRuntimePlugins(require.resolve("@module-federation/modern-js/inject-node-fetch"), runtimePlugins);
149
+ if (!mfConfig.library) {
150
+ mfConfig.library = {
151
+ type: "commonjs-module",
152
+ name: mfConfig.name
153
+ };
154
+ } else {
155
+ if (!mfConfig.library.type) {
156
+ mfConfig.library.type = "commonjs-module";
157
+ }
158
+ if (!mfConfig.library.name) {
159
+ mfConfig.library.name = mfConfig.name;
160
+ }
161
+ }
162
+ }
163
+ mfConfig.runtimePlugins = runtimePlugins;
164
+ if (!isServer) {
165
+ var _mfConfig_library;
166
+ if (((_mfConfig_library = mfConfig.library) === null || _mfConfig_library === void 0 ? void 0 : _mfConfig_library.type) === "commonjs-module") {
167
+ mfConfig.library.type = "global";
168
+ }
169
+ return mfConfig;
170
+ }
171
+ mfConfig.dts = false;
172
+ mfConfig.dev = false;
173
+ return mfConfig;
174
+ };
175
+ function patchIgnoreWarning(chain) {
176
+ var ignoreWarnings = chain.get("ignoreWarnings") || [];
177
+ var ignoredMsgs = [
178
+ "external script",
179
+ "process.env.WS_NO_BUFFER_UTIL",
180
+ "Can't resolve 'utf-8-validate"
181
+ ];
182
+ ignoreWarnings.push(function(warning) {
183
+ if (ignoredMsgs.some(function(msg) {
184
+ return warning.message.includes(msg);
185
+ })) {
186
+ return true;
187
+ }
188
+ return false;
189
+ });
190
+ chain.ignoreWarnings(ignoreWarnings);
191
+ }
192
+ function addMyTypes2Ignored(chain, mfConfig) {
193
+ var watchOptions = chain.get("watchOptions");
194
+ if (!watchOptions || !watchOptions.ignored) {
195
+ chain.watchOptions({
196
+ ignored: /[\\/](?:\.git|node_modules|@mf-types)[\\/]/
197
+ });
198
+ return;
199
+ }
200
+ var ignored = watchOptions.ignored;
201
+ var DEFAULT_IGNORED_GLOB = "**/@mf-types/**";
202
+ if (Array.isArray(ignored)) {
203
+ if (mfConfig.dts !== false && _type_of(mfConfig.dts) === "object" && _type_of(mfConfig.dts.consumeTypes) === "object" && mfConfig.dts.consumeTypes.remoteTypesFolder) {
204
+ chain.watchOptions(_object_spread_props(_object_spread({}, watchOptions), {
205
+ ignored: ignored.concat("**/".concat(mfConfig.dts.consumeTypes.remoteTypesFolder, "/**"))
206
+ }));
207
+ } else {
208
+ chain.watchOptions(_object_spread_props(_object_spread({}, watchOptions), {
209
+ ignored: ignored.concat(DEFAULT_IGNORED_GLOB)
210
+ }));
211
+ }
212
+ return;
213
+ }
214
+ if (typeof ignored !== "string") {
215
+ chain.watchOptions(_object_spread_props(_object_spread({}, watchOptions), {
216
+ ignored: /[\\/](?:\.git|node_modules|@mf-types)[\\/]/
217
+ }));
218
+ return;
219
+ }
220
+ chain.watchOptions(_object_spread_props(_object_spread({}, watchOptions), {
221
+ ignored: ignored.concat(DEFAULT_IGNORED_GLOB)
222
+ }));
223
+ }
224
+ function patchBundlerConfig(options) {
225
+ var _modernjsConfig_deploy;
226
+ var chain = options.chain, modernjsConfig = options.modernjsConfig, isServer = options.isServer, mfConfig = options.mfConfig, enableSSR = options.enableSSR;
227
+ chain.optimization.delete("runtimeChunk");
228
+ patchIgnoreWarning(chain);
229
+ if (!chain.output.get("chunkLoadingGlobal")) {
230
+ chain.output.chunkLoadingGlobal("chunk_".concat(mfConfig.name));
231
+ }
232
+ if (!chain.output.get("uniqueName")) {
233
+ chain.output.uniqueName(mfConfig.name);
234
+ }
235
+ var splitChunkConfig = chain.optimization.splitChunks.entries();
236
+ if (!isServer) {
237
+ autoDeleteSplitChunkCacheGroups(mfConfig, splitChunkConfig);
238
+ }
239
+ if (!isServer && enableSSR && splitChunkConfig && (typeof splitChunkConfig === "undefined" ? "undefined" : _type_of(splitChunkConfig)) === "object" && splitChunkConfig.cacheGroups) {
240
+ splitChunkConfig.chunks = "async";
241
+ logger.warn('splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"');
242
+ }
243
+ if (isDev && chain.output.get("publicPath") === "auto") {
244
+ var _modernjsConfig_dev, _modernjsConfig_server;
245
+ 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;
246
+ var publicPath = "http://localhost:".concat(port, "/");
247
+ chain.output.publicPath(publicPath);
248
+ }
249
+ if (isServer && enableSSR) {
250
+ var uniqueName = mfConfig.name || chain.output.get("uniqueName");
251
+ var chunkFileName = chain.output.get("chunkFilename");
252
+ if (typeof chunkFileName === "string" && uniqueName && !chunkFileName.includes(uniqueName)) {
253
+ var suffix = "".concat(encodeName(uniqueName), "-[chunkhash].js");
254
+ chain.output.chunkFilename(chunkFileName.replace(".js", suffix));
255
+ }
256
+ }
257
+ if (isDev && enableSSR && !isServer) {
258
+ chain.resolve.fallback.set("crypto", false).set("stream", false).set("vm", false);
259
+ }
260
+ if (((_modernjsConfig_deploy = modernjsConfig.deploy) === null || _modernjsConfig_deploy === void 0 ? void 0 : _modernjsConfig_deploy.microFrontend) && Object.keys(mfConfig.exposes || {}).length) {
261
+ chain.optimization.usedExports(false);
262
+ }
263
+ }
264
+ var moduleFederationConfigPlugin = function(userConfig) {
265
+ return {
266
+ name: "@modern-js/plugin-module-federation-config",
267
+ pre: [
268
+ "@modern-js/plugin-initialize"
269
+ ],
270
+ post: [
271
+ "@modern-js/plugin-module-federation"
272
+ ],
273
+ setup: function() {
274
+ var _ref = _async_to_generator(function(api) {
275
+ var _userConfig_userConfig, _modernjsConfig_server, modernjsConfig, mfConfig, csrConfig, ssrConfig, _userConfig_userConfig_ssr, enableSSR;
276
+ return _ts_generator(this, function(_state) {
277
+ switch (_state.label) {
278
+ case 0:
279
+ modernjsConfig = api.getConfig();
280
+ return [
281
+ 4,
282
+ getMFConfig(userConfig.originPluginOptions)
283
+ ];
284
+ case 1:
285
+ mfConfig = _state.sent();
286
+ csrConfig = userConfig.csrConfig || JSON.parse(JSON.stringify(mfConfig));
287
+ ssrConfig = userConfig.ssrConfig || JSON.parse(JSON.stringify(mfConfig));
288
+ userConfig.ssrConfig = ssrConfig;
289
+ userConfig.csrConfig = csrConfig;
290
+ 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));
291
+ api.modifyBundlerChain(function(chain) {
292
+ var target = chain.get("target");
293
+ if (skipByTarget(target)) {
294
+ return;
295
+ }
296
+ var isWeb = isWebTarget(target);
297
+ addMyTypes2Ignored(chain, !isWeb ? ssrConfig : csrConfig);
298
+ var targetMFConfig = !isWeb ? ssrConfig : csrConfig;
299
+ patchMFConfig(targetMFConfig, !isWeb, userConfig.remoteIpStrategy || "ipv4");
300
+ patchBundlerConfig({
301
+ // @ts-expect-error chain type is not correct
302
+ chain,
303
+ isServer: !isWeb,
304
+ modernjsConfig,
305
+ mfConfig,
306
+ enableSSR
307
+ });
308
+ userConfig.distOutputDir = chain.output.get("path") || path.resolve(process.cwd(), "dist");
309
+ });
310
+ api.config(function() {
311
+ var _modernjsConfig_tools, _userConfig_csrConfig, _modernjsConfig_source, _modernjsConfig_source1, _modernjsConfig_dev;
312
+ var bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
313
+ var ipv4 = getIPV4();
314
+ if (userConfig.remoteIpStrategy === void 0) {
315
+ if (!enableSSR) {
316
+ userConfig.remoteIpStrategy = "inherit";
317
+ } else {
318
+ userConfig.remoteIpStrategy = "ipv4";
319
+ }
320
+ }
321
+ var devServerConfig = (_modernjsConfig_tools = modernjsConfig.tools) === null || _modernjsConfig_tools === void 0 ? void 0 : _modernjsConfig_tools.devServer;
322
+ var corsWarnMsgs = [
323
+ "View https://module-federation.io/guide/troubleshooting/other.html#cors-warn for more details."
324
+ ];
325
+ if ((typeof devServerConfig === "undefined" ? "undefined" : _type_of(devServerConfig)) !== "object" || !("headers" in devServerConfig)) {
326
+ 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.');
327
+ }
328
+ var exposes = (_userConfig_csrConfig = userConfig.csrConfig) === null || _userConfig_csrConfig === void 0 ? void 0 : _userConfig_csrConfig.exposes;
329
+ var hasExposes = exposes && Array.isArray(exposes) ? exposes.length : Object.keys(exposes !== null && exposes !== void 0 ? exposes : {}).length;
330
+ if (corsWarnMsgs.length > 1 && hasExposes) {
331
+ logger.warn(corsWarnMsgs.join("\n"));
332
+ }
333
+ var corsHeaders = hasExposes ? {
334
+ "Access-Control-Allow-Origin": "*",
335
+ "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
336
+ "Access-Control-Allow-Headers": "*"
337
+ } : void 0;
338
+ var _modernjsConfig_source_enableAsyncEntry;
339
+ return {
340
+ tools: {
341
+ devServer: {
342
+ headers: corsHeaders
343
+ }
344
+ },
345
+ source: {
346
+ alias: {
347
+ // TODO: deprecated
348
+ "@modern-js/runtime/mf": require.resolve("@module-federation/modern-js/runtime")
349
+ },
350
+ define: {
351
+ FEDERATION_IPV4: JSON.stringify(ipv4),
352
+ REMOTE_IP_STRATEGY: JSON.stringify(userConfig.remoteIpStrategy)
353
+ },
354
+ 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
355
+ },
356
+ dev: {
357
+ 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
358
+ }
359
+ };
360
+ });
361
+ return [
362
+ 2
363
+ ];
364
+ }
365
+ });
366
+ });
367
+ return function(api) {
368
+ return _ref.apply(this, arguments);
369
+ };
370
+ }()
371
+ };
372
+ };
373
+ var configPlugin_default = moduleFederationConfigPlugin;
374
+ export {
375
+ addMyTypes2Ignored,
376
+ configPlugin_default as default,
377
+ getMFConfig,
378
+ isWebTarget,
379
+ moduleFederationConfigPlugin,
380
+ patchBundlerConfig,
381
+ patchMFConfig,
382
+ setEnv,
383
+ skipByTarget
384
+ };
@@ -0,0 +1,108 @@
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/node/runtimePlugin"),
47
+ require.resolve("@module-federation/modern-js/inject-node-fetch")
48
+ ],
49
+ shared: {
50
+ react: {
51
+ eager: true,
52
+ singleton: true
53
+ },
54
+ "react-dom": {
55
+ eager: true,
56
+ singleton: true
57
+ }
58
+ }
59
+ });
60
+ return [
61
+ 2
62
+ ];
63
+ });
64
+ }));
65
+ it("patchMFConfig: client", /* @__PURE__ */ _async_to_generator(function() {
66
+ var patchedConfig, ipv4;
67
+ return _ts_generator(this, function(_state2) {
68
+ patchedConfig = JSON.parse(JSON.stringify(mfConfig));
69
+ patchMFConfig(patchedConfig, false);
70
+ ipv4 = getIPV4();
71
+ expect(patchedConfig).toStrictEqual({
72
+ filename: "remoteEntry.js",
73
+ name: "host",
74
+ remotes: {
75
+ remote: "http://".concat(ipv4, ":3000/remoteEntry.js")
76
+ },
77
+ remoteType: "script",
78
+ runtimePlugins: [
79
+ require.resolve("@module-federation/modern-js/shared-strategy")
80
+ ],
81
+ shared: {
82
+ react: {
83
+ eager: true,
84
+ singleton: true
85
+ },
86
+ "react-dom": {
87
+ eager: true,
88
+ singleton: true
89
+ }
90
+ },
91
+ dts: {
92
+ consumeTypes: {
93
+ runtimePkgs: [
94
+ "@module-federation/modern-js/runtime"
95
+ ]
96
+ }
97
+ }
98
+ });
99
+ return [
100
+ 2
101
+ ];
102
+ });
103
+ }));
104
+ return [
105
+ 2
106
+ ];
107
+ });
108
+ }));
@@ -0,0 +1,4 @@
1
+ var isDev = process.env.NODE_ENV === "development";
2
+ export {
3
+ isDev
4
+ };