@module-federation/bridge-react 0.0.0-next-20240619092117 → 0.0.0-next-20240619100937
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/{context-CoFgcMIF.js → context-CPtN38Ur.js} +15 -2
- package/dist/{context-C7FfFcIe.cjs → context-ePt4wynZ.cjs} +13 -0
- package/dist/index.cjs.js +58 -20
- package/dist/index.d.ts +3 -2
- package/dist/index.es.js +46 -24
- package/dist/router.cjs.js +1 -1
- package/dist/router.es.js +8 -8
- package/package.json +5 -5
- package/src/provider.tsx +39 -18
- package/src/utils.ts +20 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import React__default from "react";
|
|
2
2
|
var a = Object.defineProperty;
|
|
3
3
|
var c = (s, e, t) => e in s ? a(s, e, { enumerable: true, configurable: true, writable: true, value: t }) : s[e] = t;
|
|
4
4
|
var i = (s, e, t) => (c(s, typeof e != "symbol" ? e + "" : e, t), t);
|
|
@@ -35,9 +35,22 @@ function f() {
|
|
|
35
35
|
window.dispatchEvent(s);
|
|
36
36
|
}
|
|
37
37
|
const LoggerInstance = new g("bridge-react");
|
|
38
|
-
|
|
38
|
+
function atLeastReact18(React2) {
|
|
39
|
+
if (React2 && typeof React2.version === "string" && React2.version.indexOf(".") >= 0) {
|
|
40
|
+
const majorVersionString = React2.version.split(".")[0];
|
|
41
|
+
try {
|
|
42
|
+
return Number(majorVersionString) >= 18;
|
|
43
|
+
} catch (err) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const RouterContext = React__default.createContext(null);
|
|
39
51
|
export {
|
|
40
52
|
LoggerInstance as L,
|
|
41
53
|
RouterContext as R,
|
|
54
|
+
atLeastReact18 as a,
|
|
42
55
|
f
|
|
43
56
|
};
|
|
@@ -36,7 +36,20 @@ function f() {
|
|
|
36
36
|
window.dispatchEvent(s);
|
|
37
37
|
}
|
|
38
38
|
const LoggerInstance = new g("bridge-react");
|
|
39
|
+
function atLeastReact18(React2) {
|
|
40
|
+
if (React2 && typeof React2.version === "string" && React2.version.indexOf(".") >= 0) {
|
|
41
|
+
const majorVersionString = React2.version.split(".")[0];
|
|
42
|
+
try {
|
|
43
|
+
return Number(majorVersionString) >= 18;
|
|
44
|
+
} catch (err) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
39
51
|
const RouterContext = React.createContext(null);
|
|
40
52
|
exports.LoggerInstance = LoggerInstance;
|
|
41
53
|
exports.RouterContext = RouterContext;
|
|
54
|
+
exports.atLeastReact18 = atLeastReact18;
|
|
42
55
|
exports.f = f;
|
package/dist/index.cjs.js
CHANGED
|
@@ -2,8 +2,25 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const React = require("react");
|
|
4
4
|
const reactRouterDom = require("react-router-dom");
|
|
5
|
-
const context = require("./context-
|
|
6
|
-
const
|
|
5
|
+
const context = require("./context-ePt4wynZ.cjs");
|
|
6
|
+
const ReactDOM = require("react-dom");
|
|
7
|
+
function _interopNamespaceDefault(e) {
|
|
8
|
+
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
9
|
+
if (e) {
|
|
10
|
+
for (const k in e) {
|
|
11
|
+
if (k !== "default") {
|
|
12
|
+
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
13
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: () => e[k]
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
n.default = e;
|
|
21
|
+
return Object.freeze(n);
|
|
22
|
+
}
|
|
23
|
+
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
|
|
7
24
|
const RemoteApp = ({
|
|
8
25
|
name,
|
|
9
26
|
memoryRoute,
|
|
@@ -107,7 +124,7 @@ function createRemoteComponent(lazyComponent, info) {
|
|
|
107
124
|
};
|
|
108
125
|
}
|
|
109
126
|
var client = {};
|
|
110
|
-
var m =
|
|
127
|
+
var m = ReactDOM;
|
|
111
128
|
if (process.env.NODE_ENV === "production") {
|
|
112
129
|
client.createRoot = m.createRoot;
|
|
113
130
|
client.hydrateRoot = m.hydrateRoot;
|
|
@@ -136,34 +153,55 @@ function createBridgeComponent(bridgeInfo) {
|
|
|
136
153
|
const RawComponent = (info) => {
|
|
137
154
|
const { appInfo, propsInfo } = info;
|
|
138
155
|
const { name, memoryRoute, basename = "/" } = appInfo;
|
|
139
|
-
return /* @__PURE__ */
|
|
156
|
+
return /* @__PURE__ */ React__namespace.createElement(context.RouterContext.Provider, { value: { name, basename, memoryRoute } }, /* @__PURE__ */ React__namespace.createElement(bridgeInfo.rootComponent, { ...propsInfo, basename }));
|
|
140
157
|
};
|
|
141
158
|
return {
|
|
142
159
|
render(info) {
|
|
143
160
|
context.LoggerInstance.log(`createBridgeComponent render Info`, info);
|
|
144
|
-
const root = client.createRoot(info.dom);
|
|
145
|
-
rootMap.set(info.dom, root);
|
|
146
161
|
const { name, basename, memoryRoute, ...propsInfo } = info;
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
162
|
+
if (context.atLeastReact18(React__namespace)) {
|
|
163
|
+
const root = client.createRoot(info.dom);
|
|
164
|
+
rootMap.set(info.dom, root);
|
|
165
|
+
root.render(
|
|
166
|
+
/* @__PURE__ */ React__namespace.createElement(
|
|
167
|
+
RawComponent,
|
|
168
|
+
{
|
|
169
|
+
propsInfo,
|
|
170
|
+
appInfo: {
|
|
171
|
+
name,
|
|
172
|
+
basename,
|
|
173
|
+
memoryRoute
|
|
174
|
+
}
|
|
156
175
|
}
|
|
157
|
-
|
|
158
|
-
)
|
|
159
|
-
|
|
176
|
+
)
|
|
177
|
+
);
|
|
178
|
+
} else {
|
|
179
|
+
ReactDOM.render(
|
|
180
|
+
/* @__PURE__ */ React__namespace.createElement(
|
|
181
|
+
RawComponent,
|
|
182
|
+
{
|
|
183
|
+
propsInfo,
|
|
184
|
+
appInfo: {
|
|
185
|
+
name,
|
|
186
|
+
basename,
|
|
187
|
+
memoryRoute
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
),
|
|
191
|
+
info.dom
|
|
192
|
+
);
|
|
193
|
+
}
|
|
160
194
|
},
|
|
161
195
|
destroy(info) {
|
|
162
196
|
context.LoggerInstance.log(`createBridgeComponent destroy Info`, {
|
|
163
197
|
dom: info.dom
|
|
164
198
|
});
|
|
165
|
-
|
|
166
|
-
|
|
199
|
+
if (context.atLeastReact18(React__namespace)) {
|
|
200
|
+
const root = rootMap.get(info.dom);
|
|
201
|
+
root == null ? void 0 : root.unmount();
|
|
202
|
+
} else {
|
|
203
|
+
ReactDOM.unmountComponentAtNode(info.dom);
|
|
204
|
+
}
|
|
167
205
|
},
|
|
168
206
|
rawComponent: bridgeInfo.rootComponent,
|
|
169
207
|
__BRIDGE_FN__: (_args) => {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { default as default_2 } from 'react';
|
|
2
|
+
import * as React_2 from 'react';
|
|
2
3
|
import { ReactNode } from 'react';
|
|
3
4
|
|
|
4
5
|
export declare function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>): () => {
|
|
@@ -6,7 +7,7 @@ export declare function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>
|
|
|
6
7
|
destroy(info: {
|
|
7
8
|
dom: HTMLElement;
|
|
8
9
|
}): void;
|
|
9
|
-
rawComponent:
|
|
10
|
+
rawComponent: React_2.ComponentType<T>;
|
|
10
11
|
__BRIDGE_FN__: (_args: T) => void;
|
|
11
12
|
};
|
|
12
13
|
|
|
@@ -19,7 +20,7 @@ export declare function createRemoteComponent<T, E extends keyof T>(lazyComponen
|
|
|
19
20
|
} & ("__BRIDGE_FN__" extends keyof (T[E] extends (...args: any) => any ? ReturnType<T[E]> : never) ? (T[E] extends (...args: any) => any ? ReturnType<T[E]> : never)["__BRIDGE_FN__"] extends (...args: any) => any ? Parameters<(T[E] extends (...args: any) => any ? ReturnType<T[E]> : never)["__BRIDGE_FN__"]>[0] : {} : {})) => default_2.JSX.Element;
|
|
20
21
|
|
|
21
22
|
declare type ProviderFnParams<T> = {
|
|
22
|
-
rootComponent:
|
|
23
|
+
rootComponent: React_2.ComponentType<T>;
|
|
23
24
|
};
|
|
24
25
|
|
|
25
26
|
export declare interface ProviderParams {
|
package/dist/index.es.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import React__default, { useContext, useMemo, useRef, useState, useEffect } from "react";
|
|
2
3
|
import { UNSAFE_RouteContext, useLocation } from "react-router-dom";
|
|
3
|
-
import { L as LoggerInstance, f, R as RouterContext } from "./context-
|
|
4
|
-
import
|
|
4
|
+
import { L as LoggerInstance, f, a as atLeastReact18, R as RouterContext } from "./context-CPtN38Ur.js";
|
|
5
|
+
import ReactDOM from "react-dom";
|
|
5
6
|
const RemoteApp = ({
|
|
6
7
|
name,
|
|
7
8
|
memoryRoute,
|
|
@@ -58,7 +59,7 @@ const RemoteApp = ({
|
|
|
58
59
|
});
|
|
59
60
|
};
|
|
60
61
|
}, []);
|
|
61
|
-
return /* @__PURE__ */
|
|
62
|
+
return /* @__PURE__ */ React__default.createElement("div", { ref: rootRef });
|
|
62
63
|
};
|
|
63
64
|
RemoteApp["__APP_VERSION__"] = "0.0.1";
|
|
64
65
|
function createRemoteComponent(lazyComponent, info) {
|
|
@@ -70,7 +71,7 @@ function createRemoteComponent(lazyComponent, info) {
|
|
|
70
71
|
basename = routerContextVal.matches[0].pathnameBase;
|
|
71
72
|
}
|
|
72
73
|
const LazyComponent = useMemo(() => {
|
|
73
|
-
return
|
|
74
|
+
return React__default.lazy(async () => {
|
|
74
75
|
LoggerInstance.log(`createRemoteComponent LazyComponent create >>>`, {
|
|
75
76
|
basename,
|
|
76
77
|
lazyComponent,
|
|
@@ -86,7 +87,7 @@ function createRemoteComponent(lazyComponent, info) {
|
|
|
86
87
|
const exportFn = m2[exportName];
|
|
87
88
|
if (exportName in m2 && typeof exportFn === "function") {
|
|
88
89
|
return {
|
|
89
|
-
default: () => /* @__PURE__ */
|
|
90
|
+
default: () => /* @__PURE__ */ React__default.createElement(
|
|
90
91
|
RemoteApp,
|
|
91
92
|
{
|
|
92
93
|
name: moduleName,
|
|
@@ -101,11 +102,11 @@ function createRemoteComponent(lazyComponent, info) {
|
|
|
101
102
|
throw Error("module not found");
|
|
102
103
|
});
|
|
103
104
|
}, [exportName, basename, props.memoryRoute]);
|
|
104
|
-
return /* @__PURE__ */
|
|
105
|
+
return /* @__PURE__ */ React__default.createElement(React__default.Suspense, { fallback: props.fallback }, /* @__PURE__ */ React__default.createElement(LazyComponent, null));
|
|
105
106
|
};
|
|
106
107
|
}
|
|
107
108
|
var client = {};
|
|
108
|
-
var m =
|
|
109
|
+
var m = ReactDOM;
|
|
109
110
|
if (process.env.NODE_ENV === "production") {
|
|
110
111
|
client.createRoot = m.createRoot;
|
|
111
112
|
client.hydrateRoot = m.hydrateRoot;
|
|
@@ -139,29 +140,50 @@ function createBridgeComponent(bridgeInfo) {
|
|
|
139
140
|
return {
|
|
140
141
|
render(info) {
|
|
141
142
|
LoggerInstance.log(`createBridgeComponent render Info`, info);
|
|
142
|
-
const root = client.createRoot(info.dom);
|
|
143
|
-
rootMap.set(info.dom, root);
|
|
144
143
|
const { name, basename, memoryRoute, ...propsInfo } = info;
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
144
|
+
if (atLeastReact18(React)) {
|
|
145
|
+
const root = client.createRoot(info.dom);
|
|
146
|
+
rootMap.set(info.dom, root);
|
|
147
|
+
root.render(
|
|
148
|
+
/* @__PURE__ */ React.createElement(
|
|
149
|
+
RawComponent,
|
|
150
|
+
{
|
|
151
|
+
propsInfo,
|
|
152
|
+
appInfo: {
|
|
153
|
+
name,
|
|
154
|
+
basename,
|
|
155
|
+
memoryRoute
|
|
156
|
+
}
|
|
154
157
|
}
|
|
155
|
-
|
|
156
|
-
)
|
|
157
|
-
|
|
158
|
+
)
|
|
159
|
+
);
|
|
160
|
+
} else {
|
|
161
|
+
ReactDOM.render(
|
|
162
|
+
/* @__PURE__ */ React.createElement(
|
|
163
|
+
RawComponent,
|
|
164
|
+
{
|
|
165
|
+
propsInfo,
|
|
166
|
+
appInfo: {
|
|
167
|
+
name,
|
|
168
|
+
basename,
|
|
169
|
+
memoryRoute
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
),
|
|
173
|
+
info.dom
|
|
174
|
+
);
|
|
175
|
+
}
|
|
158
176
|
},
|
|
159
177
|
destroy(info) {
|
|
160
178
|
LoggerInstance.log(`createBridgeComponent destroy Info`, {
|
|
161
179
|
dom: info.dom
|
|
162
180
|
});
|
|
163
|
-
|
|
164
|
-
|
|
181
|
+
if (atLeastReact18(React)) {
|
|
182
|
+
const root = rootMap.get(info.dom);
|
|
183
|
+
root == null ? void 0 : root.unmount();
|
|
184
|
+
} else {
|
|
185
|
+
ReactDOM.unmountComponentAtNode(info.dom);
|
|
186
|
+
}
|
|
165
187
|
},
|
|
166
188
|
rawComponent: bridgeInfo.rootComponent,
|
|
167
189
|
__BRIDGE_FN__: (_args) => {
|
package/dist/router.cjs.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const React = require("react");
|
|
4
4
|
const ReactRouterDom = require("react-router-dom/");
|
|
5
|
-
const context = require("./context-
|
|
5
|
+
const context = require("./context-ePt4wynZ.cjs");
|
|
6
6
|
function _interopNamespaceDefault(e) {
|
|
7
7
|
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
8
8
|
if (e) {
|
package/dist/router.es.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import React__default, { useContext } from "react";
|
|
2
2
|
import * as ReactRouterDom from "react-router-dom/";
|
|
3
3
|
export * from "react-router-dom/";
|
|
4
|
-
import { R as RouterContext, L as LoggerInstance } from "./context-
|
|
4
|
+
import { R as RouterContext, L as LoggerInstance } from "./context-CPtN38Ur.js";
|
|
5
5
|
function WraperRouter(props) {
|
|
6
6
|
const { basename, ...propsRes } = props;
|
|
7
7
|
const routerContextProps = useContext(RouterContext) || {};
|
|
@@ -11,9 +11,9 @@ function WraperRouter(props) {
|
|
|
11
11
|
WraperRouterProps: props
|
|
12
12
|
});
|
|
13
13
|
if (!routerContextProps)
|
|
14
|
-
return /* @__PURE__ */
|
|
14
|
+
return /* @__PURE__ */ React__default.createElement(ReactRouterDom.BrowserRouter, { ...props });
|
|
15
15
|
if (routerContextProps == null ? void 0 : routerContextProps.memoryRoute) {
|
|
16
|
-
return /* @__PURE__ */
|
|
16
|
+
return /* @__PURE__ */ React__default.createElement(
|
|
17
17
|
ReactRouterDom.MemoryRouter,
|
|
18
18
|
{
|
|
19
19
|
...props,
|
|
@@ -21,7 +21,7 @@ function WraperRouter(props) {
|
|
|
21
21
|
}
|
|
22
22
|
);
|
|
23
23
|
}
|
|
24
|
-
return /* @__PURE__ */
|
|
24
|
+
return /* @__PURE__ */ React__default.createElement(
|
|
25
25
|
ReactRouterDom.BrowserRouter,
|
|
26
26
|
{
|
|
27
27
|
...propsRes,
|
|
@@ -40,19 +40,19 @@ function WraperRouterProvider(props) {
|
|
|
40
40
|
router
|
|
41
41
|
});
|
|
42
42
|
if (!routerContextProps)
|
|
43
|
-
return /* @__PURE__ */
|
|
43
|
+
return /* @__PURE__ */ React__default.createElement(ReactRouterDom.RouterProvider, { ...props });
|
|
44
44
|
if (routerContextProps.memoryRoute) {
|
|
45
45
|
const MemeoryRouterInstance = ReactRouterDom.createMemoryRouter(routers, {
|
|
46
46
|
initialEntries: [routerContextProps == null ? void 0 : routerContextProps.memoryRoute.entryPath]
|
|
47
47
|
});
|
|
48
|
-
return /* @__PURE__ */
|
|
48
|
+
return /* @__PURE__ */ React__default.createElement(ReactRouterDom.RouterProvider, { router: MemeoryRouterInstance });
|
|
49
49
|
} else {
|
|
50
50
|
const BrowserRouterInstance = ReactRouterDom.createBrowserRouter(routers, {
|
|
51
51
|
basename: routerContextProps.basename,
|
|
52
52
|
future: router.future,
|
|
53
53
|
window: router.window
|
|
54
54
|
});
|
|
55
|
-
return /* @__PURE__ */
|
|
55
|
+
return /* @__PURE__ */ React__default.createElement(
|
|
56
56
|
ReactRouterDom.RouterProvider,
|
|
57
57
|
{
|
|
58
58
|
...propsRes,
|
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-20240619100937",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@loadable/component": "^5.16.4",
|
|
27
27
|
"react-error-boundary": "^4.0.13",
|
|
28
|
-
"@module-federation/bridge-shared": "0.0.0-next-
|
|
28
|
+
"@module-federation/bridge-shared": "0.0.0-next-20240619100937"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"react": "
|
|
32
|
-
"react-dom": "
|
|
33
|
-
"react-router-dom": ">=
|
|
31
|
+
"react": ">=16.9.0",
|
|
32
|
+
"react-dom": ">=16.9.0",
|
|
33
|
+
"react-router-dom": ">=4"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@testing-library/react": "15.0.7",
|
package/src/provider.tsx
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { useLayoutEffect, useRef, useState } from 'react';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import ReactDOM from 'react-dom';
|
|
4
|
+
import ReactDOMClient from 'react-dom/client';
|
|
3
5
|
import { RouterContext } from './context';
|
|
4
6
|
import type {
|
|
5
7
|
ProviderParams,
|
|
6
8
|
RenderFnParams,
|
|
7
9
|
} from '@module-federation/bridge-shared';
|
|
8
|
-
import { LoggerInstance } from './utils';
|
|
10
|
+
import { LoggerInstance, atLeastReact18 } from './utils';
|
|
9
11
|
|
|
10
12
|
type ProviderFnParams<T> = {
|
|
11
13
|
rootComponent: React.ComponentType<T>;
|
|
@@ -13,7 +15,7 @@ type ProviderFnParams<T> = {
|
|
|
13
15
|
|
|
14
16
|
export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
|
|
15
17
|
return () => {
|
|
16
|
-
const rootMap = new Map<any,
|
|
18
|
+
const rootMap = new Map<any, ReactDOMClient.Root>();
|
|
17
19
|
|
|
18
20
|
const RawComponent = (info: { propsInfo: T; appInfo: ProviderParams }) => {
|
|
19
21
|
const { appInfo, propsInfo } = info;
|
|
@@ -29,26 +31,45 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
|
|
|
29
31
|
return {
|
|
30
32
|
render(info: RenderFnParams & any) {
|
|
31
33
|
LoggerInstance.log(`createBridgeComponent render Info`, info);
|
|
32
|
-
const root = ReactDOM.createRoot(info.dom);
|
|
33
|
-
rootMap.set(info.dom, root);
|
|
34
34
|
const { name, basename, memoryRoute, ...propsInfo } = info;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
35
|
+
|
|
36
|
+
if (atLeastReact18(React)) {
|
|
37
|
+
const root = ReactDOMClient.createRoot(info.dom);
|
|
38
|
+
rootMap.set(info.dom, root);
|
|
39
|
+
root.render(
|
|
40
|
+
<RawComponent
|
|
41
|
+
propsInfo={propsInfo}
|
|
42
|
+
appInfo={{
|
|
43
|
+
name,
|
|
44
|
+
basename,
|
|
45
|
+
memoryRoute,
|
|
46
|
+
}}
|
|
47
|
+
/>,
|
|
48
|
+
);
|
|
49
|
+
} else {
|
|
50
|
+
ReactDOM.render(
|
|
51
|
+
<RawComponent
|
|
52
|
+
propsInfo={propsInfo}
|
|
53
|
+
appInfo={{
|
|
54
|
+
name,
|
|
55
|
+
basename,
|
|
56
|
+
memoryRoute,
|
|
57
|
+
}}
|
|
58
|
+
/>,
|
|
59
|
+
info.dom,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
45
62
|
},
|
|
46
63
|
destroy(info: { dom: HTMLElement }) {
|
|
47
64
|
LoggerInstance.log(`createBridgeComponent destroy Info`, {
|
|
48
65
|
dom: info.dom,
|
|
49
66
|
});
|
|
50
|
-
|
|
51
|
-
|
|
67
|
+
if (atLeastReact18(React)) {
|
|
68
|
+
const root = rootMap.get(info.dom);
|
|
69
|
+
root?.unmount();
|
|
70
|
+
} else {
|
|
71
|
+
ReactDOM.unmountComponentAtNode(info.dom);
|
|
72
|
+
}
|
|
52
73
|
},
|
|
53
74
|
rawComponent: bridgeInfo.rootComponent,
|
|
54
75
|
__BRIDGE_FN__: (_args: T) => {},
|
package/src/utils.ts
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
import React from 'react';
|
|
1
2
|
import { Logger } from '@module-federation/bridge-shared';
|
|
2
3
|
|
|
3
4
|
export const LoggerInstance = new Logger('bridge-react');
|
|
5
|
+
|
|
6
|
+
type typeReact = typeof React;
|
|
7
|
+
|
|
8
|
+
export function atLeastReact18(React: typeReact) {
|
|
9
|
+
if (
|
|
10
|
+
React &&
|
|
11
|
+
typeof React.version === 'string' &&
|
|
12
|
+
React.version.indexOf('.') >= 0
|
|
13
|
+
) {
|
|
14
|
+
const majorVersionString = React.version.split('.')[0];
|
|
15
|
+
try {
|
|
16
|
+
return Number(majorVersionString) >= 18;
|
|
17
|
+
} catch (err) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|