@module-federation/bridge-react 0.0.0-next-20240619092013
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 +7 -0
- package/LICENSE +21 -0
- package/README.md +134 -0
- package/__tests__/bridge.spec.tsx +68 -0
- package/__tests__/router.spec.tsx +82 -0
- package/__tests__/util.ts +36 -0
- package/dist/context-C7FfFcIe.cjs +42 -0
- package/dist/context-CoFgcMIF.js +43 -0
- package/dist/index.cjs.js +175 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.es.js +175 -0
- package/dist/router.cjs.js +90 -0
- package/dist/router.d.ts +11 -0
- package/dist/router.es.js +67 -0
- package/package.json +55 -0
- package/project.json +27 -0
- package/src/.eslintrc.js +9 -0
- package/src/context.tsx +4 -0
- package/src/create.tsx +178 -0
- package/src/index.ts +6 -0
- package/src/modern-app-env.d.ts +2 -0
- package/src/provider.tsx +67 -0
- package/src/router.tsx +74 -0
- package/src/utils.ts +3 -0
- package/tsconfig.json +42 -0
- package/tsconfig.node.json +11 -0
- package/vite.config.ts +42 -0
- package/vitest.config.ts +22 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 ScriptedAlchemy LLC (Zack Jackson) Zhou Shaw (zhouxiao)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# React Bridge
|
|
2
|
+
|
|
3
|
+
React bridge is used to load the routing module in mf, so that the routing module can work properly with the host environment.
|
|
4
|
+
|
|
5
|
+
> When to use
|
|
6
|
+
|
|
7
|
+
* Load the route module
|
|
8
|
+
* Load across the front end framework
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
## How to use
|
|
12
|
+
|
|
13
|
+
# 1. Install the react bridge library
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @module-federation/bridge-react
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
# 2. Configure the react bridge library
|
|
20
|
+
|
|
21
|
+
> Use createBridgeComponent create component provider
|
|
22
|
+
|
|
23
|
+
```jsx
|
|
24
|
+
// ./src/index.tsx
|
|
25
|
+
import { createBridgeComponent } from '@module-federation/bridge-react';
|
|
26
|
+
|
|
27
|
+
function App() {
|
|
28
|
+
return ( <BrowserRouter basename="/">
|
|
29
|
+
<Routes>
|
|
30
|
+
<Route path="/" Component={()=> <div>Home page</div>}>
|
|
31
|
+
<Route path="/detail" Component={()=> <div>Detail page</div>}>
|
|
32
|
+
</Routes>
|
|
33
|
+
</BrowserRouter>)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default createBridgeComponent({
|
|
37
|
+
rootComponent: App
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
> set alias to proxy
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
//rsbuild.config.ts
|
|
45
|
+
export default defineConfig({
|
|
46
|
+
source: {
|
|
47
|
+
alias: {
|
|
48
|
+
'react-router-dom$': path.resolve(
|
|
49
|
+
__dirname,
|
|
50
|
+
'node_modules/@module-federation/bridge-react/dist/router.es.js',
|
|
51
|
+
),
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
server: {
|
|
55
|
+
port: 2001,
|
|
56
|
+
host: 'localhost',
|
|
57
|
+
},
|
|
58
|
+
dev: {
|
|
59
|
+
assetPrefix: 'http://localhost:2001',
|
|
60
|
+
},
|
|
61
|
+
tools: {
|
|
62
|
+
rspack: (config, { appendPlugins }) => {
|
|
63
|
+
delete config.optimization?.splitChunks;
|
|
64
|
+
config.output!.uniqueName = 'remote1';
|
|
65
|
+
appendPlugins([
|
|
66
|
+
new ModuleFederationPlugin({
|
|
67
|
+
name: 'remote1',
|
|
68
|
+
exposes: {
|
|
69
|
+
'./export-app': './src/index.tsx',
|
|
70
|
+
}
|
|
71
|
+
}),
|
|
72
|
+
]);
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
# 3. Load the module with routing
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
//rsbuild.config.ts
|
|
82
|
+
export default defineConfig({
|
|
83
|
+
tools: {
|
|
84
|
+
rspack: (config, { appendPlugins }) => {
|
|
85
|
+
config.output!.uniqueName = 'host';
|
|
86
|
+
appendPlugins([
|
|
87
|
+
new ModuleFederationPlugin({
|
|
88
|
+
name: 'host',
|
|
89
|
+
remotes: {
|
|
90
|
+
remote1: 'remote1@http://localhost:2001/mf-manifest.json',
|
|
91
|
+
},
|
|
92
|
+
}),
|
|
93
|
+
]);
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
> Use the module
|
|
100
|
+
|
|
101
|
+
```jsx
|
|
102
|
+
// ./src/index.tsx
|
|
103
|
+
import { createBridgeComponent } from '@module-federation/bridge-react';
|
|
104
|
+
|
|
105
|
+
const Remote1 = createBridgeComponent(()=> import('remote1/export-app'));
|
|
106
|
+
|
|
107
|
+
function App() {
|
|
108
|
+
return ( <BrowserRouter basename="/">
|
|
109
|
+
<ul>
|
|
110
|
+
<li>
|
|
111
|
+
<Link to="/">
|
|
112
|
+
Home
|
|
113
|
+
</Link>
|
|
114
|
+
</li>
|
|
115
|
+
<li>
|
|
116
|
+
<Link to="/remote1">
|
|
117
|
+
Remote1
|
|
118
|
+
</Link>
|
|
119
|
+
</li>
|
|
120
|
+
</ul>
|
|
121
|
+
<Routes>
|
|
122
|
+
<Route path="/" Component={()=> <div>Home page</div>}>
|
|
123
|
+
<Route path="/remote1" Component={()=> <Remote1 />}>
|
|
124
|
+
</Routes>
|
|
125
|
+
</BrowserRouter>)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const root = ReactDOM.createRoot(document.getElementById('root')!);
|
|
129
|
+
root.render(
|
|
130
|
+
<App />
|
|
131
|
+
);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { assert, describe, it } from 'vitest';
|
|
3
|
+
import { createBridgeComponent, createRemoteComponent } from '../src';
|
|
4
|
+
import {
|
|
5
|
+
act,
|
|
6
|
+
fireEvent,
|
|
7
|
+
render,
|
|
8
|
+
screen,
|
|
9
|
+
waitFor,
|
|
10
|
+
} from '@testing-library/react';
|
|
11
|
+
import { createContainer, getHtml, sleep } from './util';
|
|
12
|
+
|
|
13
|
+
describe('bridge', () => {
|
|
14
|
+
let containerInfo: ReturnType<typeof createContainer>;
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
containerInfo = createContainer();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
containerInfo?.clean();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('createBridgeComponent life cycle', async () => {
|
|
24
|
+
function Component() {
|
|
25
|
+
return <div>life cycle render</div>;
|
|
26
|
+
}
|
|
27
|
+
const lifeCycle = createBridgeComponent({
|
|
28
|
+
rootComponent: Component,
|
|
29
|
+
})();
|
|
30
|
+
|
|
31
|
+
lifeCycle.render({
|
|
32
|
+
dom: containerInfo?.container,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
await sleep(200);
|
|
36
|
+
expect(document.querySelector('#container')!.innerHTML).toContain(
|
|
37
|
+
'<div>life cycle render</div>',
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
lifeCycle.destroy({
|
|
41
|
+
dom: containerInfo?.container,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
expect(document.querySelector('#container')!.innerHTML).toContain('');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('createRemoteComponent', async () => {
|
|
48
|
+
function Component(info: { msg: string }) {
|
|
49
|
+
return <div>life cycle render {info.msg}</div>;
|
|
50
|
+
}
|
|
51
|
+
const BridgeComponent = createBridgeComponent({
|
|
52
|
+
rootComponent: Component,
|
|
53
|
+
});
|
|
54
|
+
const RemoteComponent = createRemoteComponent(async () => {
|
|
55
|
+
return {
|
|
56
|
+
default: BridgeComponent,
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const { container } = render(
|
|
61
|
+
<RemoteComponent fallback={<div>loading</div>} msg={'hello world'} />,
|
|
62
|
+
);
|
|
63
|
+
expect(getHtml(container)).toMatch('loading');
|
|
64
|
+
|
|
65
|
+
await sleep(200);
|
|
66
|
+
expect(getHtml(container)).toMatch('life cycle render hello world');
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { assert, describe, it } from 'vitest';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import {
|
|
5
|
+
Link,
|
|
6
|
+
Routes,
|
|
7
|
+
Route,
|
|
8
|
+
Outlet,
|
|
9
|
+
createBrowserRouter,
|
|
10
|
+
} from 'react-router-dom';
|
|
11
|
+
import { BrowserRouter, RouterProvider } from '../src/router';
|
|
12
|
+
import { RouterContext } from '../src/context';
|
|
13
|
+
import { getHtml, getWindowImpl } from './util';
|
|
14
|
+
|
|
15
|
+
describe('react router proxy', () => {
|
|
16
|
+
it('BrowserRouter not wraper context', async () => {
|
|
17
|
+
let { container } = render(
|
|
18
|
+
<RouterContext.Provider value={{ name: 'test', basename: '/test' }}>
|
|
19
|
+
<BrowserRouter basename="/" window={getWindowImpl('/test', false)}>
|
|
20
|
+
<ul>
|
|
21
|
+
<li>
|
|
22
|
+
<Link to="/">Home</Link>
|
|
23
|
+
</li>
|
|
24
|
+
<li>
|
|
25
|
+
<Link to="/detail">Detail</Link>
|
|
26
|
+
</li>
|
|
27
|
+
</ul>
|
|
28
|
+
<Routes>
|
|
29
|
+
<Route path="/" Component={() => <div>home page</div>} />
|
|
30
|
+
<Route path="/detail" Component={() => <div>detail page</div>} />
|
|
31
|
+
</Routes>
|
|
32
|
+
</BrowserRouter>
|
|
33
|
+
</RouterContext.Provider>,
|
|
34
|
+
);
|
|
35
|
+
expect(getHtml(container)).toMatch('home page');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('RouterProvider', async () => {
|
|
39
|
+
function Layout() {
|
|
40
|
+
return (
|
|
41
|
+
<>
|
|
42
|
+
<ul>
|
|
43
|
+
<li>
|
|
44
|
+
<Link to="/">Home</Link>
|
|
45
|
+
</li>
|
|
46
|
+
<li>
|
|
47
|
+
<Link to="/detail">Detail</Link>
|
|
48
|
+
</li>
|
|
49
|
+
</ul>
|
|
50
|
+
<Outlet />
|
|
51
|
+
</>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
const router = createBrowserRouter(
|
|
55
|
+
[
|
|
56
|
+
{
|
|
57
|
+
path: '/',
|
|
58
|
+
element: <Layout />,
|
|
59
|
+
children: [
|
|
60
|
+
{
|
|
61
|
+
index: true,
|
|
62
|
+
element: <div>home page</div>,
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
path: '/detail',
|
|
66
|
+
element: <div>detail page</div>,
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
{
|
|
72
|
+
window: getWindowImpl('/test', false),
|
|
73
|
+
},
|
|
74
|
+
);
|
|
75
|
+
let { container } = render(
|
|
76
|
+
<RouterContext.Provider value={{ name: 'test', basename: '/test' }}>
|
|
77
|
+
<RouterProvider router={router} />
|
|
78
|
+
</RouterContext.Provider>,
|
|
79
|
+
);
|
|
80
|
+
expect(getHtml(container)).toMatch('home page');
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { JSDOM } from 'jsdom';
|
|
2
|
+
import { prettyDOM } from '@testing-library/react';
|
|
3
|
+
|
|
4
|
+
export async function sleep(time: number) {
|
|
5
|
+
return new Promise((resolve) => {
|
|
6
|
+
setTimeout(() => {
|
|
7
|
+
resolve(null);
|
|
8
|
+
}, time);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function createContainer() {
|
|
13
|
+
const container = document.createElement('div');
|
|
14
|
+
container.setAttribute('id', 'container');
|
|
15
|
+
document.body.appendChild(container);
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
clean: () => {
|
|
19
|
+
document.body.removeChild(container);
|
|
20
|
+
},
|
|
21
|
+
container,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function getWindowImpl(initialUrl: string, isHash = false): Window {
|
|
26
|
+
// Need to use our own custom DOM in order to get a working history
|
|
27
|
+
const dom = new JSDOM(`<!DOCTYPE html>`, { url: 'http://localhost/' });
|
|
28
|
+
dom.window.history.replaceState(null, '', (isHash ? '#' : '') + initialUrl);
|
|
29
|
+
return dom.window as unknown as Window;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function getHtml(container: HTMLElement) {
|
|
33
|
+
return prettyDOM(container, undefined, {
|
|
34
|
+
highlight: false,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const React = require("react");
|
|
3
|
+
var a = Object.defineProperty;
|
|
4
|
+
var c = (s, e, t) => e in s ? a(s, e, { enumerable: true, configurable: true, writable: true, value: t }) : s[e] = t;
|
|
5
|
+
var i = (s, e, t) => (c(s, typeof e != "symbol" ? e + "" : e, t), t);
|
|
6
|
+
class g {
|
|
7
|
+
constructor(e) {
|
|
8
|
+
i(this, "name");
|
|
9
|
+
i(this, "isDebugEnabled");
|
|
10
|
+
i(this, "color");
|
|
11
|
+
this.name = e, this.isDebugEnabled = false, this.color = this.stringToColor(e), typeof window < "u" && typeof localStorage < "u" && (this.isDebugEnabled = localStorage.getItem("debug") === "true"), typeof process < "u" && process.env && (this.isDebugEnabled = process.env.DEBUG === "true");
|
|
12
|
+
}
|
|
13
|
+
log(...e) {
|
|
14
|
+
var t, n;
|
|
15
|
+
if (this.isDebugEnabled) {
|
|
16
|
+
const o = `color: ${this.color}; font-weight: bold`, l = `%c[${this.name}]`, r = ((n = (t = new Error().stack) == null ? void 0 : t.split(`
|
|
17
|
+
`)[2]) == null ? void 0 : n.trim()) || "Stack information not available";
|
|
18
|
+
typeof console < "u" && console.debug && console.debug(l, o, ...e, `
|
|
19
|
+
(${r})`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
stringToColor(e) {
|
|
23
|
+
let t = 0;
|
|
24
|
+
for (let o = 0; o < e.length; o++)
|
|
25
|
+
t = e.charCodeAt(o) + ((t << 5) - t);
|
|
26
|
+
let n = "#";
|
|
27
|
+
for (let o = 0; o < 3; o++) {
|
|
28
|
+
const l = t >> o * 8 & 255;
|
|
29
|
+
n += ("00" + l.toString(16)).substr(-2);
|
|
30
|
+
}
|
|
31
|
+
return n;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function f() {
|
|
35
|
+
const s = new PopStateEvent("popstate", { state: window.history.state });
|
|
36
|
+
window.dispatchEvent(s);
|
|
37
|
+
}
|
|
38
|
+
const LoggerInstance = new g("bridge-react");
|
|
39
|
+
const RouterContext = React.createContext(null);
|
|
40
|
+
exports.LoggerInstance = LoggerInstance;
|
|
41
|
+
exports.RouterContext = RouterContext;
|
|
42
|
+
exports.f = f;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
var a = Object.defineProperty;
|
|
3
|
+
var c = (s, e, t) => e in s ? a(s, e, { enumerable: true, configurable: true, writable: true, value: t }) : s[e] = t;
|
|
4
|
+
var i = (s, e, t) => (c(s, typeof e != "symbol" ? e + "" : e, t), t);
|
|
5
|
+
class g {
|
|
6
|
+
constructor(e) {
|
|
7
|
+
i(this, "name");
|
|
8
|
+
i(this, "isDebugEnabled");
|
|
9
|
+
i(this, "color");
|
|
10
|
+
this.name = e, this.isDebugEnabled = false, this.color = this.stringToColor(e), typeof window < "u" && typeof localStorage < "u" && (this.isDebugEnabled = localStorage.getItem("debug") === "true"), typeof process < "u" && process.env && (this.isDebugEnabled = process.env.DEBUG === "true");
|
|
11
|
+
}
|
|
12
|
+
log(...e) {
|
|
13
|
+
var t, n;
|
|
14
|
+
if (this.isDebugEnabled) {
|
|
15
|
+
const o = `color: ${this.color}; font-weight: bold`, l = `%c[${this.name}]`, r = ((n = (t = new Error().stack) == null ? void 0 : t.split(`
|
|
16
|
+
`)[2]) == null ? void 0 : n.trim()) || "Stack information not available";
|
|
17
|
+
typeof console < "u" && console.debug && console.debug(l, o, ...e, `
|
|
18
|
+
(${r})`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
stringToColor(e) {
|
|
22
|
+
let t = 0;
|
|
23
|
+
for (let o = 0; o < e.length; o++)
|
|
24
|
+
t = e.charCodeAt(o) + ((t << 5) - t);
|
|
25
|
+
let n = "#";
|
|
26
|
+
for (let o = 0; o < 3; o++) {
|
|
27
|
+
const l = t >> o * 8 & 255;
|
|
28
|
+
n += ("00" + l.toString(16)).substr(-2);
|
|
29
|
+
}
|
|
30
|
+
return n;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function f() {
|
|
34
|
+
const s = new PopStateEvent("popstate", { state: window.history.state });
|
|
35
|
+
window.dispatchEvent(s);
|
|
36
|
+
}
|
|
37
|
+
const LoggerInstance = new g("bridge-react");
|
|
38
|
+
const RouterContext = React.createContext(null);
|
|
39
|
+
export {
|
|
40
|
+
LoggerInstance as L,
|
|
41
|
+
RouterContext as R,
|
|
42
|
+
f
|
|
43
|
+
};
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const React = require("react");
|
|
4
|
+
const reactRouterDom = require("react-router-dom");
|
|
5
|
+
const context = require("./context-C7FfFcIe.cjs");
|
|
6
|
+
const require$$0 = require("react-dom");
|
|
7
|
+
const RemoteApp = ({
|
|
8
|
+
name,
|
|
9
|
+
memoryRoute,
|
|
10
|
+
basename,
|
|
11
|
+
providerInfo,
|
|
12
|
+
...resProps
|
|
13
|
+
}) => {
|
|
14
|
+
const rootRef = React.useRef(null);
|
|
15
|
+
const renderDom = React.useRef(null);
|
|
16
|
+
const location = reactRouterDom.useLocation();
|
|
17
|
+
const [pathname, setPathname] = React.useState(location.pathname);
|
|
18
|
+
const providerInfoRef = React.useRef(null);
|
|
19
|
+
React.useEffect(() => {
|
|
20
|
+
if (pathname !== "" && pathname !== location.pathname) {
|
|
21
|
+
context.LoggerInstance.log(`createRemoteComponent dispatchPopstateEnv >>>`, {
|
|
22
|
+
name,
|
|
23
|
+
pathname: location.pathname
|
|
24
|
+
});
|
|
25
|
+
context.f();
|
|
26
|
+
}
|
|
27
|
+
setPathname(location.pathname);
|
|
28
|
+
}, [location]);
|
|
29
|
+
React.useEffect(() => {
|
|
30
|
+
const renderTimeout = setTimeout(() => {
|
|
31
|
+
const providerReturn = providerInfo();
|
|
32
|
+
providerInfoRef.current = providerReturn;
|
|
33
|
+
const renderProps = {
|
|
34
|
+
name,
|
|
35
|
+
dom: rootRef.current,
|
|
36
|
+
basename,
|
|
37
|
+
memoryRoute,
|
|
38
|
+
...resProps
|
|
39
|
+
};
|
|
40
|
+
renderDom.current = rootRef.current;
|
|
41
|
+
context.LoggerInstance.log(
|
|
42
|
+
`createRemoteComponent LazyComponent render >>>`,
|
|
43
|
+
renderProps
|
|
44
|
+
);
|
|
45
|
+
providerReturn.render(renderProps);
|
|
46
|
+
});
|
|
47
|
+
return () => {
|
|
48
|
+
clearTimeout(renderTimeout);
|
|
49
|
+
setTimeout(() => {
|
|
50
|
+
var _a, _b;
|
|
51
|
+
if ((_a = providerInfoRef.current) == null ? void 0 : _a.destroy) {
|
|
52
|
+
context.LoggerInstance.log(
|
|
53
|
+
`createRemoteComponent LazyComponent destroy >>>`,
|
|
54
|
+
{ name, basename, dom: renderDom.current }
|
|
55
|
+
);
|
|
56
|
+
(_b = providerInfoRef.current) == null ? void 0 : _b.destroy({
|
|
57
|
+
dom: renderDom.current
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
}, []);
|
|
63
|
+
return /* @__PURE__ */ React.createElement("div", { ref: rootRef });
|
|
64
|
+
};
|
|
65
|
+
RemoteApp["__APP_VERSION__"] = "0.0.1";
|
|
66
|
+
function createRemoteComponent(lazyComponent, info) {
|
|
67
|
+
return (props) => {
|
|
68
|
+
const exportName = (info == null ? void 0 : info.export) || "default";
|
|
69
|
+
const routerContextVal = React.useContext(reactRouterDom.UNSAFE_RouteContext);
|
|
70
|
+
let basename = "/";
|
|
71
|
+
if (routerContextVal.matches[0] && routerContextVal.matches[0].pathnameBase) {
|
|
72
|
+
basename = routerContextVal.matches[0].pathnameBase;
|
|
73
|
+
}
|
|
74
|
+
const LazyComponent = React.useMemo(() => {
|
|
75
|
+
return React.lazy(async () => {
|
|
76
|
+
context.LoggerInstance.log(`createRemoteComponent LazyComponent create >>>`, {
|
|
77
|
+
basename,
|
|
78
|
+
lazyComponent,
|
|
79
|
+
exportName,
|
|
80
|
+
props
|
|
81
|
+
});
|
|
82
|
+
const m2 = await lazyComponent();
|
|
83
|
+
const moduleName = m2 && m2[Symbol.for("mf_module_id")];
|
|
84
|
+
context.LoggerInstance.log(
|
|
85
|
+
`createRemoteComponent LazyComponent loadRemote info >>>`,
|
|
86
|
+
{ basename, name: moduleName, module: m2, exportName, props }
|
|
87
|
+
);
|
|
88
|
+
const exportFn = m2[exportName];
|
|
89
|
+
if (exportName in m2 && typeof exportFn === "function") {
|
|
90
|
+
return {
|
|
91
|
+
default: () => /* @__PURE__ */ React.createElement(
|
|
92
|
+
RemoteApp,
|
|
93
|
+
{
|
|
94
|
+
name: moduleName,
|
|
95
|
+
...info,
|
|
96
|
+
...props,
|
|
97
|
+
providerInfo: exportFn,
|
|
98
|
+
basename
|
|
99
|
+
}
|
|
100
|
+
)
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
throw Error("module not found");
|
|
104
|
+
});
|
|
105
|
+
}, [exportName, basename, props.memoryRoute]);
|
|
106
|
+
return /* @__PURE__ */ React.createElement(React.Suspense, { fallback: props.fallback }, /* @__PURE__ */ React.createElement(LazyComponent, null));
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
var client = {};
|
|
110
|
+
var m = require$$0;
|
|
111
|
+
if (process.env.NODE_ENV === "production") {
|
|
112
|
+
client.createRoot = m.createRoot;
|
|
113
|
+
client.hydrateRoot = m.hydrateRoot;
|
|
114
|
+
} else {
|
|
115
|
+
var i = m.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
|
|
116
|
+
client.createRoot = function(c, o) {
|
|
117
|
+
i.usingClientEntryPoint = true;
|
|
118
|
+
try {
|
|
119
|
+
return m.createRoot(c, o);
|
|
120
|
+
} finally {
|
|
121
|
+
i.usingClientEntryPoint = false;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
client.hydrateRoot = function(c, h, o) {
|
|
125
|
+
i.usingClientEntryPoint = true;
|
|
126
|
+
try {
|
|
127
|
+
return m.hydrateRoot(c, h, o);
|
|
128
|
+
} finally {
|
|
129
|
+
i.usingClientEntryPoint = false;
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function createBridgeComponent(bridgeInfo) {
|
|
134
|
+
return () => {
|
|
135
|
+
const rootMap = /* @__PURE__ */ new Map();
|
|
136
|
+
const RawComponent = (info) => {
|
|
137
|
+
const { appInfo, propsInfo } = info;
|
|
138
|
+
const { name, memoryRoute, basename = "/" } = appInfo;
|
|
139
|
+
return /* @__PURE__ */ React.createElement(context.RouterContext.Provider, { value: { name, basename, memoryRoute } }, /* @__PURE__ */ React.createElement(bridgeInfo.rootComponent, { ...propsInfo, basename }));
|
|
140
|
+
};
|
|
141
|
+
return {
|
|
142
|
+
render(info) {
|
|
143
|
+
context.LoggerInstance.log(`createBridgeComponent render Info`, info);
|
|
144
|
+
const root = client.createRoot(info.dom);
|
|
145
|
+
rootMap.set(info.dom, root);
|
|
146
|
+
const { name, basename, memoryRoute, ...propsInfo } = info;
|
|
147
|
+
root.render(
|
|
148
|
+
/* @__PURE__ */ React.createElement(
|
|
149
|
+
RawComponent,
|
|
150
|
+
{
|
|
151
|
+
propsInfo,
|
|
152
|
+
appInfo: {
|
|
153
|
+
name,
|
|
154
|
+
basename,
|
|
155
|
+
memoryRoute
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
)
|
|
159
|
+
);
|
|
160
|
+
},
|
|
161
|
+
destroy(info) {
|
|
162
|
+
context.LoggerInstance.log(`createBridgeComponent destroy Info`, {
|
|
163
|
+
dom: info.dom
|
|
164
|
+
});
|
|
165
|
+
const root = rootMap.get(info.dom);
|
|
166
|
+
root == null ? void 0 : root.unmount();
|
|
167
|
+
},
|
|
168
|
+
rawComponent: bridgeInfo.rootComponent,
|
|
169
|
+
__BRIDGE_FN__: (_args) => {
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
exports.createBridgeComponent = createBridgeComponent;
|
|
175
|
+
exports.createRemoteComponent = createRemoteComponent;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { default as default_2 } from 'react';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
export declare function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>): () => {
|
|
5
|
+
render(info: RenderFnParams & any): void;
|
|
6
|
+
destroy(info: {
|
|
7
|
+
dom: HTMLElement;
|
|
8
|
+
}): void;
|
|
9
|
+
rawComponent: default_2.ComponentType<T>;
|
|
10
|
+
__BRIDGE_FN__: (_args: T) => void;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export declare function createRemoteComponent<T, E extends keyof T>(lazyComponent: () => Promise<T>, info?: {
|
|
14
|
+
export?: E;
|
|
15
|
+
}): (props: {
|
|
16
|
+
basename?: ProviderParams['basename'];
|
|
17
|
+
memoryRoute?: ProviderParams['memoryRoute'];
|
|
18
|
+
fallback: ReactNode;
|
|
19
|
+
} & ("__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
|
+
declare type ProviderFnParams<T> = {
|
|
22
|
+
rootComponent: default_2.ComponentType<T>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export declare interface ProviderParams {
|
|
26
|
+
name?: string;
|
|
27
|
+
basename?: string;
|
|
28
|
+
memoryRoute?: {
|
|
29
|
+
entryPath: string;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export declare interface RenderFnParams extends ProviderParams {
|
|
34
|
+
dom: HTMLElement;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { }
|