@module-federation/bridge-react 0.0.0-next-20240726075341 → 0.0.0-next-20240726084328
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 -2
- package/dist/index.cjs.js +111 -74
- package/dist/index.d.ts +15 -7
- package/dist/index.es.js +113 -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 +19 -24
- package/src/provider.tsx +37 -23
- package/src/remote/index.tsx +78 -55
- 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-20240726084328",
|
|
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-20240726084328"
|
|
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 {
|
|
@@ -10,13 +11,10 @@ import RemoteApp from './remote';
|
|
|
10
11
|
export interface RenderFnParams extends ProviderParams {
|
|
11
12
|
dom?: any;
|
|
12
13
|
}
|
|
13
|
-
|
|
14
14
|
interface RemoteModule {
|
|
15
15
|
provider: () => {
|
|
16
16
|
render: (
|
|
17
|
-
info:
|
|
18
|
-
dom: any;
|
|
19
|
-
},
|
|
17
|
+
info: RenderFnParams,
|
|
20
18
|
) => void;
|
|
21
19
|
destroy: (info: { dom: any }) => void;
|
|
22
20
|
};
|
|
@@ -27,6 +25,7 @@ function createLazyRemoteComponent<T, E extends keyof T>(info: {
|
|
|
27
25
|
loading: React.ReactNode;
|
|
28
26
|
fallback: ErrorBoundaryPropsWithComponent['FallbackComponent'];
|
|
29
27
|
export?: E;
|
|
28
|
+
dom?: string;
|
|
30
29
|
}) {
|
|
31
30
|
const exportName = info?.export || 'default';
|
|
32
31
|
return React.lazy(async () => {
|
|
@@ -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
|
);
|
|
@@ -89,30 +89,25 @@ export function createRemoteComponent<T, E extends keyof T>(info: {
|
|
|
89
89
|
loading: React.ReactNode;
|
|
90
90
|
fallback: ErrorBoundaryPropsWithComponent['FallbackComponent'];
|
|
91
91
|
export?: E;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const LazyComponent = createLazyRemoteComponent(info);
|
|
92
|
+
dom?: string;
|
|
93
|
+
}): ForwardRefExoticComponent<PropsWithoutRef<ProviderParams> & RefAttributes<HTMLElement | HTMLDivElement>> {
|
|
94
|
+
// type ExportType = T[E] extends (...args: any) => any
|
|
95
|
+
// ? ReturnType<T[E]>
|
|
96
|
+
// : never;
|
|
97
|
+
// type RawComponentType = '__BRIDGE_FN__' extends keyof ExportType
|
|
98
|
+
// ? ExportType['__BRIDGE_FN__'] extends (...args: any) => any
|
|
99
|
+
// ? Parameters<ExportType['__BRIDGE_FN__']>[0]
|
|
100
|
+
// : {}
|
|
101
|
+
// : {};
|
|
103
102
|
|
|
104
|
-
return (
|
|
105
|
-
|
|
106
|
-
basename?: ProviderParams['basename'];
|
|
107
|
-
memoryRoute?: ProviderParams['memoryRoute'];
|
|
108
|
-
} & RawComponentType,
|
|
109
|
-
) => {
|
|
103
|
+
return forwardRef(function (props, ref) {
|
|
104
|
+
const LazyComponent = createLazyRemoteComponent(info);
|
|
110
105
|
return (
|
|
111
106
|
<ErrorBoundary FallbackComponent={info.fallback}>
|
|
112
107
|
<React.Suspense fallback={info.loading}>
|
|
113
|
-
<LazyComponent {...props} />
|
|
108
|
+
<LazyComponent {...props} dom={info?.dom} ref={ref} />
|
|
114
109
|
</React.Suspense>
|
|
115
110
|
</ErrorBoundary>
|
|
116
111
|
);
|
|
117
|
-
};
|
|
112
|
+
});
|
|
118
113
|
}
|
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,11 +1,10 @@
|
|
|
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';
|
|
5
5
|
import { dispatchPopstateEnv } from '@module-federation/bridge-shared';
|
|
6
6
|
|
|
7
7
|
declare const __APP_VERSION__: string;
|
|
8
|
-
|
|
9
8
|
export interface RenderFnParams extends ProviderParams {
|
|
10
9
|
dom?: any;
|
|
11
10
|
}
|
|
@@ -25,69 +24,89 @@ interface RemoteAppParams {
|
|
|
25
24
|
name: string;
|
|
26
25
|
providerInfo: NonNullable<RemoteModule['provider']>;
|
|
27
26
|
exportName: string | number | symbol;
|
|
27
|
+
dom?: string;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return () => {
|
|
61
|
-
clearTimeout(renderTimeout);
|
|
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
|
-
});
|
|
30
|
+
const RemoteAppWrapper = forwardRef(function (props: RemoteAppParams & ProviderParams, ref) {
|
|
31
|
+
const RemoteApp = () => {
|
|
32
|
+
const {
|
|
33
|
+
name,
|
|
34
|
+
memoryRoute,
|
|
35
|
+
basename,
|
|
36
|
+
providerInfo,
|
|
37
|
+
dom,
|
|
38
|
+
className,
|
|
39
|
+
style,
|
|
40
|
+
...resProps
|
|
41
|
+
} = props;
|
|
42
|
+
|
|
43
|
+
const rootRef: React.MutableRefObject<HTMLElement | null> = ref && 'current' in ref ? ref as React.MutableRefObject<HTMLElement | null> : useRef(null);
|
|
44
|
+
const renderDom: React.MutableRefObject<HTMLElement | null> = useRef(null);
|
|
45
|
+
const providerInfoRef = useRef<any>(null);
|
|
46
|
+
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
const renderTimeout = setTimeout(() => {
|
|
49
|
+
const providerReturn = providerInfo();
|
|
50
|
+
providerInfoRef.current = providerReturn;
|
|
51
|
+
|
|
52
|
+
let domElement = null;
|
|
53
|
+
if (dom) {
|
|
54
|
+
domElement = document.getElementById(dom.replace('#', ''));
|
|
55
|
+
// TODO: if domElement is null, throw Error.
|
|
56
|
+
rootRef.current = domElement;
|
|
57
|
+
} else {
|
|
58
|
+
domElement = rootRef.current;
|
|
71
59
|
}
|
|
60
|
+
|
|
61
|
+
const renderProps = {
|
|
62
|
+
name,
|
|
63
|
+
// dom: rootRef.current,
|
|
64
|
+
dom: domElement,
|
|
65
|
+
basename,
|
|
66
|
+
memoryRoute,
|
|
67
|
+
...resProps,
|
|
68
|
+
};
|
|
69
|
+
renderDom.current = rootRef.current;
|
|
70
|
+
LoggerInstance.log(
|
|
71
|
+
`createRemoteComponent LazyComponent render >>>`,
|
|
72
|
+
renderProps,
|
|
73
|
+
);
|
|
74
|
+
providerReturn.render(renderProps);
|
|
72
75
|
});
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
|
|
77
|
+
return () => {
|
|
78
|
+
clearTimeout(renderTimeout);
|
|
79
|
+
setTimeout(() => {
|
|
80
|
+
if (providerInfoRef.current?.destroy) {
|
|
81
|
+
LoggerInstance.log(
|
|
82
|
+
`createRemoteComponent LazyComponent destroy >>>`,
|
|
83
|
+
{ name, basename, dom: renderDom.current },
|
|
84
|
+
);
|
|
85
|
+
providerInfoRef.current?.destroy({
|
|
86
|
+
dom: renderDom.current,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
}, []);
|
|
75
92
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
};
|
|
93
|
+
return dom ? null : <div className={props?.className} style={props?.style} ref={rootRef}></div>;
|
|
94
|
+
}
|
|
79
95
|
|
|
80
|
-
(RemoteApp as any)['__APP_VERSION__'] = __APP_VERSION__;
|
|
96
|
+
(RemoteApp as any)['__APP_VERSION__'] = __APP_VERSION__;
|
|
97
|
+
return <RemoteApp />
|
|
98
|
+
});
|
|
81
99
|
|
|
82
100
|
interface ExtraDataProps {
|
|
83
101
|
basename?: string;
|
|
84
102
|
}
|
|
85
103
|
|
|
86
|
-
export function withRouterData<P extends Parameters<typeof
|
|
104
|
+
export function withRouterData<P extends Parameters<typeof RemoteAppWrapper>[0]>(
|
|
87
105
|
WrappedComponent: React.ComponentType<P & ExtraDataProps>,
|
|
88
106
|
): React.FC<Omit<P, keyof ExtraDataProps>> {
|
|
89
|
-
|
|
90
|
-
|
|
107
|
+
|
|
108
|
+
const Component = forwardRef(function (props: any, ref) {
|
|
109
|
+
let enableDispathPopstate = false
|
|
91
110
|
let routerContextVal: any;
|
|
92
111
|
try {
|
|
93
112
|
ReactRouterDOM.useLocation();
|
|
@@ -158,8 +177,12 @@ export function withRouterData<P extends Parameters<typeof RemoteApp>[0]>(
|
|
|
158
177
|
}, [location]);
|
|
159
178
|
}
|
|
160
179
|
|
|
161
|
-
return <WrappedComponent {...(props as P)} basename={basename} />;
|
|
162
|
-
};
|
|
180
|
+
return <WrappedComponent {...(props as P)} basename={basename} ref={ref} />;
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
return forwardRef(function (props, ref) {
|
|
184
|
+
return <Component {...props} ref={ref} />
|
|
185
|
+
}) as any;
|
|
163
186
|
}
|
|
164
187
|
|
|
165
|
-
export default withRouterData(
|
|
188
|
+
export default withRouterData(RemoteAppWrapper);
|