@module-federation/modern-js 0.0.0-next-20250612075655 → 0.0.0-next-20250612085542

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 (122) hide show
  1. package/package.json +8 -8
  2. package/dist/LICENSE +0 -21
  3. package/dist/cjs/cli/configPlugin.js +0 -381
  4. package/dist/cjs/cli/configPlugin.spec.js +0 -114
  5. package/dist/cjs/cli/index.js +0 -95
  6. package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +0 -100
  7. package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +0 -44
  8. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +0 -82
  9. package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +0 -43
  10. package/dist/cjs/cli/server/data-fetch-server-plugin.js +0 -189
  11. package/dist/cjs/cli/ssrPlugin.js +0 -236
  12. package/dist/cjs/cli/utils.js +0 -90
  13. package/dist/cjs/constant.js +0 -78
  14. package/dist/cjs/interfaces/bundler.js +0 -16
  15. package/dist/cjs/interfaces/global.js +0 -16
  16. package/dist/cjs/logger.js +0 -27
  17. package/dist/cjs/runtime/AwaitDataFetch.js +0 -144
  18. package/dist/cjs/runtime/createRemoteComponent.js +0 -327
  19. package/dist/cjs/runtime/index.js +0 -50
  20. package/dist/cjs/runtime/wrapNoSSR.js +0 -35
  21. package/dist/cjs/server/fileCache.js +0 -84
  22. package/dist/cjs/server/fileCache.spec.js +0 -28
  23. package/dist/cjs/server/index.js +0 -58
  24. package/dist/cjs/server/staticMiddleware.js +0 -77
  25. package/dist/cjs/server/staticMiddleware.spec.js +0 -185
  26. package/dist/cjs/ssr-runtime/SSRLiveReload.js +0 -43
  27. package/dist/cjs/ssr-runtime/devPlugin.js +0 -71
  28. package/dist/cjs/ssr-runtime/downgrade.js +0 -114
  29. package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -136
  30. package/dist/cjs/types/index.js +0 -16
  31. package/dist/cjs/utils/dataFetch.js +0 -211
  32. package/dist/cjs/utils/index.js +0 -54
  33. package/dist/esm/cli/configPlugin.js +0 -388
  34. package/dist/esm/cli/configPlugin.spec.js +0 -110
  35. package/dist/esm/cli/index.js +0 -90
  36. package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +0 -76
  37. package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +0 -16
  38. package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +0 -74
  39. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +0 -25
  40. package/dist/esm/cli/server/data-fetch-server-plugin.js +0 -237
  41. package/dist/esm/cli/ssrPlugin.js +0 -219
  42. package/dist/esm/cli/utils.js +0 -53
  43. package/dist/esm/constant.js +0 -42
  44. package/dist/esm/interfaces/bundler.js +0 -0
  45. package/dist/esm/interfaces/global.js +0 -0
  46. package/dist/esm/logger.js +0 -7
  47. package/dist/esm/runtime/AwaitDataFetch.js +0 -131
  48. package/dist/esm/runtime/createRemoteComponent.js +0 -417
  49. package/dist/esm/runtime/index.js +0 -22
  50. package/dist/esm/runtime/wrapNoSSR.js +0 -12
  51. package/dist/esm/server/fileCache.js +0 -98
  52. package/dist/esm/server/fileCache.spec.js +0 -50
  53. package/dist/esm/server/index.js +0 -36
  54. package/dist/esm/server/staticMiddleware.js +0 -81
  55. package/dist/esm/server/staticMiddleware.spec.js +0 -328
  56. package/dist/esm/ssr-runtime/SSRLiveReload.js +0 -26
  57. package/dist/esm/ssr-runtime/devPlugin.js +0 -73
  58. package/dist/esm/ssr-runtime/downgrade.js +0 -150
  59. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -177
  60. package/dist/esm/types/index.js +0 -0
  61. package/dist/esm/utils/dataFetch.js +0 -237
  62. package/dist/esm/utils/index.js +0 -28
  63. package/dist/esm-node/cli/configPlugin.js +0 -340
  64. package/dist/esm-node/cli/configPlugin.spec.js +0 -91
  65. package/dist/esm-node/cli/index.js +0 -70
  66. package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +0 -70
  67. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +0 -14
  68. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +0 -62
  69. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +0 -23
  70. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +0 -159
  71. package/dist/esm-node/cli/ssrPlugin.js +0 -200
  72. package/dist/esm-node/cli/utils.js +0 -53
  73. package/dist/esm-node/constant.js +0 -42
  74. package/dist/esm-node/interfaces/bundler.js +0 -0
  75. package/dist/esm-node/interfaces/global.js +0 -0
  76. package/dist/esm-node/logger.js +0 -7
  77. package/dist/esm-node/runtime/AwaitDataFetch.js +0 -109
  78. package/dist/esm-node/runtime/createRemoteComponent.js +0 -291
  79. package/dist/esm-node/runtime/index.js +0 -22
  80. package/dist/esm-node/runtime/wrapNoSSR.js +0 -11
  81. package/dist/esm-node/server/fileCache.js +0 -49
  82. package/dist/esm-node/server/fileCache.spec.js +0 -27
  83. package/dist/esm-node/server/index.js +0 -34
  84. package/dist/esm-node/server/staticMiddleware.js +0 -43
  85. package/dist/esm-node/server/staticMiddleware.spec.js +0 -162
  86. package/dist/esm-node/ssr-runtime/SSRLiveReload.js +0 -19
  87. package/dist/esm-node/ssr-runtime/devPlugin.js +0 -37
  88. package/dist/esm-node/ssr-runtime/downgrade.js +0 -88
  89. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -101
  90. package/dist/esm-node/types/index.js +0 -0
  91. package/dist/esm-node/utils/dataFetch.js +0 -166
  92. package/dist/esm-node/utils/index.js +0 -27
  93. package/dist/types/cli/configPlugin.d.ts +0 -21
  94. package/dist/types/cli/configPlugin.spec.d.ts +0 -1
  95. package/dist/types/cli/index.d.ts +0 -6
  96. package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +0 -3
  97. package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +0 -3
  98. package/dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts +0 -3
  99. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +0 -3
  100. package/dist/types/cli/server/data-fetch-server-plugin.d.ts +0 -3
  101. package/dist/types/cli/ssrPlugin.d.ts +0 -6
  102. package/dist/types/cli/utils.d.ts +0 -6
  103. package/dist/types/constant.d.ts +0 -25
  104. package/dist/types/interfaces/bundler.d.ts +0 -18
  105. package/dist/types/interfaces/global.d.ts +0 -27
  106. package/dist/types/logger.d.ts +0 -2
  107. package/dist/types/runtime/AwaitDataFetch.d.ts +0 -19
  108. package/dist/types/runtime/createRemoteComponent.d.ts +0 -26
  109. package/dist/types/runtime/index.d.ts +0 -10
  110. package/dist/types/runtime/wrapNoSSR.d.ts +0 -9
  111. package/dist/types/server/fileCache.d.ts +0 -14
  112. package/dist/types/server/fileCache.spec.d.ts +0 -1
  113. package/dist/types/server/index.d.ts +0 -4
  114. package/dist/types/server/staticMiddleware.d.ts +0 -6
  115. package/dist/types/server/staticMiddleware.spec.d.ts +0 -1
  116. package/dist/types/ssr-runtime/SSRLiveReload.d.ts +0 -1
  117. package/dist/types/ssr-runtime/devPlugin.d.ts +0 -2
  118. package/dist/types/ssr-runtime/downgrade.d.ts +0 -4
  119. package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +0 -7
  120. package/dist/types/types/index.d.ts +0 -24
  121. package/dist/types/utils/dataFetch.d.ts +0 -26
  122. package/dist/types/utils/index.d.ts +0 -15
