@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 +2 -4
- package/__tests__/bridge.spec.tsx +4 -75
- package/__tests__/util.ts +0 -18
- package/dist/index.cjs.js +74 -129
- package/dist/index.d.ts +7 -15
- package/dist/index.es.js +76 -131
- package/package.json +2 -12
- package/src/create.tsx +25 -25
- package/src/provider.tsx +23 -45
- package/src/remote/index.tsx +54 -96
- package/src/router.tsx +0 -2
- package/vite.config.ts +0 -4
- package/dist/router-v5.cjs.js +0 -80
- package/dist/router-v5.d.ts +0 -8
- package/dist/router-v5.es.js +0 -63
- package/dist/router-v6.cjs.js +0 -87
- package/dist/router-v6.d.ts +0 -11
- package/dist/router-v6.es.js +0 -64
- package/src/router-v5.tsx +0 -78
- package/src/router-v6.tsx +0 -76
package/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
# @module-federation/bridge-react
|
|
2
2
|
|
|
3
|
-
## 0.0.0-next-
|
|
3
|
+
## 0.0.0-next-20240801085730
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
-
-
|
|
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,
|
|
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(
|
|
49
|
-
return <div>life cycle render {
|
|
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
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
|
-
|
|
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
|
|
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(
|
|
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,
|
|
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
|
-
|
|
306
|
-
|
|
307
|
-
return /* @__PURE__ */ React.createElement(ErrorBoundary, { FallbackComponent: info.fallback }, /* @__PURE__ */ React.createElement(React.Suspense, { fallback: info.loading }, /* @__PURE__ */ React.createElement(LazyComponent, { ...props
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
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
|
-
|
|
387
|
-
}
|
|
331
|
+
}
|
|
332
|
+
)
|
|
333
|
+
);
|
|
388
334
|
} else {
|
|
389
|
-
|
|
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
|
-
|
|
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):
|
|
8
|
+
render(info: RenderFnParams & any): void;
|
|
13
9
|
destroy(info: {
|
|
14
10
|
dom: HTMLElement;
|
|
15
|
-
}):
|
|
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
|
-
|
|
26
|
-
|
|
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
|
|
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 { }
|