@module-federation/bridge-react 0.0.0-next-20240814104132 → 0.0.0-next-20240815093707
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 -4
- package/__tests__/bridge.spec.tsx +3 -37
- package/dist/index.cjs.js +92 -127
- package/dist/index.d.ts +7 -10
- package/dist/index.es.js +94 -129
- package/dist/router.cjs.js +12 -8
- package/dist/router.es.js +12 -8
- package/package.json +2 -12
- package/src/create.tsx +19 -14
- package/src/provider.tsx +33 -50
- package/src/remote/index.tsx +56 -92
- package/src/router.tsx +10 -8
- package/vite.config.ts +0 -29
- package/dist/router-v5.cjs.js +0 -15
- package/dist/router-v5.d.ts +0 -11
- package/dist/router-v5.es.js +0 -8
- package/dist/router-v6.cjs.js +0 -15
- package/dist/router-v6.d.ts +0 -11
- package/dist/router-v6.es.js +0 -8
package/src/remote/index.tsx
CHANGED
|
@@ -1,20 +1,13 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
useContext,
|
|
3
|
-
useEffect,
|
|
4
|
-
useRef,
|
|
5
|
-
useState,
|
|
6
|
-
forwardRef,
|
|
7
|
-
} from 'react';
|
|
1
|
+
import React, { useContext, useEffect, useRef, useState } from 'react';
|
|
8
2
|
import * as ReactRouterDOM from 'react-router-dom';
|
|
9
3
|
import type { ProviderParams } from '@module-federation/bridge-shared';
|
|
10
4
|
import { LoggerInstance, pathJoin } from '../utils';
|
|
11
5
|
import { dispatchPopstateEnv } from '@module-federation/bridge-shared';
|
|
12
|
-
import { ErrorBoundaryPropsWithComponent } from 'react-error-boundary';
|
|
13
6
|
|
|
14
7
|
declare const __APP_VERSION__: string;
|
|
8
|
+
|
|
15
9
|
export interface RenderFnParams extends ProviderParams {
|
|
16
10
|
dom?: any;
|
|
17
|
-
fallback: ErrorBoundaryPropsWithComponent['FallbackComponent'];
|
|
18
11
|
}
|
|
19
12
|
|
|
20
13
|
interface RemoteModule {
|
|
@@ -29,96 +22,71 @@ interface RemoteModule {
|
|
|
29
22
|
}
|
|
30
23
|
|
|
31
24
|
interface RemoteAppParams {
|
|
32
|
-
|
|
25
|
+
name: string;
|
|
33
26
|
providerInfo: NonNullable<RemoteModule['provider']>;
|
|
34
27
|
exportName: string | number | symbol;
|
|
35
|
-
fallback: ErrorBoundaryPropsWithComponent['FallbackComponent'];
|
|
36
28
|
}
|
|
37
29
|
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
providerReturn.render(renderProps);
|
|
30
|
+
const RemoteApp = ({
|
|
31
|
+
name,
|
|
32
|
+
memoryRoute,
|
|
33
|
+
basename,
|
|
34
|
+
providerInfo,
|
|
35
|
+
...resProps
|
|
36
|
+
}: RemoteAppParams & ProviderParams) => {
|
|
37
|
+
const rootRef = useRef(null);
|
|
38
|
+
const renderDom = useRef(null);
|
|
39
|
+
const providerInfoRef = useRef<any>(null);
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
const renderTimeout = setTimeout(() => {
|
|
43
|
+
const providerReturn = providerInfo();
|
|
44
|
+
providerInfoRef.current = providerReturn;
|
|
45
|
+
const renderProps = {
|
|
46
|
+
name,
|
|
47
|
+
dom: rootRef.current,
|
|
48
|
+
basename,
|
|
49
|
+
memoryRoute,
|
|
50
|
+
...resProps,
|
|
51
|
+
};
|
|
52
|
+
renderDom.current = rootRef.current;
|
|
53
|
+
LoggerInstance.log(
|
|
54
|
+
`createRemoteComponent LazyComponent render >>>`,
|
|
55
|
+
renderProps,
|
|
56
|
+
);
|
|
57
|
+
providerReturn.render(renderProps);
|
|
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
|
+
});
|
|
71
|
+
}
|
|
81
72
|
});
|
|
73
|
+
};
|
|
74
|
+
}, []);
|
|
82
75
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (providerInfoRef.current?.destroy) {
|
|
87
|
-
LoggerInstance.log(
|
|
88
|
-
`createRemoteComponent LazyComponent destroy >>>`,
|
|
89
|
-
{ moduleName, basename, dom: renderDom.current },
|
|
90
|
-
);
|
|
91
|
-
providerInfoRef.current?.destroy({
|
|
92
|
-
dom: renderDom.current,
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
};
|
|
97
|
-
}, []);
|
|
98
|
-
|
|
99
|
-
return (
|
|
100
|
-
<div
|
|
101
|
-
className={props?.className}
|
|
102
|
-
style={props?.style}
|
|
103
|
-
ref={rootRef}
|
|
104
|
-
></div>
|
|
105
|
-
);
|
|
106
|
-
};
|
|
76
|
+
//@ts-ignore
|
|
77
|
+
return <div ref={rootRef}></div>;
|
|
78
|
+
};
|
|
107
79
|
|
|
108
|
-
|
|
109
|
-
return <RemoteApp />;
|
|
110
|
-
});
|
|
80
|
+
(RemoteApp as any)['__APP_VERSION__'] = __APP_VERSION__;
|
|
111
81
|
|
|
112
82
|
interface ExtraDataProps {
|
|
113
83
|
basename?: string;
|
|
114
84
|
}
|
|
115
85
|
|
|
116
|
-
export function withRouterData<
|
|
117
|
-
P extends Parameters<typeof RemoteAppWrapper>[0],
|
|
118
|
-
>(
|
|
86
|
+
export function withRouterData<P extends Parameters<typeof RemoteApp>[0]>(
|
|
119
87
|
WrappedComponent: React.ComponentType<P & ExtraDataProps>,
|
|
120
88
|
): React.FC<Omit<P, keyof ExtraDataProps>> {
|
|
121
|
-
|
|
89
|
+
return (props: any) => {
|
|
122
90
|
let enableDispathPopstate = false;
|
|
123
91
|
let routerContextVal: any;
|
|
124
92
|
try {
|
|
@@ -190,12 +158,8 @@ export function withRouterData<
|
|
|
190
158
|
}, [location]);
|
|
191
159
|
}
|
|
192
160
|
|
|
193
|
-
return <WrappedComponent {...(props as P)} basename={basename}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
return forwardRef(function (props, ref) {
|
|
197
|
-
return <Component {...props} ref={ref} />;
|
|
198
|
-
}) as any;
|
|
161
|
+
return <WrappedComponent {...(props as P)} basename={basename} />;
|
|
162
|
+
};
|
|
199
163
|
}
|
|
200
164
|
|
|
201
|
-
export default withRouterData(
|
|
165
|
+
export default withRouterData(RemoteApp);
|
package/src/router.tsx
CHANGED
|
@@ -4,7 +4,7 @@ import * as ReactRouterDom from 'react-router-dom/';
|
|
|
4
4
|
import { RouterContext } from './context';
|
|
5
5
|
import { LoggerInstance } from './utils';
|
|
6
6
|
|
|
7
|
-
function
|
|
7
|
+
function WraperRouter(
|
|
8
8
|
props:
|
|
9
9
|
| Parameters<typeof ReactRouterDom.BrowserRouter>[0]
|
|
10
10
|
| Parameters<typeof ReactRouterDom.MemoryRouter>[0],
|
|
@@ -12,11 +12,12 @@ function WrapperRouter(
|
|
|
12
12
|
const { basename, ...propsRes } = props;
|
|
13
13
|
const routerContextProps = useContext(RouterContext) || {};
|
|
14
14
|
|
|
15
|
-
LoggerInstance.log(`
|
|
15
|
+
LoggerInstance.log(`WraperRouter info >>>`, {
|
|
16
16
|
...routerContextProps,
|
|
17
17
|
routerContextProps,
|
|
18
|
-
|
|
18
|
+
WraperRouterProps: props,
|
|
19
19
|
});
|
|
20
|
+
if (!routerContextProps) return <ReactRouterDom.BrowserRouter {...props} />;
|
|
20
21
|
|
|
21
22
|
if (routerContextProps?.memoryRoute) {
|
|
22
23
|
return (
|
|
@@ -34,16 +35,16 @@ function WrapperRouter(
|
|
|
34
35
|
);
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
function
|
|
38
|
+
function WraperRouterProvider(
|
|
38
39
|
props: Parameters<typeof ReactRouterDom.RouterProvider>[0],
|
|
39
40
|
) {
|
|
40
41
|
const { router, ...propsRes } = props;
|
|
41
42
|
const routerContextProps = useContext(RouterContext) || {};
|
|
42
43
|
const routers = router.routes;
|
|
43
|
-
LoggerInstance.log(`
|
|
44
|
+
LoggerInstance.log(`WraperRouterProvider info >>>`, {
|
|
44
45
|
...routerContextProps,
|
|
45
46
|
routerContextProps,
|
|
46
|
-
|
|
47
|
+
WraperRouterProviderProps: props,
|
|
47
48
|
router,
|
|
48
49
|
});
|
|
49
50
|
const RouterProvider = (ReactRouterDom as any)['Router' + 'Provider'];
|
|
@@ -51,6 +52,7 @@ function WrapperRouterProvider(
|
|
|
51
52
|
const createBrowserRouter = (ReactRouterDom as any)[
|
|
52
53
|
'create' + 'BrowserRouter'
|
|
53
54
|
];
|
|
55
|
+
if (!routerContextProps) return <RouterProvider {...props} />;
|
|
54
56
|
|
|
55
57
|
if (routerContextProps.memoryRoute) {
|
|
56
58
|
const MemeoryRouterInstance = createMemoryRouter(routers, {
|
|
@@ -69,5 +71,5 @@ function WrapperRouterProvider(
|
|
|
69
71
|
|
|
70
72
|
export * from 'react-router-dom/';
|
|
71
73
|
|
|
72
|
-
export {
|
|
73
|
-
export {
|
|
74
|
+
export { WraperRouter as BrowserRouter };
|
|
75
|
+
export { WraperRouterProvider as RouterProvider };
|
package/vite.config.ts
CHANGED
|
@@ -22,8 +22,6 @@ export default defineConfig({
|
|
|
22
22
|
entry: {
|
|
23
23
|
index: path.resolve(__dirname, 'src/index.ts'),
|
|
24
24
|
router: path.resolve(__dirname, 'src/router.tsx'),
|
|
25
|
-
'router-v5': path.resolve(__dirname, 'src/router.tsx'),
|
|
26
|
-
'router-v6': path.resolve(__dirname, 'src/router.tsx'),
|
|
27
25
|
},
|
|
28
26
|
formats: ['cjs', 'es'],
|
|
29
27
|
fileName: (format, entryName) => `${entryName}.${format}.js`,
|
|
@@ -34,33 +32,6 @@ export default defineConfig({
|
|
|
34
32
|
'@remix-run/router',
|
|
35
33
|
'react-router',
|
|
36
34
|
'react-router-dom/',
|
|
37
|
-
'react-router-dom/index.js',
|
|
38
|
-
'react-router-dom/dist/index.js',
|
|
39
|
-
],
|
|
40
|
-
plugins: [
|
|
41
|
-
{
|
|
42
|
-
name: 'modify-output-plugin',
|
|
43
|
-
generateBundle(options, bundle) {
|
|
44
|
-
for (const fileName in bundle) {
|
|
45
|
-
const chunk = bundle[fileName];
|
|
46
|
-
if (fileName.includes('router-v6') && chunk.type === 'chunk') {
|
|
47
|
-
chunk.code = chunk.code.replace(
|
|
48
|
-
// Match 'react-router-dom/' followed by single quotes, double quotes, or backticks, replacing only 'react-router-dom/' to react-router-v6 dist file structure
|
|
49
|
-
/react-router-dom\/(?=[\'\"\`])/g,
|
|
50
|
-
'react-router-dom/dist/index.js',
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (fileName.includes('router-v5') && chunk.type === 'chunk') {
|
|
55
|
-
chunk.code = chunk.code.replace(
|
|
56
|
-
// Match 'react-router-dom/' followed by single quotes, double quotes, or backticks, replacing only 'react-router-dom/' to react-router-v5 dist file structure
|
|
57
|
-
/react-router-dom\/(?=[\'\"\`])/g,
|
|
58
|
-
'react-router-dom/index.js',
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
35
|
],
|
|
65
36
|
},
|
|
66
37
|
minify: false,
|
package/dist/router-v5.cjs.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
require("react");
|
|
4
|
-
const ReactRouterDom = require("react-router-dom/index.js");
|
|
5
|
-
require("./context--mtFt3tp.cjs");
|
|
6
|
-
const router = require("./router.cjs.js");
|
|
7
|
-
exports.BrowserRouter = router.BrowserRouter;
|
|
8
|
-
exports.RouterProvider = router.RouterProvider;
|
|
9
|
-
Object.keys(ReactRouterDom).forEach((k) => {
|
|
10
|
-
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k))
|
|
11
|
-
Object.defineProperty(exports, k, {
|
|
12
|
-
enumerable: true,
|
|
13
|
-
get: () => ReactRouterDom[k]
|
|
14
|
-
});
|
|
15
|
-
});
|
package/dist/router-v5.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { default as default_2 } from 'react';
|
|
2
|
-
import * as ReactRouterDom from 'react-router-dom/';
|
|
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/";
|
|
10
|
-
|
|
11
|
-
export { }
|
package/dist/router-v5.es.js
DELETED
package/dist/router-v6.cjs.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
require("react");
|
|
4
|
-
const ReactRouterDom = require("react-router-dom/dist/index.js");
|
|
5
|
-
require("./context--mtFt3tp.cjs");
|
|
6
|
-
const router = require("./router.cjs.js");
|
|
7
|
-
exports.BrowserRouter = router.BrowserRouter;
|
|
8
|
-
exports.RouterProvider = router.RouterProvider;
|
|
9
|
-
Object.keys(ReactRouterDom).forEach((k) => {
|
|
10
|
-
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k))
|
|
11
|
-
Object.defineProperty(exports, k, {
|
|
12
|
-
enumerable: true,
|
|
13
|
-
get: () => ReactRouterDom[k]
|
|
14
|
-
});
|
|
15
|
-
});
|
package/dist/router-v6.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { default as default_2 } from 'react';
|
|
2
|
-
import * as ReactRouterDom from 'react-router-dom/';
|
|
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/";
|
|
10
|
-
|
|
11
|
-
export { }
|