@module-federation/modern-js 0.0.0-next-20250613153606 → 0.0.0-next-20250616091910

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 (57) hide show
  1. package/dist/cjs/cli/configPlugin.js +1 -1
  2. package/dist/cjs/cli/configPlugin.spec.js +2 -2
  3. package/dist/cjs/cli/server/data-fetch-server-plugin.js +9 -10
  4. package/dist/cjs/constant.js +2 -49
  5. package/dist/cjs/runtime/index.js +3 -20
  6. package/dist/cjs/ssr-runtime/devPlugin.js +2 -2
  7. package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +6 -101
  8. package/dist/esm/cli/configPlugin.js +1 -1
  9. package/dist/esm/cli/configPlugin.spec.js +2 -2
  10. package/dist/esm/cli/server/data-fetch-server-plugin.js +3 -4
  11. package/dist/esm/constant.js +1 -37
  12. package/dist/esm/runtime/index.js +2 -18
  13. package/dist/esm/ssr-runtime/devPlugin.js +1 -1
  14. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +5 -147
  15. package/dist/esm-node/cli/configPlugin.js +1 -1
  16. package/dist/esm-node/cli/configPlugin.spec.js +2 -2
  17. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +3 -4
  18. package/dist/esm-node/constant.js +1 -37
  19. package/dist/esm-node/runtime/index.js +2 -18
  20. package/dist/esm-node/ssr-runtime/devPlugin.js +1 -1
  21. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +5 -89
  22. package/dist/types/constant.d.ts +0 -23
  23. package/dist/types/runtime/index.d.ts +2 -9
  24. package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +0 -3
  25. package/package.json +9 -16
  26. package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +0 -100
  27. package/dist/cjs/interfaces/global.js +0 -16
  28. package/dist/cjs/runtime/AwaitDataFetch.js +0 -144
  29. package/dist/cjs/runtime/createRemoteComponent.js +0 -327
  30. package/dist/cjs/runtime/wrapNoSSR.js +0 -35
  31. package/dist/cjs/ssr-runtime/downgrade.js +0 -114
  32. package/dist/cjs/utils/dataFetch.js +0 -211
  33. package/dist/cjs/utils/index.js +0 -54
  34. package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +0 -76
  35. package/dist/esm/interfaces/global.js +0 -0
  36. package/dist/esm/runtime/AwaitDataFetch.js +0 -131
  37. package/dist/esm/runtime/createRemoteComponent.js +0 -417
  38. package/dist/esm/runtime/wrapNoSSR.js +0 -12
  39. package/dist/esm/ssr-runtime/downgrade.js +0 -150
  40. package/dist/esm/utils/dataFetch.js +0 -237
  41. package/dist/esm/utils/index.js +0 -28
  42. package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +0 -70
  43. package/dist/esm-node/interfaces/global.js +0 -0
  44. package/dist/esm-node/runtime/AwaitDataFetch.js +0 -109
  45. package/dist/esm-node/runtime/createRemoteComponent.js +0 -291
  46. package/dist/esm-node/runtime/wrapNoSSR.js +0 -11
  47. package/dist/esm-node/ssr-runtime/downgrade.js +0 -88
  48. package/dist/esm-node/utils/dataFetch.js +0 -166
  49. package/dist/esm-node/utils/index.js +0 -27
  50. package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +0 -3
  51. package/dist/types/interfaces/global.d.ts +0 -27
  52. package/dist/types/runtime/AwaitDataFetch.d.ts +0 -19
  53. package/dist/types/runtime/createRemoteComponent.d.ts +0 -26
  54. package/dist/types/runtime/wrapNoSSR.d.ts +0 -9
  55. package/dist/types/ssr-runtime/downgrade.d.ts +0 -4
  56. package/dist/types/utils/dataFetch.d.ts +0 -26
  57. package/dist/types/utils/index.d.ts +0 -15
