@module-federation/bridge-react 0.16.0 → 0.17.1

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 (80) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/__tests__/bridge.spec.tsx +10 -10
  3. package/__tests__/createLazyComponent.spec.tsx +209 -0
  4. package/__tests__/prefetch.spec.ts +156 -0
  5. package/__tests__/router.spec.tsx +3 -3
  6. package/__tests__/setupTests.ts +8 -0
  7. package/dist/{bridge-base-P6pEjY1q.js → bridge-base-BoshEggF.mjs} +1 -1
  8. package/dist/{bridge-base-BBH982Tz.cjs → bridge-base-UGCwcMnG.js} +1 -1
  9. package/dist/data-fetch-server-middleware.cjs.js +163 -0
  10. package/dist/data-fetch-server-middleware.d.ts +15 -0
  11. package/dist/data-fetch-server-middleware.es.js +164 -0
  12. package/dist/data-fetch-utils.cjs.js +24 -0
  13. package/dist/data-fetch-utils.d.ts +81 -0
  14. package/dist/data-fetch-utils.es.js +26 -0
  15. package/dist/index-C0fDZB5b.js +45 -0
  16. package/dist/index-CqxytsLY.mjs +46 -0
  17. package/dist/index.cjs.js +35 -9
  18. package/dist/index.d.ts +141 -1
  19. package/dist/index.es.js +39 -13
  20. package/dist/index.esm-BCeUd-x9.mjs +418 -0
  21. package/dist/index.esm-j_1sIRzg.js +417 -0
  22. package/dist/lazy-load-component-plugin-B80Ud11k.js +521 -0
  23. package/dist/lazy-load-component-plugin-_UbR2mWQ.mjs +522 -0
  24. package/dist/lazy-load-component-plugin.cjs.js +6 -0
  25. package/dist/lazy-load-component-plugin.d.ts +16 -0
  26. package/dist/lazy-load-component-plugin.es.js +6 -0
  27. package/dist/lazy-utils.cjs.js +24 -0
  28. package/dist/lazy-utils.d.ts +149 -0
  29. package/dist/lazy-utils.es.js +24 -0
  30. package/dist/plugin.d.ts +13 -4
  31. package/dist/prefetch-BaKIdUwP.js +1338 -0
  32. package/dist/prefetch-YpJYjpWC.mjs +1339 -0
  33. package/dist/router-v5.cjs.js +1 -1
  34. package/dist/router-v5.d.ts +9 -0
  35. package/dist/router-v5.es.js +1 -1
  36. package/dist/router-v6.cjs.js +1 -1
  37. package/dist/router-v6.d.ts +9 -0
  38. package/dist/router-v6.es.js +1 -1
  39. package/dist/router.cjs.js +1 -1
  40. package/dist/router.d.ts +9 -0
  41. package/dist/router.es.js +1 -1
  42. package/dist/utils-C4oPJV34.mjs +2016 -0
  43. package/dist/utils-iEVlDmyk.js +2015 -0
  44. package/dist/v18.cjs.js +1 -1
  45. package/dist/v18.d.ts +9 -0
  46. package/dist/v18.es.js +1 -1
  47. package/dist/v19.cjs.js +1 -1
  48. package/dist/v19.d.ts +9 -0
  49. package/dist/v19.es.js +1 -1
  50. package/jest.config.ts +21 -0
  51. package/package.json +48 -6
  52. package/project.json +4 -8
  53. package/src/index.ts +32 -1
  54. package/src/lazy/AwaitDataFetch.tsx +215 -0
  55. package/src/lazy/constant.ts +30 -0
  56. package/src/lazy/createLazyComponent.tsx +411 -0
  57. package/src/lazy/data-fetch/cache.ts +291 -0
  58. package/src/lazy/data-fetch/call-data-fetch.ts +13 -0
  59. package/src/lazy/data-fetch/data-fetch-server-middleware.ts +196 -0
  60. package/src/lazy/data-fetch/index.ts +16 -0
  61. package/src/lazy/data-fetch/inject-data-fetch.ts +109 -0
  62. package/src/lazy/data-fetch/prefetch.ts +106 -0
  63. package/src/lazy/data-fetch/runtime-plugin.ts +115 -0
  64. package/src/lazy/index.ts +35 -0
  65. package/src/lazy/logger.ts +6 -0
  66. package/src/lazy/types.ts +75 -0
  67. package/src/lazy/utils.ts +372 -0
  68. package/src/lazy/wrapNoSSR.tsx +10 -0
  69. package/src/plugins/lazy-load-component-plugin.spec.ts +21 -0
  70. package/src/plugins/lazy-load-component-plugin.ts +57 -0
  71. package/src/provider/plugin.ts +4 -4
  72. package/src/remote/component.tsx +3 -3
  73. package/src/remote/create.tsx +18 -5
  74. package/tsconfig.json +1 -1
  75. package/tsconfig.spec.json +26 -0
  76. package/vite.config.ts +13 -0
  77. package/vitest.config.ts +6 -1
  78. package/dist/index-Cv3p6r66.cjs +0 -235
  79. package/dist/index-D4yt7Udv.js +0 -236
  80. package/src/.eslintrc.js +0 -9
