@module-federation/bridge-react 0.0.0-next-20240725030707 → 0.0.0-next-20240725035430
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 +103 -75
- package/dist/index.d.ts +19 -6
- package/dist/index.es.js +105 -77
- 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 +22 -22
- package/src/provider.tsx +37 -23
- package/src/remote/index.tsx +69 -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-20240725035430",
|
|
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-20240725035430"
|
|
29
39
|
},
|
|
30
40
|
"peerDependencies": {
|
|
31
41
|
"react": ">=16.9.0",
|
package/src/create.tsx
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import React, { forwardRef } from 'react';
|
|
2
|
+
import type { ForwardRefExoticComponent, PropsWithoutRef, RefAttributes } from 'react';
|
|
2
3
|
import type { ProviderParams } from '@module-federation/bridge-shared';
|
|
3
4
|
import { LoggerInstance } from './utils';
|
|
4
5
|
import {
|
|
@@ -6,11 +7,9 @@ import {
|
|
|
6
7
|
ErrorBoundaryPropsWithComponent,
|
|
7
8
|
} from 'react-error-boundary';
|
|
8
9
|
import RemoteApp from './remote';
|
|
9
|
-
|
|
10
10
|
export interface RenderFnParams extends ProviderParams {
|
|
11
11
|
dom?: any;
|
|
12
12
|
}
|
|
13
|
-
|
|
14
13
|
interface RemoteModule {
|
|
15
14
|
provider: () => {
|
|
16
15
|
render: (
|
|
@@ -53,12 +52,13 @@ function createLazyRemoteComponent<T, E extends keyof T>(info: {
|
|
|
53
52
|
basename?: ProviderParams['basename'];
|
|
54
53
|
memoryRoute?: ProviderParams['memoryRoute'];
|
|
55
54
|
}
|
|
56
|
-
>((props,
|
|
55
|
+
>((props, ref) => {
|
|
57
56
|
return (
|
|
58
57
|
<RemoteApp
|
|
59
58
|
name={moduleName}
|
|
60
59
|
providerInfo={exportFn}
|
|
61
60
|
exportName={info.export || 'default'}
|
|
61
|
+
ref={ref}
|
|
62
62
|
{...props}
|
|
63
63
|
/>
|
|
64
64
|
);
|
|
@@ -84,35 +84,35 @@ function createLazyRemoteComponent<T, E extends keyof T>(info: {
|
|
|
84
84
|
});
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
+
type RemoteProps = {
|
|
88
|
+
basename?: ProviderParams['basename'];
|
|
89
|
+
memoryRoute?: ProviderParams['memoryRoute'];
|
|
90
|
+
[key: string]: any;
|
|
91
|
+
};
|
|
92
|
+
|
|
87
93
|
export function createRemoteComponent<T, E extends keyof T>(info: {
|
|
88
94
|
loader: () => Promise<T>;
|
|
89
95
|
loading: React.ReactNode;
|
|
90
96
|
fallback: ErrorBoundaryPropsWithComponent['FallbackComponent'];
|
|
91
97
|
export?: E;
|
|
92
|
-
}) {
|
|
93
|
-
type ExportType = T[E] extends (...args: any) => any
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
type RawComponentType = '__BRIDGE_FN__' extends keyof ExportType
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
98
|
+
}): ForwardRefExoticComponent< PropsWithoutRef<RemoteProps> & RefAttributes<HTMLElement | HTMLDivElement>> {
|
|
99
|
+
// type ExportType = T[E] extends (...args: any) => any
|
|
100
|
+
// ? ReturnType<T[E]>
|
|
101
|
+
// : never;
|
|
102
|
+
// type RawComponentType = '__BRIDGE_FN__' extends keyof ExportType
|
|
103
|
+
// ? ExportType['__BRIDGE_FN__'] extends (...args: any) => any
|
|
104
|
+
// ? Parameters<ExportType['__BRIDGE_FN__']>[0]
|
|
105
|
+
// : {}
|
|
106
|
+
// : {};
|
|
101
107
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return (
|
|
105
|
-
props: {
|
|
106
|
-
basename?: ProviderParams['basename'];
|
|
107
|
-
memoryRoute?: ProviderParams['memoryRoute'];
|
|
108
|
-
} & RawComponentType,
|
|
109
|
-
) => {
|
|
108
|
+
return forwardRef(function (props, ref) {
|
|
109
|
+
const LazyComponent = createLazyRemoteComponent(info);
|
|
110
110
|
return (
|
|
111
111
|
<ErrorBoundary FallbackComponent={info.fallback}>
|
|
112
112
|
<React.Suspense fallback={info.loading}>
|
|
113
|
-
<LazyComponent {...props} />
|
|
113
|
+
<LazyComponent {...props} ref={ref} />
|
|
114
114
|
</React.Suspense>
|
|
115
115
|
</ErrorBoundary>
|
|
116
116
|
);
|
|
117
|
-
};
|
|
117
|
+
});
|
|
118
118
|
}
|
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,76 @@ 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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
+
className,
|
|
38
|
+
style,
|
|
39
|
+
...resProps
|
|
40
|
+
} = props;
|
|
41
|
+
|
|
42
|
+
const rootRef: React.MutableRefObject<HTMLElement | null> = ref && 'current' in ref ? ref as React.MutableRefObject<HTMLElement | null> : useRef(null);
|
|
43
|
+
const renderDom: React.MutableRefObject<HTMLElement | null> = useRef(null);
|
|
44
|
+
const providerInfoRef = useRef<any>(null);
|
|
45
|
+
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
const renderTimeout = setTimeout(() => {
|
|
48
|
+
const providerReturn = providerInfo();
|
|
49
|
+
providerInfoRef.current = providerReturn;
|
|
50
|
+
const renderProps = {
|
|
51
|
+
name,
|
|
52
|
+
dom: rootRef.current,
|
|
53
|
+
basename,
|
|
54
|
+
memoryRoute,
|
|
55
|
+
...resProps,
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
renderDom.current = rootRef.current;
|
|
59
|
+
LoggerInstance.log(
|
|
60
|
+
`createRemoteComponent LazyComponent render >>>`,
|
|
61
|
+
renderProps,
|
|
62
|
+
);
|
|
63
|
+
providerReturn.render(renderProps);
|
|
72
64
|
});
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
65
|
+
|
|
66
|
+
return () => {
|
|
67
|
+
clearTimeout(renderTimeout);
|
|
68
|
+
setTimeout(() => {
|
|
69
|
+
if (providerInfoRef.current?.destroy) {
|
|
70
|
+
LoggerInstance.log(
|
|
71
|
+
`createRemoteComponent LazyComponent destroy >>>`,
|
|
72
|
+
{ name, basename, dom: renderDom.current },
|
|
73
|
+
);
|
|
74
|
+
providerInfoRef.current?.destroy({
|
|
75
|
+
dom: renderDom.current,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
}, []);
|
|
81
|
+
|
|
82
|
+
//@ts-ignore
|
|
83
|
+
return <div className={className} style={style} ref={rootRef}></div>;
|
|
84
|
+
}
|
|
79
85
|
|
|
80
|
-
(RemoteApp as any)['__APP_VERSION__'] = __APP_VERSION__;
|
|
86
|
+
(RemoteApp as any)['__APP_VERSION__'] = __APP_VERSION__;
|
|
87
|
+
return <RemoteApp />
|
|
88
|
+
});
|
|
81
89
|
|
|
82
90
|
interface ExtraDataProps {
|
|
83
91
|
basename?: string;
|
|
84
92
|
}
|
|
85
93
|
|
|
86
|
-
export function withRouterData<P extends Parameters<typeof
|
|
94
|
+
export function withRouterData<P extends Parameters<typeof RemoteAppWrapper>[0]>(
|
|
87
95
|
WrappedComponent: React.ComponentType<P & ExtraDataProps>,
|
|
88
96
|
): React.FC<Omit<P, keyof ExtraDataProps>> {
|
|
89
|
-
|
|
90
|
-
|
|
97
|
+
|
|
98
|
+
const Component = forwardRef(function (props: any, ref) {
|
|
99
|
+
let enableDispathPopstate = false
|
|
91
100
|
let routerContextVal: any;
|
|
92
101
|
try {
|
|
93
102
|
ReactRouterDOM.useLocation();
|
|
@@ -158,8 +167,12 @@ export function withRouterData<P extends Parameters<typeof RemoteApp>[0]>(
|
|
|
158
167
|
}, [location]);
|
|
159
168
|
}
|
|
160
169
|
|
|
161
|
-
return <WrappedComponent {...(props as P)} basename={basename} />;
|
|
162
|
-
};
|
|
170
|
+
return <WrappedComponent {...(props as P)} basename={basename} ref={ref} />;
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
return forwardRef(function (props, ref) {
|
|
174
|
+
return <Component {...props} ref={ref} />
|
|
175
|
+
}) as any;
|
|
163
176
|
}
|
|
164
177
|
|
|
165
|
-
export default withRouterData(
|
|
178
|
+
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 };
|