@module-federation/bridge-react 0.0.0-next-20250618090118 → 0.0.0-next-20250620084158

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