@module-federation/bridge-react 0.0.0-feat-support-bridge-react-router-v7-20251015102312
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 +670 -0
- package/LICENSE +21 -0
- package/README.md +131 -0
- package/__tests__/bridge.spec.tsx +160 -0
- package/__tests__/createLazyComponent.spec.tsx +209 -0
- package/__tests__/prefetch.spec.ts +156 -0
- package/__tests__/router.spec.tsx +82 -0
- package/__tests__/setupTests.ts +8 -0
- package/__tests__/util.ts +36 -0
- package/dist/bridge-base-29j7dZKh.mjs +201 -0
- package/dist/bridge-base-CTNdBioh.js +216 -0
- package/dist/data-fetch-server-middleware.cjs.js +163 -0
- package/dist/data-fetch-server-middleware.d.ts +15 -0
- package/dist/data-fetch-server-middleware.es.js +164 -0
- package/dist/data-fetch-utils.cjs.js +24 -0
- package/dist/data-fetch-utils.d.ts +81 -0
- package/dist/data-fetch-utils.es.js +26 -0
- package/dist/index-Dm-M9ouh.mjs +46 -0
- package/dist/index-DqCpgmgH.js +45 -0
- package/dist/index.cjs.js +293 -0
- package/dist/index.d.ts +274 -0
- package/dist/index.es.js +276 -0
- package/dist/index.esm-CzoIcLts.js +459 -0
- package/dist/index.esm-JLwyxgUK.mjs +460 -0
- package/dist/lazy-load-component-plugin-B4fdMQTL.js +521 -0
- package/dist/lazy-load-component-plugin-ijw36GA0.mjs +522 -0
- package/dist/lazy-load-component-plugin.cjs.js +6 -0
- package/dist/lazy-load-component-plugin.d.ts +16 -0
- package/dist/lazy-load-component-plugin.es.js +6 -0
- package/dist/lazy-utils.cjs.js +24 -0
- package/dist/lazy-utils.d.ts +149 -0
- package/dist/lazy-utils.es.js +24 -0
- package/dist/plugin.cjs.js +14 -0
- package/dist/plugin.d.ts +22 -0
- package/dist/plugin.es.js +14 -0
- package/dist/prefetch-Bgm31z9s.js +1272 -0
- package/dist/prefetch-DjeoxOeZ.mjs +1273 -0
- package/dist/router-v5.cjs.js +55 -0
- package/dist/router-v5.d.ts +18 -0
- package/dist/router-v5.es.js +32 -0
- package/dist/router-v6.cjs.js +84 -0
- package/dist/router-v6.d.ts +20 -0
- package/dist/router-v6.es.js +61 -0
- package/dist/router-v7.cjs.js +83 -0
- package/dist/router-v7.d.ts +20 -0
- package/dist/router-v7.es.js +61 -0
- package/dist/router.cjs.js +82 -0
- package/dist/router.d.ts +20 -0
- package/dist/router.es.js +60 -0
- package/dist/utils-0HFFqmd4.js +2015 -0
- package/dist/utils-BTpxHmva.mjs +2016 -0
- package/dist/v18.cjs.js +15 -0
- package/dist/v18.d.ts +114 -0
- package/dist/v18.es.js +15 -0
- package/dist/v19.cjs.js +15 -0
- package/dist/v19.d.ts +115 -0
- package/dist/v19.es.js +15 -0
- package/jest.config.ts +21 -0
- package/package.json +144 -0
- package/project.json +23 -0
- package/src/index.ts +45 -0
- package/src/lazy/AwaitDataFetch.tsx +215 -0
- package/src/lazy/constant.ts +30 -0
- package/src/lazy/createLazyComponent.tsx +411 -0
- package/src/lazy/data-fetch/cache.ts +291 -0
- package/src/lazy/data-fetch/call-data-fetch.ts +13 -0
- package/src/lazy/data-fetch/data-fetch-server-middleware.ts +196 -0
- package/src/lazy/data-fetch/index.ts +16 -0
- package/src/lazy/data-fetch/inject-data-fetch.ts +109 -0
- package/src/lazy/data-fetch/prefetch.ts +112 -0
- package/src/lazy/data-fetch/runtime-plugin.ts +115 -0
- package/src/lazy/index.ts +35 -0
- package/src/lazy/logger.ts +6 -0
- package/src/lazy/types.ts +75 -0
- package/src/lazy/utils.ts +372 -0
- package/src/lazy/wrapNoSSR.tsx +10 -0
- package/src/modern-app-env.d.ts +2 -0
- package/src/plugins/lazy-load-component-plugin.spec.ts +21 -0
- package/src/plugins/lazy-load-component-plugin.ts +57 -0
- package/src/provider/context.tsx +4 -0
- package/src/provider/plugin.ts +22 -0
- package/src/provider/versions/bridge-base.tsx +127 -0
- package/src/provider/versions/legacy.ts +87 -0
- package/src/provider/versions/v18.ts +47 -0
- package/src/provider/versions/v19.ts +48 -0
- package/src/remote/component.tsx +211 -0
- package/src/remote/create.tsx +103 -0
- package/src/router/default.tsx +73 -0
- package/src/router/v5.tsx +43 -0
- package/src/router/v6.tsx +74 -0
- package/src/router/v7.tsx +75 -0
- package/src/types.ts +147 -0
- package/src/utils/index.ts +44 -0
- package/src/v18.ts +9 -0
- package/src/v19.ts +9 -0
- package/tsconfig.json +42 -0
- package/tsconfig.node.json +11 -0
- package/tsconfig.spec.json +26 -0
- package/vite.config.ts +106 -0
- package/vitest.config.ts +27 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// Test file for router
|
|
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/default';
|
|
12
|
+
import { RouterContext } from '../src/provider/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={{ basename: '/test' } as any}>
|
|
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={{ basename: '/test' } as any}>
|
|
77
|
+
<RouterProvider router={router} />
|
|
78
|
+
</RouterContext.Provider>,
|
|
79
|
+
);
|
|
80
|
+
expect(getHtml(container)).toMatch('home page');
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// In vitest, you can use the setupFiles option in your configuration file to import any necessary setup files for your tests.
|
|
2
|
+
// For example, if you want to use testing-library's custom matchers, you can import them in a setup file like this:
|
|
3
|
+
import '@testing-library/jest-dom';
|
|
4
|
+
|
|
5
|
+
// Fix TextEncoder/TextDecoder not defined in Node.js
|
|
6
|
+
import { TextEncoder, TextDecoder } from 'util';
|
|
7
|
+
global.TextEncoder = TextEncoder;
|
|
8
|
+
global.TextDecoder = TextDecoder as any;
|
|
@@ -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,201 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { Component, createElement, createContext } from "react";
|
|
3
|
+
import { L as LoggerInstance, R as RouterContext } from "./index-Dm-M9ouh.mjs";
|
|
4
|
+
import { federationRuntime } from "./plugin.es.js";
|
|
5
|
+
const ErrorBoundaryContext = createContext(null);
|
|
6
|
+
const initialState = {
|
|
7
|
+
didCatch: false,
|
|
8
|
+
error: null
|
|
9
|
+
};
|
|
10
|
+
class ErrorBoundary extends Component {
|
|
11
|
+
constructor(props) {
|
|
12
|
+
super(props);
|
|
13
|
+
this.resetErrorBoundary = this.resetErrorBoundary.bind(this);
|
|
14
|
+
this.state = initialState;
|
|
15
|
+
}
|
|
16
|
+
static getDerivedStateFromError(error) {
|
|
17
|
+
return {
|
|
18
|
+
didCatch: true,
|
|
19
|
+
error
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
resetErrorBoundary() {
|
|
23
|
+
const {
|
|
24
|
+
error
|
|
25
|
+
} = this.state;
|
|
26
|
+
if (error !== null) {
|
|
27
|
+
var _this$props$onReset, _this$props;
|
|
28
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
29
|
+
args[_key] = arguments[_key];
|
|
30
|
+
}
|
|
31
|
+
(_this$props$onReset = (_this$props = this.props).onReset) === null || _this$props$onReset === void 0 ? void 0 : _this$props$onReset.call(_this$props, {
|
|
32
|
+
args,
|
|
33
|
+
reason: "imperative-api"
|
|
34
|
+
});
|
|
35
|
+
this.setState(initialState);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
componentDidCatch(error, info) {
|
|
39
|
+
var _this$props$onError, _this$props2;
|
|
40
|
+
(_this$props$onError = (_this$props2 = this.props).onError) === null || _this$props$onError === void 0 ? void 0 : _this$props$onError.call(_this$props2, error, info);
|
|
41
|
+
}
|
|
42
|
+
componentDidUpdate(prevProps, prevState) {
|
|
43
|
+
const {
|
|
44
|
+
didCatch
|
|
45
|
+
} = this.state;
|
|
46
|
+
const {
|
|
47
|
+
resetKeys
|
|
48
|
+
} = this.props;
|
|
49
|
+
if (didCatch && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) {
|
|
50
|
+
var _this$props$onReset2, _this$props3;
|
|
51
|
+
(_this$props$onReset2 = (_this$props3 = this.props).onReset) === null || _this$props$onReset2 === void 0 ? void 0 : _this$props$onReset2.call(_this$props3, {
|
|
52
|
+
next: resetKeys,
|
|
53
|
+
prev: prevProps.resetKeys,
|
|
54
|
+
reason: "keys"
|
|
55
|
+
});
|
|
56
|
+
this.setState(initialState);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
render() {
|
|
60
|
+
const {
|
|
61
|
+
children,
|
|
62
|
+
fallbackRender,
|
|
63
|
+
FallbackComponent,
|
|
64
|
+
fallback
|
|
65
|
+
} = this.props;
|
|
66
|
+
const {
|
|
67
|
+
didCatch,
|
|
68
|
+
error
|
|
69
|
+
} = this.state;
|
|
70
|
+
let childToRender = children;
|
|
71
|
+
if (didCatch) {
|
|
72
|
+
const props = {
|
|
73
|
+
error,
|
|
74
|
+
resetErrorBoundary: this.resetErrorBoundary
|
|
75
|
+
};
|
|
76
|
+
if (typeof fallbackRender === "function") {
|
|
77
|
+
childToRender = fallbackRender(props);
|
|
78
|
+
} else if (FallbackComponent) {
|
|
79
|
+
childToRender = createElement(FallbackComponent, props);
|
|
80
|
+
} else if (fallback !== void 0) {
|
|
81
|
+
childToRender = fallback;
|
|
82
|
+
} else {
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return createElement(ErrorBoundaryContext.Provider, {
|
|
87
|
+
value: {
|
|
88
|
+
didCatch,
|
|
89
|
+
error,
|
|
90
|
+
resetErrorBoundary: this.resetErrorBoundary
|
|
91
|
+
}
|
|
92
|
+
}, childToRender);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function hasArrayChanged() {
|
|
96
|
+
let a = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
|
|
97
|
+
let b = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
|
|
98
|
+
return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
|
|
99
|
+
}
|
|
100
|
+
function createBaseBridgeComponent({
|
|
101
|
+
createRoot,
|
|
102
|
+
defaultRootOptions,
|
|
103
|
+
...bridgeInfo
|
|
104
|
+
}) {
|
|
105
|
+
return () => {
|
|
106
|
+
const rootMap = /* @__PURE__ */ new Map();
|
|
107
|
+
const instance = federationRuntime.instance;
|
|
108
|
+
LoggerInstance.debug(
|
|
109
|
+
`createBridgeComponent instance from props >>>`,
|
|
110
|
+
instance
|
|
111
|
+
);
|
|
112
|
+
const RawComponent = (info) => {
|
|
113
|
+
const { appInfo, propsInfo, ...restProps } = info;
|
|
114
|
+
const { moduleName, memoryRoute, basename = "/" } = appInfo;
|
|
115
|
+
return /* @__PURE__ */ React.createElement(RouterContext.Provider, { value: { moduleName, basename, memoryRoute } }, /* @__PURE__ */ React.createElement(
|
|
116
|
+
bridgeInfo.rootComponent,
|
|
117
|
+
{
|
|
118
|
+
...propsInfo,
|
|
119
|
+
basename,
|
|
120
|
+
...restProps
|
|
121
|
+
}
|
|
122
|
+
));
|
|
123
|
+
};
|
|
124
|
+
return {
|
|
125
|
+
async render(info) {
|
|
126
|
+
var _a, _b, _c, _d, _e, _f;
|
|
127
|
+
LoggerInstance.debug(`createBridgeComponent render Info`, info);
|
|
128
|
+
const {
|
|
129
|
+
moduleName,
|
|
130
|
+
dom,
|
|
131
|
+
basename,
|
|
132
|
+
memoryRoute,
|
|
133
|
+
fallback,
|
|
134
|
+
rootOptions,
|
|
135
|
+
...propsInfo
|
|
136
|
+
} = info;
|
|
137
|
+
const mergedRootOptions = {
|
|
138
|
+
...defaultRootOptions,
|
|
139
|
+
...rootOptions
|
|
140
|
+
};
|
|
141
|
+
const beforeBridgeRenderRes = ((_c = (_b = (_a = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _a.lifecycle) == null ? void 0 : _b.beforeBridgeRender) == null ? void 0 : _c.emit(info)) || {};
|
|
142
|
+
const BridgeWrapper = ({ basename: basename2 }) => /* @__PURE__ */ React.createElement(
|
|
143
|
+
ErrorBoundary,
|
|
144
|
+
{
|
|
145
|
+
FallbackComponent: fallback
|
|
146
|
+
},
|
|
147
|
+
/* @__PURE__ */ React.createElement(
|
|
148
|
+
RawComponent,
|
|
149
|
+
{
|
|
150
|
+
appInfo: {
|
|
151
|
+
moduleName,
|
|
152
|
+
basename: basename2,
|
|
153
|
+
memoryRoute
|
|
154
|
+
},
|
|
155
|
+
propsInfo: {
|
|
156
|
+
...propsInfo,
|
|
157
|
+
basename: basename2,
|
|
158
|
+
...beforeBridgeRenderRes == null ? void 0 : beforeBridgeRenderRes.extraProps
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
)
|
|
162
|
+
);
|
|
163
|
+
const rootComponentWithErrorBoundary = /* @__PURE__ */ React.createElement(BridgeWrapper, { basename });
|
|
164
|
+
if (bridgeInfo.render) {
|
|
165
|
+
await Promise.resolve(
|
|
166
|
+
bridgeInfo.render(rootComponentWithErrorBoundary, dom)
|
|
167
|
+
).then((root) => rootMap.set(dom, root));
|
|
168
|
+
} else {
|
|
169
|
+
let root = rootMap.get(dom);
|
|
170
|
+
if (!root && createRoot) {
|
|
171
|
+
root = createRoot(dom, mergedRootOptions);
|
|
172
|
+
rootMap.set(dom, root);
|
|
173
|
+
}
|
|
174
|
+
if (root && "render" in root) {
|
|
175
|
+
root.render(rootComponentWithErrorBoundary);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
((_f = (_e = (_d = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _d.lifecycle) == null ? void 0 : _e.afterBridgeRender) == null ? void 0 : _f.emit(info)) || {};
|
|
179
|
+
},
|
|
180
|
+
destroy(info) {
|
|
181
|
+
var _a, _b, _c;
|
|
182
|
+
const { dom } = info;
|
|
183
|
+
LoggerInstance.debug(`createBridgeComponent destroy Info`, info);
|
|
184
|
+
const root = rootMap.get(dom);
|
|
185
|
+
if (root) {
|
|
186
|
+
if ("unmount" in root) {
|
|
187
|
+
root.unmount();
|
|
188
|
+
} else {
|
|
189
|
+
LoggerInstance.warn("Root does not have unmount method");
|
|
190
|
+
}
|
|
191
|
+
rootMap.delete(dom);
|
|
192
|
+
}
|
|
193
|
+
(_c = (_b = (_a = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _a.lifecycle) == null ? void 0 : _b.afterBridgeDestroy) == null ? void 0 : _c.emit(info);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
export {
|
|
199
|
+
ErrorBoundary as E,
|
|
200
|
+
createBaseBridgeComponent as c
|
|
201
|
+
};
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const React = require("react");
|
|
3
|
+
const index = require("./index-DqCpgmgH.js");
|
|
4
|
+
const plugin = require("./plugin.cjs.js");
|
|
5
|
+
function _interopNamespaceDefault(e) {
|
|
6
|
+
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
7
|
+
if (e) {
|
|
8
|
+
for (const k in e) {
|
|
9
|
+
if (k !== "default") {
|
|
10
|
+
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
11
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: () => e[k]
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
n.default = e;
|
|
19
|
+
return Object.freeze(n);
|
|
20
|
+
}
|
|
21
|
+
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
|
|
22
|
+
const ErrorBoundaryContext = React.createContext(null);
|
|
23
|
+
const initialState = {
|
|
24
|
+
didCatch: false,
|
|
25
|
+
error: null
|
|
26
|
+
};
|
|
27
|
+
class ErrorBoundary extends React.Component {
|
|
28
|
+
constructor(props) {
|
|
29
|
+
super(props);
|
|
30
|
+
this.resetErrorBoundary = this.resetErrorBoundary.bind(this);
|
|
31
|
+
this.state = initialState;
|
|
32
|
+
}
|
|
33
|
+
static getDerivedStateFromError(error) {
|
|
34
|
+
return {
|
|
35
|
+
didCatch: true,
|
|
36
|
+
error
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
resetErrorBoundary() {
|
|
40
|
+
const {
|
|
41
|
+
error
|
|
42
|
+
} = this.state;
|
|
43
|
+
if (error !== null) {
|
|
44
|
+
var _this$props$onReset, _this$props;
|
|
45
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
46
|
+
args[_key] = arguments[_key];
|
|
47
|
+
}
|
|
48
|
+
(_this$props$onReset = (_this$props = this.props).onReset) === null || _this$props$onReset === void 0 ? void 0 : _this$props$onReset.call(_this$props, {
|
|
49
|
+
args,
|
|
50
|
+
reason: "imperative-api"
|
|
51
|
+
});
|
|
52
|
+
this.setState(initialState);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
componentDidCatch(error, info) {
|
|
56
|
+
var _this$props$onError, _this$props2;
|
|
57
|
+
(_this$props$onError = (_this$props2 = this.props).onError) === null || _this$props$onError === void 0 ? void 0 : _this$props$onError.call(_this$props2, error, info);
|
|
58
|
+
}
|
|
59
|
+
componentDidUpdate(prevProps, prevState) {
|
|
60
|
+
const {
|
|
61
|
+
didCatch
|
|
62
|
+
} = this.state;
|
|
63
|
+
const {
|
|
64
|
+
resetKeys
|
|
65
|
+
} = this.props;
|
|
66
|
+
if (didCatch && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) {
|
|
67
|
+
var _this$props$onReset2, _this$props3;
|
|
68
|
+
(_this$props$onReset2 = (_this$props3 = this.props).onReset) === null || _this$props$onReset2 === void 0 ? void 0 : _this$props$onReset2.call(_this$props3, {
|
|
69
|
+
next: resetKeys,
|
|
70
|
+
prev: prevProps.resetKeys,
|
|
71
|
+
reason: "keys"
|
|
72
|
+
});
|
|
73
|
+
this.setState(initialState);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
render() {
|
|
77
|
+
const {
|
|
78
|
+
children,
|
|
79
|
+
fallbackRender,
|
|
80
|
+
FallbackComponent,
|
|
81
|
+
fallback
|
|
82
|
+
} = this.props;
|
|
83
|
+
const {
|
|
84
|
+
didCatch,
|
|
85
|
+
error
|
|
86
|
+
} = this.state;
|
|
87
|
+
let childToRender = children;
|
|
88
|
+
if (didCatch) {
|
|
89
|
+
const props = {
|
|
90
|
+
error,
|
|
91
|
+
resetErrorBoundary: this.resetErrorBoundary
|
|
92
|
+
};
|
|
93
|
+
if (typeof fallbackRender === "function") {
|
|
94
|
+
childToRender = fallbackRender(props);
|
|
95
|
+
} else if (FallbackComponent) {
|
|
96
|
+
childToRender = React.createElement(FallbackComponent, props);
|
|
97
|
+
} else if (fallback !== void 0) {
|
|
98
|
+
childToRender = fallback;
|
|
99
|
+
} else {
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return React.createElement(ErrorBoundaryContext.Provider, {
|
|
104
|
+
value: {
|
|
105
|
+
didCatch,
|
|
106
|
+
error,
|
|
107
|
+
resetErrorBoundary: this.resetErrorBoundary
|
|
108
|
+
}
|
|
109
|
+
}, childToRender);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function hasArrayChanged() {
|
|
113
|
+
let a = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
|
|
114
|
+
let b = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
|
|
115
|
+
return a.length !== b.length || a.some((item, index2) => !Object.is(item, b[index2]));
|
|
116
|
+
}
|
|
117
|
+
function createBaseBridgeComponent({
|
|
118
|
+
createRoot,
|
|
119
|
+
defaultRootOptions,
|
|
120
|
+
...bridgeInfo
|
|
121
|
+
}) {
|
|
122
|
+
return () => {
|
|
123
|
+
const rootMap = /* @__PURE__ */ new Map();
|
|
124
|
+
const instance = plugin.federationRuntime.instance;
|
|
125
|
+
index.LoggerInstance.debug(
|
|
126
|
+
`createBridgeComponent instance from props >>>`,
|
|
127
|
+
instance
|
|
128
|
+
);
|
|
129
|
+
const RawComponent = (info) => {
|
|
130
|
+
const { appInfo, propsInfo, ...restProps } = info;
|
|
131
|
+
const { moduleName, memoryRoute, basename = "/" } = appInfo;
|
|
132
|
+
return /* @__PURE__ */ React__namespace.createElement(index.RouterContext.Provider, { value: { moduleName, basename, memoryRoute } }, /* @__PURE__ */ React__namespace.createElement(
|
|
133
|
+
bridgeInfo.rootComponent,
|
|
134
|
+
{
|
|
135
|
+
...propsInfo,
|
|
136
|
+
basename,
|
|
137
|
+
...restProps
|
|
138
|
+
}
|
|
139
|
+
));
|
|
140
|
+
};
|
|
141
|
+
return {
|
|
142
|
+
async render(info) {
|
|
143
|
+
var _a, _b, _c, _d, _e, _f;
|
|
144
|
+
index.LoggerInstance.debug(`createBridgeComponent render Info`, info);
|
|
145
|
+
const {
|
|
146
|
+
moduleName,
|
|
147
|
+
dom,
|
|
148
|
+
basename,
|
|
149
|
+
memoryRoute,
|
|
150
|
+
fallback,
|
|
151
|
+
rootOptions,
|
|
152
|
+
...propsInfo
|
|
153
|
+
} = info;
|
|
154
|
+
const mergedRootOptions = {
|
|
155
|
+
...defaultRootOptions,
|
|
156
|
+
...rootOptions
|
|
157
|
+
};
|
|
158
|
+
const beforeBridgeRenderRes = ((_c = (_b = (_a = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _a.lifecycle) == null ? void 0 : _b.beforeBridgeRender) == null ? void 0 : _c.emit(info)) || {};
|
|
159
|
+
const BridgeWrapper = ({ basename: basename2 }) => /* @__PURE__ */ React__namespace.createElement(
|
|
160
|
+
ErrorBoundary,
|
|
161
|
+
{
|
|
162
|
+
FallbackComponent: fallback
|
|
163
|
+
},
|
|
164
|
+
/* @__PURE__ */ React__namespace.createElement(
|
|
165
|
+
RawComponent,
|
|
166
|
+
{
|
|
167
|
+
appInfo: {
|
|
168
|
+
moduleName,
|
|
169
|
+
basename: basename2,
|
|
170
|
+
memoryRoute
|
|
171
|
+
},
|
|
172
|
+
propsInfo: {
|
|
173
|
+
...propsInfo,
|
|
174
|
+
basename: basename2,
|
|
175
|
+
...beforeBridgeRenderRes == null ? void 0 : beforeBridgeRenderRes.extraProps
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
)
|
|
179
|
+
);
|
|
180
|
+
const rootComponentWithErrorBoundary = /* @__PURE__ */ React__namespace.createElement(BridgeWrapper, { basename });
|
|
181
|
+
if (bridgeInfo.render) {
|
|
182
|
+
await Promise.resolve(
|
|
183
|
+
bridgeInfo.render(rootComponentWithErrorBoundary, dom)
|
|
184
|
+
).then((root) => rootMap.set(dom, root));
|
|
185
|
+
} else {
|
|
186
|
+
let root = rootMap.get(dom);
|
|
187
|
+
if (!root && createRoot) {
|
|
188
|
+
root = createRoot(dom, mergedRootOptions);
|
|
189
|
+
rootMap.set(dom, root);
|
|
190
|
+
}
|
|
191
|
+
if (root && "render" in root) {
|
|
192
|
+
root.render(rootComponentWithErrorBoundary);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
((_f = (_e = (_d = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _d.lifecycle) == null ? void 0 : _e.afterBridgeRender) == null ? void 0 : _f.emit(info)) || {};
|
|
196
|
+
},
|
|
197
|
+
destroy(info) {
|
|
198
|
+
var _a, _b, _c;
|
|
199
|
+
const { dom } = info;
|
|
200
|
+
index.LoggerInstance.debug(`createBridgeComponent destroy Info`, info);
|
|
201
|
+
const root = rootMap.get(dom);
|
|
202
|
+
if (root) {
|
|
203
|
+
if ("unmount" in root) {
|
|
204
|
+
root.unmount();
|
|
205
|
+
} else {
|
|
206
|
+
index.LoggerInstance.warn("Root does not have unmount method");
|
|
207
|
+
}
|
|
208
|
+
rootMap.delete(dom);
|
|
209
|
+
}
|
|
210
|
+
(_c = (_b = (_a = instance == null ? void 0 : instance.bridgeHook) == null ? void 0 : _a.lifecycle) == null ? void 0 : _b.afterBridgeDestroy) == null ? void 0 : _c.emit(info);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
exports.ErrorBoundary = ErrorBoundary;
|
|
216
|
+
exports.createBaseBridgeComponent = createBaseBridgeComponent;
|