@module-federation/bridge-react 0.0.0-next-20240822035014 → 0.0.0-next-20240822090000
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 +3 -10
- package/__tests__/bridge.spec.tsx +3 -37
- package/dist/index.cjs.js +112 -142
- package/dist/index.d.ts +15 -16
- package/dist/index.es.js +114 -144
- package/dist/router.cjs.js +12 -8
- package/dist/router.es.js +12 -8
- package/package.json +2 -12
- package/src/create.tsx +25 -21
- package/src/provider.tsx +64 -69
- package/src/remote/index.tsx +56 -93
- package/src/router.tsx +10 -8
- package/vite.config.ts +0 -29
- package/dist/router-v5.cjs.js +0 -56
- package/dist/router-v5.d.ts +0 -9
- package/dist/router-v5.es.js +0 -32
- package/dist/router-v6.cjs.js +0 -83
- package/dist/router-v6.d.ts +0 -11
- package/dist/router-v6.es.js +0 -60
- package/src/router-v5.tsx +0 -44
- package/src/router-v6.tsx +0 -73
package/CHANGELOG.md
CHANGED
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
# @module-federation/bridge-react
|
|
2
2
|
|
|
3
|
-
## 0.0.0-next-
|
|
3
|
+
## 0.0.0-next-20240822090000
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
|
|
9
|
-
## 0.5.0
|
|
10
|
-
|
|
11
|
-
### Patch Changes
|
|
12
|
-
|
|
13
|
-
- 49d6135: feat(@module-federation/bridge): enhance Bridge capabilities and fix some issues
|
|
14
|
-
- Updated dependencies [49d6135]
|
|
15
|
-
- @module-federation/bridge-shared@0.5.0
|
|
7
|
+
- 3082116: feat: support module isolated reported
|
|
8
|
+
- @module-federation/bridge-shared@0.0.0-next-20240822090000
|
|
16
9
|
|
|
17
10
|
## 0.4.0
|
|
18
11
|
|
|
@@ -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,45 +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 and obtain ref property', async () => {
|
|
75
|
-
const ref = {
|
|
76
|
-
current: null,
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
function Component({ props }: { props?: Record<string, any> }) {
|
|
80
|
-
return <div>life cycle render {props?.msg}</div>;
|
|
81
|
-
}
|
|
82
|
-
const BridgeComponent = createBridgeComponent({
|
|
83
|
-
rootComponent: Component,
|
|
84
|
-
});
|
|
85
|
-
const RemoteComponent = createRemoteComponent({
|
|
86
|
-
loader: async () => {
|
|
87
|
-
return {
|
|
88
|
-
default: BridgeComponent,
|
|
89
|
-
};
|
|
90
|
-
},
|
|
91
|
-
fallback: () => <div></div>,
|
|
92
|
-
loading: <div>loading</div>,
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
const { container } = render(
|
|
96
|
-
<RemoteComponent ref={ref} props={{ msg: 'hello world' }} />,
|
|
97
|
-
);
|
|
64
|
+
const { container } = render(<RemoteComponent msg={'hello world'} />);
|
|
98
65
|
expect(getHtml(container)).toMatch('loading');
|
|
99
66
|
|
|
100
67
|
await sleep(200);
|
|
101
68
|
expect(getHtml(container)).toMatch('life cycle render');
|
|
102
69
|
expect(getHtml(container)).toMatch('hello world');
|
|
103
|
-
expect(ref.current).not.toBeNull();
|
|
104
70
|
});
|
|
105
71
|
});
|
package/dist/index.cjs.js
CHANGED
|
@@ -117,71 +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
|
-
const renderProps = {
|
|
141
|
-
moduleName,
|
|
142
|
-
dom: rootRef.current,
|
|
143
|
-
basename,
|
|
144
|
-
memoryRoute,
|
|
145
|
-
fallback,
|
|
146
|
-
...resProps
|
|
147
|
-
};
|
|
148
|
-
renderDom.current = rootRef.current;
|
|
149
|
-
context.LoggerInstance.log(
|
|
150
|
-
`createRemoteComponent LazyComponent render >>>`,
|
|
151
|
-
renderProps
|
|
152
|
-
);
|
|
153
|
-
providerReturn.render(renderProps);
|
|
154
|
-
});
|
|
155
|
-
return () => {
|
|
156
|
-
clearTimeout(renderTimeout);
|
|
157
|
-
setTimeout(() => {
|
|
158
|
-
var _a, _b;
|
|
159
|
-
if ((_a = providerInfoRef.current) == null ? void 0 : _a.destroy) {
|
|
160
|
-
context.LoggerInstance.log(
|
|
161
|
-
`createRemoteComponent LazyComponent destroy >>>`,
|
|
162
|
-
{ moduleName, basename, dom: renderDom.current }
|
|
163
|
-
);
|
|
164
|
-
(_b = providerInfoRef.current) == null ? void 0 : _b.destroy({
|
|
165
|
-
dom: renderDom.current
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
});
|
|
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
|
|
169
140
|
};
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
+
});
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
}, []);
|
|
164
|
+
return /* @__PURE__ */ React.createElement("div", { ref: rootRef });
|
|
165
|
+
};
|
|
166
|
+
RemoteApp["__APP_VERSION__"] = "0.4.0";
|
|
183
167
|
function withRouterData(WrappedComponent) {
|
|
184
|
-
|
|
168
|
+
return (props) => {
|
|
185
169
|
var _a;
|
|
186
170
|
let enableDispathPopstate = false;
|
|
187
171
|
let routerContextVal;
|
|
@@ -239,13 +223,10 @@ function withRouterData(WrappedComponent) {
|
|
|
239
223
|
setPathname(location.pathname);
|
|
240
224
|
}, [location]);
|
|
241
225
|
}
|
|
242
|
-
return /* @__PURE__ */ React.createElement(WrappedComponent, { ...props, basename
|
|
243
|
-
}
|
|
244
|
-
return React.forwardRef(function(props, ref) {
|
|
245
|
-
return /* @__PURE__ */ React.createElement(Component, { ...props, ref });
|
|
246
|
-
});
|
|
226
|
+
return /* @__PURE__ */ React.createElement(WrappedComponent, { ...props, basename });
|
|
227
|
+
};
|
|
247
228
|
}
|
|
248
|
-
const RemoteApp = withRouterData(
|
|
229
|
+
const RemoteApp$1 = withRouterData(RemoteApp);
|
|
249
230
|
function createLazyRemoteComponent(info) {
|
|
250
231
|
const exportName = (info == null ? void 0 : info.export) || "default";
|
|
251
232
|
return React.lazy(async () => {
|
|
@@ -262,15 +243,13 @@ function createLazyRemoteComponent(info) {
|
|
|
262
243
|
);
|
|
263
244
|
const exportFn = m2[exportName];
|
|
264
245
|
if (exportName in m2 && typeof exportFn === "function") {
|
|
265
|
-
const RemoteAppComponent = React.forwardRef((props,
|
|
246
|
+
const RemoteAppComponent = React.forwardRef((props, _ref) => {
|
|
266
247
|
return /* @__PURE__ */ React.createElement(
|
|
267
|
-
RemoteApp,
|
|
248
|
+
RemoteApp$1,
|
|
268
249
|
{
|
|
269
|
-
moduleName,
|
|
250
|
+
name: moduleName,
|
|
270
251
|
providerInfo: exportFn,
|
|
271
252
|
exportName: info.export || "default",
|
|
272
|
-
fallback: info.fallback,
|
|
273
|
-
ref,
|
|
274
253
|
...props
|
|
275
254
|
}
|
|
276
255
|
);
|
|
@@ -295,12 +274,10 @@ function createLazyRemoteComponent(info) {
|
|
|
295
274
|
});
|
|
296
275
|
}
|
|
297
276
|
function createRemoteComponent(info) {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
303
|
-
);
|
|
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
|
+
};
|
|
304
281
|
}
|
|
305
282
|
var client = {};
|
|
306
283
|
var m = ReactDOM;
|
|
@@ -327,76 +304,69 @@ if (process.env.NODE_ENV === "production") {
|
|
|
327
304
|
};
|
|
328
305
|
}
|
|
329
306
|
function createBridgeComponent(bridgeInfo) {
|
|
307
|
+
let provider;
|
|
330
308
|
return () => {
|
|
331
309
|
const rootMap = /* @__PURE__ */ new Map();
|
|
332
310
|
const RawComponent = (info) => {
|
|
333
|
-
const { appInfo, propsInfo
|
|
334
|
-
const {
|
|
335
|
-
return /* @__PURE__ */ React__namespace.createElement(context.RouterContext.Provider, { value: {
|
|
336
|
-
bridgeInfo.rootComponent,
|
|
337
|
-
{
|
|
338
|
-
...propsInfo,
|
|
339
|
-
basename,
|
|
340
|
-
...restProps
|
|
341
|
-
}
|
|
342
|
-
));
|
|
311
|
+
const { appInfo, propsInfo } = info;
|
|
312
|
+
const { name, memoryRoute, basename = "/" } = appInfo;
|
|
313
|
+
return /* @__PURE__ */ React__namespace.createElement(context.RouterContext.Provider, { value: { name, basename, memoryRoute } }, /* @__PURE__ */ React__namespace.createElement(bridgeInfo.rootComponent, { ...propsInfo, basename }));
|
|
343
314
|
};
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
basename,
|
|
351
|
-
memoryRoute,
|
|
352
|
-
fallback,
|
|
353
|
-
...propsInfo
|
|
354
|
-
} = info;
|
|
355
|
-
const rootComponentWithErrorBoundary = (
|
|
356
|
-
// set ErrorBoundary for RawComponent rendering error, usually caused by user app rendering error
|
|
357
|
-
/* @__PURE__ */ React__namespace.createElement(ErrorBoundary, { FallbackComponent: fallback }, /* @__PURE__ */ React__namespace.createElement(
|
|
358
|
-
RawComponent,
|
|
359
|
-
{
|
|
360
|
-
appInfo: {
|
|
361
|
-
moduleName,
|
|
362
|
-
basename,
|
|
363
|
-
memoryRoute
|
|
364
|
-
},
|
|
365
|
-
propsInfo
|
|
366
|
-
}
|
|
367
|
-
))
|
|
368
|
-
);
|
|
369
|
-
if (context.atLeastReact18(React__namespace)) {
|
|
370
|
-
if (bridgeInfo == null ? void 0 : bridgeInfo.render) {
|
|
371
|
-
Promise.resolve(
|
|
372
|
-
bridgeInfo == null ? void 0 : bridgeInfo.render(rootComponentWithErrorBoundary, dom)
|
|
373
|
-
).then((root) => rootMap.set(info.dom, root));
|
|
374
|
-
} else {
|
|
315
|
+
if (!provider) {
|
|
316
|
+
provider = {
|
|
317
|
+
render(info) {
|
|
318
|
+
context.LoggerInstance.log(`createBridgeComponent render Info`, info);
|
|
319
|
+
const { name, basename, memoryRoute, ...propsInfo } = info;
|
|
320
|
+
if (context.atLeastReact18(React__namespace)) {
|
|
375
321
|
const root = client.createRoot(info.dom);
|
|
376
|
-
root.render(rootComponentWithErrorBoundary);
|
|
377
322
|
rootMap.set(info.dom, root);
|
|
323
|
+
root.render(
|
|
324
|
+
/* @__PURE__ */ React__namespace.createElement(
|
|
325
|
+
RawComponent,
|
|
326
|
+
{
|
|
327
|
+
propsInfo,
|
|
328
|
+
appInfo: {
|
|
329
|
+
name,
|
|
330
|
+
basename,
|
|
331
|
+
memoryRoute
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
)
|
|
335
|
+
);
|
|
336
|
+
} else {
|
|
337
|
+
ReactDOM.render(
|
|
338
|
+
/* @__PURE__ */ React__namespace.createElement(
|
|
339
|
+
RawComponent,
|
|
340
|
+
{
|
|
341
|
+
propsInfo,
|
|
342
|
+
appInfo: {
|
|
343
|
+
name,
|
|
344
|
+
basename,
|
|
345
|
+
memoryRoute
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
),
|
|
349
|
+
info.dom
|
|
350
|
+
);
|
|
378
351
|
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
ReactDOM.unmountComponentAtNode(info.dom);
|
|
352
|
+
},
|
|
353
|
+
destroy(info) {
|
|
354
|
+
context.LoggerInstance.log(`createBridgeComponent destroy Info`, {
|
|
355
|
+
dom: info.dom
|
|
356
|
+
});
|
|
357
|
+
if (context.atLeastReact18(React__namespace)) {
|
|
358
|
+
const root = rootMap.get(info.dom);
|
|
359
|
+
root == null ? void 0 : root.unmount();
|
|
360
|
+
} else {
|
|
361
|
+
ReactDOM.unmountComponentAtNode(info.dom);
|
|
362
|
+
}
|
|
363
|
+
},
|
|
364
|
+
rawComponent: bridgeInfo.rootComponent,
|
|
365
|
+
__BRIDGE_FN__: (_args) => {
|
|
394
366
|
}
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
}
|
|
399
|
-
};
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
return provider;
|
|
400
370
|
};
|
|
401
371
|
}
|
|
402
372
|
exports.createBridgeComponent = createBridgeComponent;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,25 +1,20 @@
|
|
|
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
4
|
import { PropsWithChildren } from 'react';
|
|
6
5
|
import * as React_2 from 'react';
|
|
7
6
|
|
|
8
|
-
export declare function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>): () =>
|
|
9
|
-
render(info: RenderFnParams & any): Promise<void>;
|
|
10
|
-
destroy(info: {
|
|
11
|
-
dom: HTMLElement;
|
|
12
|
-
}): Promise<void>;
|
|
13
|
-
rawComponent: React_2.ComponentType<T>;
|
|
14
|
-
__BRIDGE_FN__: (_args: T) => void;
|
|
15
|
-
};
|
|
7
|
+
export declare function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>): () => Provider<T>;
|
|
16
8
|
|
|
17
9
|
export declare function createRemoteComponent<T, E extends keyof T>(info: {
|
|
18
10
|
loader: () => Promise<T>;
|
|
19
11
|
loading: default_2.ReactNode;
|
|
20
12
|
fallback: ErrorBoundaryPropsWithComponent['FallbackComponent'];
|
|
21
13
|
export?: E;
|
|
22
|
-
}):
|
|
14
|
+
}): (props: {
|
|
15
|
+
basename?: ProviderParams['basename'];
|
|
16
|
+
memoryRoute?: ProviderParams['memoryRoute'];
|
|
17
|
+
} & ("__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;
|
|
23
18
|
|
|
24
19
|
declare type ErrorBoundaryPropsWithComponent = ErrorBoundarySharedProps & {
|
|
25
20
|
fallback?: never;
|
|
@@ -45,25 +40,29 @@ declare type FallbackProps = {
|
|
|
45
40
|
resetErrorBoundary: (...args: any[]) => void;
|
|
46
41
|
};
|
|
47
42
|
|
|
43
|
+
declare interface Provider<T> {
|
|
44
|
+
render(info: any): void;
|
|
45
|
+
destroy(info: {
|
|
46
|
+
dom: HTMLElement;
|
|
47
|
+
}): void;
|
|
48
|
+
rawComponent: React_2.ComponentType;
|
|
49
|
+
__BRIDGE_FN__: (_args: T) => void;
|
|
50
|
+
}
|
|
51
|
+
|
|
48
52
|
declare type ProviderFnParams<T> = {
|
|
49
53
|
rootComponent: React_2.ComponentType<T>;
|
|
50
|
-
render?: (App: React_2.ReactElement, id?: HTMLElement | string) => RootType | Promise<RootType>;
|
|
51
54
|
};
|
|
52
55
|
|
|
53
56
|
export declare interface ProviderParams {
|
|
54
|
-
|
|
57
|
+
name?: string;
|
|
55
58
|
basename?: string;
|
|
56
59
|
memoryRoute?: {
|
|
57
60
|
entryPath: string;
|
|
58
61
|
};
|
|
59
|
-
style?: React.CSSProperties;
|
|
60
|
-
className?: string;
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
export declare interface RenderFnParams extends ProviderParams {
|
|
64
65
|
dom: HTMLElement;
|
|
65
66
|
}
|
|
66
67
|
|
|
67
|
-
declare type RootType = HTMLElement | default_3.Root;
|
|
68
|
-
|
|
69
68
|
export { }
|