@module-federation/bridge-react 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/CHANGELOG.md +21 -0
- package/__tests__/bridge.spec.tsx +7 -7
- package/__tests__/createLazyComponent.spec.tsx +210 -0
- package/__tests__/prefetch.spec.ts +153 -0
- package/__tests__/setupTests.ts +3 -0
- package/dist/{bridge-base-P6pEjY1q.js → bridge-base-BoshEggF.mjs} +1 -1
- package/dist/{bridge-base-BBH982Tz.cjs → bridge-base-UGCwcMnG.js} +1 -1
- package/dist/data-fetch-server-middleware.cjs.js +163 -0
- package/dist/data-fetch-server-middleware.d.ts +15 -0
- package/dist/data-fetch-server-middleware.es.js +164 -0
- package/dist/data-fetch-utils.cjs.js +24 -0
- package/dist/data-fetch-utils.d.ts +81 -0
- package/dist/data-fetch-utils.es.js +26 -0
- package/dist/index-C0fDZB5b.js +45 -0
- package/dist/index-CqxytsLY.mjs +46 -0
- package/dist/index.cjs.js +35 -9
- package/dist/index.d.ts +140 -0
- package/dist/index.es.js +38 -12
- package/dist/index.esm-BCeUd-x9.mjs +418 -0
- package/dist/index.esm-j_1sIRzg.js +417 -0
- package/dist/lazy-load-component-plugin-C1tVve-W.js +521 -0
- package/dist/lazy-load-component-plugin-PERjiaFJ.mjs +522 -0
- package/dist/lazy-load-component-plugin.cjs.js +6 -0
- package/dist/lazy-load-component-plugin.d.ts +16 -0
- package/dist/lazy-load-component-plugin.es.js +6 -0
- package/dist/lazy-utils.cjs.js +24 -0
- package/dist/lazy-utils.d.ts +149 -0
- package/dist/lazy-utils.es.js +24 -0
- package/dist/plugin.d.ts +13 -4
- package/dist/prefetch-CZvoIftg.js +1334 -0
- package/dist/prefetch-Dux8GUpr.mjs +1335 -0
- package/dist/router-v5.cjs.js +1 -1
- package/dist/router-v5.d.ts +9 -0
- package/dist/router-v5.es.js +1 -1
- package/dist/router-v6.cjs.js +1 -1
- package/dist/router-v6.d.ts +9 -0
- package/dist/router-v6.es.js +1 -1
- package/dist/router.cjs.js +1 -1
- package/dist/router.d.ts +9 -0
- package/dist/router.es.js +1 -1
- package/dist/utils-Cy-amYU5.mjs +2016 -0
- package/dist/utils-iEVlDmyk.js +2015 -0
- package/dist/v18.cjs.js +1 -1
- package/dist/v18.d.ts +9 -0
- package/dist/v18.es.js +1 -1
- package/dist/v19.cjs.js +1 -1
- package/dist/v19.d.ts +9 -0
- package/dist/v19.es.js +1 -1
- package/package.json +47 -5
- package/src/index.ts +32 -1
- package/src/lazy/AwaitDataFetch.tsx +215 -0
- package/src/lazy/constant.ts +30 -0
- package/src/lazy/createLazyComponent.tsx +411 -0
- package/src/lazy/data-fetch/cache.ts +291 -0
- package/src/lazy/data-fetch/call-data-fetch.ts +13 -0
- package/src/lazy/data-fetch/data-fetch-server-middleware.ts +196 -0
- package/src/lazy/data-fetch/index.ts +16 -0
- package/src/lazy/data-fetch/inject-data-fetch.ts +109 -0
- package/src/lazy/data-fetch/prefetch.ts +106 -0
- package/src/lazy/data-fetch/runtime-plugin.ts +115 -0
- package/src/lazy/index.ts +35 -0
- package/src/lazy/logger.ts +6 -0
- package/src/lazy/types.ts +75 -0
- package/src/lazy/utils.ts +372 -0
- package/src/lazy/wrapNoSSR.tsx +10 -0
- package/src/plugins/lazy-load-component-plugin.spec.ts +21 -0
- package/src/plugins/lazy-load-component-plugin.ts +57 -0
- package/src/provider/plugin.ts +4 -4
- package/src/remote/component.tsx +3 -3
- package/src/remote/create.tsx +17 -4
- package/tsconfig.json +1 -1
- package/vite.config.ts +13 -0
- package/vitest.config.ts +6 -1
- package/dist/index-Cv3p6r66.cjs +0 -235
- package/dist/index-D4yt7Udv.js +0 -236
- package/src/.eslintrc.js +0 -9
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const prefetch = require("./prefetch-CZvoIftg.js");
|
|
3
|
+
const lazyUtils = require("./utils-iEVlDmyk.js");
|
|
4
|
+
const React = require("react");
|
|
5
|
+
const autoFetchData = () => {
|
|
6
|
+
lazyUtils.initDataFetchMap();
|
|
7
|
+
prefetch.injectDataFetch();
|
|
8
|
+
return {
|
|
9
|
+
name: "auto-fetch-data-plugin",
|
|
10
|
+
afterLoadSnapshot(args) {
|
|
11
|
+
const { id, moduleInfo, remoteSnapshot, host } = args;
|
|
12
|
+
if (typeof id === "string" && lazyUtils.isDataLoaderExpose(id)) {
|
|
13
|
+
return args;
|
|
14
|
+
}
|
|
15
|
+
if (!remoteSnapshot || !id || !("modules" in remoteSnapshot)) {
|
|
16
|
+
return args;
|
|
17
|
+
}
|
|
18
|
+
const { name, alias } = moduleInfo;
|
|
19
|
+
const dataFetchInfo = lazyUtils.getDataFetchInfo({
|
|
20
|
+
name,
|
|
21
|
+
alias,
|
|
22
|
+
id,
|
|
23
|
+
remoteSnapshot
|
|
24
|
+
});
|
|
25
|
+
if (!dataFetchInfo) {
|
|
26
|
+
return args;
|
|
27
|
+
}
|
|
28
|
+
const { dataFetchId, dataFetchName } = dataFetchInfo;
|
|
29
|
+
const dataFetchMapKey = lazyUtils.getDataFetchMapKey(dataFetchInfo, {
|
|
30
|
+
name: host.name,
|
|
31
|
+
version: host.options.version
|
|
32
|
+
});
|
|
33
|
+
lazyUtils.logger.debug(
|
|
34
|
+
"======= auto fetch plugin dataFetchMapKey: ",
|
|
35
|
+
dataFetchMapKey
|
|
36
|
+
);
|
|
37
|
+
if (!dataFetchMapKey) {
|
|
38
|
+
return args;
|
|
39
|
+
}
|
|
40
|
+
const dataFetchItem = lazyUtils.getDataFetchItem(dataFetchMapKey);
|
|
41
|
+
if (dataFetchItem) {
|
|
42
|
+
return args;
|
|
43
|
+
}
|
|
44
|
+
const dataFetchMap = lazyUtils.getDataFetchMap();
|
|
45
|
+
const hasSSRAsset = Boolean(remoteSnapshot.ssrRemoteEntry);
|
|
46
|
+
const hasDataFetchClient = Boolean(
|
|
47
|
+
remoteSnapshot.modules.find(
|
|
48
|
+
(module2) => module2.moduleName === `${dataFetchName}${lazyUtils.DATA_FETCH_CLIENT_SUFFIX}`
|
|
49
|
+
)
|
|
50
|
+
);
|
|
51
|
+
const downgradeType = hasDataFetchClient ? lazyUtils.MF_DATA_FETCH_TYPE.FETCH_CLIENT : hasSSRAsset ? lazyUtils.MF_DATA_FETCH_TYPE.FETCH_SERVER : lazyUtils.MF_DATA_FETCH_TYPE.FETCH_CLIENT;
|
|
52
|
+
let finalDataFetchId = dataFetchId;
|
|
53
|
+
if (!lazyUtils.isServerEnv()) {
|
|
54
|
+
finalDataFetchId = downgradeType === lazyUtils.MF_DATA_FETCH_TYPE.FETCH_CLIENT ? hasDataFetchClient ? `${dataFetchId}${lazyUtils.DATA_FETCH_CLIENT_SUFFIX}` : dataFetchId : dataFetchId;
|
|
55
|
+
}
|
|
56
|
+
const getDataFetchGetter = () => lazyUtils.loadDataFetchModule(host, finalDataFetchId);
|
|
57
|
+
const dataFetchFnItem = [
|
|
58
|
+
getDataFetchGetter,
|
|
59
|
+
downgradeType
|
|
60
|
+
];
|
|
61
|
+
if (typeof window === "undefined" || lazyUtils.isCSROnly()) {
|
|
62
|
+
dataFetchFnItem.push(getDataFetchGetter());
|
|
63
|
+
}
|
|
64
|
+
dataFetchMap[dataFetchMapKey] = [
|
|
65
|
+
dataFetchFnItem,
|
|
66
|
+
void 0,
|
|
67
|
+
lazyUtils.MF_DATA_FETCH_STATUS.AWAIT
|
|
68
|
+
];
|
|
69
|
+
return args;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
function isPromise(obj) {
|
|
74
|
+
return !!obj && (typeof obj === "object" || typeof obj === "function") && typeof obj.then === "function";
|
|
75
|
+
}
|
|
76
|
+
const AWAIT_ERROR_PREFIX = "<Await /> caught the following error during render: ";
|
|
77
|
+
const transformError = (err) => {
|
|
78
|
+
const errMsg = err instanceof Error ? err.message : err;
|
|
79
|
+
const originalMsg = errMsg.replace(AWAIT_ERROR_PREFIX, "");
|
|
80
|
+
const dataFetchMapKey = lazyUtils.getDataFetchIdWithErrorMsgs(originalMsg);
|
|
81
|
+
if (originalMsg.indexOf(lazyUtils.DATA_FETCH_ERROR_PREFIX) === 0) {
|
|
82
|
+
return {
|
|
83
|
+
error: new Error(
|
|
84
|
+
originalMsg.replace(lazyUtils.DATA_FETCH_ERROR_PREFIX, "").replace(lazyUtils.wrapDataFetchId(dataFetchMapKey), "")
|
|
85
|
+
),
|
|
86
|
+
errorType: lazyUtils.ERROR_TYPE.DATA_FETCH,
|
|
87
|
+
dataFetchMapKey
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
if (originalMsg.indexOf(lazyUtils.LOAD_REMOTE_ERROR_PREFIX) === 0) {
|
|
91
|
+
return {
|
|
92
|
+
error: new Error(
|
|
93
|
+
originalMsg.replace(lazyUtils.LOAD_REMOTE_ERROR_PREFIX, "").replace(lazyUtils.wrapDataFetchId(dataFetchMapKey), "")
|
|
94
|
+
),
|
|
95
|
+
errorType: lazyUtils.ERROR_TYPE.LOAD_REMOTE,
|
|
96
|
+
dataFetchMapKey
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
error: new Error(originalMsg.replace(lazyUtils.wrapDataFetchId(dataFetchMapKey), "")),
|
|
101
|
+
errorType: lazyUtils.ERROR_TYPE.UNKNOWN,
|
|
102
|
+
dataFetchMapKey
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
const DefaultLoading = /* @__PURE__ */ React.createElement(React.Fragment, null);
|
|
106
|
+
const DefaultErrorElement = (_data) => /* @__PURE__ */ React.createElement("div", null, "Error");
|
|
107
|
+
function AwaitDataFetch({
|
|
108
|
+
resolve,
|
|
109
|
+
loading = DefaultLoading,
|
|
110
|
+
errorElement = DefaultErrorElement,
|
|
111
|
+
children,
|
|
112
|
+
params,
|
|
113
|
+
delayLoading
|
|
114
|
+
}) {
|
|
115
|
+
const dataRef = React.useRef();
|
|
116
|
+
const data = dataRef.current || resolve;
|
|
117
|
+
const getData = isPromise(data) ? fetchData(data, dataRef) : () => data;
|
|
118
|
+
return /* @__PURE__ */ React.createElement(
|
|
119
|
+
AwaitSuspense,
|
|
120
|
+
{
|
|
121
|
+
params,
|
|
122
|
+
loading,
|
|
123
|
+
errorElement,
|
|
124
|
+
delayLoading,
|
|
125
|
+
resolve: getData
|
|
126
|
+
},
|
|
127
|
+
children
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
const DelayedLoading = ({
|
|
131
|
+
delayLoading,
|
|
132
|
+
children
|
|
133
|
+
}) => {
|
|
134
|
+
const [show, setShow] = React.useState(false);
|
|
135
|
+
const timerSet = React.useRef(false);
|
|
136
|
+
if (!delayLoading) {
|
|
137
|
+
return children;
|
|
138
|
+
}
|
|
139
|
+
if (typeof window !== "undefined" && !show && !timerSet.current) {
|
|
140
|
+
timerSet.current = true;
|
|
141
|
+
setTimeout(() => {
|
|
142
|
+
setShow(true);
|
|
143
|
+
}, delayLoading);
|
|
144
|
+
}
|
|
145
|
+
return show ? children : null;
|
|
146
|
+
};
|
|
147
|
+
function AwaitSuspense({
|
|
148
|
+
resolve,
|
|
149
|
+
children,
|
|
150
|
+
loading = DefaultLoading,
|
|
151
|
+
errorElement = DefaultErrorElement,
|
|
152
|
+
delayLoading
|
|
153
|
+
}) {
|
|
154
|
+
return /* @__PURE__ */ React.createElement(
|
|
155
|
+
React.Suspense,
|
|
156
|
+
{
|
|
157
|
+
fallback: /* @__PURE__ */ React.createElement(DelayedLoading, { delayLoading }, loading)
|
|
158
|
+
},
|
|
159
|
+
/* @__PURE__ */ React.createElement(ResolveAwait, { resolve, errorElement }, children)
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
function ResolveAwait({
|
|
163
|
+
children,
|
|
164
|
+
resolve,
|
|
165
|
+
errorElement,
|
|
166
|
+
params
|
|
167
|
+
}) {
|
|
168
|
+
const data = resolve();
|
|
169
|
+
lazyUtils.logger.debug("resolve data: ", data);
|
|
170
|
+
if (typeof data === "string" && data.indexOf(AWAIT_ERROR_PREFIX) === 0) {
|
|
171
|
+
const transformedError = transformError(data);
|
|
172
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, typeof errorElement === "function" ? /* @__PURE__ */ React.createElement(React.Fragment, null, globalThis.FEDERATION_SSR && /* @__PURE__ */ React.createElement(
|
|
173
|
+
"script",
|
|
174
|
+
{
|
|
175
|
+
suppressHydrationWarning: true,
|
|
176
|
+
dangerouslySetInnerHTML: {
|
|
177
|
+
__html: String.raw`
|
|
178
|
+
globalThis['${lazyUtils.DATA_FETCH_FUNCTION}'] = globalThis['${lazyUtils.DATA_FETCH_FUNCTION}'] || []
|
|
179
|
+
globalThis['${lazyUtils.DATA_FETCH_FUNCTION}'].push([${transformedError.dataFetchMapKey ? `'${transformedError.dataFetchMapKey}'` : ""},${params ? JSON.stringify(params) : null},true]);`
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
), errorElement(transformedError)) : errorElement);
|
|
183
|
+
}
|
|
184
|
+
const toRender = typeof children === "function" ? children(data) : children;
|
|
185
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, toRender);
|
|
186
|
+
}
|
|
187
|
+
const fetchData = (promise, ref) => {
|
|
188
|
+
let data;
|
|
189
|
+
let status = "pending";
|
|
190
|
+
const suspender = promise.then((res) => {
|
|
191
|
+
status = "success";
|
|
192
|
+
data = res;
|
|
193
|
+
ref.current = res;
|
|
194
|
+
}).catch((e) => {
|
|
195
|
+
status = "success";
|
|
196
|
+
console.warn(e);
|
|
197
|
+
data = AWAIT_ERROR_PREFIX + e;
|
|
198
|
+
});
|
|
199
|
+
return () => {
|
|
200
|
+
if (status === "pending") {
|
|
201
|
+
throw suspender;
|
|
202
|
+
}
|
|
203
|
+
return data;
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
function getTargetModuleInfo(id, instance) {
|
|
207
|
+
if (!instance) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const loadedRemoteInfo = lazyUtils.getLoadedRemoteInfos(id, instance);
|
|
211
|
+
if (!loadedRemoteInfo) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const { snapshot } = loadedRemoteInfo;
|
|
215
|
+
if (!snapshot) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
|
|
219
|
+
if (!publicPath) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const modules = "modules" in snapshot ? snapshot.modules : [];
|
|
223
|
+
const targetModule = modules.find(
|
|
224
|
+
(m) => m.modulePath === loadedRemoteInfo.expose
|
|
225
|
+
);
|
|
226
|
+
if (!targetModule) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
|
|
230
|
+
if (!remoteEntry) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
module: targetModule,
|
|
235
|
+
publicPath,
|
|
236
|
+
remoteEntry
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function collectSSRAssets(options) {
|
|
240
|
+
const {
|
|
241
|
+
id,
|
|
242
|
+
injectLink = true,
|
|
243
|
+
injectScript = false
|
|
244
|
+
} = typeof options === "string" ? { id: options } : options;
|
|
245
|
+
const links = [];
|
|
246
|
+
const scripts = [];
|
|
247
|
+
const instance = options.instance;
|
|
248
|
+
if (!instance || !injectLink && !injectScript) {
|
|
249
|
+
return [...scripts, ...links];
|
|
250
|
+
}
|
|
251
|
+
const moduleAndPublicPath = getTargetModuleInfo(id, instance);
|
|
252
|
+
if (!moduleAndPublicPath) {
|
|
253
|
+
return [...scripts, ...links];
|
|
254
|
+
}
|
|
255
|
+
const { module: targetModule, publicPath, remoteEntry } = moduleAndPublicPath;
|
|
256
|
+
if (injectLink) {
|
|
257
|
+
[...targetModule.assets.css.sync, ...targetModule.assets.css.async].sort().forEach((file, index) => {
|
|
258
|
+
links.push(
|
|
259
|
+
/* @__PURE__ */ React.createElement(
|
|
260
|
+
"link",
|
|
261
|
+
{
|
|
262
|
+
key: `${file.split(".")[0]}_${index}`,
|
|
263
|
+
href: `${publicPath}${file}`,
|
|
264
|
+
rel: "stylesheet",
|
|
265
|
+
type: "text/css"
|
|
266
|
+
}
|
|
267
|
+
)
|
|
268
|
+
);
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
if (injectScript) {
|
|
272
|
+
scripts.push(
|
|
273
|
+
/* @__PURE__ */ React.createElement(
|
|
274
|
+
"script",
|
|
275
|
+
{
|
|
276
|
+
async: true,
|
|
277
|
+
key: remoteEntry.split(".")[0],
|
|
278
|
+
src: `${publicPath}${remoteEntry}`,
|
|
279
|
+
crossOrigin: "anonymous"
|
|
280
|
+
}
|
|
281
|
+
)
|
|
282
|
+
);
|
|
283
|
+
[...targetModule.assets.js.sync].sort().forEach((file, index) => {
|
|
284
|
+
scripts.push(
|
|
285
|
+
/* @__PURE__ */ React.createElement(
|
|
286
|
+
"script",
|
|
287
|
+
{
|
|
288
|
+
key: `${file.split(".")[0]}_${index}`,
|
|
289
|
+
async: true,
|
|
290
|
+
src: `${publicPath}${file}`,
|
|
291
|
+
crossOrigin: "anonymous"
|
|
292
|
+
}
|
|
293
|
+
)
|
|
294
|
+
);
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
return [...scripts, ...links];
|
|
298
|
+
}
|
|
299
|
+
function getServerNeedRemoteInfo(loadedRemoteInfo, id, noSSR) {
|
|
300
|
+
var _a;
|
|
301
|
+
if (noSSR || typeof window !== "undefined" && window.location.href !== window[lazyUtils.FS_HREF]) {
|
|
302
|
+
if (!(loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.version)) {
|
|
303
|
+
throw new Error(`${loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.name} version is empty`);
|
|
304
|
+
}
|
|
305
|
+
const { snapshot } = loadedRemoteInfo;
|
|
306
|
+
if (!snapshot) {
|
|
307
|
+
throw new Error(`${loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.name} snapshot is empty`);
|
|
308
|
+
}
|
|
309
|
+
const dataFetchItem = lazyUtils.getDataFetchItem(id);
|
|
310
|
+
const isFetchServer = ((_a = dataFetchItem == null ? void 0 : dataFetchItem[0]) == null ? void 0 : _a[1]) === lazyUtils.MF_DATA_FETCH_TYPE.FETCH_SERVER;
|
|
311
|
+
if (isFetchServer && (!("ssrPublicPath" in snapshot) || !snapshot.ssrPublicPath)) {
|
|
312
|
+
throw new Error(
|
|
313
|
+
`ssrPublicPath is required while fetching ${loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.name} data in SSR project!`
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
if (isFetchServer && (!("ssrRemoteEntry" in snapshot) || !snapshot.ssrRemoteEntry)) {
|
|
317
|
+
throw new Error(
|
|
318
|
+
`ssrRemoteEntry is required while loading ${loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.name} data loader in SSR project!`
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
return {
|
|
322
|
+
name: loadedRemoteInfo.name,
|
|
323
|
+
version: loadedRemoteInfo.version,
|
|
324
|
+
ssrPublicPath: "ssrPublicPath" in snapshot ? snapshot.ssrPublicPath || "" : "",
|
|
325
|
+
ssrRemoteEntry: "ssrRemoteEntry" in snapshot ? snapshot.ssrRemoteEntry || "" : "",
|
|
326
|
+
globalName: loadedRemoteInfo.entryGlobalName
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
return void 0;
|
|
330
|
+
}
|
|
331
|
+
function createLazyComponent(options) {
|
|
332
|
+
const { instance, injectScript, injectLink } = options;
|
|
333
|
+
if (!instance) {
|
|
334
|
+
throw new Error("instance is required for createLazyComponent!");
|
|
335
|
+
}
|
|
336
|
+
const exportName = (options == null ? void 0 : options.export) || "default";
|
|
337
|
+
const callLoader = async () => {
|
|
338
|
+
lazyUtils.logger.debug("callLoader start", Date.now());
|
|
339
|
+
const m = await options.loader();
|
|
340
|
+
lazyUtils.logger.debug("callLoader end", Date.now());
|
|
341
|
+
if (!m) {
|
|
342
|
+
throw new Error("load remote failed");
|
|
343
|
+
}
|
|
344
|
+
return m;
|
|
345
|
+
};
|
|
346
|
+
const getData = async (noSSR) => {
|
|
347
|
+
let loadedRemoteInfo;
|
|
348
|
+
let moduleId;
|
|
349
|
+
try {
|
|
350
|
+
const m = await callLoader();
|
|
351
|
+
moduleId = m && m[Symbol.for("mf_module_id")];
|
|
352
|
+
if (!moduleId) {
|
|
353
|
+
throw new Error("moduleId is empty");
|
|
354
|
+
}
|
|
355
|
+
loadedRemoteInfo = lazyUtils.getLoadedRemoteInfos(moduleId, instance);
|
|
356
|
+
if (!loadedRemoteInfo) {
|
|
357
|
+
throw new Error(`can not find loaded remote('${moduleId}') info!`);
|
|
358
|
+
}
|
|
359
|
+
} catch (e) {
|
|
360
|
+
const errMsg = `${lazyUtils.LOAD_REMOTE_ERROR_PREFIX}${e}`;
|
|
361
|
+
lazyUtils.logger.debug(e);
|
|
362
|
+
throw new Error(errMsg);
|
|
363
|
+
}
|
|
364
|
+
let dataFetchMapKey;
|
|
365
|
+
try {
|
|
366
|
+
dataFetchMapKey = lazyUtils.getDataFetchMapKey(
|
|
367
|
+
lazyUtils.getDataFetchInfo({
|
|
368
|
+
name: loadedRemoteInfo.name,
|
|
369
|
+
alias: loadedRemoteInfo.alias,
|
|
370
|
+
id: moduleId,
|
|
371
|
+
remoteSnapshot: loadedRemoteInfo.snapshot
|
|
372
|
+
}),
|
|
373
|
+
{ name: instance.name, version: instance.options.version }
|
|
374
|
+
);
|
|
375
|
+
lazyUtils.logger.debug("getData dataFetchMapKey: ", dataFetchMapKey);
|
|
376
|
+
if (!dataFetchMapKey) {
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
const data = await lazyUtils.fetchData(
|
|
380
|
+
dataFetchMapKey,
|
|
381
|
+
{
|
|
382
|
+
...options.dataFetchParams,
|
|
383
|
+
isDowngrade: false
|
|
384
|
+
},
|
|
385
|
+
getServerNeedRemoteInfo(loadedRemoteInfo, dataFetchMapKey, noSSR)
|
|
386
|
+
);
|
|
387
|
+
lazyUtils.setDataFetchItemLoadedStatus(dataFetchMapKey);
|
|
388
|
+
lazyUtils.logger.debug("get data res: \n", data);
|
|
389
|
+
return data;
|
|
390
|
+
} catch (err) {
|
|
391
|
+
const errMsg = `${lazyUtils.DATA_FETCH_ERROR_PREFIX}${lazyUtils.wrapDataFetchId(dataFetchMapKey)}${err}`;
|
|
392
|
+
lazyUtils.logger.debug(errMsg);
|
|
393
|
+
throw new Error(errMsg);
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
const LazyComponent = React.lazy(async () => {
|
|
397
|
+
const m = await callLoader();
|
|
398
|
+
const moduleId = m && m[Symbol.for("mf_module_id")];
|
|
399
|
+
const loadedRemoteInfo = lazyUtils.getLoadedRemoteInfos(moduleId, instance);
|
|
400
|
+
loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.snapshot;
|
|
401
|
+
const dataFetchMapKey = loadedRemoteInfo ? lazyUtils.getDataFetchMapKey(
|
|
402
|
+
lazyUtils.getDataFetchInfo({
|
|
403
|
+
name: loadedRemoteInfo.name,
|
|
404
|
+
alias: loadedRemoteInfo.alias,
|
|
405
|
+
id: moduleId,
|
|
406
|
+
remoteSnapshot: loadedRemoteInfo.snapshot
|
|
407
|
+
}),
|
|
408
|
+
{ name: instance.name, version: instance == null ? void 0 : instance.options.version }
|
|
409
|
+
) : void 0;
|
|
410
|
+
lazyUtils.logger.debug("LazyComponent dataFetchMapKey: ", dataFetchMapKey);
|
|
411
|
+
const assets = collectSSRAssets({
|
|
412
|
+
id: moduleId,
|
|
413
|
+
instance,
|
|
414
|
+
injectLink,
|
|
415
|
+
injectScript
|
|
416
|
+
});
|
|
417
|
+
const Com = m[exportName];
|
|
418
|
+
if (exportName in m && typeof Com === "function") {
|
|
419
|
+
return {
|
|
420
|
+
default: (props) => /* @__PURE__ */ React.createElement(React.Fragment, null, globalThis.FEDERATION_SSR && dataFetchMapKey && /* @__PURE__ */ React.createElement(
|
|
421
|
+
"script",
|
|
422
|
+
{
|
|
423
|
+
suppressHydrationWarning: true,
|
|
424
|
+
dangerouslySetInnerHTML: {
|
|
425
|
+
__html: String.raw`
|
|
426
|
+
globalThis['${lazyUtils.DATA_FETCH_FUNCTION}'] = globalThis['${lazyUtils.DATA_FETCH_FUNCTION}'] || [];
|
|
427
|
+
globalThis['${lazyUtils.DATA_FETCH_FUNCTION}'].push(['${dataFetchMapKey}',${JSON.stringify(props.mfData)}]);
|
|
428
|
+
`
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
), globalThis.FEDERATION_SSR && assets, /* @__PURE__ */ React.createElement(Com, { ...props }))
|
|
432
|
+
};
|
|
433
|
+
} else {
|
|
434
|
+
throw Error(
|
|
435
|
+
`Make sure that ${moduleId} has the correct export when export is ${String(
|
|
436
|
+
exportName
|
|
437
|
+
)}`
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
return (props) => {
|
|
442
|
+
const { key, ...args } = props;
|
|
443
|
+
if (!options.noSSR) {
|
|
444
|
+
return /* @__PURE__ */ React.createElement(
|
|
445
|
+
AwaitDataFetch,
|
|
446
|
+
{
|
|
447
|
+
resolve: getData(options.noSSR),
|
|
448
|
+
loading: options.loading,
|
|
449
|
+
delayLoading: options.delayLoading,
|
|
450
|
+
errorElement: options.fallback
|
|
451
|
+
},
|
|
452
|
+
(data) => /* @__PURE__ */ React.createElement(LazyComponent, { ...args, mfData: data })
|
|
453
|
+
);
|
|
454
|
+
} else {
|
|
455
|
+
const [data, setData] = React.useState(null);
|
|
456
|
+
const [loading, setLoading] = React.useState(true);
|
|
457
|
+
const [error, setError] = React.useState(null);
|
|
458
|
+
React.useEffect(() => {
|
|
459
|
+
let isMounted = true;
|
|
460
|
+
const fetchDataAsync = async () => {
|
|
461
|
+
try {
|
|
462
|
+
setLoading(true);
|
|
463
|
+
const result = await getData(options.noSSR);
|
|
464
|
+
if (isMounted) {
|
|
465
|
+
setData(result);
|
|
466
|
+
}
|
|
467
|
+
} catch (e) {
|
|
468
|
+
if (isMounted) {
|
|
469
|
+
setError(transformError(e));
|
|
470
|
+
}
|
|
471
|
+
} finally {
|
|
472
|
+
if (isMounted) {
|
|
473
|
+
setLoading(false);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
fetchDataAsync();
|
|
478
|
+
return () => {
|
|
479
|
+
isMounted = false;
|
|
480
|
+
};
|
|
481
|
+
}, []);
|
|
482
|
+
if (loading) {
|
|
483
|
+
return /* @__PURE__ */ React.createElement(DelayedLoading, { delayLoading: options.delayLoading }, options.loading);
|
|
484
|
+
}
|
|
485
|
+
if (error) {
|
|
486
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, typeof options.fallback === "function" ? options.fallback(error) : options.fallback);
|
|
487
|
+
}
|
|
488
|
+
return /* @__PURE__ */ React.createElement(LazyComponent, { ...args, mfData: data });
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
function lazyLoadComponentPlugin() {
|
|
493
|
+
return {
|
|
494
|
+
name: "lazy-load-component-plugin",
|
|
495
|
+
apply(instance) {
|
|
496
|
+
instance.registerPlugins([autoFetchData()]);
|
|
497
|
+
instance.createLazyComponent = (options) => {
|
|
498
|
+
return createLazyComponent({
|
|
499
|
+
instance,
|
|
500
|
+
...options
|
|
501
|
+
});
|
|
502
|
+
};
|
|
503
|
+
instance.prefetch = (options) => {
|
|
504
|
+
return prefetch.prefetch({
|
|
505
|
+
instance,
|
|
506
|
+
...options
|
|
507
|
+
});
|
|
508
|
+
};
|
|
509
|
+
instance.collectSSRAssets = (options) => {
|
|
510
|
+
return collectSSRAssets({
|
|
511
|
+
instance,
|
|
512
|
+
...options
|
|
513
|
+
});
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
exports.autoFetchData = autoFetchData;
|
|
519
|
+
exports.collectSSRAssets = collectSSRAssets;
|
|
520
|
+
exports.createLazyComponent = createLazyComponent;
|
|
521
|
+
exports.lazyLoadComponentPlugin = lazyLoadComponentPlugin;
|