package/dist/index.d.ts CHANGED
@@ -1,11 +1,92 @@
1
1
  import { default as default_2 } from 'react';
2
+ import { getInstance } from '@module-federation/runtime';
3
+ import { ModuleFederationRuntimePlugin } from '@module-federation/runtime';
2
4
  import * as React_2 from 'react';
5
+ import { ReactNode } from 'react';
6
+
7
+ export declare const autoFetchDataPlugin: () => ModuleFederationRuntimePlugin;
8
+
9
+ export declare function cache<T>(fn: DataFetch<T>, options?: CacheOptions): DataFetch<T>;
10
+
11
+ declare interface CacheConfig {
12
+ maxSize?: number;
13
+ unstable_shouldDisable?: ({ request, }: {
14
+ request: Request;
15
+ }) => boolean | Promise<boolean>;
16
+ }
17
+
18
+ declare interface CacheOptions {
19
+ tag?: string | string[];
20
+ maxAge?: number;
21
+ revalidate?: number;
22
+ getKey?: <Args extends any[]>(...args: Args) => string;
23
+ onCache?: (info: CacheStatsInfo) => boolean;
24
+ }
25
+
26
+ export declare const CacheSize: {
27
+ readonly KB: 1024;
28
+ readonly MB: number;
29
+ readonly GB: number;
30
+ };
31
+
32
+ export declare interface CacheStatsInfo {
33
+ status: CacheStatus;
34
+ key: string | symbol;
35
+ params: DataFetchParams;
36
+ result: any;
37
+ }
38
+
39
+ export declare type CacheStatus = 'hit' | 'stale' | 'miss';
40
+
41
+ export declare const CacheTime: {
42
+ readonly SECOND: 1000;
43
+ readonly MINUTE: number;
44
+ readonly HOUR: number;
45
+ readonly DAY: number;
46
+ readonly WEEK: number;
47
+ readonly MONTH: number;
48
+ };
49
+
50
+ export declare function callDataFetch(): Promise<void>;
51
+
52
+ export declare function clearStore(): void;
53
+
54
+ export declare function collectSSRAssets(options: CollectSSRAssetsOptions): default_2.ReactNode[];
55
+
56
+ export declare type CollectSSRAssetsOptions = {
57
+ id: string;
58
+ instance: ReturnType<typeof getInstance>;
59
+ injectScript?: boolean;
60
+ injectLink?: boolean;
61
+ };
62
+
63
+ export declare function configureCache(config: CacheConfig): void;
3
64
 
4
65
  export declare function createBridgeComponent<T = any>(bridgeInfo: Omit<ProviderFnParams<T>, 'createRoot'>): () => {
5
66
  render(info: RenderParams): Promise<void>;
6
67
  destroy(info: DestroyParams): void;
7
68
  };
8
69
 
