@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,372 @@
|
|
|
1
|
+
import { isBrowserEnv, composeKeyWithSeparator } from '@module-federation/sdk';
|
|
2
|
+
import logger from './logger';
|
|
3
|
+
import {
|
|
4
|
+
DOWNGRADE_KEY,
|
|
5
|
+
MF_DATA_FETCH_STATUS,
|
|
6
|
+
WRAP_DATA_FETCH_ID_IDENTIFIER,
|
|
7
|
+
DATA_FETCH_QUERY,
|
|
8
|
+
MF_DATA_FETCH_TYPE,
|
|
9
|
+
DATA_FETCH_IDENTIFIER,
|
|
10
|
+
DATA_FETCH_CLIENT_SUFFIX,
|
|
11
|
+
} from './constant';
|
|
12
|
+
|
|
13
|
+
import type { GlobalModuleInfo } from '@module-federation/sdk';
|
|
14
|
+
import type {
|
|
15
|
+
DataFetchParams,
|
|
16
|
+
MF_DATA_FETCH_MAP,
|
|
17
|
+
NoSSRRemoteInfo,
|
|
18
|
+
MF_SSR_DOWNGRADE,
|
|
19
|
+
MF_DATA_FETCH_MAP_VALUE_PROMISE_SET,
|
|
20
|
+
MF_DATA_FETCH_CACHE,
|
|
21
|
+
} from './types';
|
|
22
|
+
import type { ModuleFederation } from '@module-federation/runtime';
|
|
23
|
+
import { clearStore } from './data-fetch/cache';
|
|
24
|
+
|
|
25
|
+
export const getDataFetchInfo = ({
|
|
26
|
+
name,
|
|
27
|
+
alias,
|
|
28
|
+
id,
|
|
29
|
+
remoteSnapshot,
|
|
30
|
+
}: {
|
|
31
|
+
id: string;
|
|
32
|
+
name: string;
|
|
33
|
+
remoteSnapshot: GlobalModuleInfo[string];
|
|
34
|
+
alias?: string;
|
|
35
|
+
}) => {
|
|
36
|
+
if (!remoteSnapshot) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (!('modules' in remoteSnapshot)) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const regex = new RegExp(`^${name}(/[^/].*|)$`);
|
|
43
|
+
const nameOrAlias = regex.test(id) ? name : alias || name;
|
|
44
|
+
|
|
45
|
+
const expose = id.replace(nameOrAlias, '');
|
|
46
|
+
let dataFetchName = '';
|
|
47
|
+
let dataFetchId = '';
|
|
48
|
+
let dataFetchKey = '';
|
|
49
|
+
if (expose.startsWith('/')) {
|
|
50
|
+
dataFetchName = `${expose.slice(1)}.${DATA_FETCH_IDENTIFIER}`;
|
|
51
|
+
dataFetchId = `${id}.${DATA_FETCH_IDENTIFIER}`;
|
|
52
|
+
dataFetchKey = `${name}${expose}.${DATA_FETCH_IDENTIFIER}`;
|
|
53
|
+
} else if (expose === '') {
|
|
54
|
+
dataFetchName = DATA_FETCH_IDENTIFIER;
|
|
55
|
+
dataFetchId = `${id}/${DATA_FETCH_IDENTIFIER}`;
|
|
56
|
+
dataFetchKey = `${name}/${DATA_FETCH_IDENTIFIER}`;
|
|
57
|
+
} else {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!dataFetchName || !dataFetchId || !dataFetchKey) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (
|
|
66
|
+
!remoteSnapshot.modules.find(
|
|
67
|
+
(module) => module.moduleName === dataFetchName,
|
|
68
|
+
)
|
|
69
|
+
) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
dataFetchName,
|
|
75
|
+
dataFetchId,
|
|
76
|
+
dataFetchKey,
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export function initDataFetchMap() {
|
|
81
|
+
globalThis.__MF_DATA_FETCH_MAP__ ||= {};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function getDataFetchItem(id: string) {
|
|
85
|
+
return (globalThis.__MF_DATA_FETCH_MAP__ as MF_DATA_FETCH_MAP)?.[id];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function getDataFetchMap() {
|
|
89
|
+
return globalThis.__MF_DATA_FETCH_MAP__ as MF_DATA_FETCH_MAP;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function getDataFetchCache() {
|
|
93
|
+
return globalThis.__MF_DATA_FETCH_CACHE__ as MF_DATA_FETCH_CACHE;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const flushDataFetch = () => {
|
|
97
|
+
globalThis.__MF_DATA_FETCH_MAP__ = {};
|
|
98
|
+
globalThis[DOWNGRADE_KEY] = undefined;
|
|
99
|
+
clearStore();
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export function setDataFetchItemLoadedStatus(id: string) {
|
|
103
|
+
const dataFetchItem = getDataFetchItem(id);
|
|
104
|
+
if (!dataFetchItem) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
dataFetchItem[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export const wrapDataFetchId = (id?: string) => {
|
|
111
|
+
return `${WRAP_DATA_FETCH_ID_IDENTIFIER}${id}${WRAP_DATA_FETCH_ID_IDENTIFIER}`;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export const getDataFetchIdWithErrorMsgs = (errMsgs: string) => {
|
|
115
|
+
const firstIdentifierIndex = errMsgs.indexOf(WRAP_DATA_FETCH_ID_IDENTIFIER);
|
|
116
|
+
if (firstIdentifierIndex === -1) {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const secondIdentifierIndex = errMsgs.indexOf(
|
|
121
|
+
WRAP_DATA_FETCH_ID_IDENTIFIER,
|
|
122
|
+
firstIdentifierIndex + WRAP_DATA_FETCH_ID_IDENTIFIER.length,
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
if (secondIdentifierIndex === -1) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return errMsgs.substring(
|
|
130
|
+
firstIdentifierIndex + WRAP_DATA_FETCH_ID_IDENTIFIER.length,
|
|
131
|
+
secondIdentifierIndex,
|
|
132
|
+
);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export async function fetchData(
|
|
136
|
+
id: string,
|
|
137
|
+
params: DataFetchParams,
|
|
138
|
+
remoteInfo?: NoSSRRemoteInfo,
|
|
139
|
+
): Promise<unknown | undefined> {
|
|
140
|
+
const callFetchData = async () => {
|
|
141
|
+
const item = getDataFetchItem(id);
|
|
142
|
+
if (!item) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const [fetchDataFnArr, ..._rest] = item;
|
|
146
|
+
|
|
147
|
+
const fetchDataFn = await fetchDataFnArr[2];
|
|
148
|
+
if (!fetchDataFn) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
return fetchDataFn({
|
|
152
|
+
...params,
|
|
153
|
+
_id: id,
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
if (isBrowserEnv()) {
|
|
157
|
+
const dataFetchItem = getDataFetchItem(id);
|
|
158
|
+
if (!dataFetchItem) {
|
|
159
|
+
throw new Error(`dataFetchItem not found, id: ${id}`);
|
|
160
|
+
}
|
|
161
|
+
if (dataFetchItem[1]?.[0]) {
|
|
162
|
+
return dataFetchItem[1][0];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (isCSROnly()) {
|
|
166
|
+
logger.debug('==========csr only!');
|
|
167
|
+
return callFetchData();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (remoteInfo) {
|
|
171
|
+
return callDowngrade(id, params, remoteInfo);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const mfDowngrade = getDowngradeTag();
|
|
175
|
+
if (
|
|
176
|
+
mfDowngrade &&
|
|
177
|
+
(typeof mfDowngrade === 'boolean' || mfDowngrade.includes(id))
|
|
178
|
+
) {
|
|
179
|
+
return callDowngrade(id, { ...params, isDowngrade: true });
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
let res;
|
|
183
|
+
let rej;
|
|
184
|
+
const p = new Promise((resolve, reject) => {
|
|
185
|
+
res = resolve;
|
|
186
|
+
rej = reject;
|
|
187
|
+
});
|
|
188
|
+
dataFetchItem[1] = [p, res, rej];
|
|
189
|
+
dataFetchItem[2] = MF_DATA_FETCH_STATUS.AWAIT;
|
|
190
|
+
return dataFetchItem[1][0];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return callFetchData();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function getDataFetchMapKey(
|
|
197
|
+
dataFetchInfo?: ReturnType<typeof getDataFetchInfo>,
|
|
198
|
+
hostInfo?: { name: string; version?: string },
|
|
199
|
+
) {
|
|
200
|
+
if (!dataFetchInfo || !hostInfo) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const { dataFetchKey } = dataFetchInfo;
|
|
205
|
+
|
|
206
|
+
return composeKeyWithSeparator(dataFetchKey, hostInfo.name, hostInfo.version);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export async function loadDataFetchModule(
|
|
210
|
+
instance: ModuleFederation,
|
|
211
|
+
id: string,
|
|
212
|
+
) {
|
|
213
|
+
return instance.loadRemote(id).then((m) => {
|
|
214
|
+
if (
|
|
215
|
+
m &&
|
|
216
|
+
typeof m === 'object' &&
|
|
217
|
+
'fetchData' in m &&
|
|
218
|
+
typeof m.fetchData === 'function'
|
|
219
|
+
) {
|
|
220
|
+
return m.fetchData as (params: DataFetchParams) => Promise<unknown>;
|
|
221
|
+
}
|
|
222
|
+
throw new Error(
|
|
223
|
+
`fetchData not found in remote ${id}, ${JSON.stringify(m)}`,
|
|
224
|
+
);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function isDataLoaderExpose(exposeKey: string) {
|
|
229
|
+
return (
|
|
230
|
+
exposeKey.endsWith(DATA_FETCH_IDENTIFIER) ||
|
|
231
|
+
exposeKey.endsWith(DATA_FETCH_CLIENT_SUFFIX)
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function getDowngradeTag() {
|
|
236
|
+
return globalThis[DOWNGRADE_KEY] as MF_SSR_DOWNGRADE;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function callAllDowngrade() {
|
|
240
|
+
const dataFetchMap = getDataFetchMap();
|
|
241
|
+
if (!dataFetchMap) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
Object.keys(dataFetchMap).forEach((key) => {
|
|
245
|
+
callDowngrade(key);
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export async function callDowngrade(
|
|
250
|
+
id: string,
|
|
251
|
+
params?: DataFetchParams,
|
|
252
|
+
remoteInfo?: NoSSRRemoteInfo,
|
|
253
|
+
) {
|
|
254
|
+
const dataFetchMap = getDataFetchMap();
|
|
255
|
+
if (!dataFetchMap) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
const mfDataFetch = dataFetchMap[id];
|
|
259
|
+
if (mfDataFetch?.[2] === MF_DATA_FETCH_STATUS.AWAIT) {
|
|
260
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADING;
|
|
261
|
+
let promise: MF_DATA_FETCH_MAP_VALUE_PROMISE_SET[0];
|
|
262
|
+
let res: MF_DATA_FETCH_MAP_VALUE_PROMISE_SET[1];
|
|
263
|
+
let rej: MF_DATA_FETCH_MAP_VALUE_PROMISE_SET[2];
|
|
264
|
+
if (mfDataFetch[1]) {
|
|
265
|
+
promise = mfDataFetch[1][0];
|
|
266
|
+
res = mfDataFetch[1][1];
|
|
267
|
+
rej = mfDataFetch[1][2];
|
|
268
|
+
} else {
|
|
269
|
+
promise = new Promise((resolve, reject) => {
|
|
270
|
+
res = resolve;
|
|
271
|
+
rej = reject;
|
|
272
|
+
});
|
|
273
|
+
mfDataFetch[1] = [promise, res, rej];
|
|
274
|
+
}
|
|
275
|
+
const dataFetchType = mfDataFetch[0][1];
|
|
276
|
+
if (dataFetchType === MF_DATA_FETCH_TYPE.FETCH_CLIENT) {
|
|
277
|
+
try {
|
|
278
|
+
mfDataFetch[0][0]().then(async (getDataFetchFn) => {
|
|
279
|
+
return getDataFetchFn({
|
|
280
|
+
...params,
|
|
281
|
+
isDowngrade: true,
|
|
282
|
+
_id: id,
|
|
283
|
+
}).then((data) => {
|
|
284
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
285
|
+
res && res(data);
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
} catch (e) {
|
|
289
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
|
|
290
|
+
rej && rej(e);
|
|
291
|
+
}
|
|
292
|
+
} else if (dataFetchType === MF_DATA_FETCH_TYPE.FETCH_SERVER) {
|
|
293
|
+
try {
|
|
294
|
+
const currentUrl = new URL(window.location.href);
|
|
295
|
+
currentUrl.searchParams.set(DATA_FETCH_QUERY, encodeURIComponent(id));
|
|
296
|
+
if (params) {
|
|
297
|
+
currentUrl.searchParams.set(
|
|
298
|
+
'params',
|
|
299
|
+
encodeURIComponent(JSON.stringify(params)),
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
if (remoteInfo) {
|
|
303
|
+
currentUrl.searchParams.set(
|
|
304
|
+
'remoteInfo',
|
|
305
|
+
encodeURIComponent(JSON.stringify(remoteInfo)),
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
const fetchServerQuery = globalThis.FEDERATION_SERVER_QUERY;
|
|
309
|
+
if (fetchServerQuery && typeof fetchServerQuery === 'object') {
|
|
310
|
+
Object.keys(fetchServerQuery).forEach((key) => {
|
|
311
|
+
currentUrl.searchParams.set(
|
|
312
|
+
key,
|
|
313
|
+
JSON.stringify(fetchServerQuery[key]),
|
|
314
|
+
);
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
const fetchUrl = currentUrl.toString();
|
|
318
|
+
const data = await fetch(fetchUrl).then((res) => res.json());
|
|
319
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
320
|
+
res && res(data);
|
|
321
|
+
} catch (e) {
|
|
322
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
|
|
323
|
+
rej && rej(e);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return promise;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export function isCSROnly() {
|
|
332
|
+
// @ts-ignore modern.js will inject window._SSR_DATA if enable ssr
|
|
333
|
+
return window._SSR_DATA === undefined;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function isServerEnv() {
|
|
337
|
+
return typeof window === 'undefined';
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export function setSSREnv({
|
|
341
|
+
fetchServerQuery,
|
|
342
|
+
}: {
|
|
343
|
+
fetchServerQuery?: Record<string, unknown>;
|
|
344
|
+
}) {
|
|
345
|
+
globalThis.FEDERATION_SSR = true;
|
|
346
|
+
globalThis.FEDERATION_SERVER_QUERY = fetchServerQuery;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export function getLoadedRemoteInfos(
|
|
350
|
+
id: string,
|
|
351
|
+
instance: ModuleFederation | null,
|
|
352
|
+
) {
|
|
353
|
+
if (!instance) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
const { name, expose } = instance.remoteHandler.idToRemoteMap[id] || {};
|
|
357
|
+
if (!name) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
const module = instance.moduleCache.get(name);
|
|
361
|
+
if (!module) {
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
const { remoteSnapshot } = instance.snapshotHandler.getGlobalRemoteInfo(
|
|
365
|
+
module.remoteInfo,
|
|
366
|
+
);
|
|
367
|
+
return {
|
|
368
|
+
...module.remoteInfo,
|
|
369
|
+
snapshot: remoteSnapshot,
|
|
370
|
+
expose,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ModuleFederation,
|
|
3
|
+
FederationRuntimePlugin,
|
|
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(): FederationRuntimePlugin {
|
|
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/plugin.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
ModuleFederationRuntimePlugin,
|
|
3
|
+
ModuleFederation,
|
|
4
4
|
} from '@module-federation/runtime';
|
|
5
5
|
|
|
6
6
|
export type FederationRuntimeType = {
|
|
7
|
-
instance:
|
|
7
|
+
instance: ModuleFederation | null;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
10
|
export const federationRuntime: FederationRuntimeType = { instance: null };
|
|
11
11
|
|
|
12
|
-
function BridgeReactPlugin():
|
|
12
|
+
function BridgeReactPlugin(): ModuleFederationRuntimePlugin {
|
|
13
13
|
return {
|
|
14
14
|
name: 'bridge-react-plugin',
|
|
15
15
|
beforeInit(args) {
|
package/src/remote/component.tsx
CHANGED
|
@@ -48,7 +48,7 @@ const RemoteAppWrapper = forwardRef(function (
|
|
|
48
48
|
return () => {
|
|
49
49
|
if (providerInfoRef.current?.destroy) {
|
|
50
50
|
LoggerInstance.debug(
|
|
51
|
-
`
|
|
51
|
+
`createRemoteAppComponent LazyComponent destroy >>>`,
|
|
52
52
|
{ moduleName, basename, dom: renderDom.current },
|
|
53
53
|
);
|
|
54
54
|
|
|
@@ -171,7 +171,7 @@ export function withRouterData<
|
|
|
171
171
|
}
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
LoggerInstance.debug(`
|
|
174
|
+
LoggerInstance.debug(`createRemoteAppComponent withRouterData >>>`, {
|
|
175
175
|
...props,
|
|
176
176
|
basename,
|
|
177
177
|
routerContextVal,
|
|
@@ -185,7 +185,7 @@ export function withRouterData<
|
|
|
185
185
|
useEffect(() => {
|
|
186
186
|
if (pathname !== '' && pathname !== location.pathname) {
|
|
187
187
|
LoggerInstance.debug(
|
|
188
|
-
`
|
|
188
|
+
`createRemoteAppComponent dispatchPopstateEnv >>>`,
|
|
189
189
|
{
|
|
190
190
|
name: props.name,
|
|
191
191
|
pathname: location.pathname,
|
package/src/remote/create.tsx
CHANGED
|
@@ -16,7 +16,7 @@ function createLazyRemoteComponent<
|
|
|
16
16
|
>(info: LazyRemoteComponentInfo<T, E>) {
|
|
17
17
|
const exportName = info?.export || 'default';
|
|
18
18
|
return React.lazy(async () => {
|
|
19
|
-
LoggerInstance.debug(`
|
|
19
|
+
LoggerInstance.debug(`createRemoteAppComponent LazyComponent create >>>`, {
|
|
20
20
|
lazyComponent: info.loader,
|
|
21
21
|
exportName,
|
|
22
22
|
});
|
|
@@ -26,7 +26,7 @@ function createLazyRemoteComponent<
|
|
|
26
26
|
// @ts-ignore
|
|
27
27
|
const moduleName = m && m[Symbol.for('mf_module_id')];
|
|
28
28
|
LoggerInstance.debug(
|
|
29
|
-
`
|
|
29
|
+
`createRemoteAppComponent LazyComponent loadRemote info >>>`,
|
|
30
30
|
{ name: moduleName, module: m, exportName },
|
|
31
31
|
);
|
|
32
32
|
|
|
@@ -55,7 +55,7 @@ function createLazyRemoteComponent<
|
|
|
55
55
|
};
|
|
56
56
|
} else {
|
|
57
57
|
LoggerInstance.debug(
|
|
58
|
-
`
|
|
58
|
+
`createRemoteAppComponent LazyComponent module not found >>>`,
|
|
59
59
|
{ name: moduleName, module: m, exportName },
|
|
60
60
|
);
|
|
61
61
|
throw Error(
|
|
@@ -70,7 +70,7 @@ function createLazyRemoteComponent<
|
|
|
70
70
|
});
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
export function
|
|
73
|
+
export function createRemoteAppComponent<
|
|
74
74
|
T = Record<string, unknown>,
|
|
75
75
|
E extends keyof T = keyof T,
|
|
76
76
|
>(info: LazyRemoteComponentInfo<T, E>) {
|
|
@@ -87,3 +87,16 @@ export function createRemoteComponent<
|
|
|
87
87
|
);
|
|
88
88
|
});
|
|
89
89
|
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @deprecated createRemoteAppComponent is deprecated, please use createRemoteAppComponent instead!
|
|
93
|
+
*/
|
|
94
|
+
export function createRemoteComponent<
|
|
95
|
+
T = Record<string, unknown>,
|
|
96
|
+
E extends keyof T = keyof T,
|
|
97
|
+
>(info: LazyRemoteComponentInfo<T, E>) {
|
|
98
|
+
LoggerInstance.warn(
|
|
99
|
+
`createRemoteAppComponent is deprecated, please use createRemoteAppComponent instead!`,
|
|
100
|
+
);
|
|
101
|
+
return createRemoteAppComponent(info);
|
|
102
|
+
}
|
package/tsconfig.json
CHANGED
package/vite.config.ts
CHANGED
|
@@ -26,6 +26,19 @@ export default defineConfig({
|
|
|
26
26
|
'router-v6': path.resolve(__dirname, 'src/router/v6.tsx'),
|
|
27
27
|
v18: path.resolve(__dirname, 'src/v18.ts'),
|
|
28
28
|
v19: path.resolve(__dirname, 'src/v19.ts'),
|
|
29
|
+
'lazy-load-component-plugin': path.resolve(
|
|
30
|
+
__dirname,
|
|
31
|
+
'src/plugins/lazy-load-component-plugin.ts',
|
|
32
|
+
),
|
|
33
|
+
'data-fetch-server-middleware': path.resolve(
|
|
34
|
+
__dirname,
|
|
35
|
+
'src/lazy/data-fetch/data-fetch-server-middleware.ts',
|
|
36
|
+
),
|
|
37
|
+
'lazy-utils': path.resolve(__dirname, 'src/lazy/utils.ts'),
|
|
38
|
+
'data-fetch-utils': path.resolve(
|
|
39
|
+
__dirname,
|
|
40
|
+
'src/lazy/data-fetch/index.ts',
|
|
41
|
+
),
|
|
29
42
|
},
|
|
30
43
|
formats: ['cjs', 'es'],
|
|
31
44
|
fileName: (format, entryName) => `${entryName}.${format}.js`,
|
package/vitest.config.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { defineConfig } from 'vitest/config';
|
|
2
2
|
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
|
|
3
|
+
import react from '@vitejs/plugin-react';
|
|
3
4
|
import path from 'path';
|
|
4
5
|
export default defineConfig({
|
|
5
6
|
define: {
|
|
@@ -9,7 +10,10 @@ export default defineConfig({
|
|
|
9
10
|
__VERSION__: '"unknown"',
|
|
10
11
|
__APP_VERSION__: '"0.0.0"',
|
|
11
12
|
},
|
|
12
|
-
|
|
13
|
+
resolve: {
|
|
14
|
+
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'],
|
|
15
|
+
},
|
|
16
|
+
plugins: [react(), nxViteTsPaths()],
|
|
13
17
|
test: {
|
|
14
18
|
environment: 'jsdom',
|
|
15
19
|
include: [
|
|
@@ -18,5 +22,6 @@ export default defineConfig({
|
|
|
18
22
|
],
|
|
19
23
|
globals: true,
|
|
20
24
|
testTimeout: 10000,
|
|
25
|
+
setupFiles: [path.resolve(__dirname, '__tests__/setupTests.ts')],
|
|
21
26
|
},
|
|
22
27
|
});
|