@module-federation/modern-js 0.16.0 → 0.17.1

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 (65) hide show
  1. package/dist/cjs/cli/configPlugin.js +4 -3
  2. package/dist/cjs/cli/configPlugin.spec.js +1 -3
  3. package/dist/cjs/cli/server/data-fetch-server-plugin.js +2 -143
  4. package/dist/cjs/constant.js +2 -49
  5. package/dist/cjs/{runtime/wrapNoSSR.js → react/index.js} +22 -13
  6. package/dist/cjs/runtime/index.js +0 -28
  7. package/dist/cjs/ssr-runtime/devPlugin.js +2 -2
  8. package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +6 -102
  9. package/dist/esm/cli/configPlugin.js +4 -3
  10. package/dist/esm/cli/configPlugin.spec.js +1 -3
  11. package/dist/esm/cli/server/data-fetch-server-plugin.js +2 -219
  12. package/dist/esm/constant.js +1 -37
  13. package/dist/esm/react/index.js +12 -0
  14. package/dist/esm/runtime/index.js +0 -21
  15. package/dist/esm/ssr-runtime/devPlugin.js +1 -1
  16. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +4 -147
  17. package/dist/esm-node/cli/configPlugin.js +4 -3
  18. package/dist/esm-node/cli/configPlugin.spec.js +1 -3
  19. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +2 -143
  20. package/dist/esm-node/constant.js +1 -37
  21. package/dist/esm-node/react/index.js +12 -0
  22. package/dist/esm-node/runtime/index.js +0 -21
  23. package/dist/esm-node/ssr-runtime/devPlugin.js +1 -1
  24. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +4 -89
  25. package/dist/types/cli/index.d.ts +1 -1
  26. package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +2 -2
  27. package/dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts +2 -2
  28. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +2 -2
  29. package/dist/types/constant.d.ts +0 -23
  30. package/dist/types/react/index.d.ts +2 -0
  31. package/dist/types/runtime/index.d.ts +0 -9
  32. package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +0 -3
  33. package/dist/types/types/index.d.ts +1 -3
  34. package/package.json +26 -24
  35. package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +0 -100
  36. package/dist/cjs/interfaces/global.js +0 -16
  37. package/dist/cjs/runtime/AwaitDataFetch.js +0 -144
  38. package/dist/cjs/runtime/createRemoteComponent.js +0 -327
  39. package/dist/cjs/ssr-runtime/downgrade.js +0 -114
  40. package/dist/cjs/utils/dataFetch.js +0 -211
  41. package/dist/cjs/utils/index.js +0 -54
  42. package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +0 -76
  43. package/dist/esm/interfaces/global.js +0 -0
  44. package/dist/esm/runtime/AwaitDataFetch.js +0 -131
  45. package/dist/esm/runtime/createRemoteComponent.js +0 -417
  46. package/dist/esm/runtime/wrapNoSSR.js +0 -12
  47. package/dist/esm/ssr-runtime/downgrade.js +0 -150
  48. package/dist/esm/utils/dataFetch.js +0 -237
  49. package/dist/esm/utils/index.js +0 -28
  50. package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +0 -70
  51. package/dist/esm-node/interfaces/global.js +0 -0
  52. package/dist/esm-node/runtime/AwaitDataFetch.js +0 -109
  53. package/dist/esm-node/runtime/createRemoteComponent.js +0 -291
  54. package/dist/esm-node/runtime/wrapNoSSR.js +0 -11
  55. package/dist/esm-node/ssr-runtime/downgrade.js +0 -88
  56. package/dist/esm-node/utils/dataFetch.js +0 -166
  57. package/dist/esm-node/utils/index.js +0 -27
  58. package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +0 -3
  59. package/dist/types/interfaces/global.d.ts +0 -27
  60. package/dist/types/runtime/AwaitDataFetch.d.ts +0 -19
  61. package/dist/types/runtime/createRemoteComponent.d.ts +0 -26
  62. package/dist/types/runtime/wrapNoSSR.d.ts +0 -9
  63. package/dist/types/ssr-runtime/downgrade.d.ts +0 -4
  64. package/dist/types/utils/dataFetch.d.ts +0 -26
  65. package/dist/types/utils/index.d.ts +0 -15
