@module-federation/modern-js 0.15.0 → 0.17.0
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.
- package/dist/cjs/cli/configPlugin.js +5 -4
- package/dist/cjs/cli/configPlugin.spec.js +1 -3
- package/dist/cjs/cli/server/data-fetch-server-plugin.js +2 -143
- package/dist/cjs/cli/ssrPlugin.js +8 -8
- package/dist/cjs/constant.js +2 -49
- package/dist/cjs/{runtime/wrapNoSSR.js → react/index.js} +22 -13
- package/dist/cjs/runtime/index.js +0 -28
- package/dist/cjs/ssr-runtime/devPlugin.js +2 -2
- package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +6 -102
- package/dist/esm/cli/configPlugin.js +5 -4
- package/dist/esm/cli/configPlugin.spec.js +1 -3
- package/dist/esm/cli/server/data-fetch-server-plugin.js +2 -219
- package/dist/esm/cli/ssrPlugin.js +1 -1
- package/dist/esm/constant.js +1 -37
- package/dist/esm/react/index.js +12 -0
- package/dist/esm/runtime/index.js +0 -21
- package/dist/esm/ssr-runtime/devPlugin.js +1 -1
- package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +4 -147
- package/dist/esm-node/cli/configPlugin.js +5 -4
- package/dist/esm-node/cli/configPlugin.spec.js +1 -3
- package/dist/esm-node/cli/server/data-fetch-server-plugin.js +2 -143
- package/dist/esm-node/cli/ssrPlugin.js +1 -1
- package/dist/esm-node/constant.js +1 -37
- package/dist/esm-node/react/index.js +12 -0
- package/dist/esm-node/runtime/index.js +0 -21
- package/dist/esm-node/ssr-runtime/devPlugin.js +1 -1
- package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +4 -89
- package/dist/types/cli/index.d.ts +1 -1
- package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +2 -2
- package/dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts +2 -2
- package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +2 -2
- package/dist/types/constant.d.ts +0 -23
- package/dist/types/react/index.d.ts +2 -0
- package/dist/types/runtime/index.d.ts +0 -9
- package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +0 -3
- package/package.json +24 -24
- package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +0 -98
- package/dist/cjs/interfaces/global.js +0 -16
- package/dist/cjs/runtime/AwaitDataFetch.js +0 -144
- package/dist/cjs/runtime/createRemoteComponent.js +0 -327
- package/dist/cjs/ssr-runtime/downgrade.js +0 -114
- package/dist/cjs/utils/dataFetch.js +0 -211
- package/dist/cjs/utils/index.js +0 -54
- package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +0 -74
- package/dist/esm/interfaces/global.js +0 -0
- package/dist/esm/runtime/AwaitDataFetch.js +0 -131
- package/dist/esm/runtime/createRemoteComponent.js +0 -417
- package/dist/esm/runtime/wrapNoSSR.js +0 -12
- package/dist/esm/ssr-runtime/downgrade.js +0 -150
- package/dist/esm/utils/dataFetch.js +0 -237
- package/dist/esm/utils/index.js +0 -28
- package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +0 -68
- package/dist/esm-node/interfaces/global.js +0 -0
- package/dist/esm-node/runtime/AwaitDataFetch.js +0 -109
- package/dist/esm-node/runtime/createRemoteComponent.js +0 -291
- package/dist/esm-node/runtime/wrapNoSSR.js +0 -11
- package/dist/esm-node/ssr-runtime/downgrade.js +0 -88
- package/dist/esm-node/utils/dataFetch.js +0 -166
- package/dist/esm-node/utils/index.js +0 -27
- package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +0 -3
- package/dist/types/interfaces/global.d.ts +0 -27
- package/dist/types/runtime/AwaitDataFetch.d.ts +0 -19
- package/dist/types/runtime/createRemoteComponent.d.ts +0 -26
- package/dist/types/runtime/wrapNoSSR.d.ts +0 -9
- package/dist/types/ssr-runtime/downgrade.d.ts +0 -4
- package/dist/types/utils/dataFetch.d.ts +0 -26
- package/dist/types/utils/index.d.ts +0 -15
|
@@ -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
|
-
};
|
package/dist/esm/utils/index.js
DELETED
|
@@ -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
|
-
};
|
|
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
|
-
};
|