@module-federation/bridge-react 0.0.0-next-20250707074728 → 0.0.0-next-20250708033956
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 +8 -4
- package/__tests__/bridge.spec.tsx +7 -7
- 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-runtime-plugin.cjs.js +73 -0
- package/dist/data-fetch-runtime-plugin.d.ts +6 -0
- package/dist/data-fetch-runtime-plugin.es.js +74 -0
- package/dist/data-fetch-server-middleware.cjs.js +163 -0
- package/dist/data-fetch-server-middleware.d.ts +6 -0
- package/dist/data-fetch-server-middleware.es.js +164 -0
- package/dist/data-fetch-utils.cjs.js +1273 -0
- package/dist/data-fetch-utils.d.ts +77 -0
- package/dist/data-fetch-utils.es.js +1275 -0
- package/dist/index-C0fDZB5b.js +45 -0
- package/dist/index-CqxytsLY.mjs +46 -0
- package/dist/index.cjs.js +461 -9
- package/dist/index.d.ts +143 -0
- package/dist/index.es.js +464 -11
- package/dist/index.esm-BCeUd-x9.mjs +418 -0
- package/dist/index.esm-j_1sIRzg.js +417 -0
- package/dist/inject-data-fetch-CAvi-gSf.js +79 -0
- package/dist/inject-data-fetch-errCdqBS.mjs +80 -0
- package/dist/lazy-utils.cjs.js +24 -0
- package/dist/lazy-utils.d.ts +140 -0
- package/dist/lazy-utils.es.js +24 -0
- package/dist/plugin.d.ts +4 -4
- package/dist/router-v5.cjs.js +1 -1
- package/dist/router-v5.es.js +1 -1
- package/dist/router-v6.cjs.js +1 -1
- package/dist/router-v6.es.js +1 -1
- package/dist/router.cjs.js +1 -1
- package/dist/router.es.js +1 -1
- package/dist/utils-Bk8hGjjF.mjs +2016 -0
- package/dist/utils-iEVlDmyk.js +2015 -0
- package/dist/v18.cjs.js +1 -1
- package/dist/v18.es.js +1 -1
- package/dist/v19.cjs.js +1 -1
- package/dist/v19.es.js +1 -1
- package/package.json +45 -5
- package/src/index.ts +30 -1
- package/src/lazy/AwaitDataFetch.tsx +215 -0
- package/src/lazy/constant.ts +30 -0
- package/src/lazy/createLazyComponent.tsx +404 -0
- package/src/lazy/data-fetch/cache.ts +296 -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 +15 -0
- package/src/lazy/data-fetch/inject-data-fetch.ts +109 -0
- package/src/lazy/data-fetch/prefetch.ts +102 -0
- package/src/lazy/data-fetch/runtime-plugin.ts +116 -0
- package/src/lazy/index.ts +31 -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/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/dist/index-Cv3p6r66.cjs +0 -235
- package/dist/index-D4yt7Udv.js +0 -236
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const React = require("react");
|
|
3
|
+
const index_esm = require("./index.esm-j_1sIRzg.js");
|
|
4
|
+
const RouterContext = React.createContext(null);
|
|
5
|
+
const LoggerInstance = index_esm.createLogger(
|
|
6
|
+
"[ Module Federation Bridge React ]"
|
|
7
|
+
);
|
|
8
|
+
function pathJoin(...args) {
|
|
9
|
+
const res = args.reduce((res2, path) => {
|
|
10
|
+
let nPath = path;
|
|
11
|
+
if (!nPath || typeof nPath !== "string") {
|
|
12
|
+
return res2;
|
|
13
|
+
}
|
|
14
|
+
if (nPath[0] !== "/") {
|
|
15
|
+
nPath = `/${nPath}`;
|
|
16
|
+
}
|
|
17
|
+
const lastIndex = nPath.length - 1;
|
|
18
|
+
if (nPath[lastIndex] === "/") {
|
|
19
|
+
nPath = nPath.substring(0, lastIndex);
|
|
20
|
+
}
|
|
21
|
+
return res2 + nPath;
|
|
22
|
+
}, "");
|
|
23
|
+
return res || "/";
|
|
24
|
+
}
|
|
25
|
+
const getModuleName = (id) => {
|
|
26
|
+
if (!id) {
|
|
27
|
+
return id;
|
|
28
|
+
}
|
|
29
|
+
const idArray = id.split("/");
|
|
30
|
+
if (idArray.length < 2) {
|
|
31
|
+
return id;
|
|
32
|
+
}
|
|
33
|
+
return idArray[0] + "/" + idArray[1];
|
|
34
|
+
};
|
|
35
|
+
const getRootDomDefaultClassName = (moduleName) => {
|
|
36
|
+
if (!moduleName) {
|
|
37
|
+
return "";
|
|
38
|
+
}
|
|
39
|
+
const name = getModuleName(moduleName).replace(/\@/, "").replace(/\//, "-");
|
|
40
|
+
return `bridge-root-component-${name}`;
|
|
41
|
+
};
|
|
42
|
+
exports.LoggerInstance = LoggerInstance;
|
|
43
|
+
exports.RouterContext = RouterContext;
|
|
44
|
+
exports.getRootDomDefaultClassName = getRootDomDefaultClassName;
|
|
45
|
+
exports.pathJoin = pathJoin;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import React__default from "react";
|
|
2
|
+
import { c as createLogger } from "./index.esm-BCeUd-x9.mjs";
|
|
3
|
+
const RouterContext = React__default.createContext(null);
|
|
4
|
+
const LoggerInstance = createLogger(
|
|
5
|
+
"[ Module Federation Bridge React ]"
|
|
6
|
+
);
|
|
7
|
+
function pathJoin(...args) {
|
|
8
|
+
const res = args.reduce((res2, path) => {
|
|
9
|
+
let nPath = path;
|
|
10
|
+
if (!nPath || typeof nPath !== "string") {
|
|
11
|
+
return res2;
|
|
12
|
+
}
|
|
13
|
+
if (nPath[0] !== "/") {
|
|
14
|
+
nPath = `/${nPath}`;
|
|
15
|
+
}
|
|
16
|
+
const lastIndex = nPath.length - 1;
|
|
17
|
+
if (nPath[lastIndex] === "/") {
|
|
18
|
+
nPath = nPath.substring(0, lastIndex);
|
|
19
|
+
}
|
|
20
|
+
return res2 + nPath;
|
|
21
|
+
}, "");
|
|
22
|
+
return res || "/";
|
|
23
|
+
}
|
|
24
|
+
const getModuleName = (id) => {
|
|
25
|
+
if (!id) {
|
|
26
|
+
return id;
|
|
27
|
+
}
|
|
28
|
+
const idArray = id.split("/");
|
|
29
|
+
if (idArray.length < 2) {
|
|
30
|
+
return id;
|
|
31
|
+
}
|
|
32
|
+
return idArray[0] + "/" + idArray[1];
|
|
33
|
+
};
|
|
34
|
+
const getRootDomDefaultClassName = (moduleName) => {
|
|
35
|
+
if (!moduleName) {
|
|
36
|
+
return "";
|
|
37
|
+
}
|
|
38
|
+
const name = getModuleName(moduleName).replace(/\@/, "").replace(/\//, "-");
|
|
39
|
+
return `bridge-root-component-${name}`;
|
|
40
|
+
};
|
|
41
|
+
export {
|
|
42
|
+
LoggerInstance as L,
|
|
43
|
+
RouterContext as R,
|
|
44
|
+
getRootDomDefaultClassName as g,
|
|
45
|
+
pathJoin as p
|
|
46
|
+
};
|
package/dist/index.cjs.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const bridgeBase = require("./bridge-base-
|
|
3
|
+
const bridgeBase = require("./bridge-base-UGCwcMnG.js");
|
|
4
4
|
const ReactDOM = require("react-dom");
|
|
5
5
|
const React = require("react");
|
|
6
|
-
const index = require("./index-
|
|
6
|
+
const index = require("./index-C0fDZB5b.js");
|
|
7
7
|
const ReactRouterDOM = require("react-router-dom");
|
|
8
8
|
const plugin = require("./plugin.cjs.js");
|
|
9
|
+
const dataFetchRuntimePlugin = require("./data-fetch-runtime-plugin.cjs.js");
|
|
10
|
+
const lazyUtils = require("./utils-iEVlDmyk.js");
|
|
11
|
+
const dataFetchUtils = require("./data-fetch-utils.cjs.js");
|
|
9
12
|
function _interopNamespaceDefault(e2) {
|
|
10
13
|
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
11
14
|
if (e2) {
|
|
@@ -83,7 +86,7 @@ const RemoteAppWrapper = React.forwardRef(function(props, ref) {
|
|
|
83
86
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
84
87
|
if ((_a = providerInfoRef.current) == null ? void 0 : _a.destroy) {
|
|
85
88
|
index.LoggerInstance.debug(
|
|
86
|
-
`
|
|
89
|
+
`createRemoteAppComponent LazyComponent destroy >>>`,
|
|
87
90
|
{ moduleName, basename, dom: renderDom.current }
|
|
88
91
|
);
|
|
89
92
|
(_d = (_c = (_b = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _b.lifecycle) == null ? void 0 : _c.beforeBridgeDestroy) == null ? void 0 : _d.emit({
|
|
@@ -171,7 +174,7 @@ function withRouterData(WrappedComponent) {
|
|
|
171
174
|
}
|
|
172
175
|
}
|
|
173
176
|
}
|
|
174
|
-
index.LoggerInstance.debug(`
|
|
177
|
+
index.LoggerInstance.debug(`createRemoteAppComponent withRouterData >>>`, {
|
|
175
178
|
...props,
|
|
176
179
|
basename,
|
|
177
180
|
routerContextVal,
|
|
@@ -183,7 +186,7 @@ function withRouterData(WrappedComponent) {
|
|
|
183
186
|
React.useEffect(() => {
|
|
184
187
|
if (pathname !== "" && pathname !== location.pathname) {
|
|
185
188
|
index.LoggerInstance.debug(
|
|
186
|
-
`
|
|
189
|
+
`createRemoteAppComponent dispatchPopstateEnv >>>`,
|
|
187
190
|
{
|
|
188
191
|
name: props.name,
|
|
189
192
|
pathname: location.pathname
|
|
@@ -204,7 +207,7 @@ const RemoteApp = withRouterData(RemoteAppWrapper);
|
|
|
204
207
|
function createLazyRemoteComponent(info) {
|
|
205
208
|
const exportName = (info == null ? void 0 : info.export) || "default";
|
|
206
209
|
return React.lazy(async () => {
|
|
207
|
-
index.LoggerInstance.debug(`
|
|
210
|
+
index.LoggerInstance.debug(`createRemoteAppComponent LazyComponent create >>>`, {
|
|
208
211
|
lazyComponent: info.loader,
|
|
209
212
|
exportName
|
|
210
213
|
});
|
|
@@ -212,7 +215,7 @@ function createLazyRemoteComponent(info) {
|
|
|
212
215
|
const m = await info.loader();
|
|
213
216
|
const moduleName = m && m[Symbol.for("mf_module_id")];
|
|
214
217
|
index.LoggerInstance.debug(
|
|
215
|
-
`
|
|
218
|
+
`createRemoteAppComponent LazyComponent loadRemote info >>>`,
|
|
216
219
|
{ name: moduleName, module: m, exportName }
|
|
217
220
|
);
|
|
218
221
|
const exportFn = m[exportName];
|
|
@@ -235,7 +238,7 @@ function createLazyRemoteComponent(info) {
|
|
|
235
238
|
};
|
|
236
239
|
} else {
|
|
237
240
|
index.LoggerInstance.debug(
|
|
238
|
-
`
|
|
241
|
+
`createRemoteAppComponent LazyComponent module not found >>>`,
|
|
239
242
|
{ name: moduleName, module: m, exportName }
|
|
240
243
|
);
|
|
241
244
|
throw Error(
|
|
@@ -249,7 +252,7 @@ function createLazyRemoteComponent(info) {
|
|
|
249
252
|
}
|
|
250
253
|
});
|
|
251
254
|
}
|
|
252
|
-
function
|
|
255
|
+
function createRemoteAppComponent(info) {
|
|
253
256
|
const LazyComponent = createLazyRemoteComponent(info);
|
|
254
257
|
return React.forwardRef((props, ref) => {
|
|
255
258
|
return /* @__PURE__ */ React.createElement(
|
|
@@ -261,5 +264,454 @@ function createRemoteComponent(info) {
|
|
|
261
264
|
);
|
|
262
265
|
});
|
|
263
266
|
}
|
|
267
|
+
function createRemoteComponent(info) {
|
|
268
|
+
index.LoggerInstance.warn(
|
|
269
|
+
`createRemoteAppComponent is deprecated, please use createRemoteAppComponent instead!`
|
|
270
|
+
);
|
|
271
|
+
return createRemoteAppComponent(info);
|
|
272
|
+
}
|
|
273
|
+
function isPromise(obj) {
|
|
274
|
+
return !!obj && (typeof obj === "object" || typeof obj === "function") && typeof obj.then === "function";
|
|
275
|
+
}
|
|
276
|
+
const AWAIT_ERROR_PREFIX = "<Await /> caught the following error during render: ";
|
|
277
|
+
const transformError = (err) => {
|
|
278
|
+
const errMsg = err instanceof Error ? err.message : err;
|
|
279
|
+
const originalMsg = errMsg.replace(AWAIT_ERROR_PREFIX, "");
|
|
280
|
+
const dataFetchMapKey = lazyUtils.getDataFetchIdWithErrorMsgs(originalMsg);
|
|
281
|
+
if (originalMsg.indexOf(lazyUtils.DATA_FETCH_ERROR_PREFIX) === 0) {
|
|
282
|
+
return {
|
|
283
|
+
error: new Error(
|
|
284
|
+
originalMsg.replace(lazyUtils.DATA_FETCH_ERROR_PREFIX, "").replace(lazyUtils.wrapDataFetchId(dataFetchMapKey), "")
|
|
285
|
+
),
|
|
286
|
+
errorType: lazyUtils.ERROR_TYPE.DATA_FETCH,
|
|
287
|
+
dataFetchMapKey
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
if (originalMsg.indexOf(lazyUtils.LOAD_REMOTE_ERROR_PREFIX) === 0) {
|
|
291
|
+
return {
|
|
292
|
+
error: new Error(
|
|
293
|
+
originalMsg.replace(lazyUtils.LOAD_REMOTE_ERROR_PREFIX, "").replace(lazyUtils.wrapDataFetchId(dataFetchMapKey), "")
|
|
294
|
+
),
|
|
295
|
+
errorType: lazyUtils.ERROR_TYPE.LOAD_REMOTE,
|
|
296
|
+
dataFetchMapKey
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
return {
|
|
300
|
+
error: new Error(originalMsg.replace(lazyUtils.wrapDataFetchId(dataFetchMapKey), "")),
|
|
301
|
+
errorType: lazyUtils.ERROR_TYPE.UNKNOWN,
|
|
302
|
+
dataFetchMapKey
|
|
303
|
+
};
|
|
304
|
+
};
|
|
305
|
+
const DefaultLoading = /* @__PURE__ */ React.createElement(React.Fragment, null);
|
|
306
|
+
const DefaultErrorElement = (_data) => /* @__PURE__ */ React.createElement("div", null, "Error");
|
|
307
|
+
function AwaitDataFetch({
|
|
308
|
+
resolve,
|
|
309
|
+
loading = DefaultLoading,
|
|
310
|
+
errorElement = DefaultErrorElement,
|
|
311
|
+
children,
|
|
312
|
+
params,
|
|
313
|
+
delayLoading
|
|
314
|
+
}) {
|
|
315
|
+
const dataRef = React.useRef();
|
|
316
|
+
const data = dataRef.current || resolve;
|
|
317
|
+
const getData = isPromise(data) ? fetchData(data, dataRef) : () => data;
|
|
318
|
+
return /* @__PURE__ */ React.createElement(
|
|
319
|
+
AwaitSuspense,
|
|
320
|
+
{
|
|
321
|
+
params,
|
|
322
|
+
loading,
|
|
323
|
+
errorElement,
|
|
324
|
+
delayLoading,
|
|
325
|
+
resolve: getData
|
|
326
|
+
},
|
|
327
|
+
children
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
const DelayedLoading = ({
|
|
331
|
+
delayLoading,
|
|
332
|
+
children
|
|
333
|
+
}) => {
|
|
334
|
+
const [show, setShow] = React.useState(false);
|
|
335
|
+
const timerSet = React.useRef(false);
|
|
336
|
+
if (!delayLoading) {
|
|
337
|
+
return children;
|
|
338
|
+
}
|
|
339
|
+
if (typeof window !== "undefined" && !show && !timerSet.current) {
|
|
340
|
+
timerSet.current = true;
|
|
341
|
+
setTimeout(() => {
|
|
342
|
+
setShow(true);
|
|
343
|
+
}, delayLoading);
|
|
344
|
+
}
|
|
345
|
+
return show ? children : null;
|
|
346
|
+
};
|
|
347
|
+
function AwaitSuspense({
|
|
348
|
+
resolve,
|
|
349
|
+
children,
|
|
350
|
+
loading = DefaultLoading,
|
|
351
|
+
errorElement = DefaultErrorElement,
|
|
352
|
+
delayLoading
|
|
353
|
+
}) {
|
|
354
|
+
return /* @__PURE__ */ React.createElement(
|
|
355
|
+
React.Suspense,
|
|
356
|
+
{
|
|
357
|
+
fallback: /* @__PURE__ */ React.createElement(DelayedLoading, { delayLoading }, loading)
|
|
358
|
+
},
|
|
359
|
+
/* @__PURE__ */ React.createElement(ResolveAwait, { resolve, errorElement }, children)
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
function ResolveAwait({
|
|
363
|
+
children,
|
|
364
|
+
resolve,
|
|
365
|
+
errorElement,
|
|
366
|
+
params
|
|
367
|
+
}) {
|
|
368
|
+
const data = resolve();
|
|
369
|
+
lazyUtils.logger.debug("resolve data: ", data);
|
|
370
|
+
if (typeof data === "string" && data.indexOf(AWAIT_ERROR_PREFIX) === 0) {
|
|
371
|
+
const transformedError = transformError(data);
|
|
372
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, typeof errorElement === "function" ? /* @__PURE__ */ React.createElement(React.Fragment, null, globalThis.FEDERATION_SSR && /* @__PURE__ */ React.createElement(
|
|
373
|
+
"script",
|
|
374
|
+
{
|
|
375
|
+
suppressHydrationWarning: true,
|
|
376
|
+
dangerouslySetInnerHTML: {
|
|
377
|
+
__html: String.raw`
|
|
378
|
+
globalThis['${lazyUtils.DATA_FETCH_FUNCTION}'] = globalThis['${lazyUtils.DATA_FETCH_FUNCTION}'] || []
|
|
379
|
+
globalThis['${lazyUtils.DATA_FETCH_FUNCTION}'].push([${transformedError.dataFetchMapKey ? `'${transformedError.dataFetchMapKey}'` : ""},${params ? JSON.stringify(params) : null},true]);`
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
), errorElement(transformedError)) : errorElement);
|
|
383
|
+
}
|
|
384
|
+
const toRender = typeof children === "function" ? children(data) : children;
|
|
385
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, toRender);
|
|
386
|
+
}
|
|
387
|
+
const fetchData = (promise, ref) => {
|
|
388
|
+
let data;
|
|
389
|
+
let status = "pending";
|
|
390
|
+
const suspender = promise.then((res) => {
|
|
391
|
+
status = "success";
|
|
392
|
+
data = res;
|
|
393
|
+
ref.current = res;
|
|
394
|
+
}).catch((e2) => {
|
|
395
|
+
status = "success";
|
|
396
|
+
console.warn(e2);
|
|
397
|
+
data = AWAIT_ERROR_PREFIX + e2;
|
|
398
|
+
});
|
|
399
|
+
return () => {
|
|
400
|
+
if (status === "pending") {
|
|
401
|
+
throw suspender;
|
|
402
|
+
}
|
|
403
|
+
return data;
|
|
404
|
+
};
|
|
405
|
+
};
|
|
406
|
+
function getTargetModuleInfo(id, instance) {
|
|
407
|
+
if (!instance) {
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
const loadedRemoteInfo = lazyUtils.getLoadedRemoteInfos(id, instance);
|
|
411
|
+
if (!loadedRemoteInfo) {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
const snapshot = loadedRemoteInfo.snapshot;
|
|
415
|
+
if (!snapshot) {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
const publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
|
|
419
|
+
if (!publicPath) {
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
const modules = "modules" in snapshot ? snapshot.modules : [];
|
|
423
|
+
const targetModule = modules.find(
|
|
424
|
+
(m) => m.modulePath === loadedRemoteInfo.expose
|
|
425
|
+
);
|
|
426
|
+
if (!targetModule) {
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
const remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
|
|
430
|
+
if (!remoteEntry) {
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
return {
|
|
434
|
+
module: targetModule,
|
|
435
|
+
publicPath,
|
|
436
|
+
remoteEntry
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
function collectSSRAssets(options) {
|
|
440
|
+
const {
|
|
441
|
+
id,
|
|
442
|
+
injectLink = true,
|
|
443
|
+
injectScript = false
|
|
444
|
+
} = typeof options === "string" ? { id: options } : options;
|
|
445
|
+
const links = [];
|
|
446
|
+
const scripts = [];
|
|
447
|
+
const instance = options.instance;
|
|
448
|
+
if (!instance || !injectLink && !injectScript) {
|
|
449
|
+
return [...scripts, ...links];
|
|
450
|
+
}
|
|
451
|
+
const moduleAndPublicPath = getTargetModuleInfo(id, instance);
|
|
452
|
+
if (!moduleAndPublicPath) {
|
|
453
|
+
return [...scripts, ...links];
|
|
454
|
+
}
|
|
455
|
+
const { module: targetModule, publicPath, remoteEntry } = moduleAndPublicPath;
|
|
456
|
+
if (injectLink) {
|
|
457
|
+
[...targetModule.assets.css.sync, ...targetModule.assets.css.async].sort().forEach((file, index2) => {
|
|
458
|
+
links.push(
|
|
459
|
+
/* @__PURE__ */ React.createElement(
|
|
460
|
+
"link",
|
|
461
|
+
{
|
|
462
|
+
key: `${file.split(".")[0]}_${index2}`,
|
|
463
|
+
href: `${publicPath}${file}`,
|
|
464
|
+
rel: "stylesheet",
|
|
465
|
+
type: "text/css"
|
|
466
|
+
}
|
|
467
|
+
)
|
|
468
|
+
);
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
if (injectScript) {
|
|
472
|
+
scripts.push(
|
|
473
|
+
/* @__PURE__ */ React.createElement(
|
|
474
|
+
"script",
|
|
475
|
+
{
|
|
476
|
+
async: true,
|
|
477
|
+
key: remoteEntry.split(".")[0],
|
|
478
|
+
src: `${publicPath}${remoteEntry}`,
|
|
479
|
+
crossOrigin: "anonymous"
|
|
480
|
+
}
|
|
481
|
+
)
|
|
482
|
+
);
|
|
483
|
+
[...targetModule.assets.js.sync].sort().forEach((file, index2) => {
|
|
484
|
+
scripts.push(
|
|
485
|
+
/* @__PURE__ */ React.createElement(
|
|
486
|
+
"script",
|
|
487
|
+
{
|
|
488
|
+
key: `${file.split(".")[0]}_${index2}`,
|
|
489
|
+
async: true,
|
|
490
|
+
src: `${publicPath}${file}`,
|
|
491
|
+
crossOrigin: "anonymous"
|
|
492
|
+
}
|
|
493
|
+
)
|
|
494
|
+
);
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
return [...scripts, ...links];
|
|
498
|
+
}
|
|
499
|
+
function getServerNeedRemoteInfo(loadedRemoteInfo, id, noSSR) {
|
|
500
|
+
var _a;
|
|
501
|
+
if (noSSR || typeof window !== "undefined" && window.location.href !== window[lazyUtils.FS_HREF]) {
|
|
502
|
+
if (!(loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.version)) {
|
|
503
|
+
throw new Error(`${loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.name} version is empty`);
|
|
504
|
+
}
|
|
505
|
+
const { snapshot } = loadedRemoteInfo;
|
|
506
|
+
if (!snapshot) {
|
|
507
|
+
throw new Error(`${loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.name} snapshot is empty`);
|
|
508
|
+
}
|
|
509
|
+
const dataFetchItem = lazyUtils.getDataFetchItem(id);
|
|
510
|
+
const isFetchServer = ((_a = dataFetchItem == null ? void 0 : dataFetchItem[0]) == null ? void 0 : _a[1]) === lazyUtils.MF_DATA_FETCH_TYPE.FETCH_SERVER;
|
|
511
|
+
if (isFetchServer && (!("ssrPublicPath" in snapshot) || !snapshot.ssrPublicPath)) {
|
|
512
|
+
throw new Error(
|
|
513
|
+
`ssrPublicPath is required while fetching ${loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.name} data in SSR project!`
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
if (isFetchServer && (!("ssrRemoteEntry" in snapshot) || !snapshot.ssrRemoteEntry)) {
|
|
517
|
+
throw new Error(
|
|
518
|
+
`ssrRemoteEntry is required while loading ${loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.name} data loader in SSR project!`
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
return {
|
|
522
|
+
name: loadedRemoteInfo.name,
|
|
523
|
+
version: loadedRemoteInfo.version,
|
|
524
|
+
ssrPublicPath: "ssrPublicPath" in snapshot ? snapshot.ssrPublicPath || "" : "",
|
|
525
|
+
ssrRemoteEntry: "ssrRemoteEntry" in snapshot ? snapshot.ssrRemoteEntry || "" : "",
|
|
526
|
+
globalName: loadedRemoteInfo.entryGlobalName
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
return void 0;
|
|
530
|
+
}
|
|
531
|
+
function createLazyComponent(options) {
|
|
532
|
+
const { instance, cacheData } = options;
|
|
533
|
+
if (!instance) {
|
|
534
|
+
throw new Error("instance is required for createLazyComponent!");
|
|
535
|
+
}
|
|
536
|
+
let dataCache = null;
|
|
537
|
+
const exportName = (options == null ? void 0 : options.export) || "default";
|
|
538
|
+
const callLoader = async () => {
|
|
539
|
+
lazyUtils.logger.debug("callLoader start", Date.now());
|
|
540
|
+
const m = await options.loader();
|
|
541
|
+
lazyUtils.logger.debug("callLoader end", Date.now());
|
|
542
|
+
if (!m) {
|
|
543
|
+
throw new Error("load remote failed");
|
|
544
|
+
}
|
|
545
|
+
return m;
|
|
546
|
+
};
|
|
547
|
+
const getData = async (noSSR) => {
|
|
548
|
+
let loadedRemoteInfo;
|
|
549
|
+
let moduleId;
|
|
550
|
+
try {
|
|
551
|
+
const m = await callLoader();
|
|
552
|
+
moduleId = m && m[Symbol.for("mf_module_id")];
|
|
553
|
+
if (!moduleId) {
|
|
554
|
+
throw new Error("moduleId is empty");
|
|
555
|
+
}
|
|
556
|
+
loadedRemoteInfo = lazyUtils.getLoadedRemoteInfos(moduleId, instance);
|
|
557
|
+
if (!loadedRemoteInfo) {
|
|
558
|
+
throw new Error(`can not find loaded remote('${moduleId}') info!`);
|
|
559
|
+
}
|
|
560
|
+
} catch (e2) {
|
|
561
|
+
const errMsg = `${lazyUtils.LOAD_REMOTE_ERROR_PREFIX}${e2}`;
|
|
562
|
+
lazyUtils.logger.debug(e2);
|
|
563
|
+
throw new Error(errMsg);
|
|
564
|
+
}
|
|
565
|
+
let dataFetchMapKey;
|
|
566
|
+
try {
|
|
567
|
+
dataFetchMapKey = lazyUtils.getDataFetchMapKey(
|
|
568
|
+
lazyUtils.getDataFetchInfo({
|
|
569
|
+
name: loadedRemoteInfo.name,
|
|
570
|
+
alias: loadedRemoteInfo.alias,
|
|
571
|
+
id: moduleId,
|
|
572
|
+
remoteSnapshot: loadedRemoteInfo.snapshot
|
|
573
|
+
}),
|
|
574
|
+
{ name: instance.name, version: instance.options.version }
|
|
575
|
+
);
|
|
576
|
+
lazyUtils.logger.debug("getData dataFetchMapKey: ", dataFetchMapKey);
|
|
577
|
+
if (!dataFetchMapKey) {
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
const data = await lazyUtils.fetchData(
|
|
581
|
+
dataFetchMapKey,
|
|
582
|
+
{
|
|
583
|
+
...options.dataFetchParams,
|
|
584
|
+
isDowngrade: false
|
|
585
|
+
},
|
|
586
|
+
getServerNeedRemoteInfo(loadedRemoteInfo, dataFetchMapKey, noSSR)
|
|
587
|
+
);
|
|
588
|
+
lazyUtils.setDataFetchItemLoadedStatus(dataFetchMapKey);
|
|
589
|
+
lazyUtils.logger.debug("get data res: \n", data);
|
|
590
|
+
dataCache = data;
|
|
591
|
+
return data;
|
|
592
|
+
} catch (err) {
|
|
593
|
+
const errMsg = `${lazyUtils.DATA_FETCH_ERROR_PREFIX}${lazyUtils.wrapDataFetchId(dataFetchMapKey)}${err}`;
|
|
594
|
+
lazyUtils.logger.debug(errMsg);
|
|
595
|
+
throw new Error(errMsg);
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
const LazyComponent = React.lazy(async () => {
|
|
599
|
+
const m = await callLoader();
|
|
600
|
+
const moduleId = m && m[Symbol.for("mf_module_id")];
|
|
601
|
+
const loadedRemoteInfo = lazyUtils.getLoadedRemoteInfos(moduleId, instance);
|
|
602
|
+
loadedRemoteInfo == null ? void 0 : loadedRemoteInfo.snapshot;
|
|
603
|
+
const dataFetchMapKey = loadedRemoteInfo ? lazyUtils.getDataFetchMapKey(
|
|
604
|
+
lazyUtils.getDataFetchInfo({
|
|
605
|
+
name: loadedRemoteInfo.name,
|
|
606
|
+
alias: loadedRemoteInfo.alias,
|
|
607
|
+
id: moduleId,
|
|
608
|
+
remoteSnapshot: loadedRemoteInfo.snapshot
|
|
609
|
+
}),
|
|
610
|
+
{ name: instance.name, version: instance == null ? void 0 : instance.options.version }
|
|
611
|
+
) : void 0;
|
|
612
|
+
lazyUtils.logger.debug("LazyComponent dataFetchMapKey: ", dataFetchMapKey);
|
|
613
|
+
const assets = collectSSRAssets({
|
|
614
|
+
id: moduleId,
|
|
615
|
+
instance
|
|
616
|
+
});
|
|
617
|
+
const Com = m[exportName];
|
|
618
|
+
if (exportName in m && typeof Com === "function") {
|
|
619
|
+
return {
|
|
620
|
+
default: (props) => /* @__PURE__ */ React.createElement(React.Fragment, null, globalThis.FEDERATION_SSR && dataFetchMapKey && /* @__PURE__ */ React.createElement(
|
|
621
|
+
"script",
|
|
622
|
+
{
|
|
623
|
+
suppressHydrationWarning: true,
|
|
624
|
+
dangerouslySetInnerHTML: {
|
|
625
|
+
__html: String.raw`
|
|
626
|
+
globalThis['${lazyUtils.DATA_FETCH_FUNCTION}'] = globalThis['${lazyUtils.DATA_FETCH_FUNCTION}'] || [];
|
|
627
|
+
globalThis['${lazyUtils.DATA_FETCH_FUNCTION}'].push(['${dataFetchMapKey}',${JSON.stringify(props.mfData)}]);
|
|
628
|
+
`
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
), globalThis.FEDERATION_SSR && assets, /* @__PURE__ */ React.createElement(Com, { ...props }))
|
|
632
|
+
};
|
|
633
|
+
} else {
|
|
634
|
+
throw Error(
|
|
635
|
+
`Make sure that ${moduleId} has the correct export when export is ${String(
|
|
636
|
+
exportName
|
|
637
|
+
)}`
|
|
638
|
+
);
|
|
639
|
+
}
|
|
640
|
+
});
|
|
641
|
+
return (props) => {
|
|
642
|
+
const { key, ...args } = props;
|
|
643
|
+
if (cacheData && dataCache) {
|
|
644
|
+
return /* @__PURE__ */ React.createElement(LazyComponent, { ...args, mfData: dataCache });
|
|
645
|
+
}
|
|
646
|
+
if (!options.noSSR) {
|
|
647
|
+
return /* @__PURE__ */ React.createElement(
|
|
648
|
+
AwaitDataFetch,
|
|
649
|
+
{
|
|
650
|
+
resolve: getData(options.noSSR),
|
|
651
|
+
loading: options.loading,
|
|
652
|
+
delayLoading: options.delayLoading,
|
|
653
|
+
errorElement: options.fallback
|
|
654
|
+
},
|
|
655
|
+
(data) => /* @__PURE__ */ React.createElement(LazyComponent, { ...args, mfData: data })
|
|
656
|
+
);
|
|
657
|
+
} else {
|
|
658
|
+
const [data, setData] = React.useState(null);
|
|
659
|
+
const [loading, setLoading] = React.useState(true);
|
|
660
|
+
const [error, setError] = React.useState(null);
|
|
661
|
+
React.useEffect(() => {
|
|
662
|
+
let isMounted = true;
|
|
663
|
+
const fetchDataAsync = async () => {
|
|
664
|
+
try {
|
|
665
|
+
setLoading(true);
|
|
666
|
+
const result = await getData(options.noSSR);
|
|
667
|
+
if (isMounted) {
|
|
668
|
+
setData(result);
|
|
669
|
+
}
|
|
670
|
+
} catch (e2) {
|
|
671
|
+
if (isMounted) {
|
|
672
|
+
setError(transformError(e2));
|
|
673
|
+
}
|
|
674
|
+
} finally {
|
|
675
|
+
if (isMounted) {
|
|
676
|
+
setLoading(false);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
fetchDataAsync();
|
|
681
|
+
return () => {
|
|
682
|
+
isMounted = false;
|
|
683
|
+
};
|
|
684
|
+
}, []);
|
|
685
|
+
if (loading) {
|
|
686
|
+
return /* @__PURE__ */ React.createElement(DelayedLoading, { delayLoading: options.delayLoading }, options.loading);
|
|
687
|
+
}
|
|
688
|
+
if (error) {
|
|
689
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, typeof options.fallback === "function" ? options.fallback(error) : options.fallback);
|
|
690
|
+
}
|
|
691
|
+
return /* @__PURE__ */ React.createElement(LazyComponent, { ...args, mfData: data });
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
function wrapNoSSR(createLazyComponentFn) {
|
|
696
|
+
return (options) => {
|
|
697
|
+
return createLazyComponentFn({ ...options, noSSR: true });
|
|
698
|
+
};
|
|
699
|
+
}
|
|
700
|
+
exports.autoFetchDataPlugin = dataFetchRuntimePlugin;
|
|
701
|
+
exports.CacheSize = lazyUtils.CacheSize;
|
|
702
|
+
exports.CacheTime = lazyUtils.CacheTime;
|
|
703
|
+
exports.ERROR_TYPE = lazyUtils.ERROR_TYPE;
|
|
704
|
+
exports.cache = lazyUtils.cache;
|
|
705
|
+
exports.clearStore = lazyUtils.clearStore;
|
|
706
|
+
exports.configureCache = lazyUtils.configureCache;
|
|
707
|
+
exports.generateKey = lazyUtils.generateKey;
|
|
708
|
+
exports.revalidateTag = lazyUtils.revalidateTag;
|
|
709
|
+
exports.setSSREnv = lazyUtils.setSSREnv;
|
|
710
|
+
exports.callDataFetch = dataFetchUtils.callDataFetch;
|
|
711
|
+
exports.prefetch = dataFetchUtils.prefetch;
|
|
712
|
+
exports.collectSSRAssets = collectSSRAssets;
|
|
264
713
|
exports.createBridgeComponent = createBridgeComponent;
|
|
714
|
+
exports.createLazyComponent = createLazyComponent;
|
|
715
|
+
exports.createRemoteAppComponent = createRemoteAppComponent;
|
|
265
716
|
exports.createRemoteComponent = createRemoteComponent;
|
|
717
|
+
exports.wrapNoSSR = wrapNoSSR;
|