@module-federation/modern-js 0.0.0-next-20250526074234 → 0.0.0-next-20250527094526

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 (109) hide show
  1. package/dist/cjs/cli/configPlugin.js +6 -8
  2. package/dist/cjs/cli/configPlugin.spec.js +1 -3
  3. package/dist/cjs/{runtime/wrapNoSSR.js → cli/constant.js} +6 -13
  4. package/dist/cjs/cli/index.js +9 -3
  5. package/dist/cjs/{logger.js → cli/logger.js} +1 -1
  6. package/dist/cjs/cli/manifest.js +3 -3
  7. package/dist/cjs/cli/ssrPlugin.js +15 -34
  8. package/dist/cjs/cli/utils.js +0 -5
  9. package/dist/cjs/constant.js +2 -49
  10. package/dist/cjs/runtime/createRemoteSSRComponent.js +201 -0
  11. package/dist/cjs/runtime/index.js +3 -15
  12. package/dist/cjs/server/fileCache.js +84 -0
  13. package/dist/cjs/server/fileCache.spec.js +28 -0
  14. package/dist/cjs/server/index.js +58 -0
  15. package/dist/cjs/server/staticMiddleware.js +77 -0
  16. package/dist/cjs/server/staticMiddleware.spec.js +185 -0
  17. package/dist/cjs/{interfaces/global.js → ssr-runtime/index.js} +8 -2
  18. package/dist/cjs/ssr-runtime/{devPlugin.js → plugin.js} +6 -9
  19. package/dist/esm/cli/configPlugin.js +7 -9
  20. package/dist/esm/cli/configPlugin.spec.js +1 -3
  21. package/dist/esm/cli/constant.js +4 -0
  22. package/dist/esm/cli/index.js +10 -3
  23. package/dist/esm/{logger.js → cli/logger.js} +1 -1
  24. package/dist/esm/cli/manifest.js +1 -1
  25. package/dist/esm/cli/ssrPlugin.js +8 -28
  26. package/dist/esm/cli/utils.js +0 -4
  27. package/dist/esm/constant.js +1 -37
  28. package/dist/esm/runtime/createRemoteSSRComponent.js +191 -0
  29. package/dist/esm/runtime/index.js +3 -13
  30. package/dist/esm/server/fileCache.js +98 -0
  31. package/dist/esm/server/fileCache.spec.js +50 -0
  32. package/dist/esm/server/index.js +36 -0
  33. package/dist/esm/server/staticMiddleware.js +81 -0
  34. package/dist/esm/server/staticMiddleware.spec.js +328 -0
  35. package/dist/esm/ssr-runtime/index.js +1 -0
  36. package/dist/esm/ssr-runtime/{devPlugin.js → plugin.js} +2 -5
  37. package/dist/esm-node/cli/configPlugin.js +7 -9
  38. package/dist/esm-node/cli/configPlugin.spec.js +1 -3
  39. package/dist/esm-node/cli/constant.js +4 -0
  40. package/dist/esm-node/cli/index.js +9 -3
  41. package/dist/esm-node/{logger.js → cli/logger.js} +1 -1
  42. package/dist/esm-node/cli/manifest.js +1 -1
  43. package/dist/esm-node/cli/ssrPlugin.js +8 -27
  44. package/dist/esm-node/cli/utils.js +0 -4
  45. package/dist/esm-node/constant.js +1 -37
  46. package/dist/esm-node/runtime/createRemoteSSRComponent.js +166 -0
  47. package/dist/esm-node/runtime/index.js +3 -13
  48. package/dist/esm-node/server/fileCache.js +49 -0
  49. package/dist/esm-node/server/fileCache.spec.js +27 -0
  50. package/dist/esm-node/server/index.js +34 -0
  51. package/dist/esm-node/server/staticMiddleware.js +43 -0
  52. package/dist/esm-node/server/staticMiddleware.spec.js +162 -0
  53. package/dist/esm-node/ssr-runtime/index.js +1 -0
  54. package/dist/esm-node/ssr-runtime/{devPlugin.js → plugin.js} +2 -5
  55. package/dist/types/cli/constant.d.ts +1 -0
  56. package/dist/types/cli/index.d.ts +0 -1
  57. package/dist/types/cli/utils.d.ts +0 -1
  58. package/dist/types/constant.d.ts +0 -23
  59. package/dist/types/runtime/createRemoteSSRComponent.d.ts +18 -0
  60. package/dist/types/runtime/index.d.ts +2 -7
  61. package/dist/types/server/fileCache.d.ts +14 -0
  62. package/dist/types/server/fileCache.spec.d.ts +1 -0
  63. package/dist/types/server/index.d.ts +4 -0
  64. package/dist/types/server/staticMiddleware.d.ts +6 -0
  65. package/dist/types/server/staticMiddleware.spec.d.ts +1 -0
  66. package/dist/types/ssr-runtime/index.d.ts +1 -0
  67. package/dist/types/ssr-runtime/plugin.d.ts +2 -0
  68. package/dist/types/types/index.d.ts +0 -2
  69. package/package.json +23 -39
  70. package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +0 -98
  71. package/dist/cjs/cli/server/data-fetch-server-plugin.js +0 -192
  72. package/dist/cjs/runtime/AwaitDataFetch.js +0 -144
  73. package/dist/cjs/runtime/createRemoteComponent.js +0 -327
  74. package/dist/cjs/ssr-runtime/downgrade.js +0 -114
  75. package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -136
  76. package/dist/cjs/utils/dataFetch.js +0 -211
  77. package/dist/cjs/utils/index.js +0 -54
  78. package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +0 -74
  79. package/dist/esm/cli/server/data-fetch-server-plugin.js +0 -240
  80. package/dist/esm/interfaces/global.js +0 -0
  81. package/dist/esm/runtime/AwaitDataFetch.js +0 -131
  82. package/dist/esm/runtime/createRemoteComponent.js +0 -417
  83. package/dist/esm/runtime/wrapNoSSR.js +0 -12
  84. package/dist/esm/ssr-runtime/downgrade.js +0 -150
  85. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -177
  86. package/dist/esm/utils/dataFetch.js +0 -237
  87. package/dist/esm/utils/index.js +0 -28
  88. package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +0 -68
  89. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +0 -162
  90. package/dist/esm-node/interfaces/global.js +0 -0
  91. package/dist/esm-node/runtime/AwaitDataFetch.js +0 -109
  92. package/dist/esm-node/runtime/createRemoteComponent.js +0 -291
  93. package/dist/esm-node/runtime/wrapNoSSR.js +0 -11
  94. package/dist/esm-node/ssr-runtime/downgrade.js +0 -88
  95. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -101
  96. package/dist/esm-node/utils/dataFetch.js +0 -166
  97. package/dist/esm-node/utils/index.js +0 -27
  98. package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +0 -3
  99. package/dist/types/cli/server/data-fetch-server-plugin.d.ts +0 -3
  100. package/dist/types/interfaces/global.d.ts +0 -27
  101. package/dist/types/runtime/AwaitDataFetch.d.ts +0 -19
  102. package/dist/types/runtime/createRemoteComponent.d.ts +0 -26
  103. package/dist/types/runtime/wrapNoSSR.d.ts +0 -9
  104. package/dist/types/ssr-runtime/devPlugin.d.ts +0 -2
  105. package/dist/types/ssr-runtime/downgrade.d.ts +0 -4
  106. package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +0 -7
  107. package/dist/types/utils/dataFetch.d.ts +0 -26
  108. package/dist/types/utils/index.d.ts +0 -15
  109. /package/dist/types/{logger.d.ts → cli/logger.d.ts} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/modern-js",
