@module-federation/bridge-react 0.0.0-next-20240619100937 → 0.0.0-next-20240620034636
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/__tests__/bridge.spec.tsx +2 -1
- package/dist/index.cjs.js +34 -17
- package/dist/index.es.js +33 -17
- package/package.json +2 -2
- package/src/create.tsx +41 -20
package/CHANGELOG.md
CHANGED
|
@@ -63,6 +63,7 @@ describe('bridge', () => {
|
|
|
63
63
|
expect(getHtml(container)).toMatch('loading');
|
|
64
64
|
|
|
65
65
|
await sleep(200);
|
|
66
|
-
expect(getHtml(container)).toMatch('life cycle render
|
|
66
|
+
expect(getHtml(container)).toMatch('life cycle render');
|
|
67
|
+
expect(getHtml(container)).toMatch('hello world');
|
|
67
68
|
});
|
|
68
69
|
});
|
package/dist/index.cjs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const React = require("react");
|
|
4
|
-
const
|
|
4
|
+
const ReactRouterDOM = require("react-router-dom");
|
|
5
5
|
const context = require("./context-ePt4wynZ.cjs");
|
|
6
6
|
const ReactDOM = require("react-dom");
|
|
7
7
|
function _interopNamespaceDefault(e) {
|
|
@@ -21,28 +21,32 @@ function _interopNamespaceDefault(e) {
|
|
|
21
21
|
return Object.freeze(n);
|
|
22
22
|
}
|
|
23
23
|
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
|
|
24
|
+
const ReactRouterDOM__namespace = /* @__PURE__ */ _interopNamespaceDefault(ReactRouterDOM);
|
|
24
25
|
const RemoteApp = ({
|
|
25
26
|
name,
|
|
26
27
|
memoryRoute,
|
|
27
28
|
basename,
|
|
28
29
|
providerInfo,
|
|
30
|
+
dispathPopstate,
|
|
29
31
|
...resProps
|
|
30
32
|
}) => {
|
|
31
33
|
const rootRef = React.useRef(null);
|
|
32
34
|
const renderDom = React.useRef(null);
|
|
33
|
-
const location = reactRouterDom.useLocation();
|
|
34
|
-
const [pathname, setPathname] = React.useState(location.pathname);
|
|
35
35
|
const providerInfoRef = React.useRef(null);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
36
|
+
if (dispathPopstate) {
|
|
37
|
+
const location = ReactRouterDOM__namespace.useLocation();
|
|
38
|
+
const [pathname, setPathname] = React.useState(location.pathname);
|
|
39
|
+
React.useEffect(() => {
|
|
40
|
+
if (pathname !== "" && pathname !== location.pathname) {
|
|
41
|
+
context.LoggerInstance.log(`createRemoteComponent dispatchPopstateEnv >>>`, {
|
|
42
|
+
name,
|
|
43
|
+
pathname: location.pathname
|
|
44
|
+
});
|
|
45
|
+
context.f();
|
|
46
|
+
}
|
|
47
|
+
setPathname(location.pathname);
|
|
48
|
+
}, [location]);
|
|
49
|
+
}
|
|
46
50
|
React.useEffect(() => {
|
|
47
51
|
const renderTimeout = setTimeout(() => {
|
|
48
52
|
const providerReturn = providerInfo();
|
|
@@ -83,10 +87,21 @@ RemoteApp["__APP_VERSION__"] = "0.0.1";
|
|
|
83
87
|
function createRemoteComponent(lazyComponent, info) {
|
|
84
88
|
return (props) => {
|
|
85
89
|
const exportName = (info == null ? void 0 : info.export) || "default";
|
|
86
|
-
const routerContextVal = React.useContext(reactRouterDom.UNSAFE_RouteContext);
|
|
87
90
|
let basename = "/";
|
|
88
|
-
|
|
89
|
-
|
|
91
|
+
let enableDispathPopstate = false;
|
|
92
|
+
let routerContextVal;
|
|
93
|
+
try {
|
|
94
|
+
ReactRouterDOM__namespace.useLocation();
|
|
95
|
+
enableDispathPopstate = true;
|
|
96
|
+
} catch {
|
|
97
|
+
enableDispathPopstate = false;
|
|
98
|
+
}
|
|
99
|
+
if (ReactRouterDOM__namespace.UNSAFE_RouteContext && enableDispathPopstate) {
|
|
100
|
+
routerContextVal = React.useContext(ReactRouterDOM__namespace.UNSAFE_RouteContext);
|
|
101
|
+
if (routerContextVal && routerContextVal.matches && routerContextVal.matches[0] && routerContextVal.matches[0].pathnameBase) {
|
|
102
|
+
basename = routerContextVal.matches[0].pathnameBase;
|
|
103
|
+
}
|
|
104
|
+
enableDispathPopstate = (routerContextVal == null ? void 0 : routerContextVal.matches) && (routerContextVal == null ? void 0 : routerContextVal.matches.length) > 0;
|
|
90
105
|
}
|
|
91
106
|
const LazyComponent = React.useMemo(() => {
|
|
92
107
|
return React.lazy(async () => {
|
|
@@ -94,7 +109,8 @@ function createRemoteComponent(lazyComponent, info) {
|
|
|
94
109
|
basename,
|
|
95
110
|
lazyComponent,
|
|
96
111
|
exportName,
|
|
97
|
-
props
|
|
112
|
+
props,
|
|
113
|
+
routerContextVal
|
|
98
114
|
});
|
|
99
115
|
const m2 = await lazyComponent();
|
|
100
116
|
const moduleName = m2 && m2[Symbol.for("mf_module_id")];
|
|
@@ -109,6 +125,7 @@ function createRemoteComponent(lazyComponent, info) {
|
|
|
109
125
|
RemoteApp,
|
|
110
126
|
{
|
|
111
127
|
name: moduleName,
|
|
128
|
+
dispathPopstate: enableDispathPopstate,
|
|
112
129
|
...info,
|
|
113
130
|
...props,
|
|
114
131
|
providerInfo: exportFn,
|
package/dist/index.es.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import React__default, { useContext, useMemo, useRef, useState, useEffect } from "react";
|
|
3
|
-
import
|
|
3
|
+
import * as ReactRouterDOM from "react-router-dom";
|
|
4
4
|
import { L as LoggerInstance, f, a as atLeastReact18, R as RouterContext } from "./context-CPtN38Ur.js";
|
|
5
5
|
import ReactDOM from "react-dom";
|
|
6
6
|
const RemoteApp = ({
|
|
@@ -8,23 +8,26 @@ const RemoteApp = ({
|
|
|
8
8
|
memoryRoute,
|
|
9
9
|
basename,
|
|
10
10
|
providerInfo,
|
|
11
|
+
dispathPopstate,
|
|
11
12
|
...resProps
|
|
12
13
|
}) => {
|
|
13
14
|
const rootRef = useRef(null);
|
|
14
15
|
const renderDom = useRef(null);
|
|
15
|
-
const location = useLocation();
|
|
16
|
-
const [pathname, setPathname] = useState(location.pathname);
|
|
17
16
|
const providerInfoRef = useRef(null);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
17
|
+
if (dispathPopstate) {
|
|
18
|
+
const location = ReactRouterDOM.useLocation();
|
|
19
|
+
const [pathname, setPathname] = useState(location.pathname);
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
if (pathname !== "" && pathname !== location.pathname) {
|
|
22
|
+
LoggerInstance.log(`createRemoteComponent dispatchPopstateEnv >>>`, {
|
|
23
|
+
name,
|
|
24
|
+
pathname: location.pathname
|
|
25
|
+
});
|
|
26
|
+
f();
|
|
27
|
+
}
|
|
28
|
+
setPathname(location.pathname);
|
|
29
|
+
}, [location]);
|
|
30
|
+
}
|
|
28
31
|
useEffect(() => {
|
|
29
32
|
const renderTimeout = setTimeout(() => {
|
|
30
33
|
const providerReturn = providerInfo();
|
|
@@ -65,10 +68,21 @@ RemoteApp["__APP_VERSION__"] = "0.0.1";
|
|
|
65
68
|
function createRemoteComponent(lazyComponent, info) {
|
|
66
69
|
return (props) => {
|
|
67
70
|
const exportName = (info == null ? void 0 : info.export) || "default";
|
|
68
|
-
const routerContextVal = useContext(UNSAFE_RouteContext);
|
|
69
71
|
let basename = "/";
|
|
70
|
-
|
|
71
|
-
|
|
72
|
+
let enableDispathPopstate = false;
|
|
73
|
+
let routerContextVal;
|
|
74
|
+
try {
|
|
75
|
+
ReactRouterDOM.useLocation();
|
|
76
|
+
enableDispathPopstate = true;
|
|
77
|
+
} catch {
|
|
78
|
+
enableDispathPopstate = false;
|
|
79
|
+
}
|
|
80
|
+
if (ReactRouterDOM.UNSAFE_RouteContext && enableDispathPopstate) {
|
|
81
|
+
routerContextVal = useContext(ReactRouterDOM.UNSAFE_RouteContext);
|
|
82
|
+
if (routerContextVal && routerContextVal.matches && routerContextVal.matches[0] && routerContextVal.matches[0].pathnameBase) {
|
|
83
|
+
basename = routerContextVal.matches[0].pathnameBase;
|
|
84
|
+
}
|
|
85
|
+
enableDispathPopstate = (routerContextVal == null ? void 0 : routerContextVal.matches) && (routerContextVal == null ? void 0 : routerContextVal.matches.length) > 0;
|
|
72
86
|
}
|
|
73
87
|
const LazyComponent = useMemo(() => {
|
|
74
88
|
return React__default.lazy(async () => {
|
|
@@ -76,7 +90,8 @@ function createRemoteComponent(lazyComponent, info) {
|
|
|
76
90
|
basename,
|
|
77
91
|
lazyComponent,
|
|
78
92
|
exportName,
|
|
79
|
-
props
|
|
93
|
+
props,
|
|
94
|
+
routerContextVal
|
|
80
95
|
});
|
|
81
96
|
const m2 = await lazyComponent();
|
|
82
97
|
const moduleName = m2 && m2[Symbol.for("mf_module_id")];
|
|
@@ -91,6 +106,7 @@ function createRemoteComponent(lazyComponent, info) {
|
|
|
91
106
|
RemoteApp,
|
|
92
107
|
{
|
|
93
108
|
name: moduleName,
|
|
109
|
+
dispathPopstate: enableDispathPopstate,
|
|
94
110
|
...info,
|
|
95
111
|
...props,
|
|
96
112
|
providerInfo: exportFn,
|
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-20240620034636",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -25,7 +25,7 @@
|
|
|
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-20240620034636"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"react": ">=16.9.0",
|
package/src/create.tsx
CHANGED
|
@@ -6,7 +6,7 @@ import React, {
|
|
|
6
6
|
useRef,
|
|
7
7
|
useState,
|
|
8
8
|
} from 'react';
|
|
9
|
-
import
|
|
9
|
+
import * as ReactRouterDOM from 'react-router-dom';
|
|
10
10
|
import type { ProviderParams } from '@module-federation/bridge-shared';
|
|
11
11
|
import { LoggerInstance } from './utils';
|
|
12
12
|
import { dispatchPopstateEnv } from '@module-federation/bridge-shared';
|
|
@@ -31,6 +31,7 @@ interface RemoteModule {
|
|
|
31
31
|
interface RemoteAppParams {
|
|
32
32
|
name: string;
|
|
33
33
|
providerInfo: NonNullable<RemoteModule['provider']>;
|
|
34
|
+
dispathPopstate: boolean;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
const RemoteApp = ({
|
|
@@ -38,24 +39,27 @@ const RemoteApp = ({
|
|
|
38
39
|
memoryRoute,
|
|
39
40
|
basename,
|
|
40
41
|
providerInfo,
|
|
42
|
+
dispathPopstate,
|
|
41
43
|
...resProps
|
|
42
44
|
}: RemoteAppParams & ProviderParams) => {
|
|
43
45
|
const rootRef = useRef(null);
|
|
44
46
|
const renderDom = useRef(null);
|
|
45
|
-
const location = useLocation();
|
|
46
|
-
const [pathname, setPathname] = useState(location.pathname);
|
|
47
47
|
const providerInfoRef = useRef<any>(null);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
48
|
+
if (dispathPopstate) {
|
|
49
|
+
const location = ReactRouterDOM.useLocation();
|
|
50
|
+
const [pathname, setPathname] = useState(location.pathname);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (pathname !== '' && pathname !== location.pathname) {
|
|
54
|
+
LoggerInstance.log(`createRemoteComponent dispatchPopstateEnv >>>`, {
|
|
55
|
+
name,
|
|
56
|
+
pathname: location.pathname,
|
|
57
|
+
});
|
|
58
|
+
dispatchPopstateEnv();
|
|
59
|
+
}
|
|
60
|
+
setPathname(location.pathname);
|
|
61
|
+
}, [location]);
|
|
62
|
+
}
|
|
59
63
|
|
|
60
64
|
useEffect(() => {
|
|
61
65
|
const renderTimeout = setTimeout(() => {
|
|
@@ -121,13 +125,28 @@ export function createRemoteComponent<T, E extends keyof T>(
|
|
|
121
125
|
} & RawComponentType,
|
|
122
126
|
) => {
|
|
123
127
|
const exportName = info?.export || 'default';
|
|
124
|
-
const routerContextVal = useContext(UNSAFE_RouteContext);
|
|
125
128
|
let basename = '/';
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
129
|
+
let enableDispathPopstate = false;
|
|
130
|
+
let routerContextVal: any;
|
|
131
|
+
try {
|
|
132
|
+
ReactRouterDOM.useLocation();
|
|
133
|
+
enableDispathPopstate = true;
|
|
134
|
+
} catch {
|
|
135
|
+
enableDispathPopstate = false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (ReactRouterDOM.UNSAFE_RouteContext && enableDispathPopstate) {
|
|
139
|
+
routerContextVal = useContext(ReactRouterDOM.UNSAFE_RouteContext);
|
|
140
|
+
if (
|
|
141
|
+
routerContextVal &&
|
|
142
|
+
routerContextVal.matches &&
|
|
143
|
+
routerContextVal.matches[0] &&
|
|
144
|
+
routerContextVal.matches[0].pathnameBase
|
|
145
|
+
) {
|
|
146
|
+
basename = routerContextVal.matches[0].pathnameBase;
|
|
147
|
+
}
|
|
148
|
+
enableDispathPopstate =
|
|
149
|
+
routerContextVal?.matches && routerContextVal?.matches.length > 0;
|
|
131
150
|
}
|
|
132
151
|
|
|
133
152
|
const LazyComponent = useMemo(() => {
|
|
@@ -138,6 +157,7 @@ export function createRemoteComponent<T, E extends keyof T>(
|
|
|
138
157
|
lazyComponent,
|
|
139
158
|
exportName,
|
|
140
159
|
props,
|
|
160
|
+
routerContextVal,
|
|
141
161
|
});
|
|
142
162
|
const m = (await lazyComponent()) as RemoteModule;
|
|
143
163
|
// @ts-ignore
|
|
@@ -155,6 +175,7 @@ export function createRemoteComponent<T, E extends keyof T>(
|
|
|
155
175
|
default: () => (
|
|
156
176
|
<RemoteApp
|
|
157
177
|
name={moduleName}
|
|
178
|
+
dispathPopstate={enableDispathPopstate}
|
|
158
179
|
{...info}
|
|
159
180
|
{...props}
|
|
160
181
|
providerInfo={exportFn}
|