@module-federation/bridge-react 0.21.5 → 0.22.0
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 +14 -0
- package/dist/base.cjs.js +29 -0
- package/dist/base.d.ts +311 -0
- package/dist/base.es.js +30 -0
- package/dist/createHelpers-B_L612IN.js +190 -0
- package/dist/createHelpers-Ui5pt7je.mjs +191 -0
- package/dist/data-fetch-server-middleware.es.js +1 -1
- package/dist/data-fetch-utils.cjs.js +1 -1
- package/dist/data-fetch-utils.es.js +8 -8
- package/dist/index.cjs.js +10 -178
- package/dist/index.d.ts +5 -3
- package/dist/index.es.js +19 -186
- package/dist/{lazy-load-component-plugin-BbiXdUFQ.mjs → lazy-load-component-plugin-BkcO9pUC.mjs} +2 -2
- package/dist/{lazy-load-component-plugin-2wIGAon4.js → lazy-load-component-plugin-Bt990iJq.js} +1 -1
- package/dist/lazy-load-component-plugin.cjs.js +2 -2
- package/dist/lazy-load-component-plugin.es.js +2 -2
- package/dist/lazy-utils.es.js +3 -3
- package/dist/{prefetch-CBwY1k41.js → prefetch-D-d4LlJ3.js} +1 -1
- package/dist/{prefetch-yYPFtZpU.mjs → prefetch-DAwiqbcO.mjs} +2 -2
- package/dist/{utils-Bx_8GGd-.mjs → utils-dUgb9Jkm.mjs} +6 -6
- package/package.json +13 -5
- package/src/base.ts +50 -0
- package/src/index.ts +2 -2
- package/src/remote/RemoteAppWrapper.tsx +108 -0
- package/src/remote/base-component/component.tsx +2 -0
- package/src/remote/base-component/create.tsx +23 -0
- package/src/remote/base-component/index.tsx +10 -0
- package/src/remote/createHelpers.tsx +130 -0
- package/src/remote/{component.tsx → router-component/component.tsx} +3 -110
- package/src/remote/router-component/create.tsx +23 -0
- package/src/remote/router-component/index.tsx +10 -0
- package/vite.config.ts +1 -0
- package/src/remote/create.tsx +0 -106
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import React, { forwardRef } from 'react';
|
|
2
|
+
import { ErrorBoundary, FallbackProps } from 'react-error-boundary';
|
|
3
|
+
import { LoggerInstance } from '../utils';
|
|
4
|
+
import {
|
|
5
|
+
RemoteComponentParams,
|
|
6
|
+
RemoteComponentProps,
|
|
7
|
+
RemoteModule,
|
|
8
|
+
} from '../types';
|
|
9
|
+
|
|
10
|
+
export type LazyRemoteComponentInfo<
|
|
11
|
+
T,
|
|
12
|
+
_E extends keyof T,
|
|
13
|
+
> = RemoteComponentParams<T>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Creates a factory function for creating lazy remote components
|
|
17
|
+
* @param RemoteApp The RemoteAppWrapper component to use (with or without router)
|
|
18
|
+
*/
|
|
19
|
+
export function createLazyRemoteComponentFactory(
|
|
20
|
+
RemoteApp: React.ComponentType<any>,
|
|
21
|
+
) {
|
|
22
|
+
return function createLazyRemoteComponent<
|
|
23
|
+
T = Record<string, unknown>,
|
|
24
|
+
E extends keyof T = keyof T,
|
|
25
|
+
>(info: LazyRemoteComponentInfo<T, E>) {
|
|
26
|
+
const exportName = info?.export || 'default';
|
|
27
|
+
return React.lazy(async () => {
|
|
28
|
+
LoggerInstance.debug(
|
|
29
|
+
`createRemoteAppComponent LazyComponent create >>>`,
|
|
30
|
+
{
|
|
31
|
+
lazyComponent: info.loader,
|
|
32
|
+
exportName,
|
|
33
|
+
},
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
const m = (await info.loader()) as RemoteModule;
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
const moduleName = m && m[Symbol.for('mf_module_id')];
|
|
40
|
+
LoggerInstance.debug(
|
|
41
|
+
`createRemoteAppComponent LazyComponent loadRemote info >>>`,
|
|
42
|
+
{ name: moduleName, module: m, exportName },
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
const exportFn = m[exportName];
|
|
47
|
+
if (exportName in m && typeof exportFn === 'function') {
|
|
48
|
+
const RemoteAppComponent = forwardRef<
|
|
49
|
+
HTMLDivElement,
|
|
50
|
+
RemoteComponentProps
|
|
51
|
+
>((props, ref) => {
|
|
52
|
+
return (
|
|
53
|
+
<RemoteApp
|
|
54
|
+
// change `name` key to `moduleName` to avoid same property `name` passed by user's props which may cause unexpected issues.
|
|
55
|
+
moduleName={moduleName}
|
|
56
|
+
providerInfo={exportFn}
|
|
57
|
+
exportName={info.export || 'default'}
|
|
58
|
+
fallback={info.fallback}
|
|
59
|
+
loading={info.loading}
|
|
60
|
+
ref={ref}
|
|
61
|
+
{...props}
|
|
62
|
+
/>
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
default: RemoteAppComponent,
|
|
68
|
+
};
|
|
69
|
+
} else {
|
|
70
|
+
LoggerInstance.debug(
|
|
71
|
+
`createRemoteAppComponent LazyComponent module not found >>>`,
|
|
72
|
+
{ name: moduleName, module: m, exportName },
|
|
73
|
+
);
|
|
74
|
+
throw Error(
|
|
75
|
+
`Make sure that ${moduleName} has the correct export when export is ${String(
|
|
76
|
+
exportName,
|
|
77
|
+
)}`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
} catch (error) {
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Creates a factory function for creating remote app components
|
|
89
|
+
* @param RemoteApp The RemoteAppWrapper component to use (with or without router)
|
|
90
|
+
*/
|
|
91
|
+
export function createRemoteAppComponentFactory(
|
|
92
|
+
RemoteApp: React.ComponentType<any>,
|
|
93
|
+
) {
|
|
94
|
+
const createLazyRemoteComponent = createLazyRemoteComponentFactory(RemoteApp);
|
|
95
|
+
|
|
96
|
+
return function createRemoteAppComponent<
|
|
97
|
+
T = Record<string, unknown>,
|
|
98
|
+
E extends keyof T = keyof T,
|
|
99
|
+
>(info: LazyRemoteComponentInfo<T, E>) {
|
|
100
|
+
const LazyComponent = createLazyRemoteComponent(info);
|
|
101
|
+
return forwardRef<HTMLDivElement, RemoteComponentProps>((props, ref) => {
|
|
102
|
+
return (
|
|
103
|
+
<ErrorBoundary
|
|
104
|
+
FallbackComponent={
|
|
105
|
+
info.fallback as React.ComponentType<FallbackProps>
|
|
106
|
+
}
|
|
107
|
+
>
|
|
108
|
+
<React.Suspense fallback={info.loading}>
|
|
109
|
+
<LazyComponent {...props} ref={ref} />
|
|
110
|
+
</React.Suspense>
|
|
111
|
+
</ErrorBoundary>
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Creates the deprecated createRemoteComponent function
|
|
119
|
+
*/
|
|
120
|
+
export function createDeprecatedRemoteComponentFactory<
|
|
121
|
+
T = Record<string, unknown>,
|
|
122
|
+
E extends keyof T = keyof T,
|
|
123
|
+
>(createFn: (info: LazyRemoteComponentInfo<T, E>) => any) {
|
|
124
|
+
return function createRemoteComponent(info: LazyRemoteComponentInfo<T, E>) {
|
|
125
|
+
LoggerInstance.warn(
|
|
126
|
+
`createRemoteComponent is deprecated, please use createRemoteAppComponent instead!`,
|
|
127
|
+
);
|
|
128
|
+
return createFn(info);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
@@ -1,115 +1,8 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
useContext,
|
|
3
|
-
useEffect,
|
|
4
|
-
useRef,
|
|
5
|
-
useState,
|
|
6
|
-
forwardRef,
|
|
7
|
-
} from 'react';
|
|
1
|
+
import React, { useContext, useEffect, useState, forwardRef } from 'react';
|
|
8
2
|
import * as ReactRouterDOM from 'react-router-dom';
|
|
9
3
|
import { dispatchPopstateEnv } from '@module-federation/bridge-shared';
|
|
10
|
-
import { LoggerInstance, pathJoin
|
|
11
|
-
import {
|
|
12
|
-
import { RemoteComponentProps, RemoteAppParams } from '../types';
|
|
13
|
-
|
|
14
|
-
const RemoteAppWrapper = forwardRef(function (
|
|
15
|
-
props: RemoteAppParams & RemoteComponentProps,
|
|
16
|
-
ref,
|
|
17
|
-
) {
|
|
18
|
-
const {
|
|
19
|
-
moduleName,
|
|
20
|
-
memoryRoute,
|
|
21
|
-
basename,
|
|
22
|
-
providerInfo,
|
|
23
|
-
className,
|
|
24
|
-
style,
|
|
25
|
-
fallback,
|
|
26
|
-
loading,
|
|
27
|
-
...resProps
|
|
28
|
-
} = props;
|
|
29
|
-
|
|
30
|
-
const instance = federationRuntime.instance;
|
|
31
|
-
const rootRef: React.MutableRefObject<HTMLDivElement | null> =
|
|
32
|
-
ref && 'current' in ref
|
|
33
|
-
? (ref as React.MutableRefObject<HTMLDivElement | null>)
|
|
34
|
-
: useRef(null);
|
|
35
|
-
|
|
36
|
-
const renderDom: React.MutableRefObject<HTMLElement | null> = useRef(null);
|
|
37
|
-
const providerInfoRef = useRef<any>(null);
|
|
38
|
-
const [initialized, setInitialized] = useState(false);
|
|
39
|
-
|
|
40
|
-
LoggerInstance.debug(`RemoteAppWrapper instance from props >>>`, instance);
|
|
41
|
-
|
|
42
|
-
// 初始化远程组件
|
|
43
|
-
useEffect(() => {
|
|
44
|
-
if (initialized) return;
|
|
45
|
-
const providerReturn = providerInfo();
|
|
46
|
-
providerInfoRef.current = providerReturn;
|
|
47
|
-
setInitialized(true);
|
|
48
|
-
|
|
49
|
-
return () => {
|
|
50
|
-
if (providerInfoRef.current?.destroy) {
|
|
51
|
-
LoggerInstance.debug(
|
|
52
|
-
`createRemoteAppComponent LazyComponent destroy >>>`,
|
|
53
|
-
{ moduleName, basename, dom: renderDom.current },
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
instance?.bridgeHook?.lifecycle?.beforeBridgeDestroy?.emit({
|
|
57
|
-
moduleName,
|
|
58
|
-
dom: renderDom.current,
|
|
59
|
-
basename,
|
|
60
|
-
memoryRoute,
|
|
61
|
-
fallback,
|
|
62
|
-
...resProps,
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
providerInfoRef.current?.destroy({
|
|
66
|
-
moduleName,
|
|
67
|
-
dom: renderDom.current,
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
instance?.bridgeHook?.lifecycle?.afterBridgeDestroy?.emit({
|
|
71
|
-
moduleName,
|
|
72
|
-
dom: renderDom.current,
|
|
73
|
-
basename,
|
|
74
|
-
memoryRoute,
|
|
75
|
-
fallback,
|
|
76
|
-
...resProps,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
}, [moduleName]);
|
|
81
|
-
|
|
82
|
-
// trigger render after props updated
|
|
83
|
-
useEffect(() => {
|
|
84
|
-
if (!initialized || !providerInfoRef.current) return;
|
|
85
|
-
|
|
86
|
-
let renderProps = {
|
|
87
|
-
moduleName,
|
|
88
|
-
dom: rootRef.current,
|
|
89
|
-
basename,
|
|
90
|
-
memoryRoute,
|
|
91
|
-
fallback,
|
|
92
|
-
...resProps,
|
|
93
|
-
};
|
|
94
|
-
renderDom.current = rootRef.current;
|
|
95
|
-
|
|
96
|
-
const beforeBridgeRenderRes =
|
|
97
|
-
instance?.bridgeHook?.lifecycle?.beforeBridgeRender?.emit(renderProps) ||
|
|
98
|
-
{};
|
|
99
|
-
// @ts-ignore
|
|
100
|
-
renderProps = { ...renderProps, ...beforeBridgeRenderRes.extraProps };
|
|
101
|
-
providerInfoRef.current.render(renderProps);
|
|
102
|
-
instance?.bridgeHook?.lifecycle?.afterBridgeRender?.emit(renderProps);
|
|
103
|
-
}, [initialized, ...Object.values(props)]);
|
|
104
|
-
|
|
105
|
-
// bridge-remote-root
|
|
106
|
-
const rootComponentClassName = `${getRootDomDefaultClassName(moduleName)} ${className || ''}`;
|
|
107
|
-
return (
|
|
108
|
-
<div className={rootComponentClassName} style={style} ref={rootRef}>
|
|
109
|
-
{loading}
|
|
110
|
-
</div>
|
|
111
|
-
);
|
|
112
|
-
});
|
|
4
|
+
import { LoggerInstance, pathJoin } from '../../utils';
|
|
5
|
+
import { RemoteAppWrapper } from '../RemoteAppWrapper';
|
|
113
6
|
|
|
114
7
|
interface ExtraDataProps {
|
|
115
8
|
basename?: string;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import RemoteApp from './component';
|
|
2
|
+
import {
|
|
3
|
+
createLazyRemoteComponentFactory,
|
|
4
|
+
createRemoteAppComponentFactory,
|
|
5
|
+
createDeprecatedRemoteComponentFactory,
|
|
6
|
+
type LazyRemoteComponentInfo,
|
|
7
|
+
} from '../createHelpers';
|
|
8
|
+
|
|
9
|
+
export type { LazyRemoteComponentInfo };
|
|
10
|
+
|
|
11
|
+
const createLazyRemoteComponent = createLazyRemoteComponentFactory(RemoteApp);
|
|
12
|
+
|
|
13
|
+
export const createRemoteAppComponent =
|
|
14
|
+
createRemoteAppComponentFactory(RemoteApp);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated createRemoteComponent is deprecated, please use createRemoteAppComponent instead!
|
|
18
|
+
*/
|
|
19
|
+
export const createRemoteComponent = createDeprecatedRemoteComponentFactory(
|
|
20
|
+
createRemoteAppComponent,
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
export { createLazyRemoteComponent };
|
package/vite.config.ts
CHANGED
|
@@ -20,6 +20,7 @@ export default defineConfig({
|
|
|
20
20
|
lib: {
|
|
21
21
|
entry: {
|
|
22
22
|
index: path.resolve(__dirname, 'src/index.ts'),
|
|
23
|
+
base: path.resolve(__dirname, 'src/base.ts'),
|
|
23
24
|
plugin: path.resolve(__dirname, 'src/provider/plugin.ts'),
|
|
24
25
|
router: path.resolve(__dirname, 'src/router/default.tsx'),
|
|
25
26
|
'router-v5': path.resolve(__dirname, 'src/router/v5.tsx'),
|
package/src/remote/create.tsx
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import React, { forwardRef } from 'react';
|
|
2
|
-
import { ErrorBoundary, FallbackProps } from 'react-error-boundary';
|
|
3
|
-
import { LoggerInstance } from '../utils';
|
|
4
|
-
import RemoteApp from './component';
|
|
5
|
-
import {
|
|
6
|
-
RemoteComponentParams,
|
|
7
|
-
RemoteComponentProps,
|
|
8
|
-
RemoteModule,
|
|
9
|
-
} from '../types';
|
|
10
|
-
|
|
11
|
-
export type LazyRemoteComponentInfo<
|
|
12
|
-
T,
|
|
13
|
-
_E extends keyof T,
|
|
14
|
-
> = RemoteComponentParams<T>;
|
|
15
|
-
|
|
16
|
-
function createLazyRemoteComponent<
|
|
17
|
-
T = Record<string, unknown>,
|
|
18
|
-
E extends keyof T = keyof T,
|
|
19
|
-
>(info: LazyRemoteComponentInfo<T, E>) {
|
|
20
|
-
const exportName = info?.export || 'default';
|
|
21
|
-
return React.lazy(async () => {
|
|
22
|
-
LoggerInstance.debug(`createRemoteAppComponent LazyComponent create >>>`, {
|
|
23
|
-
lazyComponent: info.loader,
|
|
24
|
-
exportName,
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
try {
|
|
28
|
-
const m = (await info.loader()) as RemoteModule;
|
|
29
|
-
// @ts-ignore
|
|
30
|
-
const moduleName = m && m[Symbol.for('mf_module_id')];
|
|
31
|
-
LoggerInstance.debug(
|
|
32
|
-
`createRemoteAppComponent LazyComponent loadRemote info >>>`,
|
|
33
|
-
{ name: moduleName, module: m, exportName },
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
// @ts-ignore
|
|
37
|
-
const exportFn = m[exportName];
|
|
38
|
-
if (exportName in m && typeof exportFn === 'function') {
|
|
39
|
-
const RemoteAppComponent = forwardRef<
|
|
40
|
-
HTMLDivElement,
|
|
41
|
-
RemoteComponentProps
|
|
42
|
-
>((props, ref) => {
|
|
43
|
-
return (
|
|
44
|
-
<RemoteApp
|
|
45
|
-
// change `name` key to `moduleName` to avoid same property `name` passed by user's props which may cause unexpected issues.
|
|
46
|
-
moduleName={moduleName}
|
|
47
|
-
providerInfo={exportFn}
|
|
48
|
-
exportName={info.export || 'default'}
|
|
49
|
-
fallback={info.fallback}
|
|
50
|
-
loading={info.loading}
|
|
51
|
-
ref={ref}
|
|
52
|
-
{...props}
|
|
53
|
-
/>
|
|
54
|
-
);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
return {
|
|
58
|
-
default: RemoteAppComponent,
|
|
59
|
-
};
|
|
60
|
-
} else {
|
|
61
|
-
LoggerInstance.debug(
|
|
62
|
-
`createRemoteAppComponent LazyComponent module not found >>>`,
|
|
63
|
-
{ name: moduleName, module: m, exportName },
|
|
64
|
-
);
|
|
65
|
-
throw Error(
|
|
66
|
-
`Make sure that ${moduleName} has the correct export when export is ${String(
|
|
67
|
-
exportName,
|
|
68
|
-
)}`,
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
} catch (error) {
|
|
72
|
-
throw error;
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export function createRemoteAppComponent<
|
|
78
|
-
T = Record<string, unknown>,
|
|
79
|
-
E extends keyof T = keyof T,
|
|
80
|
-
>(info: LazyRemoteComponentInfo<T, E>) {
|
|
81
|
-
const LazyComponent = createLazyRemoteComponent(info);
|
|
82
|
-
return forwardRef<HTMLDivElement, RemoteComponentProps>((props, ref) => {
|
|
83
|
-
return (
|
|
84
|
-
<ErrorBoundary
|
|
85
|
-
FallbackComponent={info.fallback as React.ComponentType<FallbackProps>}
|
|
86
|
-
>
|
|
87
|
-
<React.Suspense fallback={info.loading}>
|
|
88
|
-
<LazyComponent {...props} ref={ref} />
|
|
89
|
-
</React.Suspense>
|
|
90
|
-
</ErrorBoundary>
|
|
91
|
-
);
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* @deprecated createRemoteAppComponent is deprecated, please use createRemoteAppComponent instead!
|
|
97
|
-
*/
|
|
98
|
-
export function createRemoteComponent<
|
|
99
|
-
T = Record<string, unknown>,
|
|
100
|
-
E extends keyof T = keyof T,
|
|
101
|
-
>(info: LazyRemoteComponentInfo<T, E>) {
|
|
102
|
-
LoggerInstance.warn(
|
|
103
|
-
`createRemoteComponent is deprecated, please use createRemoteAppComponent instead!`,
|
|
104
|
-
);
|
|
105
|
-
return createRemoteAppComponent(info);
|
|
106
|
-
}
|