@module-federation/bridge-react 0.0.0-next-20240819031127 → 0.0.0-next-20240819112150
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 -10
- package/__tests__/bridge.spec.tsx +3 -37
- package/dist/index.cjs.js +92 -127
- package/dist/index.d.ts +7 -10
- package/dist/index.es.js +94 -129
- package/dist/router.cjs.js +12 -8
- package/dist/router.es.js +12 -8
- package/package.json +2 -12
- package/src/create.tsx +19 -14
- package/src/provider.tsx +33 -50
- package/src/remote/index.tsx +56 -92
- package/src/router.tsx +10 -8
- package/vite.config.ts +0 -29
- package/dist/router-v5.cjs.js +0 -15
- package/dist/router-v5.d.ts +0 -11
- package/dist/router-v5.es.js +0 -8
- package/dist/router-v6.cjs.js +0 -15
- package/dist/router-v6.d.ts +0 -11
- package/dist/router-v6.es.js +0 -8
package/dist/index.es.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
import React__default, { createContext, Component, createElement, isValidElement,
|
|
3
|
-
import {
|
|
2
|
+
import React__default, { createContext, Component, createElement, isValidElement, useContext, useState, useEffect, useRef, forwardRef } from "react";
|
|
3
|
+
import { p as pathJoin, L as LoggerInstance, f, a as atLeastReact18, R as RouterContext } from "./context-Bw2PEwa6.js";
|
|
4
4
|
import * as ReactRouterDOM from "react-router-dom";
|
|
5
5
|
import ReactDOM from "react-dom";
|
|
6
6
|
const ErrorBoundaryContext = createContext(null);
|
|
@@ -98,71 +98,55 @@ function hasArrayChanged() {
|
|
|
98
98
|
let b = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
|
|
99
99
|
return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
|
|
100
100
|
}
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const renderProps = {
|
|
122
|
-
moduleName,
|
|
123
|
-
dom: rootRef.current,
|
|
124
|
-
basename,
|
|
125
|
-
memoryRoute,
|
|
126
|
-
fallback,
|
|
127
|
-
...resProps
|
|
128
|
-
};
|
|
129
|
-
renderDom.current = rootRef.current;
|
|
130
|
-
LoggerInstance.log(
|
|
131
|
-
`createRemoteComponent LazyComponent render >>>`,
|
|
132
|
-
renderProps
|
|
133
|
-
);
|
|
134
|
-
providerReturn.render(renderProps);
|
|
135
|
-
});
|
|
136
|
-
return () => {
|
|
137
|
-
clearTimeout(renderTimeout);
|
|
138
|
-
setTimeout(() => {
|
|
139
|
-
var _a, _b;
|
|
140
|
-
if ((_a = providerInfoRef.current) == null ? void 0 : _a.destroy) {
|
|
141
|
-
LoggerInstance.log(
|
|
142
|
-
`createRemoteComponent LazyComponent destroy >>>`,
|
|
143
|
-
{ moduleName, basename, dom: renderDom.current }
|
|
144
|
-
);
|
|
145
|
-
(_b = providerInfoRef.current) == null ? void 0 : _b.destroy({
|
|
146
|
-
dom: renderDom.current
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
});
|
|
101
|
+
const RemoteApp = ({
|
|
102
|
+
name,
|
|
103
|
+
memoryRoute,
|
|
104
|
+
basename,
|
|
105
|
+
providerInfo,
|
|
106
|
+
...resProps
|
|
107
|
+
}) => {
|
|
108
|
+
const rootRef = useRef(null);
|
|
109
|
+
const renderDom = useRef(null);
|
|
110
|
+
const providerInfoRef = useRef(null);
|
|
111
|
+
useEffect(() => {
|
|
112
|
+
const renderTimeout = setTimeout(() => {
|
|
113
|
+
const providerReturn = providerInfo();
|
|
114
|
+
providerInfoRef.current = providerReturn;
|
|
115
|
+
const renderProps = {
|
|
116
|
+
name,
|
|
117
|
+
dom: rootRef.current,
|
|
118
|
+
basename,
|
|
119
|
+
memoryRoute,
|
|
120
|
+
...resProps
|
|
150
121
|
};
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
122
|
+
renderDom.current = rootRef.current;
|
|
123
|
+
LoggerInstance.log(
|
|
124
|
+
`createRemoteComponent LazyComponent render >>>`,
|
|
125
|
+
renderProps
|
|
126
|
+
);
|
|
127
|
+
providerReturn.render(renderProps);
|
|
128
|
+
});
|
|
129
|
+
return () => {
|
|
130
|
+
clearTimeout(renderTimeout);
|
|
131
|
+
setTimeout(() => {
|
|
132
|
+
var _a, _b;
|
|
133
|
+
if ((_a = providerInfoRef.current) == null ? void 0 : _a.destroy) {
|
|
134
|
+
LoggerInstance.log(
|
|
135
|
+
`createRemoteComponent LazyComponent destroy >>>`,
|
|
136
|
+
{ name, basename, dom: renderDom.current }
|
|
137
|
+
);
|
|
138
|
+
(_b = providerInfoRef.current) == null ? void 0 : _b.destroy({
|
|
139
|
+
dom: renderDom.current
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
}, []);
|
|
145
|
+
return /* @__PURE__ */ React__default.createElement("div", { ref: rootRef });
|
|
146
|
+
};
|
|
147
|
+
RemoteApp["__APP_VERSION__"] = "0.3.5";
|
|
164
148
|
function withRouterData(WrappedComponent) {
|
|
165
|
-
|
|
149
|
+
return (props) => {
|
|
166
150
|
var _a;
|
|
167
151
|
let enableDispathPopstate = false;
|
|
168
152
|
let routerContextVal;
|
|
@@ -220,13 +204,10 @@ function withRouterData(WrappedComponent) {
|
|
|
220
204
|
setPathname(location.pathname);
|
|
221
205
|
}, [location]);
|
|
222
206
|
}
|
|
223
|
-
return /* @__PURE__ */ React__default.createElement(WrappedComponent, { ...props, basename
|
|
224
|
-
}
|
|
225
|
-
return forwardRef(function(props, ref) {
|
|
226
|
-
return /* @__PURE__ */ React__default.createElement(Component2, { ...props, ref });
|
|
227
|
-
});
|
|
207
|
+
return /* @__PURE__ */ React__default.createElement(WrappedComponent, { ...props, basename });
|
|
208
|
+
};
|
|
228
209
|
}
|
|
229
|
-
const RemoteApp = withRouterData(
|
|
210
|
+
const RemoteApp$1 = withRouterData(RemoteApp);
|
|
230
211
|
function createLazyRemoteComponent(info) {
|
|
231
212
|
const exportName = (info == null ? void 0 : info.export) || "default";
|
|
232
213
|
return React__default.lazy(async () => {
|
|
@@ -243,15 +224,13 @@ function createLazyRemoteComponent(info) {
|
|
|
243
224
|
);
|
|
244
225
|
const exportFn = m2[exportName];
|
|
245
226
|
if (exportName in m2 && typeof exportFn === "function") {
|
|
246
|
-
const RemoteAppComponent = forwardRef((props,
|
|
227
|
+
const RemoteAppComponent = forwardRef((props, _ref) => {
|
|
247
228
|
return /* @__PURE__ */ React__default.createElement(
|
|
248
|
-
RemoteApp,
|
|
229
|
+
RemoteApp$1,
|
|
249
230
|
{
|
|
250
|
-
moduleName,
|
|
231
|
+
name: moduleName,
|
|
251
232
|
providerInfo: exportFn,
|
|
252
233
|
exportName: info.export || "default",
|
|
253
|
-
fallback: info.fallback,
|
|
254
|
-
ref,
|
|
255
234
|
...props
|
|
256
235
|
}
|
|
257
236
|
);
|
|
@@ -276,13 +255,10 @@ function createLazyRemoteComponent(info) {
|
|
|
276
255
|
});
|
|
277
256
|
}
|
|
278
257
|
function createRemoteComponent(info) {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
return (
|
|
282
|
-
|
|
283
|
-
/* @__PURE__ */ React__default.createElement(ErrorBoundary, { FallbackComponent: info.fallback }, /* @__PURE__ */ React__default.createElement(React__default.Suspense, { fallback: info.loading }, /* @__PURE__ */ React__default.createElement(LazyComponent, { ...props, ref })))
|
|
284
|
-
);
|
|
285
|
-
});
|
|
258
|
+
const LazyComponent = createLazyRemoteComponent(info);
|
|
259
|
+
return (props) => {
|
|
260
|
+
return /* @__PURE__ */ React__default.createElement(ErrorBoundary, { FallbackComponent: info.fallback }, /* @__PURE__ */ React__default.createElement(React__default.Suspense, { fallback: info.loading }, /* @__PURE__ */ React__default.createElement(LazyComponent, { ...props })));
|
|
261
|
+
};
|
|
286
262
|
}
|
|
287
263
|
var client = {};
|
|
288
264
|
var m = ReactDOM;
|
|
@@ -312,65 +288,54 @@ function createBridgeComponent(bridgeInfo) {
|
|
|
312
288
|
return () => {
|
|
313
289
|
const rootMap = /* @__PURE__ */ new Map();
|
|
314
290
|
const RawComponent = (info) => {
|
|
315
|
-
const { appInfo, propsInfo
|
|
316
|
-
const {
|
|
317
|
-
return /* @__PURE__ */ React.createElement(RouterContext.Provider, { value: {
|
|
318
|
-
bridgeInfo.rootComponent,
|
|
319
|
-
{
|
|
320
|
-
...propsInfo,
|
|
321
|
-
basename,
|
|
322
|
-
...restProps
|
|
323
|
-
}
|
|
324
|
-
));
|
|
291
|
+
const { appInfo, propsInfo } = info;
|
|
292
|
+
const { name, memoryRoute, basename = "/" } = appInfo;
|
|
293
|
+
return /* @__PURE__ */ React.createElement(RouterContext.Provider, { value: { name, basename, memoryRoute } }, /* @__PURE__ */ React.createElement(bridgeInfo.rootComponent, { ...propsInfo, basename }));
|
|
325
294
|
};
|
|
326
295
|
return {
|
|
327
|
-
|
|
296
|
+
render(info) {
|
|
328
297
|
LoggerInstance.log(`createBridgeComponent render Info`, info);
|
|
329
|
-
const {
|
|
330
|
-
moduleName,
|
|
331
|
-
dom,
|
|
332
|
-
basename,
|
|
333
|
-
memoryRoute,
|
|
334
|
-
fallback,
|
|
335
|
-
...propsInfo
|
|
336
|
-
} = info;
|
|
337
|
-
const rootComponentWithErrorBoundary = (
|
|
338
|
-
// set ErrorBoundary for RawComponent rendering error, usually caused by user app rendering error
|
|
339
|
-
/* @__PURE__ */ React.createElement(ErrorBoundary, { FallbackComponent: fallback }, /* @__PURE__ */ React.createElement(
|
|
340
|
-
RawComponent,
|
|
341
|
-
{
|
|
342
|
-
appInfo: {
|
|
343
|
-
moduleName,
|
|
344
|
-
basename,
|
|
345
|
-
memoryRoute
|
|
346
|
-
},
|
|
347
|
-
propsInfo
|
|
348
|
-
}
|
|
349
|
-
))
|
|
350
|
-
);
|
|
298
|
+
const { name, basename, memoryRoute, ...propsInfo } = info;
|
|
351
299
|
if (atLeastReact18(React)) {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
300
|
+
const root = client.createRoot(info.dom);
|
|
301
|
+
rootMap.set(info.dom, root);
|
|
302
|
+
root.render(
|
|
303
|
+
/* @__PURE__ */ React.createElement(
|
|
304
|
+
RawComponent,
|
|
305
|
+
{
|
|
306
|
+
propsInfo,
|
|
307
|
+
appInfo: {
|
|
308
|
+
name,
|
|
309
|
+
basename,
|
|
310
|
+
memoryRoute
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
)
|
|
314
|
+
);
|
|
361
315
|
} else {
|
|
362
|
-
|
|
363
|
-
|
|
316
|
+
ReactDOM.render(
|
|
317
|
+
/* @__PURE__ */ React.createElement(
|
|
318
|
+
RawComponent,
|
|
319
|
+
{
|
|
320
|
+
propsInfo,
|
|
321
|
+
appInfo: {
|
|
322
|
+
name,
|
|
323
|
+
basename,
|
|
324
|
+
memoryRoute
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
),
|
|
328
|
+
info.dom
|
|
329
|
+
);
|
|
364
330
|
}
|
|
365
331
|
},
|
|
366
|
-
|
|
332
|
+
destroy(info) {
|
|
367
333
|
LoggerInstance.log(`createBridgeComponent destroy Info`, {
|
|
368
334
|
dom: info.dom
|
|
369
335
|
});
|
|
370
336
|
if (atLeastReact18(React)) {
|
|
371
337
|
const root = rootMap.get(info.dom);
|
|
372
338
|
root == null ? void 0 : root.unmount();
|
|
373
|
-
rootMap.delete(info.dom);
|
|
374
339
|
} else {
|
|
375
340
|
ReactDOM.unmountComponentAtNode(info.dom);
|
|
376
341
|
}
|
package/dist/router.cjs.js
CHANGED
|
@@ -20,14 +20,16 @@ function _interopNamespaceDefault(e) {
|
|
|
20
20
|
return Object.freeze(n);
|
|
21
21
|
}
|
|
22
22
|
const ReactRouterDom__namespace = /* @__PURE__ */ _interopNamespaceDefault(ReactRouterDom);
|
|
23
|
-
function
|
|
23
|
+
function WraperRouter(props) {
|
|
24
24
|
const { basename, ...propsRes } = props;
|
|
25
25
|
const routerContextProps = React.useContext(context.RouterContext) || {};
|
|
26
|
-
context.LoggerInstance.log(`
|
|
26
|
+
context.LoggerInstance.log(`WraperRouter info >>>`, {
|
|
27
27
|
...routerContextProps,
|
|
28
28
|
routerContextProps,
|
|
29
|
-
|
|
29
|
+
WraperRouterProps: props
|
|
30
30
|
});
|
|
31
|
+
if (!routerContextProps)
|
|
32
|
+
return /* @__PURE__ */ React.createElement(ReactRouterDom__namespace.BrowserRouter, { ...props });
|
|
31
33
|
if (routerContextProps == null ? void 0 : routerContextProps.memoryRoute) {
|
|
32
34
|
return /* @__PURE__ */ React.createElement(
|
|
33
35
|
ReactRouterDom__namespace.MemoryRouter,
|
|
@@ -45,19 +47,21 @@ function WrapperRouter(props) {
|
|
|
45
47
|
}
|
|
46
48
|
);
|
|
47
49
|
}
|
|
48
|
-
function
|
|
50
|
+
function WraperRouterProvider(props) {
|
|
49
51
|
const { router, ...propsRes } = props;
|
|
50
52
|
const routerContextProps = React.useContext(context.RouterContext) || {};
|
|
51
53
|
const routers = router.routes;
|
|
52
|
-
context.LoggerInstance.log(`
|
|
54
|
+
context.LoggerInstance.log(`WraperRouterProvider info >>>`, {
|
|
53
55
|
...routerContextProps,
|
|
54
56
|
routerContextProps,
|
|
55
|
-
|
|
57
|
+
WraperRouterProviderProps: props,
|
|
56
58
|
router
|
|
57
59
|
});
|
|
58
60
|
const RouterProvider = ReactRouterDom__namespace["RouterProvider"];
|
|
59
61
|
const createMemoryRouter = ReactRouterDom__namespace["createMemoryRouter"];
|
|
60
62
|
const createBrowserRouter = ReactRouterDom__namespace["createBrowserRouter"];
|
|
63
|
+
if (!routerContextProps)
|
|
64
|
+
return /* @__PURE__ */ React.createElement(RouterProvider, { ...props });
|
|
61
65
|
if (routerContextProps.memoryRoute) {
|
|
62
66
|
const MemeoryRouterInstance = createMemoryRouter(routers, {
|
|
63
67
|
initialEntries: [routerContextProps == null ? void 0 : routerContextProps.memoryRoute.entryPath]
|
|
@@ -72,8 +76,8 @@ function WrapperRouterProvider(props) {
|
|
|
72
76
|
return /* @__PURE__ */ React.createElement(RouterProvider, { ...propsRes, router: BrowserRouterInstance });
|
|
73
77
|
}
|
|
74
78
|
}
|
|
75
|
-
exports.BrowserRouter =
|
|
76
|
-
exports.RouterProvider =
|
|
79
|
+
exports.BrowserRouter = WraperRouter;
|
|
80
|
+
exports.RouterProvider = WraperRouterProvider;
|
|
77
81
|
Object.keys(ReactRouterDom).forEach((k) => {
|
|
78
82
|
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k))
|
|
79
83
|
Object.defineProperty(exports, k, {
|
package/dist/router.es.js
CHANGED
|
@@ -2,14 +2,16 @@ import React__default, { useContext } from "react";
|
|
|
2
2
|
import * as ReactRouterDom from "react-router-dom/";
|
|
3
3
|
export * from "react-router-dom/";
|
|
4
4
|
import { R as RouterContext, L as LoggerInstance } from "./context-Bw2PEwa6.js";
|
|
5
|
-
function
|
|
5
|
+
function WraperRouter(props) {
|
|
6
6
|
const { basename, ...propsRes } = props;
|
|
7
7
|
const routerContextProps = useContext(RouterContext) || {};
|
|
8
|
-
LoggerInstance.log(`
|
|
8
|
+
LoggerInstance.log(`WraperRouter info >>>`, {
|
|
9
9
|
...routerContextProps,
|
|
10
10
|
routerContextProps,
|
|
11
|
-
|
|
11
|
+
WraperRouterProps: props
|
|
12
12
|
});
|
|
13
|
+
if (!routerContextProps)
|
|
14
|
+
return /* @__PURE__ */ React__default.createElement(ReactRouterDom.BrowserRouter, { ...props });
|
|
13
15
|
if (routerContextProps == null ? void 0 : routerContextProps.memoryRoute) {
|
|
14
16
|
return /* @__PURE__ */ React__default.createElement(
|
|
15
17
|
ReactRouterDom.MemoryRouter,
|
|
@@ -27,19 +29,21 @@ function WrapperRouter(props) {
|
|
|
27
29
|
}
|
|
28
30
|
);
|
|
29
31
|
}
|
|
30
|
-
function
|
|
32
|
+
function WraperRouterProvider(props) {
|
|
31
33
|
const { router, ...propsRes } = props;
|
|
32
34
|
const routerContextProps = useContext(RouterContext) || {};
|
|
33
35
|
const routers = router.routes;
|
|
34
|
-
LoggerInstance.log(`
|
|
36
|
+
LoggerInstance.log(`WraperRouterProvider info >>>`, {
|
|
35
37
|
...routerContextProps,
|
|
36
38
|
routerContextProps,
|
|
37
|
-
|
|
39
|
+
WraperRouterProviderProps: props,
|
|
38
40
|
router
|
|
39
41
|
});
|
|
40
42
|
const RouterProvider = ReactRouterDom["RouterProvider"];
|
|
41
43
|
const createMemoryRouter = ReactRouterDom["createMemoryRouter"];
|
|
42
44
|
const createBrowserRouter = ReactRouterDom["createBrowserRouter"];
|
|
45
|
+
if (!routerContextProps)
|
|
46
|
+
return /* @__PURE__ */ React__default.createElement(RouterProvider, { ...props });
|
|
43
47
|
if (routerContextProps.memoryRoute) {
|
|
44
48
|
const MemeoryRouterInstance = createMemoryRouter(routers, {
|
|
45
49
|
initialEntries: [routerContextProps == null ? void 0 : routerContextProps.memoryRoute.entryPath]
|
|
@@ -55,6 +59,6 @@ function WrapperRouterProvider(props) {
|
|
|
55
59
|
}
|
|
56
60
|
}
|
|
57
61
|
export {
|
|
58
|
-
|
|
59
|
-
|
|
62
|
+
WraperRouter as BrowserRouter,
|
|
63
|
+
WraperRouterProvider as RouterProvider
|
|
60
64
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/bridge-react",
|
|
3
|
-
"version": "0.0.0-next-
|
|
3
|
+
"version": "0.0.0-next-20240819112150",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -20,22 +20,12 @@
|
|
|
20
20
|
"import": "./dist/router.es.js",
|
|
21
21
|
"require": "./dist/router.cjs.js"
|
|
22
22
|
},
|
|
23
|
-
"./router-v5": {
|
|
24
|
-
"types": "./dist/router.d.ts",
|
|
25
|
-
"import": "./dist/router.es.js",
|
|
26
|
-
"require": "./dist/router.cjs.js"
|
|
27
|
-
},
|
|
28
|
-
"./router-v6": {
|
|
29
|
-
"types": "./dist/router.d.ts",
|
|
30
|
-
"import": "./dist/router.es.js",
|
|
31
|
-
"require": "./dist/router.cjs.js"
|
|
32
|
-
},
|
|
33
23
|
"./*": "./*"
|
|
34
24
|
},
|
|
35
25
|
"dependencies": {
|
|
36
26
|
"@loadable/component": "^5.16.4",
|
|
37
27
|
"react-error-boundary": "^4.0.13",
|
|
38
|
-
"@module-federation/bridge-shared": "0.0.0-next-
|
|
28
|
+
"@module-federation/bridge-shared": "0.0.0-next-20240819112150"
|
|
39
29
|
},
|
|
40
30
|
"peerDependencies": {
|
|
41
31
|
"react": ">=16.9.0",
|
package/src/create.tsx
CHANGED
|
@@ -13,7 +13,11 @@ export interface RenderFnParams extends ProviderParams {
|
|
|
13
13
|
|
|
14
14
|
interface RemoteModule {
|
|
15
15
|
provider: () => {
|
|
16
|
-
render: (
|
|
16
|
+
render: (
|
|
17
|
+
info: ProviderParams & {
|
|
18
|
+
dom: any;
|
|
19
|
+
},
|
|
20
|
+
) => void;
|
|
17
21
|
destroy: (info: { dom: any }) => void;
|
|
18
22
|
};
|
|
19
23
|
}
|
|
@@ -30,7 +34,6 @@ function createLazyRemoteComponent<T, E extends keyof T>(info: {
|
|
|
30
34
|
lazyComponent: info.loader,
|
|
31
35
|
exportName,
|
|
32
36
|
});
|
|
33
|
-
|
|
34
37
|
try {
|
|
35
38
|
const m = (await info.loader()) as RemoteModule;
|
|
36
39
|
// @ts-ignore
|
|
@@ -41,7 +44,8 @@ function createLazyRemoteComponent<T, E extends keyof T>(info: {
|
|
|
41
44
|
);
|
|
42
45
|
|
|
43
46
|
// @ts-ignore
|
|
44
|
-
const exportFn = m[exportName];
|
|
47
|
+
const exportFn = m[exportName] as any;
|
|
48
|
+
|
|
45
49
|
if (exportName in m && typeof exportFn === 'function') {
|
|
46
50
|
const RemoteAppComponent = forwardRef<
|
|
47
51
|
HTMLDivElement,
|
|
@@ -49,15 +53,12 @@ function createLazyRemoteComponent<T, E extends keyof T>(info: {
|
|
|
49
53
|
basename?: ProviderParams['basename'];
|
|
50
54
|
memoryRoute?: ProviderParams['memoryRoute'];
|
|
51
55
|
}
|
|
52
|
-
>((props,
|
|
56
|
+
>((props, _ref) => {
|
|
53
57
|
return (
|
|
54
58
|
<RemoteApp
|
|
55
|
-
|
|
56
|
-
moduleName={moduleName}
|
|
59
|
+
name={moduleName}
|
|
57
60
|
providerInfo={exportFn}
|
|
58
61
|
exportName={info.export || 'default'}
|
|
59
|
-
fallback={info.fallback}
|
|
60
|
-
ref={ref}
|
|
61
62
|
{...props}
|
|
62
63
|
/>
|
|
63
64
|
);
|
|
@@ -92,22 +93,26 @@ export function createRemoteComponent<T, E extends keyof T>(info: {
|
|
|
92
93
|
type ExportType = T[E] extends (...args: any) => any
|
|
93
94
|
? ReturnType<T[E]>
|
|
94
95
|
: never;
|
|
95
|
-
|
|
96
96
|
type RawComponentType = '__BRIDGE_FN__' extends keyof ExportType
|
|
97
97
|
? ExportType['__BRIDGE_FN__'] extends (...args: any) => any
|
|
98
98
|
? Parameters<ExportType['__BRIDGE_FN__']>[0]
|
|
99
99
|
: {}
|
|
100
100
|
: {};
|
|
101
101
|
|
|
102
|
-
|
|
103
|
-
|
|
102
|
+
const LazyComponent = createLazyRemoteComponent(info);
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
props: {
|
|
106
|
+
basename?: ProviderParams['basename'];
|
|
107
|
+
memoryRoute?: ProviderParams['memoryRoute'];
|
|
108
|
+
} & RawComponentType,
|
|
109
|
+
) => {
|
|
104
110
|
return (
|
|
105
|
-
// set ErrorBoundary for LazyComponent rendering error, usually caused by inner bridge logic render process
|
|
106
111
|
<ErrorBoundary FallbackComponent={info.fallback}>
|
|
107
112
|
<React.Suspense fallback={info.loading}>
|
|
108
|
-
<LazyComponent {...props}
|
|
113
|
+
<LazyComponent {...props} />
|
|
109
114
|
</React.Suspense>
|
|
110
115
|
</ErrorBoundary>
|
|
111
116
|
);
|
|
112
|
-
}
|
|
117
|
+
};
|
|
113
118
|
}
|
package/src/provider.tsx
CHANGED
|
@@ -8,84 +8,65 @@ import type {
|
|
|
8
8
|
RenderFnParams,
|
|
9
9
|
} from '@module-federation/bridge-shared';
|
|
10
10
|
import { LoggerInstance, atLeastReact18 } from './utils';
|
|
11
|
-
import { ErrorBoundary } from 'react-error-boundary';
|
|
12
11
|
|
|
13
|
-
type RootType = HTMLElement | ReactDOMClient.Root;
|
|
14
12
|
type ProviderFnParams<T> = {
|
|
15
13
|
rootComponent: React.ComponentType<T>;
|
|
16
|
-
render?: (
|
|
17
|
-
App: React.ReactElement,
|
|
18
|
-
id?: HTMLElement | string,
|
|
19
|
-
) => RootType | Promise<RootType>;
|
|
20
14
|
};
|
|
21
15
|
|
|
22
16
|
export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
|
|
23
17
|
return () => {
|
|
24
|
-
const rootMap = new Map<any,
|
|
18
|
+
const rootMap = new Map<any, ReactDOMClient.Root>();
|
|
19
|
+
|
|
25
20
|
const RawComponent = (info: { propsInfo: T; appInfo: ProviderParams }) => {
|
|
26
|
-
const { appInfo, propsInfo
|
|
27
|
-
const {
|
|
21
|
+
const { appInfo, propsInfo } = info;
|
|
22
|
+
const { name, memoryRoute, basename = '/' } = appInfo;
|
|
23
|
+
|
|
28
24
|
return (
|
|
29
|
-
<RouterContext.Provider value={{
|
|
30
|
-
<bridgeInfo.rootComponent
|
|
31
|
-
{...propsInfo}
|
|
32
|
-
basename={basename}
|
|
33
|
-
{...restProps}
|
|
34
|
-
/>
|
|
25
|
+
<RouterContext.Provider value={{ name, basename, memoryRoute }}>
|
|
26
|
+
<bridgeInfo.rootComponent {...propsInfo} basename={basename} />
|
|
35
27
|
</RouterContext.Provider>
|
|
36
28
|
);
|
|
37
29
|
};
|
|
38
30
|
|
|
39
31
|
return {
|
|
40
|
-
|
|
32
|
+
render(info: RenderFnParams & any) {
|
|
41
33
|
LoggerInstance.log(`createBridgeComponent render Info`, info);
|
|
42
|
-
const {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
...propsInfo
|
|
49
|
-
} = info;
|
|
50
|
-
const rootComponentWithErrorBoundary = (
|
|
51
|
-
// set ErrorBoundary for RawComponent rendering error, usually caused by user app rendering error
|
|
52
|
-
<ErrorBoundary FallbackComponent={fallback}>
|
|
34
|
+
const { name, basename, memoryRoute, ...propsInfo } = info;
|
|
35
|
+
|
|
36
|
+
if (atLeastReact18(React)) {
|
|
37
|
+
const root = ReactDOMClient.createRoot(info.dom);
|
|
38
|
+
rootMap.set(info.dom, root);
|
|
39
|
+
root.render(
|
|
53
40
|
<RawComponent
|
|
41
|
+
propsInfo={propsInfo}
|
|
54
42
|
appInfo={{
|
|
55
|
-
|
|
43
|
+
name,
|
|
56
44
|
basename,
|
|
57
45
|
memoryRoute,
|
|
58
46
|
}}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
</ErrorBoundary>
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
if (atLeastReact18(React)) {
|
|
65
|
-
if (bridgeInfo?.render) {
|
|
66
|
-
// in case bridgeInfo?.render is an async function, resolve this to promise
|
|
67
|
-
Promise.resolve(
|
|
68
|
-
bridgeInfo?.render(rootComponentWithErrorBoundary, dom),
|
|
69
|
-
).then((root: RootType) => rootMap.set(info.dom, root));
|
|
70
|
-
} else {
|
|
71
|
-
const root: RootType = ReactDOMClient.createRoot(info.dom);
|
|
72
|
-
root.render(rootComponentWithErrorBoundary);
|
|
73
|
-
rootMap.set(info.dom, root);
|
|
74
|
-
}
|
|
47
|
+
/>,
|
|
48
|
+
);
|
|
75
49
|
} else {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
50
|
+
ReactDOM.render(
|
|
51
|
+
<RawComponent
|
|
52
|
+
propsInfo={propsInfo}
|
|
53
|
+
appInfo={{
|
|
54
|
+
name,
|
|
55
|
+
basename,
|
|
56
|
+
memoryRoute,
|
|
57
|
+
}}
|
|
58
|
+
/>,
|
|
59
|
+
info.dom,
|
|
60
|
+
);
|
|
79
61
|
}
|
|
80
62
|
},
|
|
81
|
-
|
|
63
|
+
destroy(info: { dom: HTMLElement }) {
|
|
82
64
|
LoggerInstance.log(`createBridgeComponent destroy Info`, {
|
|
83
65
|
dom: info.dom,
|
|
84
66
|
});
|
|
85
67
|
if (atLeastReact18(React)) {
|
|
86
68
|
const root = rootMap.get(info.dom);
|
|
87
|
-
|
|
88
|
-
rootMap.delete(info.dom);
|
|
69
|
+
root?.unmount();
|
|
89
70
|
} else {
|
|
90
71
|
ReactDOM.unmountComponentAtNode(info.dom);
|
|
91
72
|
}
|
|
@@ -103,3 +84,5 @@ export function ShadowRoot(info: { children: () => JSX.Element }) {
|
|
|
103
84
|
|
|
104
85
|
return <div ref={domRef}>{root && <info.children />}</div>;
|
|
105
86
|
}
|
|
87
|
+
|
|
88
|
+
// function ShadowContent() {}
|