@module-federation/bridge-react 0.0.0-next-20240731105745 → 0.0.0-next-20240801085730

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 CHANGED
@@ -1,12 +1,10 @@
1
1
  # @module-federation/bridge-react
2
2
 
3
- ## 0.0.0-next-20240731105745
3
+ ## 0.0.0-next-20240801085730
4
4
 
5
5
  ### Patch Changes
6
6
 
7
- - 67fa05b: feat(@module-federation/bridge): optimize @module-federation/bridge package
8
- - Updated dependencies [67fa05b]
9
- - @module-federation/bridge-shared@0.0.0-next-20240731105745
7
+ - @module-federation/bridge-shared@0.0.0-next-20240801085730
10
8
 
11
9
  ## 0.3.3
12
10
 
@@ -8,7 +8,7 @@ import {
8
8
  screen,
9
9
  waitFor,
10
10
  } from '@testing-library/react';
11
- import { createContainer, createCustomContainer, getHtml, sleep } from './util';
11
+ import { createContainer, getHtml, sleep } from './util';
12
12
 
13
13
  describe('bridge', () => {
14
14
  let containerInfo: ReturnType<typeof createContainer>;
@@ -45,8 +45,8 @@ describe('bridge', () => {
45
45
  });
46
46
 
47
47
  it('createRemoteComponent', async () => {
48
- function Component({ props }: { props?: Record<string, any> }) {
49
- return <div>life cycle render {props?.msg}</div>;
48
+ function Component(info: { msg: string }) {
49
+ return <div>life cycle render {info.msg}</div>;
50
50
  }
51
51
  const BridgeComponent = createBridgeComponent({
52
52
  rootComponent: Component,
@@ -61,82 +61,11 @@ describe('bridge', () => {
61
61
  loading: <div>loading</div>,
62
62
  });
63
63
 
64
- const { container } = render(
65
- <RemoteComponent props={{ msg: 'hello world' }} />,
66
- );
67
- expect(getHtml(container)).toMatch('loading');
68
-
69
- await sleep(200);
70
- expect(getHtml(container)).toMatch('life cycle render');
71
- expect(getHtml(container)).toMatch('hello world');
72
- });
73
-
74
- it('createRemoteComponent with dom provided, will render on the provided dom', async () => {
75
- containerInfo = createCustomContainer();
76
-
77
- function Component({ props }: { props?: Record<string, any> }) {
78
- return <div>life cycle render {props?.msg}</div>;
79
- }
80
- const BridgeComponent = createBridgeComponent({
81
- rootComponent: Component,
82
- });
83
- const RemoteComponent = createRemoteComponent({
84
- loader: async () => {
85
- return {
86
- default: BridgeComponent,
87
- };
88
- },
89
- fallback: () => <div></div>,
90
- loading: <div>loading</div>,
91
- dom: '#container-custom',
92
- });
93
-
94
- const { container } = render(
95
- <RemoteComponent props={{ msg: 'hello there' }} />,
96
- );
97
- expect(getHtml(container)).toMatch('loading');
98
-
99
- await sleep(200);
100
-
101
- const element = screen.getByTestId('container-custom');
102
- expect(element.children.length).toBeGreaterThan(0);
103
- expect(element.innerHTML).toContain('life cycle render');
104
- expect(element.innerHTML).toContain('hello there');
105
-
106
- const elementDefault = screen.getByTestId('container');
107
- expect(elementDefault.children.length).toBe(0);
108
- expect(elementDefault.innerHTML).toContain('');
109
- });
110
-
111
- it('createRemoteComponent and obtain ref property', async () => {
112
- const ref = {
113
- current: null,
114
- };
115
-
116
- function Component({ props }: { props?: Record<string, any> }) {
117
- return <div>life cycle render {props?.msg}</div>;
118
- }
119
- const BridgeComponent = createBridgeComponent({
120
- rootComponent: Component,
121
- });
122
- const RemoteComponent = createRemoteComponent({
123
- loader: async () => {
124
- return {
125
- default: BridgeComponent,
126
- };
127
- },
128
- fallback: () => <div></div>,
129
- loading: <div>loading</div>,
130
- });
131
-
132
- const { container } = render(
133
- <RemoteComponent ref={ref} props={{ msg: 'hello world' }} />,
134
- );
64
+ const { container } = render(<RemoteComponent msg={'hello world'} />);
135
65
  expect(getHtml(container)).toMatch('loading');
136
66
 
137
67
  await sleep(200);
138
68
  expect(getHtml(container)).toMatch('life cycle render');
139
69
  expect(getHtml(container)).toMatch('hello world');
140
- expect(ref.current).not.toBeNull();
141
70
  });
142
71
  });
package/__tests__/util.ts CHANGED
@@ -12,24 +12,6 @@ export async function sleep(time: number) {
12
12
  export function createContainer() {
13
13
  const container = document.createElement('div');
14
14
  container.setAttribute('id', 'container');
15
- container.setAttribute('data-testid', 'container');
16
-
17
- container.setAttribute('background', 'rgb(255, 112, 127)');
18
-
19
- document.body.appendChild(container);
20
-
21
- return {
22
- clean: () => {
23
- document.body.removeChild(container);
24
- },
25
- container,
26
- };
27
- }
28
-
29
- export function createCustomContainer() {
30
- const container = document.createElement('div');
31
- container.setAttribute('id', 'container-custom');
32
- container.setAttribute('data-testid', 'container-custom');
33
15
  document.body.appendChild(container);
34
16
 
35
17
  return {
package/dist/index.cjs.js CHANGED
@@ -117,79 +117,55 @@ function hasArrayChanged() {
117
117
  let b = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
118
118
  return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
119
119
  }
120
- const RemoteAppWrapper = React.forwardRef(function(props, ref) {
121
- const RemoteApp2 = () => {
122
- const {
123
- name,
124
- memoryRoute,
125
- basename,
126
- providerInfo,
127
- dom,
128
- className,
129
- style,
130
- ...resProps
131
- } = props;
132
- const rootRef = ref && "current" in ref ? ref : React.useRef(null);
133
- const renderDom = React.useRef(null);
134
- const providerInfoRef = React.useRef(null);
135
- React.useEffect(() => {
136
- const renderTimeout = setTimeout(() => {
137
- const providerReturn = providerInfo();
138
- providerInfoRef.current = providerReturn;
139
- let domElement = null;
140
- if (dom) {
141
- domElement = document.querySelector(dom);
142
- if (!domElement || !(domElement instanceof HTMLElement)) {
143
- throw new Error(`Invalid dom: ${dom}`);
144
- }
145
- rootRef.current = domElement;
146
- } else {
147
- domElement = rootRef.current;
120
+ const RemoteApp = ({
121
+ name,
122
+ memoryRoute,
123
+ basename,
124
+ providerInfo,
125
+ ...resProps
126
+ }) => {
127
+ const rootRef = React.useRef(null);
128
+ const renderDom = React.useRef(null);
129
+ const providerInfoRef = React.useRef(null);
130
+ React.useEffect(() => {
131
+ const renderTimeout = setTimeout(() => {
132
+ const providerReturn = providerInfo();
133
+ providerInfoRef.current = providerReturn;
134
+ const renderProps = {
135
+ name,
136
+ dom: rootRef.current,
137
+ basename,
138
+ memoryRoute,
139
+ ...resProps
140
+ };
141
+ renderDom.current = rootRef.current;
142
+ context.LoggerInstance.log(
143
+ `createRemoteComponent LazyComponent render >>>`,
144
+ renderProps
145
+ );
146
+ providerReturn.render(renderProps);
147
+ });
148
+ return () => {
149
+ clearTimeout(renderTimeout);
150
+ setTimeout(() => {
151
+ var _a, _b;
152
+ if ((_a = providerInfoRef.current) == null ? void 0 : _a.destroy) {
153
+ context.LoggerInstance.log(
154
+ `createRemoteComponent LazyComponent destroy >>>`,
155
+ { name, basename, dom: renderDom.current }
156
+ );
157
+ (_b = providerInfoRef.current) == null ? void 0 : _b.destroy({
158
+ dom: renderDom.current
159
+ });
148
160
  }
149
- const renderProps = {
150
- name,
151
- dom: domElement,
152
- basename,
153
- memoryRoute,
154
- ...resProps
155
- };
156
- renderDom.current = rootRef.current;
157
- context.LoggerInstance.log(
158
- `createRemoteComponent LazyComponent render >>>`,
159
- renderProps
160
- );
161
- providerReturn.render(renderProps);
162
161
  });
163
- return () => {
164
- clearTimeout(renderTimeout);
165
- setTimeout(() => {
166
- var _a, _b;
167
- if ((_a = providerInfoRef.current) == null ? void 0 : _a.destroy) {
168
- context.LoggerInstance.log(
169
- `createRemoteComponent LazyComponent destroy >>>`,
170
- { name, basename, dom: renderDom.current }
171
- );
172
- (_b = providerInfoRef.current) == null ? void 0 : _b.destroy({
173
- dom: renderDom.current
174
- });
175
- }
176
- });
177
- };
178
- }, []);
179
- return dom ? null : /* @__PURE__ */ React.createElement(
180
- "div",
181
- {
182
- className: props == null ? void 0 : props.className,
183
- style: props == null ? void 0 : props.style,
184
- ref: rootRef
185
- }
186
- );
187
- };
188
- RemoteApp2["__APP_VERSION__"] = "0.3.3";
189
- return /* @__PURE__ */ React.createElement(RemoteApp2, null);
190
- });
162
+ };
163
+ }, []);
164
+ return /* @__PURE__ */ React.createElement("div", { ref: rootRef });
165
+ };
166
+ RemoteApp["__APP_VERSION__"] = "0.3.3";
191
167
  function withRouterData(WrappedComponent) {
192
- const Component = React.forwardRef(function(props, ref) {
168
+ return (props) => {
193
169
  var _a;
194
170
  let enableDispathPopstate = false;
195
171
  let routerContextVal;
@@ -247,13 +223,10 @@ function withRouterData(WrappedComponent) {
247
223
  setPathname(location.pathname);
248
224
  }, [location]);
249
225
  }
250
- return /* @__PURE__ */ React.createElement(WrappedComponent, { ...props, basename, ref });
251
- });
252
- return React.forwardRef(function(props, ref) {
253
- return /* @__PURE__ */ React.createElement(Component, { ...props, ref });
254
- });
226
+ return /* @__PURE__ */ React.createElement(WrappedComponent, { ...props, basename });
227
+ };
255
228
  }
256
- const RemoteApp = withRouterData(RemoteAppWrapper);
229
+ const RemoteApp$1 = withRouterData(RemoteApp);
257
230
  function createLazyRemoteComponent(info) {
258
231
  const exportName = (info == null ? void 0 : info.export) || "default";
259
232
  return React.lazy(async () => {
@@ -270,14 +243,13 @@ function createLazyRemoteComponent(info) {
270
243
  );
271
244
  const exportFn = m2[exportName];
272
245
  if (exportName in m2 && typeof exportFn === "function") {
273
- const RemoteAppComponent = React.forwardRef((props, ref) => {
246
+ const RemoteAppComponent = React.forwardRef((props, _ref) => {
274
247
  return /* @__PURE__ */ React.createElement(
275
- RemoteApp,
248
+ RemoteApp$1,
276
249
  {
277
250
  name: moduleName,
278
251
  providerInfo: exportFn,
279
252
  exportName: info.export || "default",
280
- ref,
281
253
  ...props
282
254
  }
283
255
  );
@@ -302,10 +274,10 @@ function createLazyRemoteComponent(info) {
302
274
  });
303
275
  }
304
276
  function createRemoteComponent(info) {
305
- return React.forwardRef(function(props, ref) {
306
- const LazyComponent = createLazyRemoteComponent(info);
307
- return /* @__PURE__ */ React.createElement(ErrorBoundary, { FallbackComponent: info.fallback }, /* @__PURE__ */ React.createElement(React.Suspense, { fallback: info.loading }, /* @__PURE__ */ React.createElement(LazyComponent, { ...props, dom: info == null ? void 0 : info.dom, ref })));
308
- });
277
+ const LazyComponent = createLazyRemoteComponent(info);
278
+ return (props) => {
279
+ return /* @__PURE__ */ React.createElement(ErrorBoundary, { FallbackComponent: info.fallback }, /* @__PURE__ */ React.createElement(React.Suspense, { fallback: info.loading }, /* @__PURE__ */ React.createElement(LazyComponent, { ...props })));
280
+ };
309
281
  }
310
282
  var client = {};
311
283
  var m = ReactDOM;
@@ -335,59 +307,32 @@ function createBridgeComponent(bridgeInfo) {
335
307
  return () => {
336
308
  const rootMap = /* @__PURE__ */ new Map();
337
309
  const RawComponent = (info) => {
338
- const { appInfo, propsInfo, ...restProps } = info;
310
+ const { appInfo, propsInfo } = info;
339
311
  const { name, memoryRoute, basename = "/" } = appInfo;
340
- return /* @__PURE__ */ React__namespace.createElement(context.RouterContext.Provider, { value: { name, basename, memoryRoute } }, /* @__PURE__ */ React__namespace.createElement(
341
- bridgeInfo.rootComponent,
342
- {
343
- ...propsInfo,
344
- basename,
345
- ...restProps
346
- }
347
- ));
312
+ return /* @__PURE__ */ React__namespace.createElement(context.RouterContext.Provider, { value: { name, basename, memoryRoute } }, /* @__PURE__ */ React__namespace.createElement(bridgeInfo.rootComponent, { ...propsInfo, basename }));
348
313
  };
349
314
  return {
350
- async render(info) {
315
+ render(info) {
351
316
  context.LoggerInstance.log(`createBridgeComponent render Info`, info);
352
317
  const { name, basename, memoryRoute, ...propsInfo } = info;
353
318
  if (context.atLeastReact18(React__namespace)) {
354
- if (bridgeInfo == null ? void 0 : bridgeInfo.render) {
355
- Promise.resolve(
356
- bridgeInfo == null ? void 0 : bridgeInfo.render(
357
- /* @__PURE__ */ React__namespace.createElement(
358
- RawComponent,
359
- {
360
- propsInfo,
361
- appInfo: {
362
- name,
363
- basename,
364
- memoryRoute
365
- }
366
- }
367
- ),
368
- info.dom
369
- )
370
- ).then((root) => rootMap.set(info.dom, root));
371
- } else {
372
- const root = client.createRoot(info.dom);
373
- root.render(
374
- /* @__PURE__ */ React__namespace.createElement(
375
- RawComponent,
376
- {
377
- propsInfo,
378
- appInfo: {
379
- name,
380
- basename,
381
- memoryRoute
382
- }
319
+ const root = client.createRoot(info.dom);
320
+ rootMap.set(info.dom, root);
321
+ root.render(
322
+ /* @__PURE__ */ React__namespace.createElement(
323
+ RawComponent,
324
+ {
325
+ propsInfo,
326
+ appInfo: {
327
+ name,
328
+ basename,
329
+ memoryRoute
383
330
  }
384
- )
385
- );
386
- rootMap.set(info.dom, root);
387
- }
331
+ }
332
+ )
333
+ );
388
334
  } else {
389
- const renderFunc = (bridgeInfo == null ? void 0 : bridgeInfo.render) || ReactDOM.render;
390
- renderFunc(
335
+ ReactDOM.render(
391
336
  /* @__PURE__ */ React__namespace.createElement(
392
337
  RawComponent,
393
338
  {
@@ -403,7 +348,7 @@ function createBridgeComponent(bridgeInfo) {
403
348
  );
404
349
  }
405
350
  },
406
- async destroy(info) {
351
+ destroy(info) {
407
352
  context.LoggerInstance.log(`createBridgeComponent destroy Info`, {
408
353
  dom: info.dom
409
354
  });
package/dist/index.d.ts CHANGED
@@ -1,18 +1,14 @@
1
1
  import { ComponentType } from 'react';
2
2
  import { default as default_2 } from 'react';
3
- import { default as default_3 } from 'react-dom/client';
4
3
  import { ErrorInfo } from 'react';
5
- import { ForwardRefExoticComponent } from 'react';
6
4
  import { PropsWithChildren } from 'react';
7
- import { PropsWithoutRef } from 'react';
8
5
  import * as React_2 from 'react';
9
- import { RefAttributes } from 'react';
10
6
 
11
7
  export declare function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>): () => {
12
- render(info: RenderFnParams & any): Promise<void>;
8
+ render(info: RenderFnParams & any): void;
13
9
  destroy(info: {
14
10
  dom: HTMLElement;
15
- }): Promise<void>;
11
+ }): void;
16
12
  rawComponent: React_2.ComponentType<T>;
17
13
  __BRIDGE_FN__: (_args: T) => void;
18
14
  };
@@ -22,8 +18,10 @@ export declare function createRemoteComponent<T, E extends keyof T>(info: {
22
18
  loading: default_2.ReactNode;
23
19
  fallback: ErrorBoundaryPropsWithComponent['FallbackComponent'];
24
20
  export?: E;
25
- dom?: string;
26
- }): ForwardRefExoticComponent<PropsWithoutRef<ProviderParams> & RefAttributes<HTMLElement | HTMLDivElement>>;
21
+ }): (props: {
22
+ basename?: ProviderParams['basename'];
23
+ memoryRoute?: ProviderParams['memoryRoute'];
24
+ } & ("__BRIDGE_FN__" extends keyof (T[E] extends (...args: any) => any ? ReturnType<T[E]> : never) ? (T[E] extends (...args: any) => any ? ReturnType<T[E]> : never)["__BRIDGE_FN__"] extends (...args: any) => any ? Parameters<(T[E] extends (...args: any) => any ? ReturnType<T[E]> : never)["__BRIDGE_FN__"]>[0] : {} : {})) => default_2.JSX.Element;
27
25
 
28
26
  declare type ErrorBoundaryPropsWithComponent = ErrorBoundarySharedProps & {
29
27
  fallback?: never;
@@ -51,24 +49,18 @@ declare type FallbackProps = {
51
49
 
52
50
  declare type ProviderFnParams<T> = {
53
51
  rootComponent: React_2.ComponentType<T>;
54
- render?: (App: React_2.ReactElement, id?: HTMLElement | string) => RootType | Promise<RootType>;
55
52
  };
56
53
 
57
- export declare interface ProviderParams extends default_2.HTMLAttributes<HTMLDivElement> {
54
+ export declare interface ProviderParams {
58
55
  name?: string;
59
56
  basename?: string;
60
57
  memoryRoute?: {
61
58
  entryPath: string;
62
59
  };
63
- props?: {
64
- [key: string]: any;
65
- };
66
60
  }
67
61
 
68
62
  export declare interface RenderFnParams extends ProviderParams {
69
63
  dom: HTMLElement;
70
64
  }
71
65
 
72
- declare type RootType = HTMLElement | default_3.Root;
73
-
74
66
  export { }