@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
@@ -1,191 +0,0 @@
1
- import { DATA_FETCH_QUERY, MF_DATA_FETCH_STATUS } from '../constant';
2
- import {
3
- getDataFetchMap,
4
- fetchData,
5
- initDataFetchMap,
6
- loadDataFetchModule,
7
- } from '../utils';
8
- import { SEPARATOR, MANIFEST_EXT } from '@module-federation/sdk';
9
- import logger from '../logger';
10
-
11
- import type { NoSSRRemoteInfo } from '../types';
12
- import type { MiddlewareHandler } from 'hono';
13
-
14
- function wrapSetTimeout(
15
- targetPromise: Promise<unknown>,
16
- delay = 20000,
17
- id: string,
18
- ) {
19
- if (targetPromise && typeof targetPromise.then === 'function') {
20
- return new Promise((resolve, reject) => {
21
- const timeoutId = setTimeout(() => {
22
- logger.warn(`Data fetch for ID ${id} timed out after 20 seconds.`);
23
- reject(new Error(`Data fetch for ID ${id} timed out after 20 seconds`));
24
- }, delay);
25
-
26
- targetPromise
27
- .then((value: any) => {
28
- clearTimeout(timeoutId);
29
- resolve(value);
30
- })
31
- .catch((err: any) => {
32
- clearTimeout(timeoutId);
33
- reject(err);
34
- });
35
- });
36
- }
37
- }
38
-
39
- function addProtocol(url: string) {
40
- if (url.startsWith('//')) {
41
- return 'https:' + url;
42
- }
43
- return url;
44
- }
45
-
46
- const getDecodeQuery = (url: URL, name: string) => {
47
- const res = url.searchParams.get(name);
48
- if (!res) {
49
- return null;
50
- }
51
- return decodeURIComponent(res);
52
- };
53
-
54
- const dataFetchServerMiddleware: MiddlewareHandler = async (ctx, next) => {
55
- let url: URL;
56
- let dataFetchId: string | null;
57
- let params: Record<string, unknown>;
58
- let remoteInfo: NoSSRRemoteInfo;
59
- try {
60
- url = new URL(ctx.req.url);
61
- dataFetchId = getDecodeQuery(url, DATA_FETCH_QUERY);
62
- params = JSON.parse(getDecodeQuery(url, 'params') || '{}');
63
- const remoteInfoQuery = getDecodeQuery(url, 'remoteInfo');
64
- remoteInfo = remoteInfoQuery ? JSON.parse(remoteInfoQuery) : null;
65
- } catch (e) {
66
- logger.error('fetch data from server, error: ', e);
67
- return next();
68
- }
69
-
70
- if (!dataFetchId) {
71
- return next();
72
- }
73
- logger.log('fetch data from server, dataFetchId: ', dataFetchId);
74
- logger.debug(
75
- 'fetch data from server, moduleInfo: ',
76
- globalThis.__FEDERATION__?.moduleInfo,
77
- );
78
- try {
79
- const dataFetchMap = getDataFetchMap();
80
- if (!dataFetchMap) {
81
- initDataFetchMap();
82
- }
83
- const fetchDataPromise = dataFetchMap[dataFetchId]?.[1];
84
- logger.debug(
85
- 'fetch data from server, fetchDataPromise: ',
86
- fetchDataPromise,
87
- );
88
- if (
89
- fetchDataPromise &&
90
- dataFetchMap[dataFetchId]?.[2] !== MF_DATA_FETCH_STATUS.ERROR
91
- ) {
92
- const targetPromise = fetchDataPromise[0];
93
- // Ensure targetPromise is thenable
94
- const wrappedPromise = wrapSetTimeout(targetPromise, 20000, dataFetchId);
95
- if (wrappedPromise) {
96
- const res = await wrappedPromise;
97
- logger.log('fetch data from server, fetchDataPromise res: ', res);
98
- return ctx.json(res);
99
- }
100
- logger.error(
101
- `Expected a Promise from fetchDataPromise[0] for dataFetchId ${dataFetchId}, but received:`,
102
- targetPromise,
103
- 'Will try call new dataFetch again...',
104
- );
105
- }
106
-
107
- if (remoteInfo) {
108
- try {
109
- const hostInstance = globalThis.__FEDERATION__.__INSTANCES__[0];
110
- const remoteEntry = `${addProtocol(remoteInfo.ssrPublicPath) + remoteInfo.ssrRemoteEntry}`;
111
- if (!hostInstance) {
112
- throw new Error('host instance not found!');
113
- }
114
- const remote = hostInstance.options.remotes.find(
115
- (remote) => remote.name === remoteInfo.name,
116
- );
117
- logger.debug('find remote: ', JSON.stringify(remote));
118
- if (!remote) {
119
- hostInstance.registerRemotes([
120
- {
121
- name: remoteInfo.name,
122
- entry: remoteEntry,
123
- entryGlobalName: remoteInfo.globalName,
124
- },
125
- ]);
126
- } else if (
127
- !('entry' in remote) ||
128
- !remote.entry.includes(MANIFEST_EXT)
129
- ) {
130
- const { hostGlobalSnapshot, remoteSnapshot } =
131
- hostInstance.snapshotHandler.getGlobalRemoteInfo(remoteInfo);
132
- logger.debug(
133
- 'find hostGlobalSnapshot: ',
134
- JSON.stringify(hostGlobalSnapshot),
135
- );
136
- logger.debug('find remoteSnapshot: ', JSON.stringify(remoteSnapshot));
137
-
138
- if (!hostGlobalSnapshot || !remoteSnapshot) {
139
- if ('version' in remote) {
140
- // @ts-ignore
141
- delete remote.version;
142
- }
143
- // @ts-ignore
144
- remote.entry = remoteEntry;
145
- remote.entryGlobalName = remoteInfo.globalName;
146
- }
147
- }
148
- } catch (e) {
149
- ctx.status(500);
150
- return ctx.text(
151
- `failed to fetch ${remoteInfo.name} data, error:\n ${e}`,
152
- );
153
- }
154
- }
155
-
156
- const dataFetchItem = dataFetchMap[dataFetchId];
157
- logger.debug('fetch data from server, dataFetchItem: ', dataFetchItem);
158
- if (dataFetchItem) {
159
- const callFetchDataPromise = fetchData(dataFetchId, {
160
- ...params,
161
- isDowngrade: !remoteInfo,
162
- });
163
- const wrappedPromise = wrapSetTimeout(
164
- callFetchDataPromise,
165
- 20000,
166
- dataFetchId,
167
- );
168
- if (wrappedPromise) {
169
- const res = await wrappedPromise;
170
- logger.log('fetch data from server, dataFetchItem res: ', res);
171
- return ctx.json(res);
172
- }
173
- }
174
-
175
- const remoteId = dataFetchId.split(SEPARATOR)[0];
176
- const hostInstance = globalThis.__FEDERATION__.__INSTANCES__[0];
177
- if (!hostInstance) {
178
- throw new Error('host instance not found!');
179
- }
180
- const dataFetchFn = await loadDataFetchModule(hostInstance, remoteId);
181
- const data = await dataFetchFn({ ...params, isDowngrade: !remoteInfo });
182
- logger.log('fetch data from server, loadDataFetchModule res: ', data);
183
- return ctx.json(data);
184
- } catch (e) {
185
- logger.error('server plugin data fetch error: ', e);
186
- ctx.status(500);
187
- return ctx.text(`failed to fetch ${remoteInfo.name} data, error:\n ${e}`);
188
- }
189
- };
190
-
191
- export default dataFetchServerMiddleware;
@@ -1,2 +0,0 @@
1
- export { callDataFetch } from './call-data-fetch';
2
- export { injectDataFetch } from './inject-data-fetch';
@@ -1,109 +0,0 @@
1
- import {
2
- DATA_FETCH_FUNCTION,
3
- DOWNGRADE_KEY,
4
- FS_HREF,
5
- MF_DATA_FETCH_STATUS,
6
- MF_DATA_FETCH_TYPE,
7
- } from '../constant';
8
- import logger from '../logger';
9
- import {
10
- dataFetchFunctionOptions,
11
- MF_DATA_FETCH_MAP_VALUE_PROMISE_SET,
12
- } from '../types';
13
- import {
14
- callAllDowngrade,
15
- callDowngrade,
16
- getDataFetchItem,
17
- getDataFetchMap,
18
- getDowngradeTag,
19
- initDataFetchMap,
20
- } from '../utils';
21
-
22
- const dataFetchFunction = async function (options: dataFetchFunctionOptions) {
23
- const [id, data, downgrade] = options;
24
- logger.debug('==========call data fetch function!');
25
- if (data) {
26
- if (!id) {
27
- throw new Error('id is required!');
28
- }
29
- if (!getDataFetchMap()) {
30
- initDataFetchMap();
31
- }
32
- const dataFetchItem = getDataFetchItem(id);
33
- if (dataFetchItem) {
34
- dataFetchItem[1]?.[1]?.(data);
35
- dataFetchItem[2] = MF_DATA_FETCH_STATUS.LOADED;
36
- return;
37
- }
38
- if (!dataFetchItem) {
39
- const dataFetchMap = getDataFetchMap();
40
- let res: MF_DATA_FETCH_MAP_VALUE_PROMISE_SET[1];
41
- let rej: MF_DATA_FETCH_MAP_VALUE_PROMISE_SET[2];
42
- const p = new Promise((resolve, reject) => {
43
- res = resolve;
44
- rej = reject;
45
- });
46
-
47
- dataFetchMap[id] = [
48
- [
49
- async () => async () => {
50
- return '';
51
- },
52
- MF_DATA_FETCH_TYPE.FETCH_SERVER,
53
- ],
54
- [p, res, rej],
55
- MF_DATA_FETCH_STATUS.LOADED,
56
- ];
57
- res && res(data);
58
- return;
59
- }
60
- }
61
-
62
- if (downgrade) {
63
- const mfDowngrade = getDowngradeTag();
64
- if (!mfDowngrade) {
65
- globalThis[DOWNGRADE_KEY] = id ? [id] : true;
66
- } else if (Array.isArray(mfDowngrade) && id && !mfDowngrade.includes(id)) {
67
- mfDowngrade.push(id);
68
- }
69
- }
70
-
71
- const mfDowngrade = getDowngradeTag();
72
-
73
- if (typeof mfDowngrade === 'boolean') {
74
- return callAllDowngrade();
75
- }
76
- if (Array.isArray(mfDowngrade)) {
77
- if (!id) {
78
- globalThis[DOWNGRADE_KEY] = true;
79
- return callAllDowngrade();
80
- }
81
-
82
- if (!mfDowngrade.includes(id)) {
83
- mfDowngrade.push(id);
84
- }
85
-
86
- return callDowngrade(id);
87
- }
88
- };
89
-
90
- export function injectDataFetch() {
91
- globalThis[DATA_FETCH_FUNCTION] ||= [];
92
- const dataFetch = globalThis[DATA_FETCH_FUNCTION];
93
-
94
- //@ts-ignore
95
- if (dataFetch.push === dataFetchFunction) {
96
- return;
97
- }
98
-
99
- if (typeof window === 'undefined') {
100
- return;
101
- }
102
-
103
- globalThis[FS_HREF] = window.location.href;
104
-
105
- //@ts-ignore
106
- dataFetch.push = dataFetchFunction;
107
- }
108
-
109
- export { dataFetchFunction };
@@ -1,113 +0,0 @@
1
- import {
2
- getDataFetchInfo,
3
- initDataFetchMap,
4
- getDataFetchItem,
5
- getDataFetchMap,
6
- isCSROnly,
7
- getDataFetchMapKey,
8
- isDataLoaderExpose,
9
- loadDataFetchModule,
10
- } from '../utils';
11
- import logger from '../logger';
12
- import {
13
- MF_DATA_FETCH_TYPE,
14
- MF_DATA_FETCH_STATUS,
15
- DATA_FETCH_CLIENT_SUFFIX,
16
- } from '../constant';
17
-
18
- import type { MF_DATA_FETCH_MAP_VALUE } from '../types';
19
- import type { FederationRuntimePlugin } from '@module-federation/runtime';
20
-
21
- const autoFetchData: () => FederationRuntimePlugin = () => ({
22
- name: 'auto-fetch-data-plugin',
23
- beforeInit(args) {
24
- initDataFetchMap();
25
- return args;
26
- },
27
- afterLoadSnapshot(args) {
28
- const { id, moduleInfo, remoteSnapshot, host } = args;
29
- if (typeof id === 'string' && isDataLoaderExpose(id)) {
30
- return args;
31
- }
32
- if (!remoteSnapshot || !id || !('modules' in remoteSnapshot)) {
33
- return args;
34
- }
35
-
36
- const { name, alias } = moduleInfo;
37
- const dataFetchInfo = getDataFetchInfo({
38
- name,
39
- alias,
40
- id,
41
- remoteSnapshot,
42
- });
43
- if (!dataFetchInfo) {
44
- return args;
45
- }
46
- const { dataFetchId, dataFetchName } = dataFetchInfo;
47
-
48
- const dataFetchMapKey = getDataFetchMapKey(dataFetchInfo, {
49
- name: host.name,
50
- version: host.options.version,
51
- });
52
- logger.debug(
53
- '======= auto fetch plugin dataFetchMapKey: ',
54
- dataFetchMapKey,
55
- );
56
-
57
- if (!dataFetchMapKey) {
58
- return args;
59
- }
60
-
61
- const dataFetchItem = getDataFetchItem(dataFetchMapKey);
62
- if (dataFetchItem) {
63
- return args;
64
- }
65
-
66
- const dataFetchMap = getDataFetchMap();
67
- const hasSSRAsset = Boolean(remoteSnapshot.ssrRemoteEntry);
68
- const hasDataFetchClient = Boolean(
69
- remoteSnapshot.modules.find(
70
- (module) =>
71
- module.moduleName === `${dataFetchName}${DATA_FETCH_CLIENT_SUFFIX}`,
72
- ),
73
- );
74
- const downgradeType = hasDataFetchClient
75
- ? MF_DATA_FETCH_TYPE.FETCH_CLIENT
76
- : hasSSRAsset
77
- ? MF_DATA_FETCH_TYPE.FETCH_SERVER
78
- : MF_DATA_FETCH_TYPE.FETCH_CLIENT;
79
- let finalDataFetchId = dataFetchId;
80
-
81
- if (typeof window !== 'undefined') {
82
- finalDataFetchId =
83
- downgradeType === MF_DATA_FETCH_TYPE.FETCH_CLIENT
84
- ? hasDataFetchClient
85
- ? `${dataFetchId}${DATA_FETCH_CLIENT_SUFFIX}`
86
- : dataFetchId
87
- : dataFetchId;
88
- }
89
-
90
- const getDataFetchGetter = () =>
91
- loadDataFetchModule(host, finalDataFetchId);
92
-
93
- const dataFetchFnItem: MF_DATA_FETCH_MAP_VALUE[0] = [
94
- getDataFetchGetter,
95
- downgradeType,
96
- ];
97
-
98
- // server client must execute
99
- if (typeof window === 'undefined' || isCSROnly()) {
100
- dataFetchFnItem.push(getDataFetchGetter());
101
- }
102
-
103
- dataFetchMap[dataFetchMapKey] = [
104
- dataFetchFnItem,
105
- undefined,
106
- MF_DATA_FETCH_STATUS.AWAIT,
107
- ];
108
-
109
- return args;
110
- },
111
- });
112
-
113
- export default autoFetchData;
package/src/lazy/index.ts DELETED
@@ -1,18 +0,0 @@
1
- import autoFetchDataPlugin from './data-fetch/runtime-plugin';
2
-
3
- export { ERROR_TYPE } from './constant';
4
- export type { DataFetchParams, NoSSRRemoteInfo } from './types';
5
- export type {
6
- CreateLazyComponentOptions,
7
- IProps as CollectSSRAssetsOptions,
8
- } from './createLazyComponent';
9
-
10
- export { createLazyComponent, collectSSRAssets } from './createLazyComponent';
11
-
12
- export { wrapNoSSR } from './wrapNoSSR';
13
-
14
- export { injectDataFetch, callDataFetch } from './data-fetch';
15
-
16
- export { setSSREnv } from './utils';
17
-
18
- export { autoFetchDataPlugin };
@@ -1,6 +0,0 @@
1
- import { createLogger } from '@module-federation/sdk';
2
- import { PLUGIN_IDENTIFIER } from './constant';
3
-
4
- const logger = createLogger(PLUGIN_IDENTIFIER);
5
-
6
- export default logger;
package/src/lazy/types.ts DELETED
@@ -1,49 +0,0 @@
1
- import { MF_DATA_FETCH_TYPE, MF_DATA_FETCH_STATUS } from './constant';
2
-
3
- export type dataFetchFunctionOptions = [
4
- id?: string,
5
- data?: unknown,
6
- downgrade?: boolean,
7
- ];
8
-
9
- declare global {
10
- var _mfSSRDowngrade: string[] | true | undefined;
11
- var __MF_DATA_FETCH_MAP__: MF_DATA_FETCH_MAP | undefined;
12
- var FEDERATION_SERVER_QUERY: Record<string, unknown> | undefined;
13
- var FEDERATION_SSR: boolean | undefined;
14
- var _mfFSHref: string | undefined;
15
- var _mfDataFetch: Array<dataFetchFunctionOptions> | undefined;
16
- }
17
-
18
- export type DataFetchParams = {
19
- isDowngrade: boolean;
20
- } & Record<string, unknown>;
21
- export type DataFetch<T> = (params: DataFetchParams) => Promise<T>;
22
-
23
- // loading, resolve, reject
24
- export type MF_DATA_FETCH_MAP_VALUE_PROMISE_SET = [
25
- Promise<unknown>,
26
- ((data: unknown) => void)?,
27
- ((err: unknown) => void)?,
28
- ];
29
- export type MF_DATA_FETCH_MAP_VALUE = [
30
- [
31
- // getDataFetchGetter
32
- () => Promise<DataFetch<unknown>>,
33
- MF_DATA_FETCH_TYPE,
34
- // getDataFetchPromise
35
- Promise<DataFetch<unknown>>?,
36
- ],
37
- MF_DATA_FETCH_MAP_VALUE_PROMISE_SET?,
38
- MF_DATA_FETCH_STATUS?,
39
- ];
40
- export type MF_DATA_FETCH_MAP = Record<string, MF_DATA_FETCH_MAP_VALUE>;
41
- export type MF_SSR_DOWNGRADE = string[] | true | undefined;
42
-
43
- export type NoSSRRemoteInfo = {
44
- name: string;
45
- version: string;
46
- ssrPublicPath: string;
47
- ssrRemoteEntry: string;
48
- globalName: string;
49
- };