@module-federation/bridge-react 0.0.0-next-20240722034500 → 0.0.0-next-20240723065243
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 -2
- package/dist/index.cjs.js +100 -74
- package/dist/index.d.ts +10 -6
- package/dist/index.es.js +102 -76
- package/dist/router-v5.cjs.js +80 -0
- package/dist/router-v5.d.ts +8 -0
- package/dist/router-v5.es.js +63 -0
- package/dist/router-v6.cjs.js +87 -0
- package/dist/router-v6.d.ts +11 -0
- package/dist/router-v6.es.js +64 -0
- package/package.json +12 -2
- package/src/create.tsx +9 -12
- package/src/provider.tsx +37 -23
- package/src/remote/index.tsx +67 -56
- package/src/router-v5.tsx +78 -0
- package/src/router-v6.tsx +76 -0
- package/src/router.tsx +2 -0
- package/vite.config.ts +4 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import React__default, { useContext } from "react";
|
|
2
|
+
import * as ReactRouterDom from "react-router-dom/index.js";
|
|
3
|
+
import { R as RouterContext, L as LoggerInstance } from "./context-Bw2PEwa6.js";
|
|
4
|
+
function WraperRouter(props) {
|
|
5
|
+
const { basename, ...propsRes } = props;
|
|
6
|
+
const routerContextProps = useContext(RouterContext) || {};
|
|
7
|
+
LoggerInstance.log(`WraperRouter info >>>`, {
|
|
8
|
+
...routerContextProps,
|
|
9
|
+
routerContextProps,
|
|
10
|
+
WraperRouterProps: props
|
|
11
|
+
});
|
|
12
|
+
if (!routerContextProps)
|
|
13
|
+
return /* @__PURE__ */ React__default.createElement(ReactRouterDom.BrowserRouter, { ...props });
|
|
14
|
+
if (routerContextProps == null ? void 0 : routerContextProps.memoryRoute) {
|
|
15
|
+
return /* @__PURE__ */ React__default.createElement(
|
|
16
|
+
ReactRouterDom.MemoryRouter,
|
|
17
|
+
{
|
|
18
|
+
...props,
|
|
19
|
+
initialEntries: [routerContextProps == null ? void 0 : routerContextProps.memoryRoute.entryPath]
|
|
20
|
+
}
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
return /* @__PURE__ */ React__default.createElement(
|
|
24
|
+
ReactRouterDom.BrowserRouter,
|
|
25
|
+
{
|
|
26
|
+
...propsRes,
|
|
27
|
+
basename: (routerContextProps == null ? void 0 : routerContextProps.basename) || basename
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
function WraperRouterProvider(props) {
|
|
32
|
+
const { router, ...propsRes } = props;
|
|
33
|
+
const routerContextProps = useContext(RouterContext) || {};
|
|
34
|
+
const routers = router.routes;
|
|
35
|
+
LoggerInstance.log(`WraperRouterProvider info >>>`, {
|
|
36
|
+
...routerContextProps,
|
|
37
|
+
routerContextProps,
|
|
38
|
+
WraperRouterProviderProps: props,
|
|
39
|
+
router
|
|
40
|
+
});
|
|
41
|
+
const RouterProvider = ReactRouterDom["RouterProvider"];
|
|
42
|
+
const createMemoryRouter = ReactRouterDom["createMemoryRouter"];
|
|
43
|
+
const createBrowserRouter = ReactRouterDom["createBrowserRouter"];
|
|
44
|
+
if (!routerContextProps)
|
|
45
|
+
return /* @__PURE__ */ React__default.createElement(RouterProvider, { ...props });
|
|
46
|
+
if (routerContextProps.memoryRoute) {
|
|
47
|
+
const MemeoryRouterInstance = createMemoryRouter(routers, {
|
|
48
|
+
initialEntries: [routerContextProps == null ? void 0 : routerContextProps.memoryRoute.entryPath]
|
|
49
|
+
});
|
|
50
|
+
return /* @__PURE__ */ React__default.createElement(RouterProvider, { router: MemeoryRouterInstance });
|
|
51
|
+
} else {
|
|
52
|
+
const BrowserRouterInstance = createBrowserRouter(routers, {
|
|
53
|
+
basename: routerContextProps.basename,
|
|
54
|
+
future: router.future,
|
|
55
|
+
window: router.window
|
|
56
|
+
});
|
|
57
|
+
return /* @__PURE__ */ React__default.createElement(RouterProvider, { ...propsRes, router: BrowserRouterInstance });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export {
|
|
61
|
+
WraperRouter as BrowserRouter,
|
|
62
|
+
WraperRouterProvider as RouterProvider
|
|
63
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const React = require("react");
|
|
4
|
+
const ReactRouterDom = require("react-router-dom/dist/index.js");
|
|
5
|
+
const context = require("./context--mtFt3tp.cjs");
|
|
6
|
+
function _interopNamespaceDefault(e) {
|
|
7
|
+
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
8
|
+
if (e) {
|
|
9
|
+
for (const k in e) {
|
|
10
|
+
if (k !== "default") {
|
|
11
|
+
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
12
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: () => e[k]
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
n.default = e;
|
|
20
|
+
return Object.freeze(n);
|
|
21
|
+
}
|
|
22
|
+
const ReactRouterDom__namespace = /* @__PURE__ */ _interopNamespaceDefault(ReactRouterDom);
|
|
23
|
+
function WraperRouter(props) {
|
|
24
|
+
const { basename, ...propsRes } = props;
|
|
25
|
+
const routerContextProps = React.useContext(context.RouterContext) || {};
|
|
26
|
+
context.LoggerInstance.log(`WraperRouter info >>>`, {
|
|
27
|
+
...routerContextProps,
|
|
28
|
+
routerContextProps,
|
|
29
|
+
WraperRouterProps: props
|
|
30
|
+
});
|
|
31
|
+
if (!routerContextProps)
|
|
32
|
+
return /* @__PURE__ */ React.createElement(ReactRouterDom__namespace.BrowserRouter, { ...props });
|
|
33
|
+
if (routerContextProps == null ? void 0 : routerContextProps.memoryRoute) {
|
|
34
|
+
return /* @__PURE__ */ React.createElement(
|
|
35
|
+
ReactRouterDom__namespace.MemoryRouter,
|
|
36
|
+
{
|
|
37
|
+
...props,
|
|
38
|
+
initialEntries: [routerContextProps == null ? void 0 : routerContextProps.memoryRoute.entryPath]
|
|
39
|
+
}
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return /* @__PURE__ */ React.createElement(
|
|
43
|
+
ReactRouterDom__namespace.BrowserRouter,
|
|
44
|
+
{
|
|
45
|
+
...propsRes,
|
|
46
|
+
basename: (routerContextProps == null ? void 0 : routerContextProps.basename) || basename
|
|
47
|
+
}
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
function WraperRouterProvider(props) {
|
|
51
|
+
const { router, ...propsRes } = props;
|
|
52
|
+
const routerContextProps = React.useContext(context.RouterContext) || {};
|
|
53
|
+
const routers = router.routes;
|
|
54
|
+
context.LoggerInstance.log(`WraperRouterProvider info >>>`, {
|
|
55
|
+
...routerContextProps,
|
|
56
|
+
routerContextProps,
|
|
57
|
+
WraperRouterProviderProps: props,
|
|
58
|
+
router
|
|
59
|
+
});
|
|
60
|
+
const RouterProvider = ReactRouterDom__namespace["RouterProvider"];
|
|
61
|
+
const createMemoryRouter = ReactRouterDom__namespace["createMemoryRouter"];
|
|
62
|
+
const createBrowserRouter = ReactRouterDom__namespace["createBrowserRouter"];
|
|
63
|
+
if (!routerContextProps)
|
|
64
|
+
return /* @__PURE__ */ React.createElement(RouterProvider, { ...props });
|
|
65
|
+
if (routerContextProps.memoryRoute) {
|
|
66
|
+
const MemeoryRouterInstance = createMemoryRouter(routers, {
|
|
67
|
+
initialEntries: [routerContextProps == null ? void 0 : routerContextProps.memoryRoute.entryPath]
|
|
68
|
+
});
|
|
69
|
+
return /* @__PURE__ */ React.createElement(RouterProvider, { router: MemeoryRouterInstance });
|
|
70
|
+
} else {
|
|
71
|
+
const BrowserRouterInstance = createBrowserRouter(routers, {
|
|
72
|
+
basename: routerContextProps.basename,
|
|
73
|
+
future: router.future,
|
|
74
|
+
window: router.window
|
|
75
|
+
});
|
|
76
|
+
return /* @__PURE__ */ React.createElement(RouterProvider, { ...propsRes, router: BrowserRouterInstance });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.BrowserRouter = WraperRouter;
|
|
80
|
+
exports.RouterProvider = WraperRouterProvider;
|
|
81
|
+
Object.keys(ReactRouterDom).forEach((k) => {
|
|
82
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k))
|
|
83
|
+
Object.defineProperty(exports, k, {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
get: () => ReactRouterDom[k]
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { default as default_2 } from 'react';
|
|
2
|
+
import * as ReactRouterDom from 'react-router-dom/dist/index.js';
|
|
3
|
+
|
|
4
|
+
export declare function BrowserRouter(props: Parameters<typeof ReactRouterDom.BrowserRouter>[0] | Parameters<typeof ReactRouterDom.MemoryRouter>[0]): default_2.JSX.Element;
|
|
5
|
+
|
|
6
|
+
export declare function RouterProvider(props: Parameters<typeof ReactRouterDom.RouterProvider>[0]): default_2.JSX.Element;
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
export * from "react-router-dom/dist/index.js";
|
|
10
|
+
|
|
11
|
+
export { }
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import React__default, { useContext } from "react";
|
|
2
|
+
import * as ReactRouterDom from "react-router-dom/dist/index.js";
|
|
3
|
+
export * from "react-router-dom/dist/index.js";
|
|
4
|
+
import { R as RouterContext, L as LoggerInstance } from "./context-Bw2PEwa6.js";
|
|
5
|
+
function WraperRouter(props) {
|
|
6
|
+
const { basename, ...propsRes } = props;
|
|
7
|
+
const routerContextProps = useContext(RouterContext) || {};
|
|
8
|
+
LoggerInstance.log(`WraperRouter info >>>`, {
|
|
9
|
+
...routerContextProps,
|
|
10
|
+
routerContextProps,
|
|
11
|
+
WraperRouterProps: props
|
|
12
|
+
});
|
|
13
|
+
if (!routerContextProps)
|
|
14
|
+
return /* @__PURE__ */ React__default.createElement(ReactRouterDom.BrowserRouter, { ...props });
|
|
15
|
+
if (routerContextProps == null ? void 0 : routerContextProps.memoryRoute) {
|
|
16
|
+
return /* @__PURE__ */ React__default.createElement(
|
|
17
|
+
ReactRouterDom.MemoryRouter,
|
|
18
|
+
{
|
|
19
|
+
...props,
|
|
20
|
+
initialEntries: [routerContextProps == null ? void 0 : routerContextProps.memoryRoute.entryPath]
|
|
21
|
+
}
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
return /* @__PURE__ */ React__default.createElement(
|
|
25
|
+
ReactRouterDom.BrowserRouter,
|
|
26
|
+
{
|
|
27
|
+
...propsRes,
|
|
28
|
+
basename: (routerContextProps == null ? void 0 : routerContextProps.basename) || basename
|
|
29
|
+
}
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
function WraperRouterProvider(props) {
|
|
33
|
+
const { router, ...propsRes } = props;
|
|
34
|
+
const routerContextProps = useContext(RouterContext) || {};
|
|
35
|
+
const routers = router.routes;
|
|
36
|
+
LoggerInstance.log(`WraperRouterProvider info >>>`, {
|
|
37
|
+
...routerContextProps,
|
|
38
|
+
routerContextProps,
|
|
39
|
+
WraperRouterProviderProps: props,
|
|
40
|
+
router
|
|
41
|
+
});
|
|
42
|
+
const RouterProvider = ReactRouterDom["RouterProvider"];
|
|
43
|
+
const createMemoryRouter = ReactRouterDom["createMemoryRouter"];
|
|
44
|
+
const createBrowserRouter = ReactRouterDom["createBrowserRouter"];
|
|
45
|
+
if (!routerContextProps)
|
|
46
|
+
return /* @__PURE__ */ React__default.createElement(RouterProvider, { ...props });
|
|
47
|
+
if (routerContextProps.memoryRoute) {
|
|
48
|
+
const MemeoryRouterInstance = createMemoryRouter(routers, {
|
|
49
|
+
initialEntries: [routerContextProps == null ? void 0 : routerContextProps.memoryRoute.entryPath]
|
|
50
|
+
});
|
|
51
|
+
return /* @__PURE__ */ React__default.createElement(RouterProvider, { router: MemeoryRouterInstance });
|
|
52
|
+
} else {
|
|
53
|
+
const BrowserRouterInstance = createBrowserRouter(routers, {
|
|
54
|
+
basename: routerContextProps.basename,
|
|
55
|
+
future: router.future,
|
|
56
|
+
window: router.window
|
|
57
|
+
});
|
|
58
|
+
return /* @__PURE__ */ React__default.createElement(RouterProvider, { ...propsRes, router: BrowserRouterInstance });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export {
|
|
62
|
+
WraperRouter as BrowserRouter,
|
|
63
|
+
WraperRouterProvider as RouterProvider
|
|
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-20240723065243",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -20,12 +20,22 @@
|
|
|
20
20
|
"import": "./dist/router.es.js",
|
|
21
21
|
"require": "./dist/router.cjs.js"
|
|
22
22
|
},
|
|
23
|
+
"./router-v5": {
|
|
24
|
+
"types": "./dist/router-v5.d.ts",
|
|
25
|
+
"import": "./dist/router-v5.es.js",
|
|
26
|
+
"require": "./dist/router-v5.cjs.js"
|
|
27
|
+
},
|
|
28
|
+
"./router-v6": {
|
|
29
|
+
"types": "./dist/router-v6.d.ts",
|
|
30
|
+
"import": "./dist/router-v6.es.js",
|
|
31
|
+
"require": "./dist/router-v6.cjs.js"
|
|
32
|
+
},
|
|
23
33
|
"./*": "./*"
|
|
24
34
|
},
|
|
25
35
|
"dependencies": {
|
|
26
36
|
"@loadable/component": "^5.16.4",
|
|
27
37
|
"react-error-boundary": "^4.0.13",
|
|
28
|
-
"@module-federation/bridge-shared": "0.0.0-next-
|
|
38
|
+
"@module-federation/bridge-shared": "0.0.0-next-20240723065243"
|
|
29
39
|
},
|
|
30
40
|
"peerDependencies": {
|
|
31
41
|
"react": ">=16.9.0",
|
package/src/create.tsx
CHANGED
|
@@ -6,11 +6,9 @@ import {
|
|
|
6
6
|
ErrorBoundaryPropsWithComponent,
|
|
7
7
|
} from 'react-error-boundary';
|
|
8
8
|
import RemoteApp from './remote';
|
|
9
|
-
|
|
10
9
|
export interface RenderFnParams extends ProviderParams {
|
|
11
10
|
dom?: any;
|
|
12
11
|
}
|
|
13
|
-
|
|
14
12
|
interface RemoteModule {
|
|
15
13
|
provider: () => {
|
|
16
14
|
render: (
|
|
@@ -53,12 +51,13 @@ function createLazyRemoteComponent<T, E extends keyof T>(info: {
|
|
|
53
51
|
basename?: ProviderParams['basename'];
|
|
54
52
|
memoryRoute?: ProviderParams['memoryRoute'];
|
|
55
53
|
}
|
|
56
|
-
>((props,
|
|
54
|
+
>((props, ref) => {
|
|
57
55
|
return (
|
|
58
56
|
<RemoteApp
|
|
59
57
|
name={moduleName}
|
|
60
58
|
providerInfo={exportFn}
|
|
61
59
|
exportName={info.export || 'default'}
|
|
60
|
+
ref={ref}
|
|
62
61
|
{...props}
|
|
63
62
|
/>
|
|
64
63
|
);
|
|
@@ -100,19 +99,17 @@ export function createRemoteComponent<T, E extends keyof T>(info: {
|
|
|
100
99
|
: {};
|
|
101
100
|
|
|
102
101
|
const LazyComponent = createLazyRemoteComponent(info);
|
|
103
|
-
|
|
104
|
-
return (
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
} & RawComponentType,
|
|
109
|
-
) => {
|
|
102
|
+
|
|
103
|
+
return forwardRef(function (props: {
|
|
104
|
+
basename?: ProviderParams['basename'];
|
|
105
|
+
memoryRoute?: ProviderParams['memoryRoute'];
|
|
106
|
+
} & RawComponentType, ref) {
|
|
110
107
|
return (
|
|
111
108
|
<ErrorBoundary FallbackComponent={info.fallback}>
|
|
112
109
|
<React.Suspense fallback={info.loading}>
|
|
113
|
-
<LazyComponent {...props} />
|
|
110
|
+
<LazyComponent {...props} ref={ref} />
|
|
114
111
|
</React.Suspense>
|
|
115
112
|
</ErrorBoundary>
|
|
116
113
|
);
|
|
117
|
-
};
|
|
114
|
+
});
|
|
118
115
|
}
|
package/src/provider.tsx
CHANGED
|
@@ -9,45 +9,61 @@ import type {
|
|
|
9
9
|
} from '@module-federation/bridge-shared';
|
|
10
10
|
import { LoggerInstance, atLeastReact18 } from './utils';
|
|
11
11
|
|
|
12
|
+
type RootType = HTMLElement | ReactDOMClient.Root;
|
|
12
13
|
type ProviderFnParams<T> = {
|
|
13
14
|
rootComponent: React.ComponentType<T>;
|
|
15
|
+
render?: (App: React.ReactElement, id?: HTMLElement | string) => RootType | Promise<RootType>;
|
|
14
16
|
};
|
|
15
17
|
|
|
16
18
|
export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
|
|
17
19
|
return () => {
|
|
18
|
-
const rootMap = new Map<any,
|
|
19
|
-
|
|
20
|
+
const rootMap = new Map<any, RootType>();
|
|
20
21
|
const RawComponent = (info: { propsInfo: T; appInfo: ProviderParams }) => {
|
|
21
|
-
const { appInfo, propsInfo } = info;
|
|
22
|
+
const { appInfo, propsInfo, ...restProps } = info;
|
|
22
23
|
const { name, memoryRoute, basename = '/' } = appInfo;
|
|
23
24
|
|
|
24
25
|
return (
|
|
25
26
|
<RouterContext.Provider value={{ name, basename, memoryRoute }}>
|
|
26
|
-
<bridgeInfo.rootComponent {...propsInfo} basename={basename} />
|
|
27
|
+
<bridgeInfo.rootComponent {...propsInfo} basename={basename} {...restProps} />
|
|
27
28
|
</RouterContext.Provider>
|
|
28
29
|
);
|
|
29
30
|
};
|
|
30
31
|
|
|
31
32
|
return {
|
|
32
|
-
render(info: RenderFnParams & any) {
|
|
33
|
+
async render(info: RenderFnParams & any) {
|
|
33
34
|
LoggerInstance.log(`createBridgeComponent render Info`, info);
|
|
34
35
|
const { name, basename, memoryRoute, ...propsInfo } = info;
|
|
35
|
-
|
|
36
36
|
if (atLeastReact18(React)) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
37
|
+
// render is provided by user
|
|
38
|
+
if (bridgeInfo?.render) {
|
|
39
|
+
Promise.resolve(bridgeInfo?.render(
|
|
40
|
+
<RawComponent
|
|
41
|
+
propsInfo={propsInfo}
|
|
42
|
+
appInfo={{
|
|
43
|
+
name,
|
|
44
|
+
basename,
|
|
45
|
+
memoryRoute,
|
|
46
|
+
}}
|
|
47
|
+
/>,
|
|
48
|
+
info.dom
|
|
49
|
+
)).then((root: RootType) => rootMap.set(info.dom, root));
|
|
50
|
+
} else {
|
|
51
|
+
const root: RootType = ReactDOMClient.createRoot(info.dom);
|
|
52
|
+
root.render(
|
|
53
|
+
<RawComponent
|
|
54
|
+
propsInfo={propsInfo}
|
|
55
|
+
appInfo={{
|
|
56
|
+
name,
|
|
57
|
+
basename,
|
|
58
|
+
memoryRoute,
|
|
59
|
+
}}
|
|
60
|
+
/>,
|
|
61
|
+
);
|
|
62
|
+
rootMap.set(info.dom, root);
|
|
63
|
+
}
|
|
49
64
|
} else {
|
|
50
|
-
ReactDOM.render
|
|
65
|
+
const renderFunc = bridgeInfo?.render || ReactDOM.render;
|
|
66
|
+
renderFunc(
|
|
51
67
|
<RawComponent
|
|
52
68
|
propsInfo={propsInfo}
|
|
53
69
|
appInfo={{
|
|
@@ -60,13 +76,13 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
|
|
|
60
76
|
);
|
|
61
77
|
}
|
|
62
78
|
},
|
|
63
|
-
destroy(info: { dom: HTMLElement }) {
|
|
79
|
+
async destroy(info: { dom: HTMLElement }) {
|
|
64
80
|
LoggerInstance.log(`createBridgeComponent destroy Info`, {
|
|
65
81
|
dom: info.dom,
|
|
66
82
|
});
|
|
67
83
|
if (atLeastReact18(React)) {
|
|
68
84
|
const root = rootMap.get(info.dom);
|
|
69
|
-
root?.unmount();
|
|
85
|
+
(root as ReactDOMClient.Root)?.unmount();
|
|
70
86
|
} else {
|
|
71
87
|
ReactDOM.unmountComponentAtNode(info.dom);
|
|
72
88
|
}
|
|
@@ -84,5 +100,3 @@ export function ShadowRoot(info: { children: () => JSX.Element }) {
|
|
|
84
100
|
|
|
85
101
|
return <div ref={domRef}>{root && <info.children />}</div>;
|
|
86
102
|
}
|
|
87
|
-
|
|
88
|
-
// function ShadowContent() {}
|
package/src/remote/index.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useContext, useEffect, useRef, useState } from 'react';
|
|
1
|
+
import React, { useContext, useEffect, useRef, useState, forwardRef } from 'react';
|
|
2
2
|
import * as ReactRouterDOM from 'react-router-dom';
|
|
3
3
|
import type { ProviderParams } from '@module-federation/bridge-shared';
|
|
4
4
|
import { LoggerInstance, pathJoin } from '../utils';
|
|
@@ -27,67 +27,74 @@ interface RemoteAppParams {
|
|
|
27
27
|
exportName: string | number | symbol;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
setTimeout(() => {
|
|
63
|
-
if (providerInfoRef.current?.destroy) {
|
|
64
|
-
LoggerInstance.log(
|
|
65
|
-
`createRemoteComponent LazyComponent destroy >>>`,
|
|
66
|
-
{ name, basename, dom: renderDom.current },
|
|
67
|
-
);
|
|
68
|
-
providerInfoRef.current?.destroy({
|
|
69
|
-
dom: renderDom.current,
|
|
70
|
-
});
|
|
71
|
-
}
|
|
30
|
+
const RemoteAppWrapper = forwardRef(function (props: RemoteAppParams & ProviderParams, ref) {
|
|
31
|
+
const RemoteApp = () => {
|
|
32
|
+
const {
|
|
33
|
+
name,
|
|
34
|
+
memoryRoute,
|
|
35
|
+
basename,
|
|
36
|
+
providerInfo,
|
|
37
|
+
...resProps
|
|
38
|
+
} = props;
|
|
39
|
+
|
|
40
|
+
const rootRef: React.MutableRefObject<HTMLElement | null> = ref && 'current' in ref ? ref as React.MutableRefObject<HTMLElement | null> : useRef(null);
|
|
41
|
+
const renderDom: React.MutableRefObject<HTMLElement | null> = useRef(null);
|
|
42
|
+
const providerInfoRef = useRef<any>(null);
|
|
43
|
+
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
const renderTimeout = setTimeout(() => {
|
|
46
|
+
const providerReturn = providerInfo();
|
|
47
|
+
providerInfoRef.current = providerReturn;
|
|
48
|
+
const renderProps = {
|
|
49
|
+
name,
|
|
50
|
+
dom: rootRef.current,
|
|
51
|
+
basename,
|
|
52
|
+
memoryRoute,
|
|
53
|
+
...resProps,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
renderDom.current = rootRef.current;
|
|
57
|
+
LoggerInstance.log(
|
|
58
|
+
`createRemoteComponent LazyComponent render >>>`,
|
|
59
|
+
renderProps,
|
|
60
|
+
);
|
|
61
|
+
providerReturn.render(renderProps);
|
|
72
62
|
});
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
63
|
+
|
|
64
|
+
return () => {
|
|
65
|
+
clearTimeout(renderTimeout);
|
|
66
|
+
setTimeout(() => {
|
|
67
|
+
if (providerInfoRef.current?.destroy) {
|
|
68
|
+
LoggerInstance.log(
|
|
69
|
+
`createRemoteComponent LazyComponent destroy >>>`,
|
|
70
|
+
{ name, basename, dom: renderDom.current },
|
|
71
|
+
);
|
|
72
|
+
providerInfoRef.current?.destroy({
|
|
73
|
+
dom: renderDom.current,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
}, []);
|
|
79
|
+
|
|
80
|
+
//@ts-ignore
|
|
81
|
+
return <div ref={rootRef}></div>;
|
|
82
|
+
}
|
|
79
83
|
|
|
80
|
-
(RemoteApp as any)['__APP_VERSION__'] = __APP_VERSION__;
|
|
84
|
+
(RemoteApp as any)['__APP_VERSION__'] = __APP_VERSION__;
|
|
85
|
+
return <RemoteApp />
|
|
86
|
+
});
|
|
81
87
|
|
|
82
88
|
interface ExtraDataProps {
|
|
83
89
|
basename?: string;
|
|
84
90
|
}
|
|
85
91
|
|
|
86
|
-
export function withRouterData<P extends Parameters<typeof
|
|
92
|
+
export function withRouterData<P extends Parameters<typeof RemoteAppWrapper>[0]>(
|
|
87
93
|
WrappedComponent: React.ComponentType<P & ExtraDataProps>,
|
|
88
94
|
): React.FC<Omit<P, keyof ExtraDataProps>> {
|
|
89
|
-
|
|
90
|
-
|
|
95
|
+
|
|
96
|
+
const Component = forwardRef(function (props: any, ref) {
|
|
97
|
+
let enableDispathPopstate = false
|
|
91
98
|
let routerContextVal: any;
|
|
92
99
|
try {
|
|
93
100
|
ReactRouterDOM.useLocation();
|
|
@@ -158,8 +165,12 @@ export function withRouterData<P extends Parameters<typeof RemoteApp>[0]>(
|
|
|
158
165
|
}, [location]);
|
|
159
166
|
}
|
|
160
167
|
|
|
161
|
-
return <WrappedComponent {...(props as P)} basename={basename} />;
|
|
162
|
-
};
|
|
168
|
+
return <WrappedComponent {...(props as P)} basename={basename} ref={ref} />;
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
return forwardRef(function (props, ref) {
|
|
172
|
+
return <Component {...props} ref={ref} />
|
|
173
|
+
}) as any;
|
|
163
174
|
}
|
|
164
175
|
|
|
165
|
-
export default withRouterData(
|
|
176
|
+
export default withRouterData(RemoteAppWrapper);
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import React, { useContext } from 'react';
|
|
2
|
+
// The upper alias react-router-dom$ into this file avoids the loop
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
import * as ReactRouterDom from 'react-router-dom/index.js';
|
|
5
|
+
|
|
6
|
+
import { RouterContext } from './context';
|
|
7
|
+
import { LoggerInstance } from './utils';
|
|
8
|
+
|
|
9
|
+
function WraperRouter(
|
|
10
|
+
props:
|
|
11
|
+
| Parameters<typeof ReactRouterDom.BrowserRouter>[0]
|
|
12
|
+
| Parameters<typeof ReactRouterDom.MemoryRouter>[0],
|
|
13
|
+
) {
|
|
14
|
+
const { basename, ...propsRes } = props;
|
|
15
|
+
const routerContextProps = useContext(RouterContext) || {};
|
|
16
|
+
|
|
17
|
+
LoggerInstance.log(`WraperRouter info >>>`, {
|
|
18
|
+
...routerContextProps,
|
|
19
|
+
routerContextProps,
|
|
20
|
+
WraperRouterProps: props,
|
|
21
|
+
});
|
|
22
|
+
if (!routerContextProps) return <ReactRouterDom.BrowserRouter {...props} />;
|
|
23
|
+
|
|
24
|
+
if (routerContextProps?.memoryRoute) {
|
|
25
|
+
return (
|
|
26
|
+
<ReactRouterDom.MemoryRouter
|
|
27
|
+
{...props}
|
|
28
|
+
initialEntries={[routerContextProps?.memoryRoute.entryPath]}
|
|
29
|
+
/>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
return (
|
|
33
|
+
<ReactRouterDom.BrowserRouter
|
|
34
|
+
{...propsRes}
|
|
35
|
+
basename={routerContextProps?.basename || basename}
|
|
36
|
+
/>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function WraperRouterProvider(
|
|
41
|
+
props: Parameters<typeof ReactRouterDom.RouterProvider>[0],
|
|
42
|
+
) {
|
|
43
|
+
const { router, ...propsRes } = props;
|
|
44
|
+
const routerContextProps = useContext(RouterContext) || {};
|
|
45
|
+
const routers = router.routes;
|
|
46
|
+
LoggerInstance.log(`WraperRouterProvider info >>>`, {
|
|
47
|
+
...routerContextProps,
|
|
48
|
+
routerContextProps,
|
|
49
|
+
WraperRouterProviderProps: props,
|
|
50
|
+
router,
|
|
51
|
+
});
|
|
52
|
+
const RouterProvider = (ReactRouterDom as any)['Router' + 'Provider'];
|
|
53
|
+
const createMemoryRouter = (ReactRouterDom as any)['create' + 'MemoryRouter'];
|
|
54
|
+
const createBrowserRouter = (ReactRouterDom as any)[
|
|
55
|
+
'create' + 'BrowserRouter'
|
|
56
|
+
];
|
|
57
|
+
if (!routerContextProps) return <RouterProvider {...props} />;
|
|
58
|
+
|
|
59
|
+
if (routerContextProps.memoryRoute) {
|
|
60
|
+
const MemeoryRouterInstance = createMemoryRouter(routers, {
|
|
61
|
+
initialEntries: [routerContextProps?.memoryRoute.entryPath],
|
|
62
|
+
});
|
|
63
|
+
return <RouterProvider router={MemeoryRouterInstance} />;
|
|
64
|
+
} else {
|
|
65
|
+
const BrowserRouterInstance = createBrowserRouter(routers, {
|
|
66
|
+
basename: routerContextProps.basename,
|
|
67
|
+
future: router.future,
|
|
68
|
+
window: router.window,
|
|
69
|
+
});
|
|
70
|
+
return <RouterProvider {...propsRes} router={BrowserRouterInstance} />;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// @ts-ignore
|
|
75
|
+
// export * from 'react-router-dom/index.js';
|
|
76
|
+
|
|
77
|
+
export { WraperRouter as BrowserRouter };
|
|
78
|
+
export { WraperRouterProvider as RouterProvider };
|