70
+ export declare function createLazyComponent<T, E extends keyof T>(options: CreateLazyComponentOptions<T, E>): (props: T[E] extends (...args: any) => any ? Parameters<T[E]>[0] extends undefined ? ReactKey : Parameters<T[E]>[0] & ReactKey : ReactKey) => default_2.JSX.Element;
71
+
72
+ export declare type CreateLazyComponentOptions<T, E extends keyof T> = {
73
+ loader: () => Promise<T>;
74
+ instance: ReturnType<typeof getInstance>;
75
+ loading: default_2.ReactNode;
76
+ delayLoading?: number;
77
+ fallback: ReactNode | ((errorInfo: ErrorInfo) => ReactNode);
78
+ export?: E;
79
+ dataFetchParams?: DataFetchParams;
80
+ noSSR?: boolean;
81
+ injectScript?: boolean;
82
+ injectLink?: boolean;
83
+ };
84
+
85
+ export declare function createRemoteAppComponent<T = Record<string, unknown>, E extends keyof T = keyof T>(info: LazyRemoteComponentInfo<T, E>): default_2.ForwardRefExoticComponent<Omit<RemoteComponentProps<Record<string, unknown>>, "ref"> & default_2.RefAttributes<HTMLDivElement>>;
86
+
87
+ /**
88
+ * @deprecated createRemoteAppComponent is deprecated, please use createRemoteAppComponent instead!
89
+ */
9
90
  export declare function createRemoteComponent<T = Record<string, unknown>, E extends keyof T = keyof T>(info: LazyRemoteComponentInfo<T, E>): default_2.ForwardRefExoticComponent<Omit<RemoteComponentProps<Record<string, unknown>>, "ref"> & default_2.RefAttributes<HTMLDivElement>>;
10
91
 
