@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,98 @@
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 auto_fetch_data_exports = {};
30
+ __export(auto_fetch_data_exports, {
31
+ default: () => auto_fetch_data_default
32
+ });
33
+ module.exports = __toCommonJS(auto_fetch_data_exports);
34
+ var import_utils = require("../../utils");
35
+ var import_logger = __toESM(require("../../logger"));
36
+ var import_dataFetch = require("../../utils/dataFetch");
37
+ var import_constant = require("../../constant");
38
+ var import_constant2 = require("@module-federation/rsbuild-plugin/constant");
39
+ const autoFetchData = () => ({
40
+ name: "auto-fetch-data-plugin",
41
+ beforeInit(args) {
42
+ (0, import_utils.initDataFetchMap)();
43
+ return args;
44
+ },
45
+ afterLoadSnapshot(args) {
46
+ const { id, moduleInfo, remoteSnapshot, host } = args;
47
+ if (typeof id === "string" && id.includes("data")) {
48
+ return args;
49
+ }
50
+ if (!remoteSnapshot || !id || !("modules" in remoteSnapshot)) {
51
+ return args;
52
+ }
53
+ const { name, alias } = moduleInfo;
54
+ const dataFetchInfo = (0, import_utils.getDataFetchInfo)({
55
+ name,
56
+ alias,
57
+ id,
58
+ remoteSnapshot
59
+ });
60
+ if (!dataFetchInfo) {
61
+ return args;
62
+ }
63
+ const { dataFetchId, dataFetchName } = dataFetchInfo;
64
+ const dataFetchMapKey = (0, import_dataFetch.getDataFetchMapKey)(dataFetchInfo, {
65
+ name: host.name,
66
+ version: host.options.version
67
+ });
68
+ import_logger.default.debug("======= auto fetch plugin dataFetchMapKey: ", dataFetchMapKey);
69
+ if (!dataFetchMapKey) {
70
+ return args;
71
+ }
72
+ const dataFetchItem = (0, import_utils.getDataFetchItem)(dataFetchMapKey);
73
+ if (dataFetchItem) {
74
+ return args;
75
+ }
76
+ const dataFetchMap = (0, import_utils.getDataFetchMap)();
77
+ const downgradeType = remoteSnapshot.modules.find((module2) => module2.moduleName === `${dataFetchName}${import_constant2.DATA_FETCH_CLIENT_SUFFIX}`) ? import_constant.MF_DATA_FETCH_TYPE.FETCH_CLIENT : import_constant.MF_DATA_FETCH_TYPE.FETCH_SERVER;
78
+ let finalDataFetchId = dataFetchId;
79
+ if (typeof window !== "undefined") {
80
+ finalDataFetchId = downgradeType === import_constant.MF_DATA_FETCH_TYPE.FETCH_CLIENT ? `${dataFetchId}${import_constant2.DATA_FETCH_CLIENT_SUFFIX}` : dataFetchId;
81
+ }
82
+ const getDataFetchGetter = () => (0, import_dataFetch.loadDataFetchModule)(host, finalDataFetchId);
83
+ const dataFetchFnItem = [
84
+ getDataFetchGetter,
85
+ downgradeType
86
+ ];
87
+ if (typeof window === "undefined" || (0, import_utils.isCSROnly)()) {
88
+ dataFetchFnItem.push(getDataFetchGetter());
89
+ }
90
+ dataFetchMap[dataFetchMapKey] = [
91
+ dataFetchFnItem,
92
+ void 0,
93
+ import_constant.MF_DATA_FETCH_STATUS.AWAIT
94
+ ];
95
+ return args;
96
+ }
97
+ });
98
+ var auto_fetch_data_default = autoFetchData;
@@ -0,0 +1,44 @@
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 inject_node_fetch_exports = {};
30
+ __export(inject_node_fetch_exports, {
31
+ default: () => inject_node_fetch_default
32
+ });
33
+ module.exports = __toCommonJS(inject_node_fetch_exports);
34
+ var import_node_fetch = __toESM(require("node-fetch"));
35
+ const injectNodeFetchPlugin = () => ({
36
+ name: "inject-node-fetch-plugin",
37
+ beforeInit(args) {
38
+ if (!globalThis.fetch) {
39
+ globalThis.fetch = import_node_fetch.default;
40
+ }
41
+ return args;
42
+ }
43
+ });
44
+ var inject_node_fetch_default = injectNodeFetchPlugin;
@@ -0,0 +1,82 @@
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 resolve_entry_ipv4_exports = {};
20
+ __export(resolve_entry_ipv4_exports, {
21
+ default: () => resolve_entry_ipv4_default
22
+ });
23
+ module.exports = __toCommonJS(resolve_entry_ipv4_exports);
24
+ var import_constant = require("../../constant");
25
+ const ipv4 = typeof FEDERATION_IPV4 !== "undefined" ? FEDERATION_IPV4 : "127.0.0.1";
26
+ const remoteIpStrategy = typeof REMOTE_IP_STRATEGY !== "undefined" ? REMOTE_IP_STRATEGY : "inherit";
27
+ function replaceObjectLocalhost(key, obj) {
28
+ if (remoteIpStrategy !== "ipv4") {
29
+ return;
30
+ }
31
+ if (!(key in obj)) {
32
+ return;
33
+ }
34
+ const remote = obj[key];
35
+ if (remote && typeof remote === "string" && remote.includes(import_constant.LOCALHOST)) {
36
+ obj[key] = replaceLocalhost(remote);
37
+ }
38
+ }
39
+ function replaceLocalhost(url) {
40
+ return url.replace(import_constant.LOCALHOST, ipv4);
41
+ }
42
+ const resolveEntryIpv4Plugin = () => ({
43
+ name: "resolve-entry-ipv4",
44
+ beforeRegisterRemote(args) {
45
+ const { remote } = args;
46
+ replaceObjectLocalhost("entry", remote);
47
+ return args;
48
+ },
49
+ async afterResolve(args) {
50
+ const { remoteInfo } = args;
51
+ replaceObjectLocalhost("entry", remoteInfo);
52
+ return args;
53
+ },
54
+ beforeLoadRemoteSnapshot(args) {
55
+ const { moduleInfo } = args;
56
+ if ("entry" in moduleInfo) {
57
+ replaceObjectLocalhost("entry", moduleInfo);
58
+ return args;
59
+ }
60
+ if ("version" in moduleInfo) {
61
+ replaceObjectLocalhost("version", moduleInfo);
62
+ }
63
+ return args;
64
+ },
65
+ loadRemoteSnapshot(args) {
66
+ const { remoteSnapshot } = args;
67
+ if ("publicPath" in remoteSnapshot) {
68
+ replaceObjectLocalhost("publicPath", remoteSnapshot);
69
+ }
70
+ if ("getPublicPath" in remoteSnapshot) {
71
+ replaceObjectLocalhost("getPublicPath", remoteSnapshot);
72
+ }
73
+ if (remoteSnapshot.remotesInfo) {
74
+ Object.keys(remoteSnapshot.remotesInfo).forEach((key) => {
75
+ const remoteInfo = remoteSnapshot.remotesInfo[key];
76
+ replaceObjectLocalhost("matchedVersion", remoteInfo);
77
+ });
78
+ }
79
+ return args;
80
+ }
81
+ });
82
+ var resolve_entry_ipv4_default = resolveEntryIpv4Plugin;
@@ -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 shared_strategy_exports = {};
20
+ __export(shared_strategy_exports, {
21
+ default: () => shared_strategy_default
22
+ });
23
+ module.exports = __toCommonJS(shared_strategy_exports);
24
+ const sharedStrategy = () => ({
25
+ name: "shared-strategy-plugin",
26
+ beforeInit(args) {
27
+ const { userOptions } = args;
28
+ const shared = userOptions.shared;
29
+ if (shared) {
30
+ Object.keys(shared).forEach((sharedKey) => {
31
+ const sharedConfigs = shared[sharedKey];
32
+ const arraySharedConfigs = Array.isArray(sharedConfigs) ? sharedConfigs : [
33
+ sharedConfigs
34
+ ];
35
+ arraySharedConfigs.forEach((s) => {
36
+ s.strategy = "loaded-first";
37
+ });
38
+ });
39
+ }
40
+ return args;
41
+ }
42
+ });
43
+ var shared_strategy_default = sharedStrategy;
@@ -0,0 +1,184 @@
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 data_fetch_server_plugin_exports = {};
30
+ __export(data_fetch_server_plugin_exports, {
31
+ default: () => data_fetch_server_plugin_default
32
+ });
33
+ module.exports = __toCommonJS(data_fetch_server_plugin_exports);
34
+ var import_constant = require("../../constant");
35
+ var import_logger = __toESM(require("../../logger"));
36
+ var import_utils = require("../../utils");
37
+ var import_dataFetch = require("../../utils/dataFetch");
38
+ var import_sdk = require("@module-federation/sdk");
39
+ function wrapSetTimeout(targetPromise, delay = 2e4, id) {
40
+ if (targetPromise && typeof targetPromise.then === "function") {
41
+ return new Promise((resolve, reject) => {
42
+ const timeoutId = setTimeout(() => {
43
+ import_logger.default.warn(`Data fetch for ID ${id} timed out after 20 seconds.`);
44
+ reject(new Error(`Data fetch for ID ${id} timed out after 20 seconds`));
45
+ }, delay);
46
+ targetPromise.then((value) => {
47
+ clearTimeout(timeoutId);
48
+ resolve(value);
49
+ }).catch((err) => {
50
+ clearTimeout(timeoutId);
51
+ reject(err);
52
+ });
53
+ });
54
+ }
55
+ }
56
+ function addProtocol(url) {
57
+ if (url.startsWith("//")) {
58
+ return "https:" + url;
59
+ }
60
+ return url;
61
+ }
62
+ const getDecodeQuery = (url, name) => {
63
+ const res = url.searchParams.get(name);
64
+ if (!res) {
65
+ return null;
66
+ }
67
+ return decodeURIComponent(res);
68
+ };
69
+ const middleware = async (ctx, next) => {
70
+ let url;
71
+ let dataFetchId;
72
+ let params;
73
+ let remoteInfo;
74
+ try {
75
+ url = new URL(ctx.req.url);
76
+ dataFetchId = getDecodeQuery(url, import_constant.DATA_FETCH_QUERY);
77
+ params = JSON.parse(getDecodeQuery(url, "params") || "{}");
78
+ const remoteInfoQuery = getDecodeQuery(url, "remoteInfo");
79
+ remoteInfo = remoteInfoQuery ? JSON.parse(remoteInfoQuery) : null;
80
+ } catch (e) {
81
+ console.error(e);
82
+ return next();
83
+ }
84
+ if (!dataFetchId) {
85
+ return next();
86
+ }
87
+ import_logger.default.debug("dataFetchId: ", dataFetchId);
88
+ try {
89
+ var _dataFetchMap_dataFetchId;
90
+ const dataFetchMap = (0, import_utils.getDataFetchMap)();
91
+ if (!dataFetchMap) {
92
+ (0, import_dataFetch.initDataFetchMap)();
93
+ }
94
+ const fetchDataPromise = (_dataFetchMap_dataFetchId = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId === void 0 ? void 0 : _dataFetchMap_dataFetchId[1];
95
+ if (fetchDataPromise) {
96
+ const targetPromise = fetchDataPromise[0];
97
+ const wrappedPromise = wrapSetTimeout(targetPromise, 2e4, dataFetchId);
98
+ if (wrappedPromise) {
99
+ const res = await wrappedPromise;
100
+ return ctx.json(res);
101
+ }
102
+ import_logger.default.error(`Expected a Promise from fetchDataPromise[0] for dataFetchId ${dataFetchId}, but received:`, targetPromise, "Will try call new dataFetch again...");
103
+ }
104
+ if (remoteInfo) {
105
+ try {
106
+ const hostInstance2 = globalThis.__FEDERATION__.__INSTANCES__[0];
107
+ const remoteEntry = `${addProtocol(remoteInfo.ssrPublicPath) + remoteInfo.ssrRemoteEntry}`;
108
+ if (!hostInstance2) {
109
+ throw new Error("host instance not found!");
110
+ }
111
+ const remote = hostInstance2.options.remotes.find((remote2) => remote2.name === remoteInfo.name);
112
+ import_logger.default.debug("find remote: ", JSON.stringify(remote));
113
+ if (!remote) {
114
+ hostInstance2.registerRemotes([
115
+ {
116
+ name: remoteInfo.name,
117
+ entry: remoteEntry,
118
+ entryGlobalName: remoteInfo.globalName
119
+ }
120
+ ]);
121
+ } else {
122
+ const { hostGlobalSnapshot, remoteSnapshot } = hostInstance2.snapshotHandler.getGlobalRemoteInfo(remoteInfo);
123
+ import_logger.default.debug("find hostGlobalSnapshot: ", JSON.stringify(hostGlobalSnapshot));
124
+ import_logger.default.debug("find remoteSnapshot: ", JSON.stringify(remoteSnapshot));
125
+ if (!hostGlobalSnapshot || !remoteSnapshot) {
126
+ if ("version" in remote) {
127
+ delete remote.version;
128
+ }
129
+ remote.entry = remoteEntry;
130
+ remote.entryGlobalName = remoteInfo.globalName;
131
+ }
132
+ }
133
+ } catch (e) {
134
+ ctx.status(500);
135
+ return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
136
+ ${e}`);
137
+ }
138
+ }
139
+ const dataFetchItem = dataFetchMap[dataFetchId];
140
+ if (dataFetchItem) {
141
+ const callFetchDataPromise = (0, import_dataFetch.fetchData)(dataFetchId, {
142
+ ...params,
143
+ isDowngrade: true
144
+ });
145
+ const wrappedPromise = wrapSetTimeout(callFetchDataPromise, 2e4, dataFetchId);
146
+ if (wrappedPromise) {
147
+ const res = await wrappedPromise;
148
+ return ctx.json(res);
149
+ }
150
+ return next();
151
+ }
152
+ const remoteId = dataFetchId.split(import_sdk.SEPARATOR)[0];
153
+ const hostInstance = globalThis.__FEDERATION__.__INSTANCES__[0];
154
+ if (!hostInstance) {
155
+ throw new Error("host instance not found!");
156
+ }
157
+ const dataFetchFn = await (0, import_dataFetch.loadDataFetchModule)(hostInstance, remoteId);
158
+ const data = await dataFetchFn({
159
+ ...params,
160
+ isDowngrade: !remoteInfo
161
+ });
162
+ return ctx.json(data);
163
+ } catch (e) {
164
+ console.log("data fetch error:");
165
+ console.error(e);
166
+ ctx.status(500);
167
+ return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
168
+ ${e}`);
169
+ }
170
+ };
171
+ const dataFetchServePlugin = () => ({
172
+ name: "mf-data-fetch-server-plugin",
173
+ setup: (api) => {
174
+ api.onPrepare(() => {
175
+ const { middlewares } = api.getServerContext();
176
+ middlewares.push({
177
+ name: "module-federation-serve-manifest",
178
+ // @ts-ignore type error
179
+ handler: middleware
180
+ });
181
+ });
182
+ }
183
+ });
184
+ var data_fetch_server_plugin_default = dataFetchServePlugin;
@@ -0,0 +1,222 @@
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 ssrPlugin_exports = {};
30
+ __export(ssrPlugin_exports, {
31
+ CHAIN_MF_PLUGIN_ID: () => CHAIN_MF_PLUGIN_ID,
32
+ default: () => ssrPlugin_default,
33
+ moduleFederationSSRPlugin: () => moduleFederationSSRPlugin,
34
+ setEnv: () => setEnv
35
+ });
36
+ module.exports = __toCommonJS(ssrPlugin_exports);
37
+ var import_path = __toESM(require("path"));
38
+ var import_utils = require("@modern-js/utils");
39
+ var import_webpack = require("@module-federation/enhanced/webpack");
40
+ var import_rspack = require("@module-federation/enhanced/rspack");
41
+ var import_universe_entry_chunk_tracker_plugin = __toESM(require("@module-federation/node/universe-entry-chunk-tracker-plugin"));
42
+ var import_manifest = require("./manifest");
43
+ var import_logger = __toESM(require("../logger"));
44
+ var import_utils2 = require("./utils");
45
+ var import_utils3 = require("./utils");
46
+ function setEnv() {
47
+ process.env["MF_DISABLE_EMIT_STATS"] = "true";
48
+ process.env["MF_SSR_PRJ"] = "true";
49
+ }
50
+ const CHAIN_MF_PLUGIN_ID = "plugin-module-federation-server";
51
+ const mfSSRRsbuildPlugin = (pluginOptions) => {
52
+ return {
53
+ name: "@modern-js/plugin-mf-post-config",
54
+ pre: [
55
+ "@modern-js/builder-plugin-ssr"
56
+ ],
57
+ setup(api) {
58
+ if (pluginOptions.csrConfig.getPublicPath) {
59
+ return;
60
+ }
61
+ let csrOutputPath = "";
62
+ let ssrOutputPath = "";
63
+ let ssrEnv = "";
64
+ api.modifyEnvironmentConfig((config, { name }) => {
65
+ const target = config.output.target;
66
+ if ((0, import_utils3.skipByTarget)(target)) {
67
+ return config;
68
+ }
69
+ if ((0, import_utils3.isWebTarget)(target)) {
70
+ csrOutputPath = config.output.distPath.root;
71
+ } else {
72
+ ssrOutputPath = config.output.distPath.root;
73
+ ssrEnv = name;
74
+ }
75
+ return config;
76
+ });
77
+ const modifySSRPublicPath = (config, utils) => {
78
+ if (ssrEnv !== utils.environment.name) {
79
+ return config;
80
+ }
81
+ const userSSRConfig = pluginOptions.userConfig.ssr ? typeof pluginOptions.userConfig.ssr === "object" ? pluginOptions.userConfig.ssr : {} : {};
82
+ if (userSSRConfig.distOutputDir) {
83
+ return;
84
+ }
85
+ config.output.publicPath = `${config.output.publicPath}${import_path.default.relative(csrOutputPath, ssrOutputPath)}/`;
86
+ return config;
87
+ };
88
+ api.modifyWebpackConfig((config, utils) => {
89
+ modifySSRPublicPath(config, utils);
90
+ return config;
91
+ });
92
+ api.modifyRspackConfig((config, utils) => {
93
+ modifySSRPublicPath(config, utils);
94
+ return config;
95
+ });
96
+ }
97
+ };
98
+ };
99
+ const moduleFederationSSRPlugin = (pluginOptions) => ({
100
+ name: "@modern-js/plugin-module-federation-ssr",
101
+ pre: [
102
+ "@modern-js/plugin-module-federation-config",
103
+ "@modern-js/plugin-module-federation"
104
+ ],
105
+ setup: async (api) => {
106
+ var _pluginOptions_userConfig, _modernjsConfig_server;
107
+ const modernjsConfig = api.getConfig();
108
+ var _pluginOptions_userConfig_ssr;
109
+ const enableSSR = (_pluginOptions_userConfig_ssr = (_pluginOptions_userConfig = pluginOptions.userConfig) === null || _pluginOptions_userConfig === void 0 ? void 0 : _pluginOptions_userConfig.ssr) !== null && _pluginOptions_userConfig_ssr !== void 0 ? _pluginOptions_userConfig_ssr : Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
110
+ if (!enableSSR) {
111
+ return;
112
+ }
113
+ setEnv();
114
+ api._internalRuntimePlugins(({ entrypoint, plugins }) => {
115
+ plugins.push({
116
+ name: "injectDataFetchFunction",
117
+ path: "@module-federation/modern-js/ssr-inject-data-fetch-function-plugin",
118
+ config: {}
119
+ });
120
+ if (!(0, import_utils2.isDev)()) {
121
+ return {
122
+ entrypoint,
123
+ plugins
124
+ };
125
+ }
126
+ plugins.push({
127
+ name: "mfSSRDev",
128
+ path: "@module-federation/modern-js/ssr-dev-plugin",
129
+ config: {}
130
+ });
131
+ return {
132
+ entrypoint,
133
+ plugins
134
+ };
135
+ });
136
+ api.modifyBundlerChain((chain) => {
137
+ const target = chain.get("target");
138
+ if ((0, import_utils3.skipByTarget)(target)) {
139
+ return;
140
+ }
141
+ const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
142
+ const MFPlugin = bundlerType === "webpack" ? import_webpack.ModuleFederationPlugin : import_rspack.ModuleFederationPlugin;
143
+ const isWeb = (0, import_utils3.isWebTarget)(target);
144
+ if (!isWeb) {
145
+ if (!chain.plugins.has(CHAIN_MF_PLUGIN_ID)) {
146
+ chain.plugin(CHAIN_MF_PLUGIN_ID).use(MFPlugin, [
147
+ pluginOptions.ssrConfig
148
+ ]).init((Plugin, args) => {
149
+ pluginOptions.nodePlugin = new Plugin(args[0]);
150
+ return pluginOptions.nodePlugin;
151
+ });
152
+ }
153
+ }
154
+ if (!isWeb) {
155
+ chain.target("async-node");
156
+ if ((0, import_utils2.isDev)()) {
157
+ chain.plugin("UniverseEntryChunkTrackerPlugin").use(import_universe_entry_chunk_tracker_plugin.default);
158
+ }
159
+ const userSSRConfig = pluginOptions.userConfig.ssr ? typeof pluginOptions.userConfig.ssr === "object" ? pluginOptions.userConfig.ssr : {} : {};
160
+ const publicPath = chain.output.get("publicPath");
161
+ if (userSSRConfig.distOutputDir && publicPath) {
162
+ chain.output.publicPath(`${publicPath}${userSSRConfig.distOutputDir}/`);
163
+ }
164
+ }
165
+ if ((0, import_utils2.isDev)() && isWeb) {
166
+ chain.externals({
167
+ "@module-federation/node/utils": "NOT_USED_IN_BROWSER"
168
+ });
169
+ }
170
+ });
171
+ api.config(() => {
172
+ return {
173
+ builderPlugins: [
174
+ mfSSRRsbuildPlugin(pluginOptions)
175
+ ],
176
+ tools: {
177
+ devServer: {
178
+ before: [
179
+ (req, res, next) => {
180
+ if (!enableSSR) {
181
+ next();
182
+ return;
183
+ }
184
+ try {
185
+ var _req_url, _req_url1;
186
+ if (((_req_url = req.url) === null || _req_url === void 0 ? void 0 : _req_url.includes(".json")) && !((_req_url1 = req.url) === null || _req_url1 === void 0 ? void 0 : _req_url1.includes("hot-update"))) {
187
+ const filepath = import_path.default.join(process.cwd(), `dist${req.url}`);
188
+ import_utils.fs.statSync(filepath);
189
+ res.setHeader("Access-Control-Allow-Origin", "*");
190
+ res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS");
191
+ res.setHeader("Access-Control-Allow-Headers", "*");
192
+ import_utils.fs.createReadStream(filepath).pipe(res);
193
+ } else {
194
+ next();
195
+ }
196
+ } catch (err) {
197
+ import_logger.default.debug(err);
198
+ next();
199
+ }
200
+ }
201
+ ]
202
+ }
203
+ }
204
+ };
205
+ });
206
+ api.onAfterBuild(() => {
207
+ const { nodePlugin, browserPlugin, distOutputDir } = pluginOptions;
208
+ (0, import_manifest.updateStatsAndManifest)(nodePlugin, browserPlugin, distOutputDir);
209
+ });
210
+ api.onDevCompileDone(() => {
211
+ const { nodePlugin, browserPlugin, distOutputDir } = pluginOptions;
212
+ (0, import_manifest.updateStatsAndManifest)(nodePlugin, browserPlugin, distOutputDir);
213
+ });
214
+ }
215
+ });
216
+ var ssrPlugin_default = moduleFederationSSRPlugin;
217
+ // Annotate the CommonJS export names for ESM import in node:
218
+ 0 && (module.exports = {
219
+ CHAIN_MF_PLUGIN_ID,
220
+ moduleFederationSSRPlugin,
221
+ setEnv
222
+ });