3
- "version": "0.0.0-next-20250526074234",
3
+ "version": "0.0.0-next-20250527094526",
4
4
  "files": [
5
5
  "dist/",
6
6
  "types.d.ts",
@@ -24,13 +24,9 @@
24
24
  "types": "./dist/types/runtime/index.d.ts",
25
25
  "default": "./dist/esm/runtime/index.js"
26
26
  },
27
- "./ssr-dev-plugin": {
28
- "types": "./dist/types/ssr-runtime/devPlugin.d.ts",
29
- "default": "./dist/esm/ssr-runtime/devPlugin.js"
30
- },
31
- "./ssr-inject-data-fetch-function-plugin": {
32
- "types": "./dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts",
33
- "default": "./dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js"
27
+ "./ssr-runtime": {
28
+ "types": "./dist/types/ssr-runtime/index.d.ts",
29
+ "default": "./dist/esm/ssr-runtime/index.js"
34
30
  },
35
31
  "./config-plugin": {
36
32
  "types": "./dist/types/cli/configPlugin.d.ts",
@@ -52,19 +48,14 @@
52
48
  "import": "./dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js",
53
49
  "require": "./dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js"
54
50
  },
55
- "./auto-fetch-data": {
56
- "types": "./dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts",
57
- "import": "./dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js",
58
- "require": "./dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js"
59
- },
60
51
  "./inject-node-fetch": {
61
52
  "types": "./dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts",
62
53
  "import": "./dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js",
63
54
  "require": "./dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js"
64
55
  },
65
- "./data-fetch-server-plugin": {
66
- "types": "./dist/types/cli/server/data-fetch-server-plugin.d.ts",
67
- "default": "./dist/cjs/cli/server/data-fetch-server-plugin.js"
56
+ "./server": {
57
+ "types": "./dist/types/server/index.d.ts",
58
+ "default": "./dist/cjs/server/index.js"
68
59
  }
69
60
  },
70
61
  "typesVersions": {
@@ -87,17 +78,11 @@
87
78
  "resolve-entry-ipv4": [
88
79
  "./dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts"
89
80
  ],
90
- "auto-fetch-data": [
91
- "./dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts"
92
- ],
93
81
  "inject-node-fetch": [
94
82
  "./dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts"
95
83
  ],
96
- "data-fetch-server-plugin": [
97
- "./dist/types/cli/server/data-fetch-server-plugin.d.ts"
98
- ],
99
- "ssr-inject-data-fetch-function-plugin": [
100
- "./dist/types/cli/server/data-fetch-server-plugin.d.ts"
84
+ "server": [
85
+ "./dist/types/server/index.d.ts"
101
86
  ]
102
87
  }
103
88
  },
@@ -106,27 +91,26 @@
106
91
  "author": "hanric <hanric.zhang@gmail.com>",
107
92
  "license": "MIT",
108
93
  "dependencies": {
109
- "@modern-js/node-bundle-require": "2.67.5",
110
- "@modern-js/utils": "2.67.5",
94
+ "@modern-js/node-bundle-require": "2.67.6",
95
+ "fs-extra": "11.3.0",
96
+ "lru-cache": "10.4.3",
111
97
  "@swc/helpers": "0.5.13",
112
98
  "node-fetch": "~3.3.0",
113
99
  "react-error-boundary": "4.1.2",
114
- "@module-federation/rsbuild-plugin": "0.0.0-next-20250526074234",
115
- "@module-federation/enhanced": "0.0.0-next-20250526074234",
116
- "@module-federation/runtime": "0.0.0-next-20250526074234",
117
- "@module-federation/node": "0.0.0-next-20250526074234",
118
- "@module-federation/sdk": "0.0.0-next-20250526074234",
119
- "@module-federation/cli": "0.0.0-next-20250526074234"
100
+ "@module-federation/rsbuild-plugin": "0.0.0-next-20250527094526",
101
+ "@module-federation/enhanced": "0.0.0-next-20250527094526",
102
+ "@module-federation/node": "0.0.0-next-20250527094526",
103
+ "@module-federation/sdk": "0.0.0-next-20250527094526",
104
+ "@module-federation/cli": "0.0.0-next-20250527094526"
120
105
  },
121
106
  "devDependencies": {
122
107
  "@rsbuild/core": "1.2.8",
123
- "@modern-js/app-tools": "2.67.5",
124
- "@modern-js/core": "2.67.5",
125
- "@modern-js/module-tools": "2.67.5",
126
- "@modern-js/runtime": "2.67.5",
127
- "@modern-js/tsconfig": "2.67.5",
128
- "@modern-js/server-runtime": "2.67.5",
129
- "@module-federation/manifest": "0.0.0-next-20250526074234"
108
+ "@modern-js/app-tools": "2.67.6",
109
+ "@modern-js/server-runtime": "2.67.6",
110
+ "@modern-js/module-tools": "2.67.6",
111
+ "@modern-js/runtime": "2.67.6",
112
+ "@modern-js/tsconfig": "2.67.6",
113
+ "@module-federation/manifest": "0.0.0-next-20250527094526"
130
114
  },
131
115
  "peerDependencies": {
132
116
  "react": ">=17",
@@ -1,98 +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 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;
@@ -1,192 +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
- console.log("fetch data from server, error: ", e);
83
- console.error(e);
84
- return next();
85
- }
86
- if (!dataFetchId) {
87
- return next();
88
- }
89
- import_logger.default.log("fetch data from server, dataFetchId: ", dataFetchId);
90
- console.log("fetch data from server, moduleInfo: ", (_globalThis___FEDERATION__ = globalThis.__FEDERATION__) === null || _globalThis___FEDERATION__ === void 0 ? void 0 : _globalThis___FEDERATION__.moduleInfo);
91
- try {
92
- var _dataFetchMap_dataFetchId, _dataFetchMap_dataFetchId1;
93
- const dataFetchMap = (0, import_utils.getDataFetchMap)();
94
- if (!dataFetchMap) {
95
- (0, import_dataFetch.initDataFetchMap)();
96
- }
97
- const fetchDataPromise = (_dataFetchMap_dataFetchId = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId === void 0 ? void 0 : _dataFetchMap_dataFetchId[1];
98
- console.log("fetch data from server, fetchDataPromise: ", fetchDataPromise);
99
- if (fetchDataPromise && ((_dataFetchMap_dataFetchId1 = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId1 === void 0 ? void 0 : _dataFetchMap_dataFetchId1[2]) !== import_constant.MF_DATA_FETCH_STATUS.ERROR) {
100
- import_logger.default.log("fetch data from server, fetchDataPromise: ", fetchDataPromise);
101
- const targetPromise = fetchDataPromise[0];
102
- const wrappedPromise = wrapSetTimeout(targetPromise, 2e4, dataFetchId);
103
- if (wrappedPromise) {
104
- const res = await wrappedPromise;
105
- import_logger.default.log("fetch data from server, fetchDataPromise res: ", res);
106
- return ctx.json(res);
107
- }
108
- import_logger.default.error(`Expected a Promise from fetchDataPromise[0] for dataFetchId ${dataFetchId}, but received:`, targetPromise, "Will try call new dataFetch again...");
109
- }
110
- if (remoteInfo) {
111
- try {
112
- const hostInstance2 = globalThis.__FEDERATION__.__INSTANCES__[0];
113
- const remoteEntry = `${addProtocol(remoteInfo.ssrPublicPath) + remoteInfo.ssrRemoteEntry}`;
114
- if (!hostInstance2) {
115
- throw new Error("host instance not found!");
116
- }
117
- const remote = hostInstance2.options.remotes.find((remote2) => remote2.name === remoteInfo.name);
118
- import_logger.default.debug("find remote: ", JSON.stringify(remote));
119
- if (!remote) {
120
- hostInstance2.registerRemotes([
121
- {
122
- name: remoteInfo.name,
123
- entry: remoteEntry,
124
- entryGlobalName: remoteInfo.globalName
125
- }
126
- ]);
127
- } else if (!("entry" in remote) || !remote.entry.includes(import_sdk.MANIFEST_EXT)) {
128
- const { hostGlobalSnapshot, remoteSnapshot } = hostInstance2.snapshotHandler.getGlobalRemoteInfo(remoteInfo);
129
- import_logger.default.debug("find hostGlobalSnapshot: ", JSON.stringify(hostGlobalSnapshot));
130
- import_logger.default.debug("find remoteSnapshot: ", JSON.stringify(remoteSnapshot));
131
- if (!hostGlobalSnapshot || !remoteSnapshot) {
132
- if ("version" in remote) {
133
- delete remote.version;
134
- }
135
- remote.entry = remoteEntry;
136
- remote.entryGlobalName = remoteInfo.globalName;
137
- }
138
- }
139
- } catch (e) {
140
- ctx.status(500);
141
- return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
142
- ${e}`);
143
- }
144
- }
145
- const dataFetchItem = dataFetchMap[dataFetchId];
146
- console.log("fetch data from server, dataFetchItem: ", dataFetchItem);
147
- if (dataFetchItem) {
148
- import_logger.default.log("fetch data from server, dataFetchItem: ", dataFetchItem);
149
- const callFetchDataPromise = (0, import_dataFetch.fetchData)(dataFetchId, {
150
- ...params,
151
- isDowngrade: !remoteInfo
152
- });
153
- const wrappedPromise = wrapSetTimeout(callFetchDataPromise, 2e4, dataFetchId);
154
- if (wrappedPromise) {
155
- const res = await wrappedPromise;
156
- import_logger.default.log("fetch data from server, dataFetchItem res: ", res);
157
- return ctx.json(res);
158
- }
159
- }
160
- const remoteId = dataFetchId.split(import_sdk.SEPARATOR)[0];
161
- const hostInstance = globalThis.__FEDERATION__.__INSTANCES__[0];
162
- if (!hostInstance) {
163
- throw new Error("host instance not found!");
164
- }
165
- const dataFetchFn = await (0, import_dataFetch.loadDataFetchModule)(hostInstance, remoteId);
166
- const data = await dataFetchFn({
167
- ...params,
168
- isDowngrade: !remoteInfo
169
- });
170
- import_logger.default.log("fetch data from server, loadDataFetchModule res: ", data);
171
- return ctx.json(data);
172
- } catch (e) {
173
- console.log("server plugin data fetch error: ", e);
174
- ctx.status(500);
175
- return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
176
- ${e}`);
177
- }
178
- };
179
- const dataFetchServePlugin = () => ({
180
- name: "mf-data-fetch-server-plugin",
181
- setup: (api) => {
182
- api.onPrepare(() => {
183
- const { middlewares } = api.getServerContext();
184
- middlewares.push({
185
- name: "module-federation-serve-manifest",
186
- // @ts-ignore type error
187
- handler: middleware
188
- });
189
- });
190
- }
191
- });
192
- var data_fetch_server_plugin_default = dataFetchServePlugin;
@@ -1,144 +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 AwaitDataFetch_exports = {};
30
- __export(AwaitDataFetch_exports, {
31
- AwaitDataFetch: () => AwaitDataFetch,
32
- transformError: () => transformError
33
- });
34
- module.exports = __toCommonJS(AwaitDataFetch_exports);
35
- var import_jsx_runtime = require("react/jsx-runtime");
36
- var import_react = require("react");
37
- var import_logger = __toESM(require("../logger"));
38
- var import_constant = require("../constant");
39
- var import_utils = require("../utils");
40
- function isPromise(obj) {
41
- return !!obj && (typeof obj === "object" || typeof obj === "function") && typeof obj.then === "function";
42
- }
43
- const AWAIT_ERROR_PREFIX = "<Await /> caught the following error during render: ";
44
- const transformError = (err) => {
45
- const errMsg = err instanceof Error ? err.message : err;
46
- const originalMsg = errMsg.replace(AWAIT_ERROR_PREFIX, "");
47
- const dataFetchMapKey = (0, import_utils.getDataFetchIdWithErrorMsgs)(originalMsg);
48
- if (originalMsg.indexOf(import_constant.DATA_FETCH_ERROR_PREFIX) === 0) {
49
- return {
50
- error: new Error(originalMsg.replace(import_constant.DATA_FETCH_ERROR_PREFIX, "").replace((0, import_utils.wrapDataFetchId)(dataFetchMapKey), "")),
51
- errorType: import_constant.ERROR_TYPE.DATA_FETCH,
52
- dataFetchMapKey
53
- };
54
- }
55
- if (originalMsg.indexOf(import_constant.LOAD_REMOTE_ERROR_PREFIX) === 0) {
56
- return {
57
- error: new Error(originalMsg.replace(import_constant.LOAD_REMOTE_ERROR_PREFIX, "").replace((0, import_utils.wrapDataFetchId)(dataFetchMapKey), "")),
58
- errorType: import_constant.ERROR_TYPE.LOAD_REMOTE,
59
- dataFetchMapKey
60
- };
61
- }
62
- return {
63
- error: new Error(originalMsg.replace((0, import_utils.wrapDataFetchId)(dataFetchMapKey), "")),
64
- errorType: import_constant.ERROR_TYPE.UNKNOWN,
65
- dataFetchMapKey
66
- };
67
- };
68
- const DefaultLoading = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {});
69
- const DefaultErrorElement = (_data) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
70
- children: "Error"
71
- });
72
- function AwaitDataFetch({ resolve, loading = DefaultLoading, errorElement = DefaultErrorElement, children, params }) {
73
- const dataRef = (0, import_react.useRef)();
74
- const data = dataRef.current || resolve;
75
- const getData = isPromise(data) ? fetchData(data, dataRef) : () => data;
76
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AwaitSuspense, {
77
- params,
78
- loading,
79
- errorElement,
80
- // @ts-ignore
81
- resolve: getData,
82
- children
83
- });
84
- }
85
- function AwaitSuspense({ resolve, children, loading = DefaultLoading, errorElement = DefaultErrorElement }) {
86
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react.Suspense, {
87
- fallback: loading,
88
- children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ResolveAwait, {
89
- resolve,
90
- errorElement,
91
- children
92
- })
93
- });
94
- }
95
- function ResolveAwait({ children, resolve, errorElement, params }) {
96
- const data = resolve();
97
- import_logger.default.debug("resolve data: ", data);
98
- if (typeof data === "string" && data.indexOf(AWAIT_ERROR_PREFIX) === 0) {
99
- const transformedError = transformError(data);
100
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
101
- children: typeof errorElement === "function" ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
102
- children: [
103
- globalThis.FEDERATION_SSR && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", {
104
- suppressHydrationWarning: true,
105
- dangerouslySetInnerHTML: {
106
- __html: String.raw`
107
- globalThis['${import_constant.DATA_FETCH_FUNCTION}'] = globalThis['${import_constant.DATA_FETCH_FUNCTION}'] || []
108
- globalThis['${import_constant.DATA_FETCH_FUNCTION}'].push([${transformedError.dataFetchMapKey ? `'${transformedError.dataFetchMapKey}'` : ""},${params ? JSON.stringify(params) : null},true]);`
109
- }
110
- }),
111
- errorElement(transformedError)
112
- ]
113
- }) : errorElement
114
- });
115
- }
116
- const toRender = typeof children === "function" ? children(data) : children;
117
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
118
- children: toRender
119
- });
120
- }
121
- const fetchData = (promise, ref) => {
122
- let data;
123
- let status = "pending";
124
- const suspender = promise.then((res) => {
125
- status = "success";
126
- data = res;
127
- ref.current = res;
128
- }).catch((e) => {
129
- status = "success";
130
- console.warn(e);
131
- data = AWAIT_ERROR_PREFIX + e;
132
- });
133
- return () => {
134
- if (status === "pending") {
135
- throw suspender;
136
- }
137
- return data;
138
- };
139
- };
140
- // Annotate the CommonJS export names for ESM import in node:
141
- 0 && (module.exports = {
142
- AwaitDataFetch,
143
- transformError
144
- });