@module-federation/bridge-react 2.3.3 → 2.4.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/package.json +9 -5
- package/.turbo/turbo-build.log +0 -85
- package/CHANGELOG.md +0 -839
- package/__tests__/bridge.spec.tsx +0 -160
- package/__tests__/createLazyComponent.spec.tsx +0 -209
- package/__tests__/prefetch.spec.ts +0 -156
- package/__tests__/router.spec.tsx +0 -82
- package/__tests__/setupTests.ts +0 -8
- package/__tests__/util.ts +0 -36
- package/jest.config.ts +0 -21
- package/src/base.ts +0 -50
- package/src/index.ts +0 -50
- package/src/lazy/AwaitDataFetch.tsx +0 -217
- package/src/lazy/constant.ts +0 -30
- package/src/lazy/createLazyComponent.tsx +0 -411
- package/src/lazy/data-fetch/cache.ts +0 -291
- package/src/lazy/data-fetch/call-data-fetch.ts +0 -13
- package/src/lazy/data-fetch/data-fetch-server-middleware.ts +0 -196
- package/src/lazy/data-fetch/index.ts +0 -16
- package/src/lazy/data-fetch/inject-data-fetch.ts +0 -109
- package/src/lazy/data-fetch/prefetch.ts +0 -112
- package/src/lazy/data-fetch/runtime-plugin.ts +0 -115
- package/src/lazy/index.ts +0 -35
- package/src/lazy/logger.ts +0 -6
- package/src/lazy/types.ts +0 -75
- package/src/lazy/utils.ts +0 -375
- package/src/lazy/wrapNoSSR.tsx +0 -10
- package/src/modern-app-env.d.ts +0 -2
- package/src/plugins/lazy-load-component-plugin.spec.ts +0 -21
- package/src/plugins/lazy-load-component-plugin.ts +0 -57
- package/src/provider/context.tsx +0 -4
- package/src/provider/plugin.ts +0 -22
- package/src/provider/versions/bridge-base.tsx +0 -153
- package/src/provider/versions/legacy.ts +0 -87
- package/src/provider/versions/v18.ts +0 -47
- package/src/provider/versions/v19.ts +0 -48
- package/src/remote/RemoteAppWrapper.tsx +0 -108
- package/src/remote/base-component/component.tsx +0 -2
- package/src/remote/base-component/create.tsx +0 -23
- package/src/remote/base-component/index.tsx +0 -10
- package/src/remote/createHelpers.tsx +0 -132
- package/src/remote/router-component/component.tsx +0 -104
- package/src/remote/router-component/create.tsx +0 -23
- package/src/remote/router-component/index.tsx +0 -10
- package/src/router/default.tsx +0 -73
- package/src/router/v5.tsx +0 -43
- package/src/router/v6.tsx +0 -74
- package/src/router/v7.tsx +0 -75
- package/src/types.ts +0 -147
- package/src/utils/index.ts +0 -44
- package/src/v18.ts +0 -9
- package/src/v19.ts +0 -9
- package/tsconfig.json +0 -42
- package/tsconfig.node.json +0 -11
- package/tsconfig.spec.json +0 -26
- package/vite.config.ts +0 -112
- package/vitest.config.ts +0 -27
package/src/lazy/utils.ts
DELETED
|
@@ -1,375 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
isBrowserEnvValue,
|
|
3
|
-
composeKeyWithSeparator,
|
|
4
|
-
} from '@module-federation/sdk';
|
|
5
|
-
import logger from './logger';
|
|
6
|
-
import {
|
|
7
|
-
DOWNGRADE_KEY,
|
|
8
|
-
MF_DATA_FETCH_STATUS,
|
|
9
|
-
WRAP_DATA_FETCH_ID_IDENTIFIER,
|
|
10
|
-
DATA_FETCH_QUERY,
|
|
11
|
-
MF_DATA_FETCH_TYPE,
|
|
12
|
-
DATA_FETCH_IDENTIFIER,
|
|
13
|
-
DATA_FETCH_CLIENT_SUFFIX,
|
|
14
|
-
} from './constant';
|
|
15
|
-
|
|
16
|
-
import type { GlobalModuleInfo } from '@module-federation/sdk';
|
|
17
|
-
import type {
|
|
18
|
-
DataFetchParams,
|
|
19
|
-
MF_DATA_FETCH_MAP,
|
|
20
|
-
NoSSRRemoteInfo,
|
|
21
|
-
MF_SSR_DOWNGRADE,
|
|
22
|
-
MF_DATA_FETCH_MAP_VALUE_PROMISE_SET,
|
|
23
|
-
MF_DATA_FETCH_CACHE,
|
|
24
|
-
} from './types';
|
|
25
|
-
import type { ModuleFederation } from '@module-federation/runtime';
|
|
26
|
-
import { clearStore } from './data-fetch/cache';
|
|
27
|
-
|
|
28
|
-
export const getDataFetchInfo = ({
|
|
29
|
-
name,
|
|
30
|
-
alias,
|
|
31
|
-
id,
|
|
32
|
-
remoteSnapshot,
|
|
33
|
-
}: {
|
|
34
|
-
id: string;
|
|
35
|
-
name: string;
|
|
36
|
-
remoteSnapshot: GlobalModuleInfo[string];
|
|
37
|
-
alias?: string;
|
|
38
|
-
}) => {
|
|
39
|
-
if (!remoteSnapshot) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
if (!('modules' in remoteSnapshot)) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
const regex = new RegExp(`^${name}(/[^/].*|)$`);
|
|
46
|
-
const nameOrAlias = regex.test(id) ? name : alias || name;
|
|
47
|
-
|
|
48
|
-
const expose = id.replace(nameOrAlias, '');
|
|
49
|
-
let dataFetchName = '';
|
|
50
|
-
let dataFetchId = '';
|
|
51
|
-
let dataFetchKey = '';
|
|
52
|
-
if (expose.startsWith('/')) {
|
|
53
|
-
dataFetchName = `${expose.slice(1)}.${DATA_FETCH_IDENTIFIER}`;
|
|
54
|
-
dataFetchId = `${id}.${DATA_FETCH_IDENTIFIER}`;
|
|
55
|
-
dataFetchKey = `${name}${expose}.${DATA_FETCH_IDENTIFIER}`;
|
|
56
|
-
} else if (expose === '') {
|
|
57
|
-
dataFetchName = DATA_FETCH_IDENTIFIER;
|
|
58
|
-
dataFetchId = `${id}/${DATA_FETCH_IDENTIFIER}`;
|
|
59
|
-
dataFetchKey = `${name}/${DATA_FETCH_IDENTIFIER}`;
|
|
60
|
-
} else {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (!dataFetchName || !dataFetchId || !dataFetchKey) {
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (
|
|
69
|
-
!remoteSnapshot.modules.find(
|
|
70
|
-
(module) => module.moduleName === dataFetchName,
|
|
71
|
-
)
|
|
72
|
-
) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return {
|
|
77
|
-
dataFetchName,
|
|
78
|
-
dataFetchId,
|
|
79
|
-
dataFetchKey,
|
|
80
|
-
};
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export function initDataFetchMap() {
|
|
84
|
-
globalThis.__MF_DATA_FETCH_MAP__ ||= {};
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export function getDataFetchItem(id: string) {
|
|
88
|
-
return (globalThis.__MF_DATA_FETCH_MAP__ as MF_DATA_FETCH_MAP)?.[id];
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function getDataFetchMap() {
|
|
92
|
-
return globalThis.__MF_DATA_FETCH_MAP__ as MF_DATA_FETCH_MAP;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function getDataFetchCache() {
|
|
96
|
-
return globalThis.__MF_DATA_FETCH_CACHE__ as MF_DATA_FETCH_CACHE;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export const flushDataFetch = () => {
|
|
100
|
-
globalThis.__MF_DATA_FETCH_MAP__ = {};
|
|
101
|
-
globalThis[DOWNGRADE_KEY] = undefined;
|
|
102
|
-
clearStore();
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
export function setDataFetchItemLoadedStatus(id: string) {
|
|
106
|
-
const dataFetchItem = getDataFetchItem(id);
|
|
107
|
-
if (!dataFetchItem) {
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
dataFetchItem[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export const wrapDataFetchId = (id?: string) => {
|
|
114
|
-
return `${WRAP_DATA_FETCH_ID_IDENTIFIER}${id}${WRAP_DATA_FETCH_ID_IDENTIFIER}`;
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
export const getDataFetchIdWithErrorMsgs = (errMsgs: string) => {
|
|
118
|
-
const firstIdentifierIndex = errMsgs.indexOf(WRAP_DATA_FETCH_ID_IDENTIFIER);
|
|
119
|
-
if (firstIdentifierIndex === -1) {
|
|
120
|
-
return undefined;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const secondIdentifierIndex = errMsgs.indexOf(
|
|
124
|
-
WRAP_DATA_FETCH_ID_IDENTIFIER,
|
|
125
|
-
firstIdentifierIndex + WRAP_DATA_FETCH_ID_IDENTIFIER.length,
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
if (secondIdentifierIndex === -1) {
|
|
129
|
-
return undefined;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return errMsgs.substring(
|
|
133
|
-
firstIdentifierIndex + WRAP_DATA_FETCH_ID_IDENTIFIER.length,
|
|
134
|
-
secondIdentifierIndex,
|
|
135
|
-
);
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
export async function fetchData(
|
|
139
|
-
id: string,
|
|
140
|
-
params: DataFetchParams,
|
|
141
|
-
remoteInfo?: NoSSRRemoteInfo,
|
|
142
|
-
): Promise<unknown | undefined> {
|
|
143
|
-
const callFetchData = async () => {
|
|
144
|
-
const item = getDataFetchItem(id);
|
|
145
|
-
if (!item) {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
const [fetchDataFnArr, ..._rest] = item;
|
|
149
|
-
|
|
150
|
-
const fetchDataFn = await fetchDataFnArr[2];
|
|
151
|
-
if (!fetchDataFn) {
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
return fetchDataFn({
|
|
155
|
-
...params,
|
|
156
|
-
_id: id,
|
|
157
|
-
});
|
|
158
|
-
};
|
|
159
|
-
if (isBrowserEnvValue) {
|
|
160
|
-
const dataFetchItem = getDataFetchItem(id);
|
|
161
|
-
if (!dataFetchItem) {
|
|
162
|
-
throw new Error(`dataFetchItem not found, id: ${id}`);
|
|
163
|
-
}
|
|
164
|
-
if (dataFetchItem[1]?.[0]) {
|
|
165
|
-
return dataFetchItem[1][0];
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (isCSROnly()) {
|
|
169
|
-
logger.debug('==========csr only!');
|
|
170
|
-
return callFetchData();
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (remoteInfo) {
|
|
174
|
-
return callDowngrade(id, params, remoteInfo);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const mfDowngrade = getDowngradeTag();
|
|
178
|
-
if (
|
|
179
|
-
mfDowngrade &&
|
|
180
|
-
(typeof mfDowngrade === 'boolean' || mfDowngrade.includes(id))
|
|
181
|
-
) {
|
|
182
|
-
return callDowngrade(id, { ...params, isDowngrade: true });
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
let res;
|
|
186
|
-
let rej;
|
|
187
|
-
const p = new Promise((resolve, reject) => {
|
|
188
|
-
res = resolve;
|
|
189
|
-
rej = reject;
|
|
190
|
-
});
|
|
191
|
-
dataFetchItem[1] = [p, res, rej];
|
|
192
|
-
dataFetchItem[2] = MF_DATA_FETCH_STATUS.AWAIT;
|
|
193
|
-
return dataFetchItem[1][0];
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
return callFetchData();
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export function getDataFetchMapKey(
|
|
200
|
-
dataFetchInfo?: ReturnType<typeof getDataFetchInfo>,
|
|
201
|
-
hostInfo?: { name: string; version?: string },
|
|
202
|
-
) {
|
|
203
|
-
if (!dataFetchInfo || !hostInfo) {
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const { dataFetchKey } = dataFetchInfo;
|
|
208
|
-
|
|
209
|
-
return composeKeyWithSeparator(dataFetchKey, hostInfo.name, hostInfo.version);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
export async function loadDataFetchModule(
|
|
213
|
-
instance: ModuleFederation,
|
|
214
|
-
id: string,
|
|
215
|
-
) {
|
|
216
|
-
return instance.loadRemote(id).then((m) => {
|
|
217
|
-
if (
|
|
218
|
-
m &&
|
|
219
|
-
typeof m === 'object' &&
|
|
220
|
-
'fetchData' in m &&
|
|
221
|
-
typeof m.fetchData === 'function'
|
|
222
|
-
) {
|
|
223
|
-
return m.fetchData as (params: DataFetchParams) => Promise<unknown>;
|
|
224
|
-
}
|
|
225
|
-
throw new Error(
|
|
226
|
-
`fetchData not found in remote ${id}, ${JSON.stringify(m)}`,
|
|
227
|
-
);
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export function isDataLoaderExpose(exposeKey: string) {
|
|
232
|
-
return (
|
|
233
|
-
exposeKey.endsWith(DATA_FETCH_IDENTIFIER) ||
|
|
234
|
-
exposeKey.endsWith(DATA_FETCH_CLIENT_SUFFIX)
|
|
235
|
-
);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
export function getDowngradeTag() {
|
|
239
|
-
return globalThis[DOWNGRADE_KEY] as MF_SSR_DOWNGRADE;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
export function callAllDowngrade() {
|
|
243
|
-
const dataFetchMap = getDataFetchMap();
|
|
244
|
-
if (!dataFetchMap) {
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
Object.keys(dataFetchMap).forEach((key) => {
|
|
248
|
-
callDowngrade(key);
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
export async function callDowngrade(
|
|
253
|
-
id: string,
|
|
254
|
-
params?: DataFetchParams,
|
|
255
|
-
remoteInfo?: NoSSRRemoteInfo,
|
|
256
|
-
) {
|
|
257
|
-
const dataFetchMap = getDataFetchMap();
|
|
258
|
-
if (!dataFetchMap) {
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
const mfDataFetch = dataFetchMap[id];
|
|
262
|
-
if (mfDataFetch?.[2] === MF_DATA_FETCH_STATUS.AWAIT) {
|
|
263
|
-
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADING;
|
|
264
|
-
let promise: MF_DATA_FETCH_MAP_VALUE_PROMISE_SET[0];
|
|
265
|
-
let res: MF_DATA_FETCH_MAP_VALUE_PROMISE_SET[1];
|
|
266
|
-
let rej: MF_DATA_FETCH_MAP_VALUE_PROMISE_SET[2];
|
|
267
|
-
if (mfDataFetch[1]) {
|
|
268
|
-
promise = mfDataFetch[1][0];
|
|
269
|
-
res = mfDataFetch[1][1];
|
|
270
|
-
rej = mfDataFetch[1][2];
|
|
271
|
-
} else {
|
|
272
|
-
promise = new Promise((resolve, reject) => {
|
|
273
|
-
res = resolve;
|
|
274
|
-
rej = reject;
|
|
275
|
-
});
|
|
276
|
-
mfDataFetch[1] = [promise, res, rej];
|
|
277
|
-
}
|
|
278
|
-
const dataFetchType = mfDataFetch[0][1];
|
|
279
|
-
if (dataFetchType === MF_DATA_FETCH_TYPE.FETCH_CLIENT) {
|
|
280
|
-
try {
|
|
281
|
-
mfDataFetch[0][0]().then(async (getDataFetchFn) => {
|
|
282
|
-
return getDataFetchFn({
|
|
283
|
-
...params,
|
|
284
|
-
isDowngrade: true,
|
|
285
|
-
_id: id,
|
|
286
|
-
}).then((data) => {
|
|
287
|
-
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
288
|
-
res && res(data);
|
|
289
|
-
});
|
|
290
|
-
});
|
|
291
|
-
} catch (e) {
|
|
292
|
-
mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
|
|
293
|
-
rej && rej(e);
|
|
294
|
-
}
|
|
295
|
-
} else if (dataFetchType === MF_DATA_FETCH_TYPE.FETCH_SERVER) {
|
|
296
|
-
try {
|
|
297
|
-
const currentUrl = new URL(window.location.href);
|
|
298
|
-
currentUrl.searchParams.set(DATA_FETCH_QUERY, encodeURIComponent(id));
|
|
299
|
-
if (params) {
|
|
300
|
-
currentUrl.searchParams.set(
|
|
301
|
-
'params',
|
|
302
|
-
encodeURIComponent(JSON.stringify(params)),
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
if (remoteInfo) {
|
|
306
|
-
currentUrl.searchParams.set(
|
|
307
|
-
'remoteInfo',
|
|
308
|
-
encodeURIComponent(JSON.stringify(remoteInfo)),
|
|
309
|
-
);
|
|
310
|
-
}
|
|
311
|
-
const fetchServerQuery = globalThis.FEDERATION_SERVER_QUERY;
|
|
312
|
-
if (fetchServerQuery && typeof fetchServerQuery === 'object') {
|
|
313
|
-
Object.keys(fetchServerQuery).forEach((key) => {
|
|
314
|
-
currentUrl.searchParams.set(
|
|
315
|
-
key,
|
|
316
|
-
JSON.stringify(fetchServerQuery[key]),
|
|
317
|
-
);
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
const fetchUrl = currentUrl.toString();
|
|
321
|
-
const data = await fetch(fetchUrl).then((res) => res.json());
|
|
322
|
-
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
323
|
-
res && res(data);
|
|
324
|
-
} catch (e) {
|
|
325
|
-
mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
|
|
326
|
-
rej && rej(e);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
return promise;
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
export function isCSROnly() {
|
|
335
|
-
// @ts-ignore modern.js will inject window._SSR_DATA if enable ssr
|
|
336
|
-
return window._SSR_DATA === undefined;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
export function isServerEnv() {
|
|
340
|
-
return typeof window === 'undefined';
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
export function setSSREnv({
|
|
344
|
-
fetchServerQuery,
|
|
345
|
-
}: {
|
|
346
|
-
fetchServerQuery?: Record<string, unknown>;
|
|
347
|
-
}) {
|
|
348
|
-
globalThis.FEDERATION_SSR = true;
|
|
349
|
-
globalThis.FEDERATION_SERVER_QUERY = fetchServerQuery;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
export function getLoadedRemoteInfos(
|
|
353
|
-
id: string,
|
|
354
|
-
instance: ModuleFederation | null,
|
|
355
|
-
) {
|
|
356
|
-
if (!instance) {
|
|
357
|
-
return;
|
|
358
|
-
}
|
|
359
|
-
const { name, expose } = instance.remoteHandler.idToRemoteMap[id] || {};
|
|
360
|
-
if (!name) {
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
|
-
const module = instance.moduleCache.get(name);
|
|
364
|
-
if (!module) {
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
const { remoteSnapshot } = instance.snapshotHandler.getGlobalRemoteInfo(
|
|
368
|
-
module.remoteInfo,
|
|
369
|
-
);
|
|
370
|
-
return {
|
|
371
|
-
...module.remoteInfo,
|
|
372
|
-
snapshot: remoteSnapshot,
|
|
373
|
-
expose,
|
|
374
|
-
};
|
|
375
|
-
}
|
package/src/lazy/wrapNoSSR.tsx
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { createLazyComponent } from './createLazyComponent';
|
|
2
|
-
import type { CreateLazyComponentOptions } from './createLazyComponent';
|
|
3
|
-
|
|
4
|
-
export function wrapNoSSR<T, E extends keyof T>(
|
|
5
|
-
createLazyComponentFn: typeof createLazyComponent<T, E>,
|
|
6
|
-
) {
|
|
7
|
-
return (options: Omit<CreateLazyComponentOptions<T, E>, 'noSSR'>) => {
|
|
8
|
-
return createLazyComponentFn({ ...options, noSSR: true });
|
|
9
|
-
};
|
|
10
|
-
}
|
package/src/modern-app-env.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
-
import { lazyLoadComponentPlugin } from './lazy-load-component-plugin';
|
|
3
|
-
import type { ModuleFederation } from '@module-federation/runtime';
|
|
4
|
-
|
|
5
|
-
describe('lazyLoadComponentPlugin', () => {
|
|
6
|
-
it('should register lazy load component methods on the instance', () => {
|
|
7
|
-
const instance = {
|
|
8
|
-
registerPlugins: vi.fn(),
|
|
9
|
-
} as unknown as ModuleFederation;
|
|
10
|
-
|
|
11
|
-
const plugin = lazyLoadComponentPlugin();
|
|
12
|
-
if (plugin.apply) {
|
|
13
|
-
plugin.apply(instance);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
expect(instance.registerPlugins).toHaveBeenCalled();
|
|
17
|
-
expect(typeof instance.createLazyComponent).toBe('function');
|
|
18
|
-
expect(typeof instance.prefetch).toBe('function');
|
|
19
|
-
expect(typeof instance.collectSSRAssets).toBe('function');
|
|
20
|
-
});
|
|
21
|
-
});
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ModuleFederation,
|
|
3
|
-
ModuleFederationRuntimePlugin,
|
|
4
|
-
} from '@module-federation/runtime';
|
|
5
|
-
import {
|
|
6
|
-
createLazyComponent,
|
|
7
|
-
prefetch,
|
|
8
|
-
collectSSRAssets,
|
|
9
|
-
autoFetchDataPlugin,
|
|
10
|
-
} from '../lazy';
|
|
11
|
-
import type { CreateLazyComponentOptions, PrefetchOptions } from '../lazy';
|
|
12
|
-
|
|
13
|
-
declare module '@module-federation/runtime-core' {
|
|
14
|
-
interface ModuleFederation {
|
|
15
|
-
createLazyComponent<T, E extends keyof T>(
|
|
16
|
-
options: Omit<CreateLazyComponentOptions<T, E>, 'instance'>,
|
|
17
|
-
): ReturnType<typeof createLazyComponent<T, E>>;
|
|
18
|
-
prefetch(
|
|
19
|
-
options: Omit<PrefetchOptions, 'instance'>,
|
|
20
|
-
): ReturnType<typeof prefetch>;
|
|
21
|
-
collectSSRAssets(
|
|
22
|
-
options: Omit<Parameters<typeof collectSSRAssets>[0], 'instance'>,
|
|
23
|
-
): ReturnType<typeof collectSSRAssets>;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function lazyLoadComponentPlugin(): ModuleFederationRuntimePlugin {
|
|
28
|
-
return {
|
|
29
|
-
name: 'lazy-load-component-plugin',
|
|
30
|
-
apply(instance: ModuleFederation) {
|
|
31
|
-
instance.registerPlugins([autoFetchDataPlugin()]);
|
|
32
|
-
|
|
33
|
-
instance.createLazyComponent = (options) => {
|
|
34
|
-
return createLazyComponent({
|
|
35
|
-
instance,
|
|
36
|
-
...options,
|
|
37
|
-
});
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
instance.prefetch = (options) => {
|
|
41
|
-
return prefetch({
|
|
42
|
-
instance,
|
|
43
|
-
...options,
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
instance.collectSSRAssets = (options) => {
|
|
48
|
-
return collectSSRAssets({
|
|
49
|
-
instance,
|
|
50
|
-
...options,
|
|
51
|
-
});
|
|
52
|
-
};
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export default lazyLoadComponentPlugin;
|
package/src/provider/context.tsx
DELETED
package/src/provider/plugin.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ModuleFederationRuntimePlugin,
|
|
3
|
-
ModuleFederation,
|
|
4
|
-
} from '@module-federation/runtime';
|
|
5
|
-
|
|
6
|
-
export type FederationRuntimeType = {
|
|
7
|
-
instance: ModuleFederation | null;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export const federationRuntime: FederationRuntimeType = { instance: null };
|
|
11
|
-
|
|
12
|
-
function BridgeReactPlugin(): ModuleFederationRuntimePlugin {
|
|
13
|
-
return {
|
|
14
|
-
name: 'bridge-react-plugin',
|
|
15
|
-
beforeInit(args) {
|
|
16
|
-
federationRuntime.instance = args.origin;
|
|
17
|
-
return args;
|
|
18
|
-
},
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export default BridgeReactPlugin;
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Base bridge component implementation
|
|
3
|
-
* This file contains bridge component logic shared across all React versions
|
|
4
|
-
*/
|
|
5
|
-
import * as React from 'react';
|
|
6
|
-
import type {
|
|
7
|
-
ProviderParams,
|
|
8
|
-
ProviderFnParams,
|
|
9
|
-
RootType,
|
|
10
|
-
DestroyParams,
|
|
11
|
-
RenderParams,
|
|
12
|
-
CreateRootOptions,
|
|
13
|
-
} from '../../types';
|
|
14
|
-
import { ErrorBoundary, FallbackProps } from 'react-error-boundary';
|
|
15
|
-
import { RouterContext } from '../context';
|
|
16
|
-
import { LoggerInstance } from '../../utils';
|
|
17
|
-
import { federationRuntime } from '../plugin';
|
|
18
|
-
|
|
19
|
-
export function createBaseBridgeComponent<T>({
|
|
20
|
-
createRoot,
|
|
21
|
-
defaultRootOptions,
|
|
22
|
-
...bridgeInfo
|
|
23
|
-
}: ProviderFnParams<T>) {
|
|
24
|
-
return () => {
|
|
25
|
-
const rootMap = new Map<any, RootType>();
|
|
26
|
-
const instance = federationRuntime.instance;
|
|
27
|
-
LoggerInstance.debug(
|
|
28
|
-
`createBridgeComponent instance from props >>>`,
|
|
29
|
-
instance,
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
const RawComponent = (info: { propsInfo: T; appInfo: ProviderParams }) => {
|
|
33
|
-
const { appInfo, propsInfo, ...restProps } = info;
|
|
34
|
-
const { moduleName, memoryRoute, basename = '/' } = appInfo;
|
|
35
|
-
return (
|
|
36
|
-
<RouterContext.Provider value={{ moduleName, basename, memoryRoute }}>
|
|
37
|
-
<bridgeInfo.rootComponent
|
|
38
|
-
{...propsInfo}
|
|
39
|
-
basename={basename}
|
|
40
|
-
{...restProps}
|
|
41
|
-
/>
|
|
42
|
-
</RouterContext.Provider>
|
|
43
|
-
);
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const DefaultFallback = ({ error }: FallbackProps) => (
|
|
47
|
-
<div role="alert">
|
|
48
|
-
<p>Something went wrong:</p>
|
|
49
|
-
<pre style={{ color: 'red' }}>{error.message}</pre>
|
|
50
|
-
</div>
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
const ErrorBoundaryComponent =
|
|
54
|
-
ErrorBoundary as unknown as React.ComponentType<any>;
|
|
55
|
-
|
|
56
|
-
const BridgeWrapper = ({
|
|
57
|
-
basename,
|
|
58
|
-
moduleName,
|
|
59
|
-
memoryRoute,
|
|
60
|
-
propsInfo,
|
|
61
|
-
fallback,
|
|
62
|
-
}: {
|
|
63
|
-
basename?: string;
|
|
64
|
-
moduleName?: string;
|
|
65
|
-
memoryRoute?: any;
|
|
66
|
-
propsInfo: T;
|
|
67
|
-
fallback?: React.ComponentType<FallbackProps>;
|
|
68
|
-
}) => (
|
|
69
|
-
<ErrorBoundaryComponent FallbackComponent={fallback || DefaultFallback}>
|
|
70
|
-
<RawComponent
|
|
71
|
-
appInfo={{
|
|
72
|
-
moduleName,
|
|
73
|
-
basename,
|
|
74
|
-
memoryRoute,
|
|
75
|
-
}}
|
|
76
|
-
propsInfo={propsInfo}
|
|
77
|
-
/>
|
|
78
|
-
</ErrorBoundaryComponent>
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
return {
|
|
82
|
-
async render(info: RenderParams) {
|
|
83
|
-
LoggerInstance.debug(`createBridgeComponent render Info`, info);
|
|
84
|
-
const {
|
|
85
|
-
moduleName,
|
|
86
|
-
dom,
|
|
87
|
-
basename,
|
|
88
|
-
memoryRoute,
|
|
89
|
-
fallback,
|
|
90
|
-
rootOptions,
|
|
91
|
-
...propsInfo
|
|
92
|
-
} = info;
|
|
93
|
-
|
|
94
|
-
const mergedRootOptions: CreateRootOptions | undefined = {
|
|
95
|
-
...defaultRootOptions,
|
|
96
|
-
...(rootOptions as CreateRootOptions),
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const beforeBridgeRenderRes =
|
|
100
|
-
instance?.bridgeHook?.lifecycle?.beforeBridgeRender?.emit(info) || {};
|
|
101
|
-
|
|
102
|
-
const rootComponentWithErrorBoundary = (
|
|
103
|
-
<BridgeWrapper
|
|
104
|
-
basename={basename}
|
|
105
|
-
moduleName={moduleName}
|
|
106
|
-
memoryRoute={memoryRoute}
|
|
107
|
-
fallback={fallback as React.ComponentType<FallbackProps>}
|
|
108
|
-
propsInfo={
|
|
109
|
-
{
|
|
110
|
-
...propsInfo,
|
|
111
|
-
basename,
|
|
112
|
-
...(beforeBridgeRenderRes as any)?.extraProps,
|
|
113
|
-
} as T
|
|
114
|
-
}
|
|
115
|
-
/>
|
|
116
|
-
);
|
|
117
|
-
|
|
118
|
-
if (bridgeInfo.render) {
|
|
119
|
-
await Promise.resolve(
|
|
120
|
-
bridgeInfo.render(rootComponentWithErrorBoundary, dom),
|
|
121
|
-
).then((root: RootType) => rootMap.set(dom, root));
|
|
122
|
-
} else {
|
|
123
|
-
let root = rootMap.get(dom);
|
|
124
|
-
// Do not call createRoot multiple times
|
|
125
|
-
if (!root && createRoot) {
|
|
126
|
-
root = createRoot(dom, mergedRootOptions);
|
|
127
|
-
rootMap.set(dom, root as any);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (root && 'render' in root) {
|
|
131
|
-
root.render(rootComponentWithErrorBoundary);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
instance?.bridgeHook?.lifecycle?.afterBridgeRender?.emit(info) || {};
|
|
135
|
-
},
|
|
136
|
-
|
|
137
|
-
destroy(info: DestroyParams) {
|
|
138
|
-
const { dom } = info;
|
|
139
|
-
LoggerInstance.debug(`createBridgeComponent destroy Info`, info);
|
|
140
|
-
const root = rootMap.get(dom);
|
|
141
|
-
if (root) {
|
|
142
|
-
if ('unmount' in root) {
|
|
143
|
-
root.unmount();
|
|
144
|
-
} else {
|
|
145
|
-
LoggerInstance.warn('Root does not have unmount method');
|
|
146
|
-
}
|
|
147
|
-
rootMap.delete(dom);
|
|
148
|
-
}
|
|
149
|
-
instance?.bridgeHook?.lifecycle?.afterBridgeDestroy?.emit(info);
|
|
150
|
-
},
|
|
151
|
-
};
|
|
152
|
-
};
|
|
153
|
-
}
|