@@ -1,150 +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 _type_of } from "@swc/helpers/_/_type_of";
5
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
6
- import { DATA_FETCH_QUERY, DOWNGRADE_KEY } from "../constant";
7
- import { MF_DATA_FETCH_STATUS, MF_DATA_FETCH_TYPE } from "../constant";
8
- import { getDataFetchMap } from "../utils";
9
- function getDowngradeTag() {
10
- return globalThis[DOWNGRADE_KEY];
11
- }
12
- function callAllDowngrade() {
13
- var dataFetchMap = getDataFetchMap();
14
- if (!dataFetchMap) {
15
- return;
16
- }
17
- Object.keys(dataFetchMap).forEach(function(key) {
18
- callDowngrade(key);
19
- });
20
- }
21
- function callDowngrade(id, params, remoteInfo) {
22
- return _callDowngrade.apply(this, arguments);
23
- }
24
- function _callDowngrade() {
25
- _callDowngrade = _async_to_generator(function(id, params, remoteInfo) {
26
- var dataFetchMap, mfDataFetch, promise, res, rej, dataFetchType, currentUrl, fetchServerQuery, fetchUrl, data, e;
27
- return _ts_generator(this, function(_state) {
28
- switch (_state.label) {
29
- case 0:
30
- dataFetchMap = getDataFetchMap();
31
- if (!dataFetchMap) {
32
- return [
33
- 2
34
- ];
35
- }
36
- mfDataFetch = dataFetchMap[id];
37
- if (!((mfDataFetch === null || mfDataFetch === void 0 ? void 0 : mfDataFetch[2]) === MF_DATA_FETCH_STATUS.AWAIT))
38
- return [
39
- 3,
40
- 6
41
- ];
42
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADING;
43
- if (mfDataFetch[1]) {
44
- promise = mfDataFetch[1][0];
45
- res = mfDataFetch[1][1];
46
- rej = mfDataFetch[1][2];
47
- } else {
48
- promise = new Promise(function(resolve, reject) {
49
- res = resolve;
50
- rej = reject;
51
- });
52
- mfDataFetch[1] = [
53
- promise,
54
- res,
55
- rej
56
- ];
57
- }
58
- dataFetchType = mfDataFetch[0][1];
59
- if (!(dataFetchType === MF_DATA_FETCH_TYPE.FETCH_CLIENT))
60
- return [
61
- 3,
62
- 1
63
- ];
64
- try {
65
- mfDataFetch[0][0]().then(function(getDataFetchFn) {
66
- return getDataFetchFn(_object_spread_props(_object_spread({}, params), {
67
- isDowngrade: true
68
- })).then(function(data2) {
69
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
70
- res(data2);
71
- });
72
- });
73
- } catch (e2) {
74
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
75
- rej(e2);
76
- }
77
- return [
78
- 3,
79
- 5
80
- ];
81
- case 1:
82
- if (!(dataFetchType === MF_DATA_FETCH_TYPE.FETCH_SERVER))
83
- return [
84
- 3,
85
- 5
86
- ];
87
- _state.label = 2;
88
- case 2:
89
- _state.trys.push([
90
- 2,
91
- 4,
92
- ,
93
- 5
94
- ]);
95
- currentUrl = new URL(window.location.href);
96
- currentUrl.searchParams.set(DATA_FETCH_QUERY, encodeURIComponent(id));
97
- if (params) {
98
- currentUrl.searchParams.set("params", encodeURIComponent(JSON.stringify(params)));
99
- }
100
- if (remoteInfo) {
101
- currentUrl.searchParams.set("remoteInfo", encodeURIComponent(JSON.stringify(remoteInfo)));
102
- }
103
- fetchServerQuery = globalThis.FEDERATION_SERVER_QUERY;
104
- if (fetchServerQuery && (typeof fetchServerQuery === "undefined" ? "undefined" : _type_of(fetchServerQuery)) === "object") {
105
- Object.keys(fetchServerQuery).forEach(function(key) {
106
- currentUrl.searchParams.set(key, JSON.stringify(fetchServerQuery[key]));
107
- });
108
- }
109
- fetchUrl = currentUrl.toString();
110
- return [
111
- 4,
112
- fetch(fetchUrl).then(function(res2) {
113
- return res2.json();
114
- })
115
- ];
116
- case 3:
117
- data = _state.sent();
118
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
119
- res(data);
120
- return [
121
- 3,
122
- 5
123
- ];
124
- case 4:
125
- e = _state.sent();
126
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
127
- rej(e);
128
- return [
129
- 3,
130
- 5
131
- ];
132
- case 5:
133
- return [
134
- 2,
135
- promise
136
- ];
137
- case 6:
138
- return [
139
- 2
140
- ];
141
- }
142
- });
143
- });
144
- return _callDowngrade.apply(this, arguments);
145
- }
146
- export {
147
- callAllDowngrade,
148
- callDowngrade,
149
- getDowngradeTag
150
- };
@@ -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,70 +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 hasSSRAsset = Boolean(remoteSnapshot.ssrRemoteEntry);
45
- const hasDataFetchClient = Boolean(remoteSnapshot.modules.find((module) => module.moduleName === `${dataFetchName}${DATA_FETCH_CLIENT_SUFFIX}`));
46
- const downgradeType = hasDataFetchClient ? MF_DATA_FETCH_TYPE.FETCH_CLIENT : hasSSRAsset ? MF_DATA_FETCH_TYPE.FETCH_SERVER : MF_DATA_FETCH_TYPE.FETCH_CLIENT;
47
- let finalDataFetchId = dataFetchId;
48
- if (typeof window !== "undefined") {
49
- finalDataFetchId = downgradeType === MF_DATA_FETCH_TYPE.FETCH_CLIENT ? hasDataFetchClient ? `${dataFetchId}${DATA_FETCH_CLIENT_SUFFIX}` : dataFetchId : dataFetchId;
50
- }
51
- const getDataFetchGetter = () => loadDataFetchModule(host, finalDataFetchId);
52
- const dataFetchFnItem = [
53
- getDataFetchGetter,
54
- downgradeType
55
- ];
56
- if (typeof window === "undefined" || isCSROnly()) {
57
- dataFetchFnItem.push(getDataFetchGetter());
58
- }
59
- dataFetchMap[dataFetchMapKey] = [
60
- dataFetchFnItem,
61
- void 0,
62
- MF_DATA_FETCH_STATUS.AWAIT
63
- ];
64
- return args;
65
- }
66
- });
67
- var auto_fetch_data_default = autoFetchData;
68
- export {
69
- auto_fetch_data_default as default
70
- };
File without changes
@@ -1,109 +0,0 @@
1
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
- import { Suspense, useRef } from "react";
3
- import logger from "../logger";
4
- import { DATA_FETCH_ERROR_PREFIX, LOAD_REMOTE_ERROR_PREFIX, ERROR_TYPE, DATA_FETCH_FUNCTION } from "../constant";
5
- import { getDataFetchIdWithErrorMsgs, wrapDataFetchId } from "../utils";
6
- function isPromise(obj) {
7
- return !!obj && (typeof obj === "object" || typeof obj === "function") && typeof obj.then === "function";
8
- }
9
- const AWAIT_ERROR_PREFIX = "<Await /> caught the following error during render: ";
10
- const transformError = (err) => {
11
- const errMsg = err instanceof Error ? err.message : err;
12
- const originalMsg = errMsg.replace(AWAIT_ERROR_PREFIX, "");
13
- const dataFetchMapKey = getDataFetchIdWithErrorMsgs(originalMsg);
14
- if (originalMsg.indexOf(DATA_FETCH_ERROR_PREFIX) === 0) {
15
- return {
16
- error: new Error(originalMsg.replace(DATA_FETCH_ERROR_PREFIX, "").replace(wrapDataFetchId(dataFetchMapKey), "")),
17
- errorType: ERROR_TYPE.DATA_FETCH,
18
- dataFetchMapKey
19
- };
20
- }
21
- if (originalMsg.indexOf(LOAD_REMOTE_ERROR_PREFIX) === 0) {
22
- return {
23
- error: new Error(originalMsg.replace(LOAD_REMOTE_ERROR_PREFIX, "").replace(wrapDataFetchId(dataFetchMapKey), "")),
24
- errorType: ERROR_TYPE.LOAD_REMOTE,
25
- dataFetchMapKey
26
- };
27
- }
28
- return {
29
- error: new Error(originalMsg.replace(wrapDataFetchId(dataFetchMapKey), "")),
30
- errorType: ERROR_TYPE.UNKNOWN,
31
- dataFetchMapKey
32
- };
33
- };
34
- const DefaultLoading = /* @__PURE__ */ _jsx(_Fragment, {});
35
- const DefaultErrorElement = (_data) => /* @__PURE__ */ _jsx("div", {
36
- children: "Error"
37
- });
38
- function AwaitDataFetch({ resolve, loading = DefaultLoading, errorElement = DefaultErrorElement, children, params }) {
39
- const dataRef = useRef();
40
- const data = dataRef.current || resolve;
41
- const getData = isPromise(data) ? fetchData(data, dataRef) : () => data;
42
- return /* @__PURE__ */ _jsx(AwaitSuspense, {
43
- params,
44
- loading,
45
- errorElement,
46
- // @ts-ignore
47
- resolve: getData,
48
- children
49
- });
50
- }
51
- function AwaitSuspense({ resolve, children, loading = DefaultLoading, errorElement = DefaultErrorElement }) {
52
- return /* @__PURE__ */ _jsx(Suspense, {
53
- fallback: loading,
54
- children: /* @__PURE__ */ _jsx(ResolveAwait, {
55
- resolve,
56
- errorElement,
57
- children
58
- })
59
- });
60
- }
61
- function ResolveAwait({ children, resolve, errorElement, params }) {
62
- const data = resolve();
63
- logger.debug("resolve data: ", data);
64
- if (typeof data === "string" && data.indexOf(AWAIT_ERROR_PREFIX) === 0) {
65
- const transformedError = transformError(data);
66
- return /* @__PURE__ */ _jsx(_Fragment, {
67
- children: typeof errorElement === "function" ? /* @__PURE__ */ _jsxs(_Fragment, {
68
- children: [
69
- globalThis.FEDERATION_SSR && /* @__PURE__ */ _jsx("script", {
70
- suppressHydrationWarning: true,
71
- dangerouslySetInnerHTML: {
72
- __html: String.raw`
73
- globalThis['${DATA_FETCH_FUNCTION}'] = globalThis['${DATA_FETCH_FUNCTION}'] || []
74
- globalThis['${DATA_FETCH_FUNCTION}'].push([${transformedError.dataFetchMapKey ? `'${transformedError.dataFetchMapKey}'` : ""},${params ? JSON.stringify(params) : null},true]);`
75
- }
76
- }),
77
- errorElement(transformedError)
78
- ]
79
- }) : errorElement
80
- });
81
- }
82
- const toRender = typeof children === "function" ? children(data) : children;
83
- return /* @__PURE__ */ _jsx(_Fragment, {
84
- children: toRender
85
- });
86
- }
87
- const fetchData = (promise, ref) => {
88
- let data;
89
- let status = "pending";
90
- const suspender = promise.then((res) => {
91
- status = "success";
92
- data = res;
93
- ref.current = res;
94
- }).catch((e) => {
95
- status = "success";
96
- console.warn(e);
97
- data = AWAIT_ERROR_PREFIX + e;
98
- });
99
- return () => {
100
- if (status === "pending") {
101
- throw suspender;
102
- }
103
- return data;
104
- };
105
- };
106
- export {
107
- AwaitDataFetch,
108
- transformError
109
- };