11
92
  export declare interface CreateRootOptions {
@@ -22,6 +103,13 @@ declare interface CreateRootOptions_2 {
22
103
  transitionCallbacks?: unknown;
23
104
  }
24
105
 
106
+ declare type DataFetch<T> = (params: DataFetchParams) => Promise<T>;
107
+
108
+ export declare type DataFetchParams = {
109
+ isDowngrade: boolean;
110
+ _id?: string;
111
+ } & Record<string, unknown>;
112
+
25
113
  /**
26
114
  * Parameters for the destroy function
27
115
  */
@@ -30,7 +118,40 @@ export declare interface DestroyParams {
30
118
  dom: HTMLElement;
31
119
  }
32
120
 
33
- declare type LazyRemoteComponentInfo<T, E extends keyof T> = RemoteComponentParams<T>;
121
+ export declare const ERROR_TYPE: {
122
+ DATA_FETCH: number;
123
+ LOAD_REMOTE: number;
124
+ UNKNOWN: number;
125
+ };
126
+
127
+ declare type ErrorInfo = {
128
+ error: Error;
129
+ errorType: number;
130
+ dataFetchMapKey?: string;
131
+ };
132
+
133
+ export declare function generateKey(dataFetchOptions: DataFetchParams): string;
134
+
135
+ export declare function lazyLoadComponentPlugin(): ModuleFederationRuntimePlugin;
136
+
137
+ declare type LazyRemoteComponentInfo<T, _E extends keyof T> = RemoteComponentParams<T>;
138
+
139
+ export declare type NoSSRRemoteInfo = {
140
+ name: string;
141
+ version: string;
142
+ ssrPublicPath: string;
143
+ ssrRemoteEntry: string;
144
+ globalName: string;
145
+ };
146
+
147
+ export declare function prefetch(options: PrefetchOptions): Promise<void>;
148
+
149
+ declare type PrefetchOptions = {
150
+ id: string;
151
+ instance: ReturnType<typeof getInstance>;
152
+ dataFetchParams?: DataFetchParams;
153
+ preloadComponentResource?: boolean;
154
+ };
34
155
 
35
156
  /**
36
157
  * Parameters for the provider function
@@ -65,6 +186,10 @@ export declare interface ProviderParams {
65
186
  className?: string;
66
187
  }
67
188
 
189
+ declare type ReactKey = {
190
+ key?: default_2.Key | null;
191
+ };
192
+
68
193
  /**
69
194
  * Parameters for the remote component loader
70
195
  */
@@ -113,6 +238,8 @@ export declare interface RenderParams {
113
238
  [key: string]: unknown;
114
239
  }
115
240
 
241
+ export declare function revalidateTag(tag: string): void;
242
+
116
243
  export declare interface Root {
117
244
  render(children: React.ReactNode): void;
118
245
  unmount(): void;
@@ -131,4 +258,17 @@ declare interface Root_2 {
131
258
  */
132
259
  export declare type RootType = HTMLElement | Root_2;
133
260
 
261
+ export declare function setSSREnv({ fetchServerQuery, }: {
262
+ fetchServerQuery?: Record<string, unknown>;
263
+ }): void;
264
+
134
265
  export { }
266
+
267
+
268
+ declare module '@module-federation/runtime-core' {
269
+ interface ModuleFederation {
270
+ createLazyComponent<T, E extends keyof T>(options: Omit<CreateLazyComponentOptions<T, E>, 'instance'>): ReturnType<typeof createLazyComponent<T, E>>;
271
+ prefetch(options: Omit<PrefetchOptions, 'instance'>): ReturnType<typeof prefetch>;
272
+ collectSSRAssets(options: Omit<Parameters<typeof collectSSRAssets>[0], 'instance'>): ReturnType<typeof collectSSRAssets>;
273
+ }
274
+ }
package/dist/index.es.js CHANGED
@@ -1,9 +1,13 @@
1
- import { c as createBaseBridgeComponent, E as ErrorBoundary } from "./bridge-base-P6pEjY1q.js";
1
+ import { c as createBaseBridgeComponent, E as ErrorBoundary } from "./bridge-base-BoshEggF.mjs";
2
2
  import ReactDOM from "react-dom";
3
- import React__default, { forwardRef, useRef, useState, useEffect, useContext } from "react";
4
- import { L as LoggerInstance, g as getRootDomDefaultClassName, p as pathJoin } from "./index-D4yt7Udv.js";
3
+ import React__default, { forwardRef, useContext, useState, useEffect, useRef } from "react";
4
+ import { p as pathJoin, L as LoggerInstance, g as getRootDomDefaultClassName } from "./index-CqxytsLY.mjs";
5
5
  import * as ReactRouterDOM from "react-router-dom";
6
6
  import { federationRuntime } from "./plugin.es.js";
7
+ import { b, a, c, l } from "./lazy-load-component-plugin-_UbR2mWQ.mjs";
8
+ import { C, b as b2, E, e, h, c as c2, d, r, s } from "./utils-C4oPJV34.mjs";
9
+ import { callDataFetch } from "./data-fetch-utils.es.js";
10
+ import { p } from "./prefetch-YpJYjpWC.mjs";
7
11
  function createReact16Or17Root(container) {
8
12
  return {
9
13
  render(children) {
@@ -34,7 +38,7 @@ function createBridgeComponent(bridgeInfo) {
34
38
  };
35
39
  return createBaseBridgeComponent(fullBridgeInfo);
36
40
  }
37
- function e() {
41
+ function e2() {
38
42
  const t = new PopStateEvent("popstate", { state: window.history.state });
39
43
  window.dispatchEvent(t);
40
44
  }
@@ -64,7 +68,7 @@ const RemoteAppWrapper = forwardRef(function(props, ref) {
64
68
  var _a, _b, _c, _d, _e, _f, _g, _h;
65
69
  if ((_a = providerInfoRef.current) == null ? void 0 : _a.destroy) {
66
70
  LoggerInstance.debug(
67
- `createRemoteComponent LazyComponent destroy >>>`,
71
+ `createRemoteAppComponent LazyComponent destroy >>>`,
68
72
  { moduleName, basename, dom: renderDom.current }
69
73
  );
70
74
  (_d = (_c = (_b = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _b.lifecycle) == null ? void 0 : _c.beforeBridgeDestroy) == null ? void 0 : _d.emit({
@@ -152,7 +156,7 @@ function withRouterData(WrappedComponent) {
152
156
  }
153
157
  }
154
158
  }
155
- LoggerInstance.debug(`createRemoteComponent withRouterData >>>`, {
159
+ LoggerInstance.debug(`createRemoteAppComponent withRouterData >>>`, {
156
160
  ...props,
157
161
  basename,
158
162
  routerContextVal,
@@ -164,13 +168,13 @@ function withRouterData(WrappedComponent) {
164
168
  useEffect(() => {
165
169
  if (pathname !== "" && pathname !== location.pathname) {
166
170
  LoggerInstance.debug(
167
- `createRemoteComponent dispatchPopstateEnv >>>`,
171
+ `createRemoteAppComponent dispatchPopstateEnv >>>`,
168
172
  {
169
173
  name: props.name,
170
174
  pathname: location.pathname
171
175
  }
172
176
  );
173
- e();
177
+ e2();
174
178
  }
175
179
  setPathname(location.pathname);
176
180
  }, [location]);
@@ -185,7 +189,7 @@ const RemoteApp = withRouterData(RemoteAppWrapper);
185
189
  function createLazyRemoteComponent(info) {
186
190
  const exportName = (info == null ? void 0 : info.export) || "default";
187
191
  return React__default.lazy(async () => {
188
- LoggerInstance.debug(`createRemoteComponent LazyComponent create >>>`, {
192
+ LoggerInstance.debug(`createRemoteAppComponent LazyComponent create >>>`, {
189
193
  lazyComponent: info.loader,
190
194
  exportName
191
195
  });
@@ -193,7 +197,7 @@ function createLazyRemoteComponent(info) {
193
197
  const m = await info.loader();
194
198
  const moduleName = m && m[Symbol.for("mf_module_id")];
195
199
  LoggerInstance.debug(
196
- `createRemoteComponent LazyComponent loadRemote info >>>`,
200
+ `createRemoteAppComponent LazyComponent loadRemote info >>>`,
197
201
  { name: moduleName, module: m, exportName }
198
202
  );
199
203
  const exportFn = m[exportName];
@@ -216,7 +220,7 @@ function createLazyRemoteComponent(info) {
216
220
  };
217
221
  } else {
218
222
  LoggerInstance.debug(
219
- `createRemoteComponent LazyComponent module not found >>>`,
223
+ `createRemoteAppComponent LazyComponent module not found >>>`,
220
224
  { name: moduleName, module: m, exportName }
221
225
  );
222
226
  throw Error(
@@ -230,7 +234,7 @@ function createLazyRemoteComponent(info) {
230
234
  }
231
235
  });
232
236
  }
233
- function createRemoteComponent(info) {
237
+ function createRemoteAppComponent(info) {
234
238
  const LazyComponent = createLazyRemoteComponent(info);
235
239
  return forwardRef((props, ref) => {
236
240
  return /* @__PURE__ */ React__default.createElement(
@@ -242,7 +246,29 @@ function createRemoteComponent(info) {
242
246
  );
243
247
  });
244
248
  }
249
+ function createRemoteComponent(info) {
250
+ LoggerInstance.warn(
251
+ `createRemoteAppComponent is deprecated, please use createRemoteAppComponent instead!`
252
+ );
253
+ return createRemoteAppComponent(info);
254
+ }
245
255
  export {
256
+ C as CacheSize,
257
+ b2 as CacheTime,
258
+ E as ERROR_TYPE,
259
+ b as autoFetchDataPlugin,
260
+ e as cache,
261
+ callDataFetch,
262
+ h as clearStore,
263
+ a as collectSSRAssets,
264
+ c2 as configureCache,
246
265
  createBridgeComponent,
247
- createRemoteComponent
266
+ c as createLazyComponent,
267
+ createRemoteAppComponent,
268
+ createRemoteComponent,
269
+ d as generateKey,
270
+ l as lazyLoadComponentPlugin,
271
+ p as prefetch,
272
+ r as revalidateTag,
273
+ s as setSSREnv
248
274
  };