@module-federation/modern-js 0.0.0-next-20250526025159 → 0.0.0-next-20250526035905

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 (82) 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/{logger.js → cli/logger.js} +1 -1
  5. package/dist/cjs/cli/ssrPlugin.js +11 -27
  6. package/dist/cjs/cli/utils.js +0 -5
  7. package/dist/cjs/constant.js +2 -49
  8. package/dist/cjs/runtime/createRemoteSSRComponent.js +210 -0
  9. package/dist/cjs/runtime/index.js +2 -11
  10. package/dist/cjs/{interfaces/global.js → ssr-runtime/index.js} +8 -2
  11. package/dist/cjs/ssr-runtime/{devPlugin.js → plugin.js} +6 -9
  12. package/dist/esm/cli/configPlugin.js +7 -9
  13. package/dist/esm/cli/configPlugin.spec.js +1 -3
  14. package/dist/esm/cli/constant.js +4 -0
  15. package/dist/esm/{logger.js → cli/logger.js} +1 -1
  16. package/dist/esm/cli/ssrPlugin.js +7 -24
  17. package/dist/esm/cli/utils.js +0 -4
  18. package/dist/esm/constant.js +1 -37
  19. package/dist/esm/runtime/createRemoteSSRComponent.js +263 -0
  20. package/dist/esm/runtime/index.js +2 -10
  21. package/dist/esm/ssr-runtime/index.js +1 -0
  22. package/dist/esm/ssr-runtime/{devPlugin.js → plugin.js} +2 -5
  23. package/dist/esm-node/cli/configPlugin.js +7 -9
  24. package/dist/esm-node/cli/configPlugin.spec.js +1 -3
  25. package/dist/esm-node/cli/constant.js +4 -0
  26. package/dist/esm-node/{logger.js → cli/logger.js} +1 -1
  27. package/dist/esm-node/cli/ssrPlugin.js +7 -23
  28. package/dist/esm-node/cli/utils.js +0 -4
  29. package/dist/esm-node/constant.js +1 -37
  30. package/dist/esm-node/runtime/createRemoteSSRComponent.js +175 -0
  31. package/dist/esm-node/runtime/index.js +2 -10
  32. package/dist/esm-node/ssr-runtime/index.js +1 -0
  33. package/dist/esm-node/ssr-runtime/{devPlugin.js → plugin.js} +2 -5
  34. package/dist/types/cli/constant.d.ts +1 -0
  35. package/dist/types/cli/index.d.ts +0 -1
  36. package/dist/types/cli/utils.d.ts +0 -1
  37. package/dist/types/constant.d.ts +0 -23
  38. package/dist/types/runtime/createRemoteSSRComponent.d.ts +19 -0
  39. package/dist/types/runtime/index.d.ts +2 -6
  40. package/dist/types/ssr-runtime/index.d.ts +1 -0
  41. package/dist/types/ssr-runtime/plugin.d.ts +2 -0
  42. package/package.json +17 -41
  43. package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +0 -98
  44. package/dist/cjs/cli/server/data-fetch-server-plugin.js +0 -192
  45. package/dist/cjs/runtime/AwaitDataFetch.js +0 -144
  46. package/dist/cjs/runtime/createRemoteComponent.js +0 -327
  47. package/dist/cjs/ssr-runtime/downgrade.js +0 -108
  48. package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -128
  49. package/dist/cjs/utils/dataFetch.js +0 -211
  50. package/dist/cjs/utils/index.js +0 -54
  51. package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +0 -74
  52. package/dist/esm/cli/server/data-fetch-server-plugin.js +0 -240
  53. package/dist/esm/interfaces/global.js +0 -0
  54. package/dist/esm/runtime/AwaitDataFetch.js +0 -131
  55. package/dist/esm/runtime/createRemoteComponent.js +0 -417
  56. package/dist/esm/runtime/wrapNoSSR.js +0 -12
  57. package/dist/esm/ssr-runtime/downgrade.js +0 -143
  58. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -168
  59. package/dist/esm/utils/dataFetch.js +0 -237
  60. package/dist/esm/utils/index.js +0 -28
  61. package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +0 -68
  62. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +0 -162
  63. package/dist/esm-node/interfaces/global.js +0 -0
  64. package/dist/esm-node/runtime/AwaitDataFetch.js +0 -109
  65. package/dist/esm-node/runtime/createRemoteComponent.js +0 -291
  66. package/dist/esm-node/runtime/wrapNoSSR.js +0 -11
  67. package/dist/esm-node/ssr-runtime/downgrade.js +0 -82
  68. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -94
  69. package/dist/esm-node/utils/dataFetch.js +0 -166
  70. package/dist/esm-node/utils/index.js +0 -27
  71. package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +0 -3
  72. package/dist/types/cli/server/data-fetch-server-plugin.d.ts +0 -3
  73. package/dist/types/interfaces/global.d.ts +0 -27
  74. package/dist/types/runtime/AwaitDataFetch.d.ts +0 -19
  75. package/dist/types/runtime/createRemoteComponent.d.ts +0 -26
  76. package/dist/types/runtime/wrapNoSSR.d.ts +0 -9
  77. package/dist/types/ssr-runtime/devPlugin.d.ts +0 -2
  78. package/dist/types/ssr-runtime/downgrade.d.ts +0 -4
  79. package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +0 -2
  80. package/dist/types/utils/dataFetch.d.ts +0 -26
  81. package/dist/types/utils/index.d.ts +0 -15
  82. /package/dist/types/{logger.d.ts → cli/logger.d.ts} +0 -0
