@module-federation/bridge-react 0.0.0-fix-lazy-comile-20250925082726
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 +657 -0
- package/LICENSE +21 -0
- package/README.md +131 -0
- package/__tests__/bridge.spec.tsx +137 -0
- package/__tests__/createLazyComponent.spec.tsx +209 -0
- package/__tests__/prefetch.spec.ts +156 -0
- package/__tests__/router.spec.tsx +82 -0
- package/__tests__/setupTests.ts +8 -0
- package/__tests__/util.ts +36 -0
- package/dist/bridge-base-BwHtOqw_.mjs +199 -0
- package/dist/bridge-base-Ds850AOx.js +214 -0
- 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-eN2xRRXs.js +45 -0
- package/dist/index-rAO0Wr0M.mjs +46 -0
- package/dist/index.cjs.js +293 -0
- package/dist/index.d.ts +274 -0
- package/dist/index.es.js +276 -0
- package/dist/index.esm-CtI0uQUR.mjs +430 -0
- package/dist/index.esm-Ju4RY-yW.js +429 -0
- package/dist/lazy-load-component-plugin-BLfwTMtM.mjs +522 -0
- package/dist/lazy-load-component-plugin-C0zAoMq3.js +521 -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.cjs.js +14 -0
- package/dist/plugin.d.ts +22 -0
- package/dist/plugin.es.js +14 -0
- package/dist/prefetch-BkgTf4W6.mjs +1371 -0
- package/dist/prefetch-DX29k_gH.js +1370 -0
- package/dist/router-v5.cjs.js +55 -0
- package/dist/router-v5.d.ts +18 -0
- package/dist/router-v5.es.js +32 -0
- package/dist/router-v6.cjs.js +84 -0
- package/dist/router-v6.d.ts +20 -0
- package/dist/router-v6.es.js +61 -0
- package/dist/router.cjs.js +82 -0
- package/dist/router.d.ts +20 -0
- package/dist/router.es.js +60 -0
- package/dist/utils-VSOJTX_o.mjs +2016 -0
- package/dist/utils-vIpCrZmn.js +2015 -0
- package/dist/v18.cjs.js +15 -0
- package/dist/v18.d.ts +114 -0
- package/dist/v18.es.js +15 -0
- package/dist/v19.cjs.js +15 -0
- package/dist/v19.d.ts +115 -0
- package/dist/v19.es.js +15 -0
- package/jest.config.ts +21 -0
- package/package.json +129 -0
- package/project.json +23 -0
- package/src/index.ts +45 -0
- 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 +112 -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/modern-app-env.d.ts +2 -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/context.tsx +4 -0
- package/src/provider/plugin.ts +22 -0
- package/src/provider/versions/bridge-base.tsx +122 -0
- package/src/provider/versions/legacy.ts +86 -0
- package/src/provider/versions/v18.ts +47 -0
- package/src/provider/versions/v19.ts +48 -0
- package/src/remote/component.tsx +211 -0
- package/src/remote/create.tsx +103 -0
- package/src/router/default.tsx +73 -0
- package/src/router/v5.tsx +43 -0
- package/src/router/v6.tsx +74 -0
- package/src/types.ts +147 -0
- package/src/utils/index.ts +44 -0
- package/src/v18.ts +9 -0
- package/src/v19.ts +9 -0
- package/tsconfig.json +42 -0
- package/tsconfig.node.json +11 -0
- package/tsconfig.spec.json +26 -0
- package/vite.config.ts +87 -0
- package/vitest.config.ts +27 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { D as DATA_FETCH_QUERY, l as logger, g as getDataFetchMap, i as initDataFetchMap, M as MF_DATA_FETCH_STATUS, f as fetchData, a as loadDataFetchModule } from "./utils-VSOJTX_o.mjs";
|
|
2
|
+
import { M as MANIFEST_EXT, S as SEPARATOR } from "./index.esm-CtI0uQUR.mjs";
|
|
3
|
+
function wrapSetTimeout(targetPromise, delay = 2e4, id) {
|
|
4
|
+
if (targetPromise && typeof targetPromise.then === "function") {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
const timeoutId = setTimeout(() => {
|
|
7
|
+
logger.warn(`Data fetch for ID ${id} timed out after 20 seconds.`);
|
|
8
|
+
reject(new Error(`Data fetch for ID ${id} timed out after 20 seconds`));
|
|
9
|
+
}, delay);
|
|
10
|
+
targetPromise.then((value) => {
|
|
11
|
+
clearTimeout(timeoutId);
|
|
12
|
+
resolve(value);
|
|
13
|
+
}).catch((err) => {
|
|
14
|
+
clearTimeout(timeoutId);
|
|
15
|
+
reject(err);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function addProtocol(url) {
|
|
21
|
+
if (url.startsWith("//")) {
|
|
22
|
+
return "https:" + url;
|
|
23
|
+
}
|
|
24
|
+
return url;
|
|
25
|
+
}
|
|
26
|
+
const getDecodeQuery = (url, name) => {
|
|
27
|
+
const res = url.searchParams.get(name);
|
|
28
|
+
if (!res) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return decodeURIComponent(res);
|
|
32
|
+
};
|
|
33
|
+
const dataFetchServerMiddleware = async (ctx, next) => {
|
|
34
|
+
var _a, _b, _c;
|
|
35
|
+
let url;
|
|
36
|
+
let dataFetchKey;
|
|
37
|
+
let params;
|
|
38
|
+
let remoteInfo;
|
|
39
|
+
try {
|
|
40
|
+
url = new URL(ctx.req.url);
|
|
41
|
+
dataFetchKey = getDecodeQuery(url, DATA_FETCH_QUERY);
|
|
42
|
+
params = JSON.parse(getDecodeQuery(url, "params") || "{}");
|
|
43
|
+
const remoteInfoQuery = getDecodeQuery(url, "remoteInfo");
|
|
44
|
+
remoteInfo = remoteInfoQuery ? JSON.parse(remoteInfoQuery) : null;
|
|
45
|
+
} catch (e) {
|
|
46
|
+
logger.error("fetch data from server, error: ", e);
|
|
47
|
+
return next();
|
|
48
|
+
}
|
|
49
|
+
if (!dataFetchKey) {
|
|
50
|
+
return next();
|
|
51
|
+
}
|
|
52
|
+
logger.log("fetch data from server, dataFetchKey: ", dataFetchKey);
|
|
53
|
+
logger.debug(
|
|
54
|
+
"fetch data from server, moduleInfo: ",
|
|
55
|
+
(_a = globalThis.__FEDERATION__) == null ? void 0 : _a.moduleInfo
|
|
56
|
+
);
|
|
57
|
+
try {
|
|
58
|
+
const dataFetchMap = getDataFetchMap();
|
|
59
|
+
if (!dataFetchMap) {
|
|
60
|
+
initDataFetchMap();
|
|
61
|
+
}
|
|
62
|
+
const fetchDataPromise = (_b = dataFetchMap[dataFetchKey]) == null ? void 0 : _b[1];
|
|
63
|
+
logger.debug(
|
|
64
|
+
"fetch data from server, fetchDataPromise: ",
|
|
65
|
+
fetchDataPromise
|
|
66
|
+
);
|
|
67
|
+
if (fetchDataPromise && ((_c = dataFetchMap[dataFetchKey]) == null ? void 0 : _c[2]) !== MF_DATA_FETCH_STATUS.ERROR) {
|
|
68
|
+
const targetPromise = fetchDataPromise[0];
|
|
69
|
+
const wrappedPromise = wrapSetTimeout(targetPromise, 2e4, dataFetchKey);
|
|
70
|
+
if (wrappedPromise) {
|
|
71
|
+
const res = await wrappedPromise;
|
|
72
|
+
logger.log("fetch data from server, fetchDataPromise res: ", res);
|
|
73
|
+
return ctx.json(res);
|
|
74
|
+
}
|
|
75
|
+
logger.error(
|
|
76
|
+
`Expected a Promise from fetchDataPromise[0] for dataFetchKey ${dataFetchKey}, but received:`,
|
|
77
|
+
targetPromise,
|
|
78
|
+
"Will try call new dataFetch again..."
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
if (remoteInfo) {
|
|
82
|
+
try {
|
|
83
|
+
const hostInstance2 = globalThis.__FEDERATION__.__INSTANCES__[0];
|
|
84
|
+
const remoteEntry = `${addProtocol(remoteInfo.ssrPublicPath) + remoteInfo.ssrRemoteEntry}`;
|
|
85
|
+
if (!hostInstance2) {
|
|
86
|
+
throw new Error("host instance not found!");
|
|
87
|
+
}
|
|
88
|
+
const remote = hostInstance2.options.remotes.find(
|
|
89
|
+
(remote2) => remote2.name === remoteInfo.name
|
|
90
|
+
);
|
|
91
|
+
logger.debug("find remote: ", JSON.stringify(remote));
|
|
92
|
+
if (!remote) {
|
|
93
|
+
hostInstance2.registerRemotes([
|
|
94
|
+
{
|
|
95
|
+
name: remoteInfo.name,
|
|
96
|
+
entry: remoteEntry,
|
|
97
|
+
entryGlobalName: remoteInfo.globalName
|
|
98
|
+
}
|
|
99
|
+
]);
|
|
100
|
+
} else if (!("entry" in remote) || !remote.entry.includes(MANIFEST_EXT)) {
|
|
101
|
+
const { hostGlobalSnapshot, remoteSnapshot } = hostInstance2.snapshotHandler.getGlobalRemoteInfo(remoteInfo);
|
|
102
|
+
logger.debug(
|
|
103
|
+
"find hostGlobalSnapshot: ",
|
|
104
|
+
JSON.stringify(hostGlobalSnapshot)
|
|
105
|
+
);
|
|
106
|
+
logger.debug("find remoteSnapshot: ", JSON.stringify(remoteSnapshot));
|
|
107
|
+
if (!hostGlobalSnapshot || !remoteSnapshot) {
|
|
108
|
+
if ("version" in remote) {
|
|
109
|
+
delete remote.version;
|
|
110
|
+
}
|
|
111
|
+
remote.entry = remoteEntry;
|
|
112
|
+
remote.entryGlobalName = remoteInfo.globalName;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
} catch (e) {
|
|
116
|
+
ctx.status(500);
|
|
117
|
+
return ctx.text(
|
|
118
|
+
`failed to fetch ${remoteInfo.name} data, error:
|
|
119
|
+
${e}`
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const dataFetchItem = dataFetchMap[dataFetchKey];
|
|
124
|
+
logger.debug("fetch data from server, dataFetchItem: ", dataFetchItem);
|
|
125
|
+
if (dataFetchItem) {
|
|
126
|
+
const callFetchDataPromise = fetchData(dataFetchKey, {
|
|
127
|
+
...params,
|
|
128
|
+
isDowngrade: !remoteInfo,
|
|
129
|
+
_id: dataFetchKey
|
|
130
|
+
});
|
|
131
|
+
const wrappedPromise = wrapSetTimeout(
|
|
132
|
+
callFetchDataPromise,
|
|
133
|
+
2e4,
|
|
134
|
+
dataFetchKey
|
|
135
|
+
);
|
|
136
|
+
if (wrappedPromise) {
|
|
137
|
+
const res = await wrappedPromise;
|
|
138
|
+
logger.log("fetch data from server, dataFetchItem res: ", res);
|
|
139
|
+
return ctx.json(res);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const remoteId = dataFetchKey.split(SEPARATOR)[0];
|
|
143
|
+
const hostInstance = globalThis.__FEDERATION__.__INSTANCES__[0];
|
|
144
|
+
if (!hostInstance) {
|
|
145
|
+
throw new Error("host instance not found!");
|
|
146
|
+
}
|
|
147
|
+
const dataFetchFn = await loadDataFetchModule(hostInstance, remoteId);
|
|
148
|
+
const data = await dataFetchFn({
|
|
149
|
+
...params,
|
|
150
|
+
isDowngrade: !remoteInfo,
|
|
151
|
+
_id: dataFetchKey
|
|
152
|
+
});
|
|
153
|
+
logger.log("fetch data from server, loadDataFetchModule res: ", data);
|
|
154
|
+
return ctx.json(data);
|
|
155
|
+
} catch (e) {
|
|
156
|
+
logger.error("server plugin data fetch error: ", e);
|
|
157
|
+
ctx.status(500);
|
|
158
|
+
return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
|
|
159
|
+
${e}`);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
export {
|
|
163
|
+
dataFetchServerMiddleware as default
|
|
164
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const lazyUtils = require("./utils-vIpCrZmn.js");
|
|
4
|
+
const prefetch = require("./prefetch-DX29k_gH.js");
|
|
5
|
+
async function callDataFetch() {
|
|
6
|
+
const dataFetch = globalThis[lazyUtils.DATA_FETCH_FUNCTION];
|
|
7
|
+
if (dataFetch) {
|
|
8
|
+
await Promise.all(
|
|
9
|
+
dataFetch.map(async (options) => {
|
|
10
|
+
await prefetch.dataFetchFunction(options);
|
|
11
|
+
})
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.CacheSize = lazyUtils.CacheSize;
|
|
16
|
+
exports.CacheTime = lazyUtils.CacheTime;
|
|
17
|
+
exports.cache = lazyUtils.cache;
|
|
18
|
+
exports.clearStore = lazyUtils.clearStore;
|
|
19
|
+
exports.configureCache = lazyUtils.configureCache;
|
|
20
|
+
exports.generateKey = lazyUtils.generateKey;
|
|
21
|
+
exports.revalidateTag = lazyUtils.revalidateTag;
|
|
22
|
+
exports.injectDataFetch = prefetch.injectDataFetch;
|
|
23
|
+
exports.prefetch = prefetch.prefetch;
|
|
24
|
+
exports.callDataFetch = callDataFetch;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { getInstance } from '@module-federation/runtime';
|
|
2
|
+
|
|
3
|
+
export declare function cache<T>(fn: DataFetch<T>, options?: CacheOptions): DataFetch<T>;
|
|
4
|
+
|
|
5
|
+
declare interface CacheConfig {
|
|
6
|
+
maxSize?: number;
|
|
7
|
+
unstable_shouldDisable?: ({ request, }: {
|
|
8
|
+
request: Request;
|
|
9
|
+
}) => boolean | Promise<boolean>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare interface CacheOptions {
|
|
13
|
+
tag?: string | string[];
|
|
14
|
+
maxAge?: number;
|
|
15
|
+
revalidate?: number;
|
|
16
|
+
getKey?: <Args extends any[]>(...args: Args) => string;
|
|
17
|
+
onCache?: (info: CacheStatsInfo) => boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export declare const CacheSize: {
|
|
21
|
+
readonly KB: 1024;
|
|
22
|
+
readonly MB: number;
|
|
23
|
+
readonly GB: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export declare interface CacheStatsInfo {
|
|
27
|
+
status: CacheStatus;
|
|
28
|
+
key: string | symbol;
|
|
29
|
+
params: DataFetchParams;
|
|
30
|
+
result: any;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export declare type CacheStatus = 'hit' | 'stale' | 'miss';
|
|
34
|
+
|
|
35
|
+
export declare const CacheTime: {
|
|
36
|
+
readonly SECOND: 1000;
|
|
37
|
+
readonly MINUTE: number;
|
|
38
|
+
readonly HOUR: number;
|
|
39
|
+
readonly DAY: number;
|
|
40
|
+
readonly WEEK: number;
|
|
41
|
+
readonly MONTH: number;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export declare function callDataFetch(): Promise<void>;
|
|
45
|
+
|
|
46
|
+
export declare function clearStore(): void;
|
|
47
|
+
|
|
48
|
+
export declare function configureCache(config: CacheConfig): void;
|
|
49
|
+
|
|
50
|
+
declare type DataFetch<T> = (params: DataFetchParams) => Promise<T>;
|
|
51
|
+
|
|
52
|
+
declare type DataFetchParams = {
|
|
53
|
+
isDowngrade: boolean;
|
|
54
|
+
_id?: string;
|
|
55
|
+
} & Record<string, unknown>;
|
|
56
|
+
|
|
57
|
+
export declare function generateKey(dataFetchOptions: DataFetchParams): string;
|
|
58
|
+
|
|
59
|
+
export declare function injectDataFetch(): void;
|
|
60
|
+
|
|
61
|
+
export declare function prefetch(options: PrefetchOptions): Promise<void>;
|
|
62
|
+
|
|
63
|
+
export declare type PrefetchOptions = {
|
|
64
|
+
id: string;
|
|
65
|
+
instance: ReturnType<typeof getInstance>;
|
|
66
|
+
dataFetchParams?: DataFetchParams;
|
|
67
|
+
preloadComponentResource?: boolean;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export declare function revalidateTag(tag: string): void;
|
|
71
|
+
|
|
72
|
+
export { }
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
declare module '@module-federation/runtime-core' {
|
|
76
|
+
interface ModuleFederation {
|
|
77
|
+
createLazyComponent<T, E extends keyof T>(options: Omit<CreateLazyComponentOptions<T, E>, 'instance'>): ReturnType<typeof createLazyComponent<T, E>>;
|
|
78
|
+
prefetch(options: Omit<PrefetchOptions, 'instance'>): ReturnType<typeof prefetch>;
|
|
79
|
+
collectSSRAssets(options: Omit<Parameters<typeof collectSSRAssets>[0], 'instance'>): ReturnType<typeof collectSSRAssets>;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { x as DATA_FETCH_FUNCTION } from "./utils-VSOJTX_o.mjs";
|
|
2
|
+
import { C, b, e, h, c, d, r } from "./utils-VSOJTX_o.mjs";
|
|
3
|
+
import { d as dataFetchFunction } from "./prefetch-BkgTf4W6.mjs";
|
|
4
|
+
import { i, p } from "./prefetch-BkgTf4W6.mjs";
|
|
5
|
+
async function callDataFetch() {
|
|
6
|
+
const dataFetch = globalThis[DATA_FETCH_FUNCTION];
|
|
7
|
+
if (dataFetch) {
|
|
8
|
+
await Promise.all(
|
|
9
|
+
dataFetch.map(async (options) => {
|
|
10
|
+
await dataFetchFunction(options);
|
|
11
|
+
})
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
C as CacheSize,
|
|
17
|
+
b as CacheTime,
|
|
18
|
+
e as cache,
|
|
19
|
+
callDataFetch,
|
|
20
|
+
h as clearStore,
|
|
21
|
+
c as configureCache,
|
|
22
|
+
d as generateKey,
|
|
23
|
+
i as injectDataFetch,
|
|
24
|
+
p as prefetch,
|
|
25
|
+
r as revalidateTag
|
|
26
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const React = require("react");
|
|
3
|
+
const index_esm = require("./index.esm-Ju4RY-yW.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-CtI0uQUR.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
|
+
};
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const bridgeBase = require("./bridge-base-Ds850AOx.js");
|
|
4
|
+
const ReactDOM = require("react-dom");
|
|
5
|
+
const React = require("react");
|
|
6
|
+
const index = require("./index-eN2xRRXs.js");
|
|
7
|
+
const ReactRouterDOM = require("react-router-dom");
|
|
8
|
+
const plugin = require("./plugin.cjs.js");
|
|
9
|
+
const lazyLoadComponentPlugin = require("./lazy-load-component-plugin-C0zAoMq3.js");
|
|
10
|
+
const lazyUtils = require("./utils-vIpCrZmn.js");
|
|
11
|
+
const dataFetchUtils = require("./data-fetch-utils.cjs.js");
|
|
12
|
+
const prefetch = require("./prefetch-DX29k_gH.js");
|
|
13
|
+
function _interopNamespaceDefault(e2) {
|
|
14
|
+
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
15
|
+
if (e2) {
|
|
16
|
+
for (const k in e2) {
|
|
17
|
+
if (k !== "default") {
|
|
18
|
+
const d = Object.getOwnPropertyDescriptor(e2, k);
|
|
19
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: () => e2[k]
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
n.default = e2;
|
|
27
|
+
return Object.freeze(n);
|
|
28
|
+
}
|
|
29
|
+
const ReactRouterDOM__namespace = /* @__PURE__ */ _interopNamespaceDefault(ReactRouterDOM);
|
|
30
|
+
function createReact16Or17Root(container) {
|
|
31
|
+
return {
|
|
32
|
+
render(children) {
|
|
33
|
+
const reactVersion = ReactDOM.version || "";
|
|
34
|
+
const isReact18 = reactVersion.startsWith("18");
|
|
35
|
+
const isReact19 = reactVersion.startsWith("19");
|
|
36
|
+
if (isReact19) {
|
|
37
|
+
throw new Error(
|
|
38
|
+
`React 19 detected in legacy mode. This is not supported. Please use the version-specific import: import { createBridgeComponent } from '@module-federation/bridge-react/v19'`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
if (isReact18) {
|
|
42
|
+
console.warn(
|
|
43
|
+
`[Bridge-React] React 18 detected in legacy mode. For better compatibility, please use the version-specific import: import { createBridgeComponent } from '@module-federation/bridge-react/v18'`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
ReactDOM.render(children, container);
|
|
47
|
+
},
|
|
48
|
+
unmount() {
|
|
49
|
+
ReactDOM.unmountComponentAtNode(container);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function createBridgeComponent(bridgeInfo) {
|
|
54
|
+
const fullBridgeInfo = {
|
|
55
|
+
createRoot: createReact16Or17Root,
|
|
56
|
+
...bridgeInfo
|
|
57
|
+
};
|
|
58
|
+
return bridgeBase.createBaseBridgeComponent(fullBridgeInfo);
|
|
59
|
+
}
|
|
60
|
+
function e() {
|
|
61
|
+
const t = new PopStateEvent("popstate", { state: window.history.state });
|
|
62
|
+
window.dispatchEvent(t);
|
|
63
|
+
}
|
|
64
|
+
const RemoteAppWrapper = React.forwardRef(function(props, ref) {
|
|
65
|
+
const {
|
|
66
|
+
moduleName,
|
|
67
|
+
memoryRoute,
|
|
68
|
+
basename,
|
|
69
|
+
providerInfo,
|
|
70
|
+
className,
|
|
71
|
+
style,
|
|
72
|
+
fallback,
|
|
73
|
+
loading,
|
|
74
|
+
...resProps
|
|
75
|
+
} = props;
|
|
76
|
+
const instance = plugin.federationRuntime.instance;
|
|
77
|
+
const rootRef = ref && "current" in ref ? ref : React.useRef(null);
|
|
78
|
+
const renderDom = React.useRef(null);
|
|
79
|
+
const providerInfoRef = React.useRef(null);
|
|
80
|
+
const [initialized, setInitialized] = React.useState(false);
|
|
81
|
+
index.LoggerInstance.debug(`RemoteAppWrapper instance from props >>>`, instance);
|
|
82
|
+
React.useEffect(() => {
|
|
83
|
+
if (initialized) return;
|
|
84
|
+
const providerReturn = providerInfo();
|
|
85
|
+
providerInfoRef.current = providerReturn;
|
|
86
|
+
setInitialized(true);
|
|
87
|
+
return () => {
|
|
88
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
89
|
+
if ((_a = providerInfoRef.current) == null ? void 0 : _a.destroy) {
|
|
90
|
+
index.LoggerInstance.debug(
|
|
91
|
+
`createRemoteAppComponent LazyComponent destroy >>>`,
|
|
92
|
+
{ moduleName, basename, dom: renderDom.current }
|
|
93
|
+
);
|
|
94
|
+
(_d = (_c = (_b = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _b.lifecycle) == null ? void 0 : _c.beforeBridgeDestroy) == null ? void 0 : _d.emit({
|
|
95
|
+
moduleName,
|
|
96
|
+
dom: renderDom.current,
|
|
97
|
+
basename,
|
|
98
|
+
memoryRoute,
|
|
99
|
+
fallback,
|
|
100
|
+
...resProps
|
|
101
|
+
});
|
|
102
|
+
(_e = providerInfoRef.current) == null ? void 0 : _e.destroy({
|
|
103
|
+
moduleName,
|
|
104
|
+
dom: renderDom.current
|
|
105
|
+
});
|
|
106
|
+
(_h = (_g = (_f = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _f.lifecycle) == null ? void 0 : _g.afterBridgeDestroy) == null ? void 0 : _h.emit({
|
|
107
|
+
moduleName,
|
|
108
|
+
dom: renderDom.current,
|
|
109
|
+
basename,
|
|
110
|
+
memoryRoute,
|
|
111
|
+
fallback,
|
|
112
|
+
...resProps
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}, [moduleName]);
|
|
117
|
+
React.useEffect(() => {
|
|
118
|
+
var _a, _b, _c, _d, _e, _f;
|
|
119
|
+
if (!initialized || !providerInfoRef.current) return;
|
|
120
|
+
let renderProps = {
|
|
121
|
+
moduleName,
|
|
122
|
+
dom: rootRef.current,
|
|
123
|
+
basename,
|
|
124
|
+
memoryRoute,
|
|
125
|
+
fallback,
|
|
126
|
+
...resProps
|
|
127
|
+
};
|
|
128
|
+
renderDom.current = rootRef.current;
|
|
129
|
+
const beforeBridgeRenderRes = ((_c = (_b = (_a = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _a.lifecycle) == null ? void 0 : _b.beforeBridgeRender) == null ? void 0 : _c.emit(renderProps)) || {};
|
|
130
|
+
renderProps = { ...renderProps, ...beforeBridgeRenderRes.extraProps };
|
|
131
|
+
providerInfoRef.current.render(renderProps);
|
|
132
|
+
(_f = (_e = (_d = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _d.lifecycle) == null ? void 0 : _e.afterBridgeRender) == null ? void 0 : _f.emit(renderProps);
|
|
133
|
+
}, [initialized, ...Object.values(props)]);
|
|
134
|
+
const rootComponentClassName = `${index.getRootDomDefaultClassName(moduleName)} ${className || ""}`;
|
|
135
|
+
return /* @__PURE__ */ React.createElement("div", { className: rootComponentClassName, style, ref: rootRef }, loading);
|
|
136
|
+
});
|
|
137
|
+
function withRouterData(WrappedComponent) {
|
|
138
|
+
const Component = React.forwardRef(function(props, ref) {
|
|
139
|
+
var _a;
|
|
140
|
+
if (props == null ? void 0 : props.basename) {
|
|
141
|
+
return /* @__PURE__ */ React.createElement(WrappedComponent, { ...props, basename: props.basename, ref });
|
|
142
|
+
}
|
|
143
|
+
let enableDispathPopstate = false;
|
|
144
|
+
let routerContextVal;
|
|
145
|
+
try {
|
|
146
|
+
ReactRouterDOM__namespace.useLocation();
|
|
147
|
+
enableDispathPopstate = true;
|
|
148
|
+
} catch {
|
|
149
|
+
enableDispathPopstate = false;
|
|
150
|
+
}
|
|
151
|
+
let basename = "/";
|
|
152
|
+
if (!props.basename && enableDispathPopstate) {
|
|
153
|
+
const ReactRouterDOMAny = ReactRouterDOM__namespace;
|
|
154
|
+
const useRouteMatch = ReactRouterDOMAny["useRouteMatch"];
|
|
155
|
+
const useHistory = ReactRouterDOMAny["useHistory"];
|
|
156
|
+
const useHref = ReactRouterDOMAny["useHref"];
|
|
157
|
+
const UNSAFE_RouteContext = ReactRouterDOMAny["UNSAFE_RouteContext"];
|
|
158
|
+
if (UNSAFE_RouteContext) {
|
|
159
|
+
if (useHref) {
|
|
160
|
+
basename = useHref == null ? void 0 : useHref("/");
|
|
161
|
+
}
|
|
162
|
+
routerContextVal = React.useContext(UNSAFE_RouteContext);
|
|
163
|
+
if (routerContextVal && routerContextVal.matches && routerContextVal.matches.length > 0) {
|
|
164
|
+
const matchIndex = routerContextVal.matches.length - 1;
|
|
165
|
+
const pathnameBase = routerContextVal.matches[matchIndex].pathnameBase;
|
|
166
|
+
basename = index.pathJoin(basename, pathnameBase || "/");
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
const match = useRouteMatch == null ? void 0 : useRouteMatch();
|
|
170
|
+
if (useHistory) {
|
|
171
|
+
const history = useHistory == null ? void 0 : useHistory();
|
|
172
|
+
basename = (_a = history == null ? void 0 : history.createHref) == null ? void 0 : _a.call(history, { pathname: "/" });
|
|
173
|
+
}
|
|
174
|
+
if (match) {
|
|
175
|
+
basename = index.pathJoin(basename, (match == null ? void 0 : match.path) || "/");
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
index.LoggerInstance.debug(`createRemoteAppComponent withRouterData >>>`, {
|
|
180
|
+
...props,
|
|
181
|
+
basename,
|
|
182
|
+
routerContextVal,
|
|
183
|
+
enableDispathPopstate
|
|
184
|
+
});
|
|
185
|
+
if (enableDispathPopstate) {
|
|
186
|
+
const location = ReactRouterDOM__namespace.useLocation();
|
|
187
|
+
const [pathname, setPathname] = React.useState(location.pathname);
|
|
188
|
+
React.useEffect(() => {
|
|
189
|
+
if (pathname !== "" && pathname !== location.pathname) {
|
|
190
|
+
index.LoggerInstance.debug(
|
|
191
|
+
`createRemoteAppComponent dispatchPopstateEnv >>>`,
|
|
192
|
+
{
|
|
193
|
+
name: props.name,
|
|
194
|
+
pathname: location.pathname
|
|
195
|
+
}
|
|
196
|
+
);
|
|
197
|
+
e();
|
|
198
|
+
}
|
|
199
|
+
setPathname(location.pathname);
|
|
200
|
+
}, [location]);
|
|
201
|
+
}
|
|
202
|
+
return /* @__PURE__ */ React.createElement(WrappedComponent, { ...props, basename, ref });
|
|
203
|
+
});
|
|
204
|
+
return React.forwardRef(function(props, ref) {
|
|
205
|
+
return /* @__PURE__ */ React.createElement(Component, { ...props, ref });
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
const RemoteApp = withRouterData(RemoteAppWrapper);
|
|
209
|
+
function createLazyRemoteComponent(info) {
|
|
210
|
+
const exportName = (info == null ? void 0 : info.export) || "default";
|
|
211
|
+
return React.lazy(async () => {
|
|
212
|
+
index.LoggerInstance.debug(`createRemoteAppComponent LazyComponent create >>>`, {
|
|
213
|
+
lazyComponent: info.loader,
|
|
214
|
+
exportName
|
|
215
|
+
});
|
|
216
|
+
try {
|
|
217
|
+
const m = await info.loader();
|
|
218
|
+
const moduleName = m && m[Symbol.for("mf_module_id")];
|
|
219
|
+
index.LoggerInstance.debug(
|
|
220
|
+
`createRemoteAppComponent LazyComponent loadRemote info >>>`,
|
|
221
|
+
{ name: moduleName, module: m, exportName }
|
|
222
|
+
);
|
|
223
|
+
const exportFn = m[exportName];
|
|
224
|
+
if (exportName in m && typeof exportFn === "function") {
|
|
225
|
+
const RemoteAppComponent = React.forwardRef((props, ref) => {
|
|
226
|
+
return /* @__PURE__ */ React.createElement(
|
|
227
|
+
RemoteApp,
|
|
228
|
+
{
|
|
229
|
+
moduleName,
|
|
230
|
+
providerInfo: exportFn,
|
|
231
|
+
exportName: info.export || "default",
|
|
232
|
+
fallback: info.fallback,
|
|
233
|
+
loading: info.loading,
|
|
234
|
+
ref,
|
|
235
|
+
...props
|
|
236
|
+
}
|
|
237
|
+
);
|
|
238
|
+
});
|
|
239
|
+
return {
|
|
240
|
+
default: RemoteAppComponent
|
|
241
|
+
};
|
|
242
|
+
} else {
|
|
243
|
+
index.LoggerInstance.debug(
|
|
244
|
+
`createRemoteAppComponent LazyComponent module not found >>>`,
|
|
245
|
+
{ name: moduleName, module: m, exportName }
|
|
246
|
+
);
|
|
247
|
+
throw Error(
|
|
248
|
+
`Make sure that ${moduleName} has the correct export when export is ${String(
|
|
249
|
+
exportName
|
|
250
|
+
)}`
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
} catch (error) {
|
|
254
|
+
throw error;
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
function createRemoteAppComponent(info) {
|
|
259
|
+
const LazyComponent = createLazyRemoteComponent(info);
|
|
260
|
+
return React.forwardRef((props, ref) => {
|
|
261
|
+
return /* @__PURE__ */ React.createElement(
|
|
262
|
+
bridgeBase.ErrorBoundary,
|
|
263
|
+
{
|
|
264
|
+
FallbackComponent: info.fallback
|
|
265
|
+
},
|
|
266
|
+
/* @__PURE__ */ React.createElement(React.Suspense, { fallback: info.loading }, /* @__PURE__ */ React.createElement(LazyComponent, { ...props, ref }))
|
|
267
|
+
);
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
function createRemoteComponent(info) {
|
|
271
|
+
index.LoggerInstance.warn(
|
|
272
|
+
`createRemoteComponent is deprecated, please use createRemoteAppComponent instead!`
|
|
273
|
+
);
|
|
274
|
+
return createRemoteAppComponent(info);
|
|
275
|
+
}
|
|
276
|
+
exports.autoFetchDataPlugin = lazyLoadComponentPlugin.autoFetchData;
|
|
277
|
+
exports.collectSSRAssets = lazyLoadComponentPlugin.collectSSRAssets;
|
|
278
|
+
exports.createLazyComponent = lazyLoadComponentPlugin.createLazyComponent;
|
|
279
|
+
exports.lazyLoadComponentPlugin = lazyLoadComponentPlugin.lazyLoadComponentPlugin;
|
|
280
|
+
exports.CacheSize = lazyUtils.CacheSize;
|
|
281
|
+
exports.CacheTime = lazyUtils.CacheTime;
|
|
282
|
+
exports.ERROR_TYPE = lazyUtils.ERROR_TYPE;
|
|
283
|
+
exports.cache = lazyUtils.cache;
|
|
284
|
+
exports.clearStore = lazyUtils.clearStore;
|
|
285
|
+
exports.configureCache = lazyUtils.configureCache;
|
|
286
|
+
exports.generateKey = lazyUtils.generateKey;
|
|
287
|
+
exports.revalidateTag = lazyUtils.revalidateTag;
|
|
288
|
+
exports.setSSREnv = lazyUtils.setSSREnv;
|
|
289
|
+
exports.callDataFetch = dataFetchUtils.callDataFetch;
|
|
290
|
+
exports.prefetch = prefetch.prefetch;
|
|
291
|
+
exports.createBridgeComponent = createBridgeComponent;
|
|
292
|
+
exports.createRemoteAppComponent = createRemoteAppComponent;
|
|
293
|
+
exports.createRemoteComponent = createRemoteComponent;
|