@@ -1,291 +0,0 @@
1
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
- import React, { useEffect, useState } from "react";
3
- import logger from "../logger";
4
- import { getInstance } from "@module-federation/enhanced/runtime";
5
- import { AwaitDataFetch, transformError } from "./AwaitDataFetch";
6
- import { fetchData, getDataFetchItem, getDataFetchMapKey } from "../utils/dataFetch";
7
- import { getDataFetchInfo, getLoadedRemoteInfos, setDataFetchItemLoadedStatus, wrapDataFetchId } from "../utils";
8
- import { DATA_FETCH_ERROR_PREFIX, DATA_FETCH_FUNCTION, FS_HREF, LOAD_REMOTE_ERROR_PREFIX, MF_DATA_FETCH_TYPE } from "../constant";
9
- function getTargetModuleInfo(id) {
10
- const instance = getInstance();
11
- if (!instance) {
12
- return;
13
- }
14
- const loadedRemoteInfo = getLoadedRemoteInfos(id, instance);
15
- if (!loadedRemoteInfo) {
16
- return;
17
- }
18
- const snapshot = loadedRemoteInfo.snapshot;
19
- if (!snapshot) {
20
- return;
21
- }
22
- const publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
23
- if (!publicPath) {
24
- return;
25
- }
26
- const modules = "modules" in snapshot ? snapshot.modules : [];
27
- const targetModule = modules.find((m) => m.modulePath === loadedRemoteInfo.expose);
28
- if (!targetModule) {
29
- return;
30
- }
31
- const remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
32
- if (!remoteEntry) {
33
- return;
34
- }
35
- return {
36
- module: targetModule,
37
- publicPath,
38
- remoteEntry
39
- };
40
- }
41
- function collectSSRAssets(options) {
42
- const { id, injectLink = true, injectScript = false } = typeof options === "string" ? {
43
- id: options
44
- } : options;
45
- const links = [];
46
- const scripts = [];
47
- const instance = getInstance();
48
- if (!instance || !injectLink && !injectScript) {
49
- return [
50
- ...scripts,
51
- ...links
52
- ];
53
- }
54
- const moduleAndPublicPath = getTargetModuleInfo(id);
55
- if (!moduleAndPublicPath) {
56
- return [
57
- ...scripts,
58
- ...links
59
- ];
60
- }
61
- const { module: targetModule, publicPath, remoteEntry } = moduleAndPublicPath;
62
- if (injectLink) {
63
- [
64
- ...targetModule.assets.css.sync,
65
- ...targetModule.assets.css.async
66
- ].sort().forEach((file, index) => {
67
- links.push(/* @__PURE__ */ _jsx("link", {
68
- href: `${publicPath}${file}`,
69
- rel: "stylesheet",
70
- type: "text/css"
71
- }, `${file.split(".")[0]}_${index}`));
72
- });
73
- }
74
- if (injectScript) {
75
- scripts.push(/* @__PURE__ */ _jsx("script", {
76
- async: true,
77
- src: `${publicPath}${remoteEntry}`,
78
- crossOrigin: "anonymous"
79
- }, remoteEntry.split(".")[0]));
80
- [
81
- ...targetModule.assets.js.sync
82
- ].sort().forEach((file, index) => {
83
- scripts.push(/* @__PURE__ */ _jsx("script", {
84
- async: true,
85
- src: `${publicPath}${file}`,
86
- crossOrigin: "anonymous"
87
- }, `${file.split(".")[0]}_${index}`));
88
- });
89
- }
90
- return [
91
- ...scripts,
92
- ...links
93
- ];
94
- }
95
- function getServerNeedRemoteInfo(loadedRemoteInfo, id, noSSR) {
96
- if (noSSR || typeof window !== "undefined" && window.location.href !== window[FS_HREF]) {
97
- var _dataFetchItem_;
98
- if (!(loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.version)) {
99
- throw new Error(`${loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.name} version is empty`);
100
- }
101
- const { snapshot } = loadedRemoteInfo;
102
- if (!snapshot) {
103
- throw new Error(`${loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.name} snapshot is empty`);
104
- }
105
- const dataFetchItem = getDataFetchItem(id);
106
- const isFetchServer = (dataFetchItem === null || dataFetchItem === void 0 ? void 0 : (_dataFetchItem_ = dataFetchItem[0]) === null || _dataFetchItem_ === void 0 ? void 0 : _dataFetchItem_[1]) === MF_DATA_FETCH_TYPE.FETCH_SERVER;
107
- if (isFetchServer && (!("ssrPublicPath" in snapshot) || !snapshot.ssrPublicPath)) {
108
- throw new Error(`ssrPublicPath is required while fetching ${loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.name} data in SSR project!`);
109
- }
110
- if (isFetchServer && (!("ssrRemoteEntry" in snapshot) || !snapshot.ssrRemoteEntry)) {
111
- throw new Error(`ssrRemoteEntry is required while loading ${loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.name} data loader in SSR project!`);
112
- }
113
- return {
114
- name: loadedRemoteInfo.name,
115
- version: loadedRemoteInfo.version,
116
- ssrPublicPath: "ssrPublicPath" in snapshot ? snapshot.ssrPublicPath || "" : "",
117
- ssrRemoteEntry: "ssrRemoteEntry" in snapshot ? snapshot.ssrRemoteEntry || "" : "",
118
- globalName: loadedRemoteInfo.entryGlobalName
119
- };
120
- }
121
- return;
122
- }
123
- function createRemoteComponent(options) {
124
- const exportName = (options === null || options === void 0 ? void 0 : options.export) || "default";
125
- const callLoader = async () => {
126
- logger.debug("callLoader start", Date.now());
127
- const m = await options.loader();
128
- logger.debug("callLoader end", Date.now());
129
- if (!m) {
130
- throw new Error("load remote failed");
131
- }
132
- return m;
133
- };
134
- const getData = async (noSSR) => {
135
- let loadedRemoteInfo;
136
- let moduleId;
137
- const instance = getInstance();
138
- try {
139
- const m = await callLoader();
140
- moduleId = m && m[Symbol.for("mf_module_id")];
141
- if (!moduleId) {
142
- throw new Error("moduleId is empty");
143
- }
144
- loadedRemoteInfo = getLoadedRemoteInfos(moduleId, instance);
145
- if (!loadedRemoteInfo) {
146
- throw new Error(`can not find loaded remote('${moduleId}') info!`);
147
- }
148
- } catch (e) {
149
- const errMsg = `${LOAD_REMOTE_ERROR_PREFIX}${e}`;
150
- logger.debug(e);
151
- throw new Error(errMsg);
152
- }
153
- let dataFetchMapKey;
154
- try {
155
- dataFetchMapKey = getDataFetchMapKey(getDataFetchInfo({
156
- name: loadedRemoteInfo.name,
157
- alias: loadedRemoteInfo.alias,
158
- id: moduleId,
159
- remoteSnapshot: loadedRemoteInfo.snapshot
160
- }), {
161
- name: instance.name,
162
- version: instance === null || instance === void 0 ? void 0 : instance.options.version
163
- });
164
- logger.debug("getData dataFetchMapKey: ", dataFetchMapKey);
165
- if (!dataFetchMapKey) {
166
- return;
167
- }
168
- const data = await fetchData(dataFetchMapKey, {
169
- ...options.dataFetchParams,
170
- isDowngrade: false
171
- }, getServerNeedRemoteInfo(loadedRemoteInfo, dataFetchMapKey, noSSR));
172
- setDataFetchItemLoadedStatus(dataFetchMapKey);
173
- logger.debug("get data res: \n", data);
174
- return data;
175
- } catch (err) {
176
- const errMsg = `${DATA_FETCH_ERROR_PREFIX}${wrapDataFetchId(dataFetchMapKey)}${err}`;
177
- logger.debug(errMsg);
178
- throw new Error(errMsg);
179
- }
180
- };
181
- const LazyComponent = /* @__PURE__ */ React.lazy(async () => {
182
- const m = await callLoader();
183
- const moduleId = m && m[Symbol.for("mf_module_id")];
184
- const instance = getInstance();
185
- const loadedRemoteInfo = getLoadedRemoteInfos(moduleId, instance);
186
- loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.snapshot;
187
- const dataFetchMapKey = loadedRemoteInfo ? getDataFetchMapKey(getDataFetchInfo({
188
- name: loadedRemoteInfo.name,
189
- alias: loadedRemoteInfo.alias,
190
- id: moduleId,
191
- remoteSnapshot: loadedRemoteInfo.snapshot
192
- }), {
193
- name: instance.name,
194
- version: instance === null || instance === void 0 ? void 0 : instance.options.version
195
- }) : void 0;
196
- logger.debug("LazyComponent dataFetchMapKey: ", dataFetchMapKey);
197
- const assets = collectSSRAssets({
198
- id: moduleId
199
- });
200
- const Com = m[exportName];
201
- if (exportName in m && typeof Com === "function") {
202
- return {
203
- default: (props) => /* @__PURE__ */ _jsxs(_Fragment, {
204
- children: [
205
- globalThis.FEDERATION_SSR && dataFetchMapKey && /* @__PURE__ */ _jsx("script", {
206
- suppressHydrationWarning: true,
207
- dangerouslySetInnerHTML: {
208
- __html: String.raw`
209
- globalThis['${DATA_FETCH_FUNCTION}'] = globalThis['${DATA_FETCH_FUNCTION}'] || [];
210
- globalThis['${DATA_FETCH_FUNCTION}'].push(['${dataFetchMapKey}',${JSON.stringify(props.mfData)}]);
211
- `
212
- }
213
- }),
214
- globalThis.FEDERATION_SSR && assets,
215
- /* @__PURE__ */ _jsx(Com, {
216
- ...props
217
- })
218
- ]
219
- })
220
- };
221
- } else {
222
- throw Error(`Make sure that ${moduleId} has the correct export when export is ${String(exportName)}`);
223
- }
224
- });
225
- return (props) => {
226
- const { key, ...args } = props;
227
- if (globalThis.FEDERATION_SSR && !options.noSSR) {
228
- const { key: key2, ...args2 } = props;
229
- return /* @__PURE__ */ _jsx(AwaitDataFetch, {
230
- resolve: getData(options.noSSR),
231
- loading: options.loading,
232
- errorElement: options.fallback,
233
- children: (data) => /* @__PURE__ */ _jsx(LazyComponent, {
234
- ...args2,
235
- mfData: data
236
- })
237
- });
238
- } else {
239
- const [data, setData] = useState(null);
240
- const [loading, setLoading] = useState(true);
241
- const [error, setError] = useState(null);
242
- useEffect(() => {
243
- let isMounted = true;
244
- const fetchDataAsync = async () => {
245
- try {
246
- setLoading(true);
247
- const result = await getData(options.noSSR);
248
- if (isMounted) {
249
- setData(result);
250
- }
251
- } catch (e) {
252
- if (isMounted) {
253
- setError(transformError(e));
254
- }
255
- } finally {
256
- if (isMounted) {
257
- setLoading(false);
258
- }
259
- }
260
- };
261
- fetchDataAsync();
262
- return () => {
263
- isMounted = false;
264
- };
265
- }, []);
266
- if (loading) {
267
- return /* @__PURE__ */ _jsx(_Fragment, {
268
- children: options.loading
269
- });
270
- }
271
- if (error) {
272
- return /* @__PURE__ */ _jsx(_Fragment, {
273
- children: typeof options.fallback === "function" ? options.fallback(error) : options.fallback
274
- });
275
- }
276
- return /* @__PURE__ */ _jsx(LazyComponent, {
277
- ...args,
278
- mfData: data
279
- });
280
- }
281
- };
282
- }
283
- function createRemoteSSRComponent(options) {
284
- logger.warn("createRemoteSSRComponent is deprecated, please use createRemoteComponent instead!");
285
- return createRemoteComponent(options);
286
- }
287
- export {
288
- collectSSRAssets,
289
- createRemoteComponent,
290
- createRemoteSSRComponent
291
- };
@@ -1,11 +0,0 @@
1
- function wrapNoSSR(createComponentFn) {
2
- return (options) => {
3
- return createComponentFn({
4
- ...options,
5
- noSSR: true
6
- });
7
- };
8
- }
9
- export {
10
- wrapNoSSR
11
- };
@@ -1,88 +0,0 @@
1
- import { DATA_FETCH_QUERY, DOWNGRADE_KEY } from "../constant";
2
- import { MF_DATA_FETCH_STATUS, MF_DATA_FETCH_TYPE } from "../constant";
3
- import { getDataFetchMap } from "../utils";
4
- function getDowngradeTag() {
5
- return globalThis[DOWNGRADE_KEY];
6
- }
7
- function callAllDowngrade() {
8
- const dataFetchMap = getDataFetchMap();
9
- if (!dataFetchMap) {
10
- return;
11
- }
12
- Object.keys(dataFetchMap).forEach((key) => {
13
- callDowngrade(key);
14
- });
15
- }
16
- async function callDowngrade(id, params, remoteInfo) {
17
- const dataFetchMap = getDataFetchMap();
18
- if (!dataFetchMap) {
19
- return;
20
- }
21
- const mfDataFetch = dataFetchMap[id];
22
- if ((mfDataFetch === null || mfDataFetch === void 0 ? void 0 : mfDataFetch[2]) === MF_DATA_FETCH_STATUS.AWAIT) {
23
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADING;
24
- let promise, res, rej;
25
- if (mfDataFetch[1]) {
26
- promise = mfDataFetch[1][0];
27
- res = mfDataFetch[1][1];
28
- rej = mfDataFetch[1][2];
29
- } else {
30
- promise = new Promise((resolve, reject) => {
31
- res = resolve;
32
- rej = reject;
33
- });
34
- mfDataFetch[1] = [
35
- promise,
36
- res,
37
- rej
38
- ];
39
- }
40
- const dataFetchType = mfDataFetch[0][1];
41
- if (dataFetchType === MF_DATA_FETCH_TYPE.FETCH_CLIENT) {
42
- try {
43
- mfDataFetch[0][0]().then((getDataFetchFn) => {
44
- return getDataFetchFn({
45
- ...params,
46
- isDowngrade: true
47
- }).then((data) => {
48
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
49
- res(data);
50
- });
51
- });
52
- } catch (e) {
53
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
54
- rej(e);
55
- }
56
- } else if (dataFetchType === MF_DATA_FETCH_TYPE.FETCH_SERVER) {
57
- try {
58
- const currentUrl = new URL(window.location.href);
59
- currentUrl.searchParams.set(DATA_FETCH_QUERY, encodeURIComponent(id));
60
- if (params) {
61
- currentUrl.searchParams.set("params", encodeURIComponent(JSON.stringify(params)));
62
- }
63
- if (remoteInfo) {
64
- currentUrl.searchParams.set("remoteInfo", encodeURIComponent(JSON.stringify(remoteInfo)));
65
- }
66
- const fetchServerQuery = globalThis.FEDERATION_SERVER_QUERY;
67
- if (fetchServerQuery && typeof fetchServerQuery === "object") {
68
- Object.keys(fetchServerQuery).forEach((key) => {
69
- currentUrl.searchParams.set(key, JSON.stringify(fetchServerQuery[key]));
70
- });
71
- }
72
- const fetchUrl = currentUrl.toString();
73
- const data = await fetch(fetchUrl).then((res2) => res2.json());
74
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
75
- res(data);
76
- } catch (e) {
77
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
78
- rej(e);
79
- }
80
- }
81
- return promise;
82
- }
83
- }
84
- export {
85
- callAllDowngrade,
86
- callDowngrade,
87
- getDowngradeTag
88
- };
@@ -1,166 +0,0 @@
1
- import { isBrowserEnv, composeKeyWithSeparator } from "@module-federation/sdk";
2
- import { isCSROnly } from "../utils";
3
- import logger from "../logger";
4
- import { callDowngrade, getDowngradeTag } from "../ssr-runtime/downgrade";
5
- import { DOWNGRADE_KEY, MF_DATA_FETCH_STATUS, WRAP_DATA_FETCH_ID_IDENTIFIER } from "../constant";
6
- import { DATA_FETCH_CLIENT_SUFFIX, DATA_FETCH_IDENTIFIER } from "@module-federation/rsbuild-plugin/constant";
7
- const getDataFetchInfo = ({ name, alias, id, remoteSnapshot }) => {
8
- if (!remoteSnapshot) {
9
- return;
10
- }
11
- if (!("modules" in remoteSnapshot)) {
12
- return;
13
- }
14
- const regex = new RegExp(`^${name}(/[^/].*|)$`);
15
- const nameOrAlias = regex.test(id) ? name : alias || name;
16
- const expose = id.replace(nameOrAlias, "");
17
- let dataFetchName = "";
18
- let dataFetchId = "";
19
- if (expose.startsWith("/")) {
20
- dataFetchName = `${expose.slice(1)}.${DATA_FETCH_IDENTIFIER}`;
21
- dataFetchId = `${id}.${DATA_FETCH_IDENTIFIER}`;
22
- } else if (expose === "") {
23
- dataFetchName = DATA_FETCH_IDENTIFIER;
24
- dataFetchId = `${id}/${DATA_FETCH_IDENTIFIER}`;
25
- } else {
26
- return;
27
- }
28
- if (!dataFetchName || !dataFetchId) {
29
- return;
30
- }
31
- if (!remoteSnapshot.modules.find((module) => module.moduleName === dataFetchName)) {
32
- return;
33
- }
34
- return {
35
- dataFetchName,
36
- dataFetchId
37
- };
38
- };
39
- function initDataFetchMap() {
40
- var _globalThis;
41
- (_globalThis = globalThis).__MF_DATA_FETCH_MAP__ || (_globalThis.__MF_DATA_FETCH_MAP__ = {});
42
- }
43
- function getDataFetchItem(id) {
44
- var _globalThis___MF_DATA_FETCH_MAP__;
45
- 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];
46
- }
47
- function getDataFetchMap() {
48
- return globalThis.__MF_DATA_FETCH_MAP__;
49
- }
50
- const flushDataFetch = () => {
51
- globalThis.__MF_DATA_FETCH_MAP__ = {};
52
- globalThis[DOWNGRADE_KEY] = void 0;
53
- };
54
- function setDataFetchItemLoadedStatus(id) {
55
- const dataFetchItem = getDataFetchItem(id);
56
- if (!dataFetchItem) {
57
- return;
58
- }
59
- dataFetchItem[2] = MF_DATA_FETCH_STATUS.LOADED;
60
- }
61
- const wrapDataFetchId = (id) => {
62
- return `${WRAP_DATA_FETCH_ID_IDENTIFIER}${id}${WRAP_DATA_FETCH_ID_IDENTIFIER}`;
63
- };
64
- const getDataFetchIdWithErrorMsgs = (errMsgs) => {
65
- const firstIdentifierIndex = errMsgs.indexOf(WRAP_DATA_FETCH_ID_IDENTIFIER);
66
- if (firstIdentifierIndex === -1) {
67
- return void 0;
68
- }
69
- const secondIdentifierIndex = errMsgs.indexOf(WRAP_DATA_FETCH_ID_IDENTIFIER, firstIdentifierIndex + WRAP_DATA_FETCH_ID_IDENTIFIER.length);
70
- if (secondIdentifierIndex === -1) {
71
- return void 0;
72
- }
73
- return errMsgs.substring(firstIdentifierIndex + WRAP_DATA_FETCH_ID_IDENTIFIER.length, secondIdentifierIndex);
74
- };
75
- async function fetchData(id, params, remoteInfo) {
76
- const callFetchData = async () => {
77
- const item = getDataFetchItem(id);
78
- if (!item) {
79
- return;
80
- }
81
- const [fetchDataFnArr, ..._rest] = item;
82
- const fetchDataFn = await fetchDataFnArr[2];
83
- if (!fetchDataFn) {
84
- return;
85
- }
86
- return fetchDataFn(params);
87
- };
88
- if (isBrowserEnv()) {
89
- var _dataFetchItem_;
90
- const dataFetchItem = getDataFetchItem(id);
91
- if (!dataFetchItem) {
92
- throw new Error(`dataFetchItem not found, id: ${id}`);
93
- }
94
- if ((_dataFetchItem_ = dataFetchItem[1]) === null || _dataFetchItem_ === void 0 ? void 0 : _dataFetchItem_[0]) {
95
- return dataFetchItem[1][0];
96
- }
97
- if (isCSROnly()) {
98
- logger.debug("==========csr only!");
99
- return callFetchData();
100
- }
101
- if (remoteInfo) {
102
- return callDowngrade(id, params, remoteInfo);
103
- }
104
- const mfDowngrade = getDowngradeTag();
105
- if (mfDowngrade) {
106
- if (typeof mfDowngrade === "boolean") {
107
- return callDowngrade(id, {
108
- ...params,
109
- isDowngrade: true
110
- });
111
- }
112
- if (mfDowngrade.includes(id)) {
113
- return callDowngrade(id, {
114
- ...params,
115
- isDowngrade: true
116
- });
117
- }
118
- }
119
- let res;
120
- let rej;
121
- const p = new Promise((resolve, reject) => {
122
- res = resolve;
123
- rej = reject;
124
- });
125
- dataFetchItem[1] = [
126
- p,
127
- res,
128
- rej
129
- ];
130
- dataFetchItem[2] = MF_DATA_FETCH_STATUS.AWAIT;
131
- return dataFetchItem[1][0];
132
- }
133
- return callFetchData();
134
- }
135
- function getDataFetchMapKey(dataFetchInfo, hostInfo) {
136
- if (!dataFetchInfo || !hostInfo) {
137
- return;
138
- }
139
- const { dataFetchId } = dataFetchInfo;
140
- return composeKeyWithSeparator(dataFetchId, hostInfo.name, hostInfo.version);
141
- }
142
- async function loadDataFetchModule(instance, id) {
143
- return instance.loadRemote(id).then((m) => {
144
- if (m && typeof m === "object" && "fetchData" in m && typeof m.fetchData === "function") {
145
- return m.fetchData;
146
- }
147
- throw new Error(`fetchData not found in remote ${id}, ${JSON.stringify(m)}`);
148
- });
149
- }
150
- function isDataLoaderExpose(exposeKey) {
151
- return exposeKey.endsWith(DATA_FETCH_IDENTIFIER) || exposeKey.endsWith(DATA_FETCH_CLIENT_SUFFIX);
152
- }
153
- export {
154
- fetchData,
155
- flushDataFetch,
156
- getDataFetchIdWithErrorMsgs,
157
- getDataFetchInfo,
158
- getDataFetchItem,
159
- getDataFetchMap,
160
- getDataFetchMapKey,
161
- initDataFetchMap,
162
- isDataLoaderExpose,
163
- loadDataFetchModule,
164
- setDataFetchItemLoadedStatus,
165
- wrapDataFetchId
166
- };
@@ -1,27 +0,0 @@
1
- function getLoadedRemoteInfos(id, instance) {
2
- if (!instance) {
3
- return;
4
- }
5
- const { name, expose } = instance.remoteHandler.idToRemoteMap[id] || {};
6
- if (!name) {
7
- return;
8
- }
9
- const module = instance.moduleCache.get(name);
10
- if (!module) {
11
- return;
12
- }
13
- const { remoteSnapshot } = instance.snapshotHandler.getGlobalRemoteInfo(module.remoteInfo);
14
- return {
15
- ...module.remoteInfo,
16
- snapshot: remoteSnapshot,
17
- expose
18
- };
19
- }
20
- function isCSROnly() {
21
- return window._SSR_DATA === void 0;
22
- }
23
- export * from "./dataFetch";
24
- export {
25
- getLoadedRemoteInfos,
26
- isCSROnly
27
- };
@@ -1,3 +0,0 @@
1
- import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
2
- declare const autoFetchData: () => FederationRuntimePlugin;
3
- export default autoFetchData;
@@ -1,27 +0,0 @@
1
- import { MF_DATA_FETCH_TYPE, MF_DATA_FETCH_STATUS } from '../constant';
2
- export type DataFetchParams = {
3
- isDowngrade: boolean;
4
- } & Record<string, unknown>;
5
- export type DataFetch<T> = (params: DataFetchParams) => Promise<T>;
6
- export type MF_DATA_FETCH_MAP_VALUE = [
7
- [
8
- () => Promise<DataFetch<unknown>>,
9
- MF_DATA_FETCH_TYPE,
10
- Promise<DataFetch<unknown>>?
11
- ],
12
- [
13
- Promise<unknown>,
14
- ((data: unknown) => void)?,
15
- ((err: unknown) => void)?
16
- ]?,
17
- MF_DATA_FETCH_STATUS?
18
- ];
19
- export type MF_DATA_FETCH_MAP = Record<string, MF_DATA_FETCH_MAP_VALUE>;
20
- export type MF_SSR_DOWNGRADE = string[] | true | undefined;
21
- export type NoSSRRemoteInfo = {
22
- name: string;
23
- version: string;
24
- ssrPublicPath: string;
25
- ssrRemoteEntry: string;
26
- globalName: string;
27
- };
@@ -1,19 +0,0 @@
1
- import React, { ReactNode } from 'react';
2
- import type { DataFetchParams } from '../interfaces/global';
3
- export type ErrorInfo = {
4
- error: Error;
5
- errorType: number;
6
- dataFetchMapKey?: string;
7
- };
8
- export declare const transformError: (err: string | Error) => ErrorInfo;
9
- export interface AwaitProps<T> {
10
- resolve: T | Promise<T>;
11
- loading?: ReactNode;
12
- errorElement?: ReactNode | ((errorInfo: ErrorInfo) => ReactNode);
13
- children: (data: T) => ReactNode;
14
- params?: DataFetchParams;
15
- }
16
- export interface AwaitErrorHandlerProps<T = any> extends Omit<AwaitProps<T>, 'resolve'> {
17
- resolve: () => T | string;
18
- }
19
- export declare function AwaitDataFetch<T>({ resolve, loading, errorElement, children, params, }: AwaitProps<T>): React.JSX.Element;
@@ -1,26 +0,0 @@
1
- import React, { ReactNode } from 'react';
2
- import type { ErrorInfo } from './AwaitDataFetch';
3
- import type { DataFetchParams } from '../interfaces/global';
4
- type IProps = {
5
- id: string;
6
- injectScript?: boolean;
7
- injectLink?: boolean;
8
- };
9
- export type CreateRemoteComponentOptions<T, E extends keyof T> = {
10
- loader: () => Promise<T>;
11
- loading: React.ReactNode;
12
- fallback: ReactNode | ((errorInfo: ErrorInfo) => ReactNode);
13
- export?: E;
14
- dataFetchParams?: DataFetchParams;
15
- noSSR?: boolean;
16
- };
17
- type ReactKey = {
18
- key?: React.Key | null;
19
- };
20
- export declare function collectSSRAssets(options: IProps): React.ReactNode[];
21
- export declare function createRemoteComponent<T, E extends keyof T>(options: CreateRemoteComponentOptions<T, E>): (props: T[E] extends (...args: any) => any ? Parameters<T[E]>[0] extends undefined ? ReactKey : Parameters<T[E]>[0] & ReactKey : ReactKey) => React.JSX.Element;
22
- /**
23
- * @deprecated createRemoteSSRComponent is deprecated, please use createRemoteComponent instead!
24
- */
25
- export declare function createRemoteSSRComponent<T, E extends keyof T>(options: CreateRemoteComponentOptions<T, E>): (props: T[E] extends (...args: any) => any ? Parameters<T[E]>[0] extends undefined ? ReactKey : Parameters<T[E]>[0] & ReactKey : ReactKey) => React.JSX.Element;
26
- export {};
@@ -1,9 +0,0 @@
1
- import { createRemoteComponent } from './createRemoteComponent';
2
- import type { CreateRemoteComponentOptions } from './createRemoteComponent';
3
- export declare function wrapNoSSR<T, E extends keyof T>(createComponentFn: typeof createRemoteComponent<T, E>): (options: Omit<CreateRemoteComponentOptions<T, E>, "noSSR">) => (props: T[E] extends (...args: any) => any ? Parameters<T[E]>[0] extends undefined ? {
4
- key?: import("react").Key | null;
5
- } : Parameters<T[E]>[0] & {
6
- key?: import("react").Key | null;
7
- } : {
8
- key?: import("react").Key | null;
9
- }) => import("react").JSX.Element;
@@ -1,4 +0,0 @@
1
- import type { DataFetchParams, MF_SSR_DOWNGRADE, NoSSRRemoteInfo } from '../interfaces/global';
2
- export declare function getDowngradeTag(): MF_SSR_DOWNGRADE;
3
- export declare function callAllDowngrade(): void;
4
- export declare function callDowngrade(id: string, params?: DataFetchParams, remoteInfo?: NoSSRRemoteInfo): Promise<any>;