@@ -1,168 +0,0 @@
1
- import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
- import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
3
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
- import { DATA_FETCH_FUNCTION, DOWNGRADE_KEY, FS_HREF, MF_DATA_FETCH_STATUS, MF_DATA_FETCH_TYPE } from "../constant";
5
- import logger from "../logger";
6
- import { getDataFetchMap, getDataFetchItem, initDataFetchMap } from "../utils";
7
- import { callAllDowngrade, callDowngrade, getDowngradeTag } from "./downgrade";
8
- var injectDataFetchFunctionPlugin = function() {
9
- return {
10
- name: "@module-federation/inject-data-fetch-function-plugin",
11
- setup: function(api) {
12
- api.onBeforeRender(/* @__PURE__ */ _async_to_generator(function() {
13
- var _globalThis, _DATA_FETCH_FUNCTION, dataFetchFunction, dataFetch;
14
- return _ts_generator(this, function(_state) {
15
- switch (_state.label) {
16
- case 0:
17
- globalThis.FEDERATION_SSR = true;
18
- if (typeof window === "undefined") {
19
- return [
20
- 2
21
- ];
22
- }
23
- dataFetchFunction = function() {
24
- var _ref = _async_to_generator(function(options) {
25
- var _options, id, data, downgrade, dataFetchItem, _dataFetchItem__, _dataFetchItem_, dataFetchMap, res, rej, p, mfDowngrade, mfDowngrade1;
26
- return _ts_generator(this, function(_state2) {
27
- _options = _sliced_to_array(options, 3), id = _options[0], data = _options[1], downgrade = _options[2];
28
- logger.debug("==========call data fetch function!");
29
- if (data) {
30
- if (!id) {
31
- throw new Error("id is required!");
32
- }
33
- if (!getDataFetchMap()) {
34
- initDataFetchMap();
35
- }
36
- dataFetchItem = getDataFetchItem(id);
37
- if (dataFetchItem) {
38
- ;
39
- (_dataFetchItem_ = dataFetchItem[1]) === null || _dataFetchItem_ === void 0 ? void 0 : (_dataFetchItem__ = _dataFetchItem_[1]) === null || _dataFetchItem__ === void 0 ? void 0 : _dataFetchItem__.call(_dataFetchItem_, data);
40
- dataFetchItem[2] = MF_DATA_FETCH_STATUS.LOADED;
41
- return [
42
- 2
43
- ];
44
- }
45
- if (!dataFetchItem) {
46
- dataFetchMap = getDataFetchMap();
47
- ;
48
- ;
49
- p = new Promise(function(resolve, reject) {
50
- res = resolve;
51
- rej = reject;
52
- });
53
- dataFetchMap[id] = [
54
- [
55
- /* @__PURE__ */ _async_to_generator(function() {
56
- return _ts_generator(this, function(_state3) {
57
- return [
58
- 2,
59
- _async_to_generator(function() {
60
- return _ts_generator(this, function(_state4) {
61
- return [
62
- 2,
63
- ""
64
- ];
65
- });
66
- })
67
- ];
68
- });
69
- }),
70
- MF_DATA_FETCH_TYPE.FETCH_SERVER
71
- ],
72
- [
73
- p,
74
- res,
75
- rej
76
- ],
77
- MF_DATA_FETCH_STATUS.LOADED
78
- ];
79
- res(data);
80
- return [
81
- 2
82
- ];
83
- }
84
- }
85
- if (downgrade) {
86
- mfDowngrade = getDowngradeTag();
87
- if (!mfDowngrade) {
88
- globalThis[DOWNGRADE_KEY] = id ? [
89
- id
90
- ] : true;
91
- } else if (Array.isArray(mfDowngrade) && id && !mfDowngrade.includes(id)) {
92
- mfDowngrade.push(id);
93
- }
94
- }
95
- mfDowngrade1 = getDowngradeTag();
96
- if (typeof mfDowngrade1 === "boolean") {
97
- return [
98
- 2,
99
- callAllDowngrade()
100
- ];
101
- }
102
- if (Array.isArray(mfDowngrade1)) {
103
- if (!id) {
104
- globalThis[DOWNGRADE_KEY] = true;
105
- return [
106
- 2,
107
- callAllDowngrade()
108
- ];
109
- }
110
- if (!mfDowngrade1.includes(id)) {
111
- mfDowngrade1.push(id);
112
- }
113
- return [
114
- 2,
115
- callDowngrade(id)
116
- ];
117
- }
118
- return [
119
- 2
120
- ];
121
- });
122
- });
123
- return function dataFetchFunction2(options) {
124
- return _ref.apply(this, arguments);
125
- };
126
- }();
127
- globalThis[FS_HREF] = window.location.href;
128
- (_globalThis = globalThis)[_DATA_FETCH_FUNCTION = DATA_FETCH_FUNCTION] || (_globalThis[_DATA_FETCH_FUNCTION] = []);
129
- dataFetch = globalThis[DATA_FETCH_FUNCTION];
130
- return [
131
- 4,
132
- Promise.all(dataFetch.map(function() {
133
- var _ref = _async_to_generator(function(options) {
134
- return _ts_generator(this, function(_state2) {
135
- switch (_state2.label) {
136
- case 0:
137
- return [
138
- 4,
139
- dataFetchFunction(options)
140
- ];
141
- case 1:
142
- _state2.sent();
143
- return [
144
- 2
145
- ];
146
- }
147
- });
148
- });
149
- return function(options) {
150
- return _ref.apply(this, arguments);
151
- };
152
- }()))
153
- ];
154
- case 1:
155
- _state.sent();
156
- dataFetch.push = dataFetchFunction;
157
- return [
158
- 2
159
- ];
160
- }
161
- });
162
- }));
163
- }
164
- };
165
- };
166
- export {
167
- injectDataFetchFunctionPlugin
168
- };
@@ -1,237 +0,0 @@
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_array } from "@swc/helpers/_/_to_array";
5
- import { _ as _type_of } from "@swc/helpers/_/_type_of";
6
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
7
- import { isBrowserEnv, composeKeyWithSeparator } from "@module-federation/sdk";
8
- import { isCSROnly } from "../utils";
9
- import logger from "../logger";
10
- import { callDowngrade, getDowngradeTag } from "../ssr-runtime/downgrade";
11
- import { DOWNGRADE_KEY, MF_DATA_FETCH_STATUS, WRAP_DATA_FETCH_ID_IDENTIFIER } from "../constant";
12
- import { DATA_FETCH_CLIENT_SUFFIX, DATA_FETCH_IDENTIFIER } from "@module-federation/rsbuild-plugin/constant";
13
- var getDataFetchInfo = function(param) {
14
- var name = param.name, alias = param.alias, id = param.id, remoteSnapshot = param.remoteSnapshot;
15
- if (!remoteSnapshot) {
16
- return;
17
- }
18
- if (!("modules" in remoteSnapshot)) {
19
- return;
20
- }
21
- var regex = new RegExp("^".concat(name, "(/[^/].*|)$"));
22
- var nameOrAlias = regex.test(id) ? name : alias || name;
23
- var expose = id.replace(nameOrAlias, "");
24
- var dataFetchName = "";
25
- var dataFetchId = "";
26
- if (expose.startsWith("/")) {
27
- dataFetchName = "".concat(expose.slice(1), ".").concat(DATA_FETCH_IDENTIFIER);
28
- dataFetchId = "".concat(id, ".").concat(DATA_FETCH_IDENTIFIER);
29
- } else if (expose === "") {
30
- dataFetchName = DATA_FETCH_IDENTIFIER;
31
- dataFetchId = "".concat(id, "/").concat(DATA_FETCH_IDENTIFIER);
32
- } else {
33
- return;
34
- }
35
- if (!dataFetchName || !dataFetchId) {
36
- return;
37
- }
38
- if (!remoteSnapshot.modules.find(function(module) {
39
- return module.moduleName === dataFetchName;
40
- })) {
41
- return;
42
- }
43
- return {
44
- dataFetchName,
45
- dataFetchId
46
- };
47
- };
48
- function initDataFetchMap() {
49
- var _globalThis;
50
- (_globalThis = globalThis).__MF_DATA_FETCH_MAP__ || (_globalThis.__MF_DATA_FETCH_MAP__ = {});
51
- }
52
- function getDataFetchItem(id) {
53
- var _globalThis___MF_DATA_FETCH_MAP__;
54
- return (_globalThis___MF_DATA_FETCH_MAP__ = globalThis.__MF_DATA_FETCH_MAP__) === null || _globalThis___MF_DATA_FETCH_MAP__ === void 0 ? void 0 : _globalThis___MF_DATA_FETCH_MAP__[id];
55
- }
56
- function getDataFetchMap() {
57
- return globalThis.__MF_DATA_FETCH_MAP__;
58
- }
59
- var flushDataFetch = function() {
60
- globalThis.__MF_DATA_FETCH_MAP__ = {};
61
- globalThis[DOWNGRADE_KEY] = void 0;
62
- };
63
- function setDataFetchItemLoadedStatus(id) {
64
- var dataFetchItem = getDataFetchItem(id);
65
- if (!dataFetchItem) {
66
- return;
67
- }
68
- dataFetchItem[2] = MF_DATA_FETCH_STATUS.LOADED;
69
- }
70
- var wrapDataFetchId = function(id) {
71
- return "".concat(WRAP_DATA_FETCH_ID_IDENTIFIER).concat(id).concat(WRAP_DATA_FETCH_ID_IDENTIFIER);
72
- };
73
- var getDataFetchIdWithErrorMsgs = function(errMsgs) {
74
- var firstIdentifierIndex = errMsgs.indexOf(WRAP_DATA_FETCH_ID_IDENTIFIER);
75
- if (firstIdentifierIndex === -1) {
76
- return void 0;
77
- }
78
- var secondIdentifierIndex = errMsgs.indexOf(WRAP_DATA_FETCH_ID_IDENTIFIER, firstIdentifierIndex + WRAP_DATA_FETCH_ID_IDENTIFIER.length);
79
- if (secondIdentifierIndex === -1) {
80
- return void 0;
81
- }
82
- return errMsgs.substring(firstIdentifierIndex + WRAP_DATA_FETCH_ID_IDENTIFIER.length, secondIdentifierIndex);
83
- };
84
- function fetchData(id, params, remoteInfo) {
85
- return _fetchData.apply(this, arguments);
86
- }
87
- function _fetchData() {
88
- _fetchData = _async_to_generator(function(id, params, remoteInfo) {
89
- var callFetchData, _dataFetchItem_, dataFetchItem, mfDowngrade, res, rej, p;
90
- return _ts_generator(this, function(_state) {
91
- callFetchData = function() {
92
- var _ref = _async_to_generator(function() {
93
- var item, _item, fetchDataFnArr, _rest, fetchDataFn;
94
- return _ts_generator(this, function(_state2) {
95
- switch (_state2.label) {
96
- case 0:
97
- item = getDataFetchItem(id);
98
- if (!item) {
99
- return [
100
- 2
101
- ];
102
- }
103
- _item = _to_array(item), fetchDataFnArr = _item[0], _rest = _item.slice(1);
104
- return [
105
- 4,
106
- fetchDataFnArr[2]
107
- ];
108
- case 1:
109
- fetchDataFn = _state2.sent();
110
- if (!fetchDataFn) {
111
- return [
112
- 2
113
- ];
114
- }
115
- return [
116
- 2,
117
- fetchDataFn(params)
118
- ];
119
- }
120
- });
121
- });
122
- return function callFetchData2() {
123
- return _ref.apply(this, arguments);
124
- };
125
- }();
126
- if (isBrowserEnv()) {
127
- ;
128
- dataFetchItem = getDataFetchItem(id);
129
- if (!dataFetchItem) {
130
- throw new Error("dataFetchItem not found, id: ".concat(id));
131
- }
132
- if ((_dataFetchItem_ = dataFetchItem[1]) === null || _dataFetchItem_ === void 0 ? void 0 : _dataFetchItem_[0]) {
133
- return [
134
- 2,
135
- dataFetchItem[1][0]
136
- ];
137
- }
138
- if (isCSROnly()) {
139
- logger.debug("==========csr only!");
140
- return [
141
- 2,
142
- callFetchData()
143
- ];
144
- }
145
- if (remoteInfo) {
146
- return [
147
- 2,
148
- callDowngrade(id, params, remoteInfo)
149
- ];
150
- }
151
- mfDowngrade = getDowngradeTag();
152
- if (mfDowngrade) {
153
- if (typeof mfDowngrade === "boolean") {
154
- return [
155
- 2,
156
- callDowngrade(id, _object_spread_props(_object_spread({}, params), {
157
- isDowngrade: true
158
- }))
159
- ];
160
- }
161
- if (mfDowngrade.includes(id)) {
162
- return [
163
- 2,
164
- callDowngrade(id, _object_spread_props(_object_spread({}, params), {
165
- isDowngrade: true
166
- }))
167
- ];
168
- }
169
- }
170
- ;
171
- ;
172
- p = new Promise(function(resolve, reject) {
173
- res = resolve;
174
- rej = reject;
175
- });
176
- dataFetchItem[1] = [
177
- p,
178
- res,
179
- rej
180
- ];
181
- dataFetchItem[2] = MF_DATA_FETCH_STATUS.AWAIT;
182
- return [
183
- 2,
184
- dataFetchItem[1][0]
185
- ];
186
- }
187
- return [
188
- 2,
189
- callFetchData()
190
- ];
191
- });
192
- });
193
- return _fetchData.apply(this, arguments);
194
- }
195
- function getDataFetchMapKey(dataFetchInfo, hostInfo) {
196
- if (!dataFetchInfo || !hostInfo) {
197
- return;
198
- }
199
- var dataFetchId = dataFetchInfo.dataFetchId;
200
- return composeKeyWithSeparator(dataFetchId, hostInfo.name, hostInfo.version);
201
- }
202
- function loadDataFetchModule(instance, id) {
203
- return _loadDataFetchModule.apply(this, arguments);
204
- }
205
- function _loadDataFetchModule() {
206
- _loadDataFetchModule = _async_to_generator(function(instance, id) {
207
- return _ts_generator(this, function(_state) {
208
- return [
209
- 2,
210
- instance.loadRemote(id).then(function(m) {
211
- if (m && (typeof m === "undefined" ? "undefined" : _type_of(m)) === "object" && "fetchData" in m && typeof m.fetchData === "function") {
212
- return m.fetchData;
213
- }
214
- throw new Error("fetchData not found in remote ".concat(id, ", ").concat(JSON.stringify(m)));
215
- })
216
- ];
217
- });
218
- });
219
- return _loadDataFetchModule.apply(this, arguments);
220
- }
221
- function isDataLoaderExpose(exposeKey) {
222
- return exposeKey.endsWith(DATA_FETCH_IDENTIFIER) || exposeKey.endsWith(DATA_FETCH_CLIENT_SUFFIX);
223
- }
224
- export {
225
- fetchData,
226
- flushDataFetch,
227
- getDataFetchIdWithErrorMsgs,
228
- getDataFetchInfo,
229
- getDataFetchItem,
230
- getDataFetchMap,
231
- getDataFetchMapKey,
232
- initDataFetchMap,
233
- isDataLoaderExpose,
234
- loadDataFetchModule,
235
- setDataFetchItemLoadedStatus,
236
- wrapDataFetchId
237
- };
@@ -1,28 +0,0 @@
1
- import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
- import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
- function getLoadedRemoteInfos(id, instance) {
4
- if (!instance) {
5
- return;
6
- }
7
- var _ref = instance.remoteHandler.idToRemoteMap[id] || {}, name = _ref.name, expose = _ref.expose;
8
- if (!name) {
9
- return;
10
- }
11
- var module = instance.moduleCache.get(name);
12
- if (!module) {
13
- return;
14
- }
15
- var remoteSnapshot = instance.snapshotHandler.getGlobalRemoteInfo(module.remoteInfo).remoteSnapshot;
16
- return _object_spread_props(_object_spread({}, module.remoteInfo), {
17
- snapshot: remoteSnapshot,
18
- expose
19
- });
20
- }
21
- function isCSROnly() {
22
- return window._SSR_DATA === void 0;
23
- }
24
- export * from "./dataFetch";
25
- export {
26
- getLoadedRemoteInfos,
27
- isCSROnly
28
- };
@@ -1,68 +0,0 @@
1
- import { getDataFetchInfo, initDataFetchMap, getDataFetchItem, getDataFetchMap, isCSROnly } from "../../utils";
2
- import logger from "../../logger";
3
- import { getDataFetchMapKey, isDataLoaderExpose, loadDataFetchModule } from "../../utils/dataFetch";
4
- import { MF_DATA_FETCH_TYPE, MF_DATA_FETCH_STATUS } from "../../constant";
5
- import { DATA_FETCH_CLIENT_SUFFIX } from "@module-federation/rsbuild-plugin/constant";
6
- const autoFetchData = () => ({
7
- name: "auto-fetch-data-plugin",
8
- beforeInit(args) {
9
- initDataFetchMap();
10
- return args;
11
- },
12
- afterLoadSnapshot(args) {
13
- const { id, moduleInfo, remoteSnapshot, host } = args;
14
- if (typeof id === "string" && isDataLoaderExpose(id)) {
15
- return args;
16
- }
17
- if (!remoteSnapshot || !id || !("modules" in remoteSnapshot)) {
18
- return args;
19
- }
20
- const { name, alias } = moduleInfo;
21
- const dataFetchInfo = getDataFetchInfo({
22
- name,
23
- alias,
24
- id,
25
- remoteSnapshot
26
- });
27
- if (!dataFetchInfo) {
28
- return args;
29
- }
30
- const { dataFetchId, dataFetchName } = dataFetchInfo;
31
- const dataFetchMapKey = getDataFetchMapKey(dataFetchInfo, {
32
- name: host.name,
33
- version: host.options.version
34
- });
35
- logger.debug("======= auto fetch plugin dataFetchMapKey: ", dataFetchMapKey);
36
- if (!dataFetchMapKey) {
37
- return args;
38
- }
39
- const dataFetchItem = getDataFetchItem(dataFetchMapKey);
40
- if (dataFetchItem) {
41
- return args;
42
- }
43
- const dataFetchMap = getDataFetchMap();
44
- const downgradeType = remoteSnapshot.modules.find((module) => module.moduleName === `${dataFetchName}${DATA_FETCH_CLIENT_SUFFIX}`) ? MF_DATA_FETCH_TYPE.FETCH_CLIENT : MF_DATA_FETCH_TYPE.FETCH_SERVER;
45
- let finalDataFetchId = dataFetchId;
46
- if (typeof window !== "undefined") {
47
- finalDataFetchId = downgradeType === MF_DATA_FETCH_TYPE.FETCH_CLIENT ? `${dataFetchId}${DATA_FETCH_CLIENT_SUFFIX}` : dataFetchId;
48
- }
49
- const getDataFetchGetter = () => loadDataFetchModule(host, finalDataFetchId);
50
- const dataFetchFnItem = [
51
- getDataFetchGetter,
52
- downgradeType
53
- ];
54
- if (typeof window === "undefined" || isCSROnly()) {
55
- dataFetchFnItem.push(getDataFetchGetter());
56
- }
57
- dataFetchMap[dataFetchMapKey] = [
58
- dataFetchFnItem,
59
- void 0,
60
- MF_DATA_FETCH_STATUS.AWAIT
61
- ];
62
- return args;
63
- }
64
- });
65
- var auto_fetch_data_default = autoFetchData;
66
- export {
67
- auto_fetch_data_default as default
68
- };
@@ -1,162 +0,0 @@
1
- import { DATA_FETCH_QUERY, MF_DATA_FETCH_STATUS } from "../../constant";
2
- import logger from "../../logger";
3
- import { getDataFetchMap } from "../../utils";
4
- import { fetchData, initDataFetchMap, loadDataFetchModule } from "../../utils/dataFetch";
5
- import { SEPARATOR, MANIFEST_EXT } from "@module-federation/sdk";
6
- function wrapSetTimeout(targetPromise, delay = 2e4, id) {
7
- if (targetPromise && typeof targetPromise.then === "function") {
8
- return new Promise((resolve, reject) => {
9
- const timeoutId = setTimeout(() => {
10
- logger.warn(`Data fetch for ID ${id} timed out after 20 seconds.`);
11
- reject(new Error(`Data fetch for ID ${id} timed out after 20 seconds`));
12
- }, delay);
13
- targetPromise.then((value) => {
14
- clearTimeout(timeoutId);
15
- resolve(value);
16
- }).catch((err) => {
17
- clearTimeout(timeoutId);
18
- reject(err);
19
- });
20
- });
21
- }
22
- }
23
- function addProtocol(url) {
24
- if (url.startsWith("//")) {
25
- return "https:" + url;
26
- }
27
- return url;
28
- }
29
- const getDecodeQuery = (url, name) => {
30
- const res = url.searchParams.get(name);
31
- if (!res) {
32
- return null;
33
- }
34
- return decodeURIComponent(res);
35
- };
36
- const middleware = async (ctx, next) => {
37
- var _globalThis___FEDERATION__;
38
- let url;
39
- let dataFetchId;
40
- let params;
41
- let remoteInfo;
42
- try {
43
- url = new URL(ctx.req.url);
44
- dataFetchId = getDecodeQuery(url, DATA_FETCH_QUERY);
45
- params = JSON.parse(getDecodeQuery(url, "params") || "{}");
46
- const remoteInfoQuery = getDecodeQuery(url, "remoteInfo");
47
- remoteInfo = remoteInfoQuery ? JSON.parse(remoteInfoQuery) : null;
48
- } catch (e) {
49
- console.log("fetch data from server, error: ", e);
50
- console.error(e);
51
- return next();
52
- }
53
- if (!dataFetchId) {
54
- return next();
55
- }
56
- logger.log("fetch data from server, dataFetchId: ", dataFetchId);
57
- console.log("fetch data from server, moduleInfo: ", (_globalThis___FEDERATION__ = globalThis.__FEDERATION__) === null || _globalThis___FEDERATION__ === void 0 ? void 0 : _globalThis___FEDERATION__.moduleInfo);
58
- try {
59
- var _dataFetchMap_dataFetchId, _dataFetchMap_dataFetchId1;
60
- const dataFetchMap = getDataFetchMap();
61
- if (!dataFetchMap) {
62
- initDataFetchMap();
63
- }
64
- const fetchDataPromise = (_dataFetchMap_dataFetchId = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId === void 0 ? void 0 : _dataFetchMap_dataFetchId[1];
65
- console.log("fetch data from server, fetchDataPromise: ", fetchDataPromise);
66
- if (fetchDataPromise && ((_dataFetchMap_dataFetchId1 = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId1 === void 0 ? void 0 : _dataFetchMap_dataFetchId1[2]) !== MF_DATA_FETCH_STATUS.ERROR) {
67
- logger.log("fetch data from server, fetchDataPromise: ", fetchDataPromise);
68
- const targetPromise = fetchDataPromise[0];
69
- const wrappedPromise = wrapSetTimeout(targetPromise, 2e4, dataFetchId);
70
- if (wrappedPromise) {
71
- const res = await wrappedPromise;
72
- logger.log("fetch data from server, fetchDataPromise res: ", res);
73
- return ctx.json(res);
74
- }
75
- logger.error(`Expected a Promise from fetchDataPromise[0] for dataFetchId ${dataFetchId}, but received:`, targetPromise, "Will try call new dataFetch again...");
76
- }
77
- if (remoteInfo) {
78
- try {
79
- const hostInstance2 = globalThis.__FEDERATION__.__INSTANCES__[0];
80
- const remoteEntry = `${addProtocol(remoteInfo.ssrPublicPath) + remoteInfo.ssrRemoteEntry}`;
81
- if (!hostInstance2) {
82
- throw new Error("host instance not found!");
83
- }
84
- const remote = hostInstance2.options.remotes.find((remote2) => remote2.name === remoteInfo.name);
85
- logger.debug("find remote: ", JSON.stringify(remote));
86
- if (!remote) {
87
- hostInstance2.registerRemotes([
88
- {
89
- name: remoteInfo.name,
90
- entry: remoteEntry,
91
- entryGlobalName: remoteInfo.globalName
92
- }
93
- ]);
94
- } else if (!("entry" in remote) || !remote.entry.includes(MANIFEST_EXT)) {
95
- const { hostGlobalSnapshot, remoteSnapshot } = hostInstance2.snapshotHandler.getGlobalRemoteInfo(remoteInfo);
96
- logger.debug("find hostGlobalSnapshot: ", JSON.stringify(hostGlobalSnapshot));
97
- logger.debug("find remoteSnapshot: ", JSON.stringify(remoteSnapshot));
98
- if (!hostGlobalSnapshot || !remoteSnapshot) {
99
- if ("version" in remote) {
100
- delete remote.version;
101
- }
102
- remote.entry = remoteEntry;
103
- remote.entryGlobalName = remoteInfo.globalName;
104
- }
105
- }
106
- } catch (e) {
107
- ctx.status(500);
108
- return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
109
- ${e}`);
110
- }
111
- }
112
- const dataFetchItem = dataFetchMap[dataFetchId];
113
- console.log("fetch data from server, dataFetchItem: ", dataFetchItem);
114
- if (dataFetchItem) {
115
- logger.log("fetch data from server, dataFetchItem: ", dataFetchItem);
116
- const callFetchDataPromise = fetchData(dataFetchId, {
117
- ...params,
118
- isDowngrade: true
119
- });
120
- const wrappedPromise = wrapSetTimeout(callFetchDataPromise, 2e4, dataFetchId);
121
- if (wrappedPromise) {
122
- const res = await wrappedPromise;
123
- logger.log("fetch data from server, dataFetchItem res: ", res);
124
- return ctx.json(res);
125
- }
126
- }
127
- const remoteId = dataFetchId.split(SEPARATOR)[0];
128
- const hostInstance = globalThis.__FEDERATION__.__INSTANCES__[0];
129
- if (!hostInstance) {
130
- throw new Error("host instance not found!");
131
- }
132
- const dataFetchFn = await loadDataFetchModule(hostInstance, remoteId);
133
- const data = await dataFetchFn({
134
- ...params,
135
- isDowngrade: !remoteInfo
136
- });
137
- logger.log("fetch data from server, loadDataFetchModule res: ", data);
138
- return ctx.json(data);
139
- } catch (e) {
140
- console.log("server plugin data fetch error: ", e);
141
- ctx.status(500);
142
- return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
143
- ${e}`);
144
- }
145
- };
146
- const dataFetchServePlugin = () => ({
147
- name: "mf-data-fetch-server-plugin",
148
- setup: (api) => {
149
- api.onPrepare(() => {
150
- const { middlewares } = api.getServerContext();
151
- middlewares.push({
152
- name: "module-federation-serve-manifest",
153
- // @ts-ignore type error
154
- handler: middleware
155
- });
156
- });
157
- }
158
- });
159
- var data_fetch_server_plugin_default = dataFetchServePlugin;
160
- export {
161
- data_fetch_server_plugin_default as default
162
- };
File without changes