@@ -1,100 +0,0 @@
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" && (0, import_dataFetch.isDataLoaderExpose)(id)) {
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 hasSSRAsset = Boolean(remoteSnapshot.ssrRemoteEntry);
78
- const hasDataFetchClient = Boolean(remoteSnapshot.modules.find((module2) => module2.moduleName === `${dataFetchName}${import_constant2.DATA_FETCH_CLIENT_SUFFIX}`));
79
- const downgradeType = hasDataFetchClient ? import_constant.MF_DATA_FETCH_TYPE.FETCH_CLIENT : hasSSRAsset ? import_constant.MF_DATA_FETCH_TYPE.FETCH_SERVER : import_constant.MF_DATA_FETCH_TYPE.FETCH_CLIENT;
80
- let finalDataFetchId = dataFetchId;
81
- if (typeof window !== "undefined") {
82
- finalDataFetchId = downgradeType === import_constant.MF_DATA_FETCH_TYPE.FETCH_CLIENT ? hasDataFetchClient ? `${dataFetchId}${import_constant2.DATA_FETCH_CLIENT_SUFFIX}` : dataFetchId : dataFetchId;
83
- }
84
- const getDataFetchGetter = () => (0, import_dataFetch.loadDataFetchModule)(host, finalDataFetchId);
85
- const dataFetchFnItem = [
86
- getDataFetchGetter,
87
- downgradeType
88
- ];
89
- if (typeof window === "undefined" || (0, import_utils.isCSROnly)()) {
90
- dataFetchFnItem.push(getDataFetchGetter());
91
- }
92
- dataFetchMap[dataFetchMapKey] = [
93
- dataFetchFnItem,
94
- void 0,
95
- import_constant.MF_DATA_FETCH_STATUS.AWAIT
96
- ];
97
- return args;
98
- }
99
- });
100
- var auto_fetch_data_default = autoFetchData;
@@ -1,44 +0,0 @@
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;
@@ -1,82 +0,0 @@
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;
@@ -1,43 +0,0 @@
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;
@@ -1,189 +0,0 @@
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
- var _globalThis___FEDERATION__;
71
- let url;
72
- let dataFetchId;
73
- let params;
74
- let remoteInfo;
75
- try {
76
- url = new URL(ctx.req.url);
77
- dataFetchId = getDecodeQuery(url, import_constant.DATA_FETCH_QUERY);
78
- params = JSON.parse(getDecodeQuery(url, "params") || "{}");
79
- const remoteInfoQuery = getDecodeQuery(url, "remoteInfo");
80
- remoteInfo = remoteInfoQuery ? JSON.parse(remoteInfoQuery) : null;
81
- } catch (e) {
82
- import_logger.default.error("fetch data from server, error: ", e);
83
- return next();
84
- }
85
- if (!dataFetchId) {
86
- return next();
87
- }
88
- import_logger.default.log("fetch data from server, dataFetchId: ", dataFetchId);
89
- import_logger.default.debug("fetch data from server, moduleInfo: ", (_globalThis___FEDERATION__ = globalThis.__FEDERATION__) === null || _globalThis___FEDERATION__ === void 0 ? void 0 : _globalThis___FEDERATION__.moduleInfo);
90
- try {
91
- var _dataFetchMap_dataFetchId, _dataFetchMap_dataFetchId1;
92
- const dataFetchMap = (0, import_utils.getDataFetchMap)();
93
- if (!dataFetchMap) {
94
- (0, import_dataFetch.initDataFetchMap)();
95
- }
96
- const fetchDataPromise = (_dataFetchMap_dataFetchId = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId === void 0 ? void 0 : _dataFetchMap_dataFetchId[1];
97
- import_logger.default.debug("fetch data from server, fetchDataPromise: ", fetchDataPromise);
98
- if (fetchDataPromise && ((_dataFetchMap_dataFetchId1 = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId1 === void 0 ? void 0 : _dataFetchMap_dataFetchId1[2]) !== import_constant.MF_DATA_FETCH_STATUS.ERROR) {
99
- const targetPromise = fetchDataPromise[0];
100
- const wrappedPromise = wrapSetTimeout(targetPromise, 2e4, dataFetchId);
101
- if (wrappedPromise) {
102
- const res = await wrappedPromise;
103
- import_logger.default.log("fetch data from server, fetchDataPromise res: ", res);
104
- return ctx.json(res);
105
- }
106
- import_logger.default.error(`Expected a Promise from fetchDataPromise[0] for dataFetchId ${dataFetchId}, but received:`, targetPromise, "Will try call new dataFetch again...");
107
- }
108
- if (remoteInfo) {
109
- try {
110
- const hostInstance2 = globalThis.__FEDERATION__.__INSTANCES__[0];
111
- const remoteEntry = `${addProtocol(remoteInfo.ssrPublicPath) + remoteInfo.ssrRemoteEntry}`;
112
- if (!hostInstance2) {
113
- throw new Error("host instance not found!");
114
- }
115
- const remote = hostInstance2.options.remotes.find((remote2) => remote2.name === remoteInfo.name);
116
- import_logger.default.debug("find remote: ", JSON.stringify(remote));
117
- if (!remote) {
118
- hostInstance2.registerRemotes([
119
- {
120
- name: remoteInfo.name,
121
- entry: remoteEntry,
122
- entryGlobalName: remoteInfo.globalName
123
- }
124
- ]);
125
- } else if (!("entry" in remote) || !remote.entry.includes(import_sdk.MANIFEST_EXT)) {
126
- const { hostGlobalSnapshot, remoteSnapshot } = hostInstance2.snapshotHandler.getGlobalRemoteInfo(remoteInfo);
127
- import_logger.default.debug("find hostGlobalSnapshot: ", JSON.stringify(hostGlobalSnapshot));
128
- import_logger.default.debug("find remoteSnapshot: ", JSON.stringify(remoteSnapshot));
129
- if (!hostGlobalSnapshot || !remoteSnapshot) {
130
- if ("version" in remote) {
131
- delete remote.version;
132
- }
133
- remote.entry = remoteEntry;
134
- remote.entryGlobalName = remoteInfo.globalName;
135
- }
136
- }
137
- } catch (e) {
138
- ctx.status(500);
139
- return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
140
- ${e}`);
141
- }
142
- }
143
- const dataFetchItem = dataFetchMap[dataFetchId];
144
- import_logger.default.debug("fetch data from server, dataFetchItem: ", dataFetchItem);
145
- if (dataFetchItem) {
146
- const callFetchDataPromise = (0, import_dataFetch.fetchData)(dataFetchId, {
147
- ...params,
148
- isDowngrade: !remoteInfo
149
- });
150
- const wrappedPromise = wrapSetTimeout(callFetchDataPromise, 2e4, dataFetchId);
151
- if (wrappedPromise) {
152
- const res = await wrappedPromise;
153
- import_logger.default.log("fetch data from server, dataFetchItem res: ", res);
154
- return ctx.json(res);
155
- }
156
- }
157
- const remoteId = dataFetchId.split(import_sdk.SEPARATOR)[0];
158
- const hostInstance = globalThis.__FEDERATION__.__INSTANCES__[0];
159
- if (!hostInstance) {
160
- throw new Error("host instance not found!");
161
- }
162
- const dataFetchFn = await (0, import_dataFetch.loadDataFetchModule)(hostInstance, remoteId);
163
- const data = await dataFetchFn({
164
- ...params,
165
- isDowngrade: !remoteInfo
166
- });
167
- import_logger.default.log("fetch data from server, loadDataFetchModule res: ", data);
168
- return ctx.json(data);
169
- } catch (e) {
170
- import_logger.default.error("server plugin data fetch error: ", e);
171
- ctx.status(500);
172
- return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
173
- ${e}`);
174
- }
175
- };
176
- const dataFetchServePlugin = () => ({
177
- name: "mf-data-fetch-server-plugin",
178
- setup: (api) => {
179
- api.onPrepare(() => {
180
- const { middlewares } = api.getServerContext();
181
- middlewares.push({
182
- name: "module-federation-serve-manifest",
183
- // @ts-ignore type error
184
- handler: middleware
185
- });
186
- });
187
- }
188
- });
189
- var data_fetch_server_plugin_default = dataFetchServePlugin;