@ice/mf-runtime 1.0.2-beta.3 → 1.0.3-beta.1
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/README.md +221 -1
- package/es2017/RemoteModule.js +73 -21
- package/es2017/__tests__/plugin-manager.test.d.ts +1 -0
- package/es2017/__tests__/plugin-manager.test.js +291 -0
- package/es2017/__tests__/setup.d.ts +1 -0
- package/es2017/__tests__/setup.js +28 -0
- package/es2017/index.d.ts +2 -0
- package/es2017/index.js +6 -4
- package/es2017/plugin-manager.d.ts +74 -0
- package/es2017/plugin-manager.js +128 -0
- package/es2017/runtime-plugin.js +29 -30
- package/es2017/types.d.ts +67 -0
- package/es2017/types.js +18 -1
- package/esm/RemoteModule.js +73 -30
- package/esm/__tests__/plugin-manager.test.d.ts +1 -0
- package/esm/__tests__/plugin-manager.test.js +343 -0
- package/esm/__tests__/setup.d.ts +1 -0
- package/esm/__tests__/setup.js +43 -0
- package/esm/index.d.ts +2 -0
- package/esm/index.js +6 -4
- package/esm/plugin-manager.d.ts +74 -0
- package/esm/plugin-manager.js +201 -0
- package/esm/runtime-plugin.js +33 -40
- package/esm/types.d.ts +67 -0
- package/esm/types.js +20 -1
- package/package.json +12 -2
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { vi } from 'vitest';
|
|
2
|
+
// Test setup for ice-mf-runtime
|
|
3
|
+
// Mock @ice/stark-app dependency
|
|
4
|
+
vi.mock('@ice/stark-app', ()=>({
|
|
5
|
+
getBasename: ()=>'/app'
|
|
6
|
+
}));
|
|
7
|
+
// Mock @module-federation/runtime
|
|
8
|
+
vi.mock('@module-federation/runtime', ()=>({
|
|
9
|
+
init: vi.fn(),
|
|
10
|
+
registerPlugins: vi.fn(),
|
|
11
|
+
loadRemote: vi.fn()
|
|
12
|
+
}));
|
|
13
|
+
// Mock performance API
|
|
14
|
+
Object.defineProperty(globalThis, 'performance', {
|
|
15
|
+
value: {
|
|
16
|
+
now: ()=>Date.now()
|
|
17
|
+
},
|
|
18
|
+
writable: true
|
|
19
|
+
});
|
|
20
|
+
// Suppress console warnings during tests
|
|
21
|
+
const originalWarn = console.warn;
|
|
22
|
+
console.warn = (...args)=>{
|
|
23
|
+
var _args__includes, _args_;
|
|
24
|
+
if ((_args_ = args[0]) === null || _args_ === void 0 ? void 0 : (_args__includes = _args_.includes) === null || _args__includes === void 0 ? void 0 : _args__includes.call(_args_, 'Warning: ReactDOM.render is no longer supported')) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
originalWarn.call(console, ...args);
|
|
28
|
+
};
|
package/es2017/index.d.ts
CHANGED
|
@@ -2,5 +2,7 @@ import type { ExtendedUserOptions, MicroMod } from './types';
|
|
|
2
2
|
export { loadRemote, registerPlugins } from '@module-federation/runtime';
|
|
3
3
|
export * from './FallBack';
|
|
4
4
|
export * from './RemoteModule';
|
|
5
|
+
export * from './types';
|
|
6
|
+
export * from './plugin-manager';
|
|
5
7
|
export declare function init(options: ExtendedUserOptions, microMods?: MicroMod[]): void;
|
|
6
8
|
export declare function initByMicroMods(microMods: MicroMod[], hostName?: string): void;
|
package/es2017/index.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { getBasename } from '@ice/stark-app';
|
|
2
|
-
import { registerPlugins, init as mfInit } from '@module-federation/runtime';
|
|
2
|
+
import { registerPlugins as mfRegisterPlugins, init as mfInit } from '@module-federation/runtime';
|
|
3
3
|
import { runtimePlugin } from './runtime-plugin';
|
|
4
4
|
import { initGlobalStore } from './mf-global-store';
|
|
5
5
|
export { loadRemote, registerPlugins } from '@module-federation/runtime';
|
|
6
6
|
export * from './FallBack';
|
|
7
7
|
export * from './RemoteModule';
|
|
8
|
+
export * from './types';
|
|
9
|
+
export * from './plugin-manager';
|
|
8
10
|
export function init(options, microMods) {
|
|
9
11
|
initGlobalStore(options, microMods);
|
|
10
12
|
mfInit(options);
|
|
11
|
-
|
|
13
|
+
mfRegisterPlugins([
|
|
12
14
|
runtimePlugin()
|
|
13
15
|
]);
|
|
14
16
|
}
|
|
@@ -26,9 +28,9 @@ export function initByMicroMods(microMods, hostName) {
|
|
|
26
28
|
name: microMod.moduleFederatedName,
|
|
27
29
|
entry: microMod.remoteEntry,
|
|
28
30
|
alias: microMod.appName,
|
|
29
|
-
extraInfo: Object.assign(
|
|
31
|
+
extraInfo: Object.assign({
|
|
30
32
|
legacy: isLegacy
|
|
31
|
-
})
|
|
33
|
+
}, microMod.extraInfo || {})
|
|
32
34
|
};
|
|
33
35
|
return remote;
|
|
34
36
|
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { EnhancedRuntimePlugin, WrapperContext, InjectionContext } from './types';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
/**
|
|
4
|
+
* 增强插件管理器 - 专门负责管理和执行增强运行时插件
|
|
5
|
+
* 标准 MF 插件交由底层 @module-federation/runtime 管理
|
|
6
|
+
*/
|
|
7
|
+
export declare class EnhancedPluginManager {
|
|
8
|
+
private plugins;
|
|
9
|
+
/**
|
|
10
|
+
* 注册增强插件
|
|
11
|
+
* @param plugin 增强运行时插件
|
|
12
|
+
*/
|
|
13
|
+
register(plugin: EnhancedRuntimePlugin): void;
|
|
14
|
+
/**
|
|
15
|
+
* 注册多个增强插件
|
|
16
|
+
* @param plugins 增强插件数组
|
|
17
|
+
*/
|
|
18
|
+
registerPlugins(plugins: EnhancedRuntimePlugin[]): void;
|
|
19
|
+
/**
|
|
20
|
+
* 获取所有增强插件列表
|
|
21
|
+
* @returns 增强插件数组
|
|
22
|
+
*/
|
|
23
|
+
getPlugins(): EnhancedRuntimePlugin[];
|
|
24
|
+
/**
|
|
25
|
+
* 应用组件包装器
|
|
26
|
+
* 按插件注册顺序依次应用所有组件包装器
|
|
27
|
+
* @param Component 原始组件
|
|
28
|
+
* @param context 包装器上下文
|
|
29
|
+
* @returns 包装后的组件
|
|
30
|
+
*/
|
|
31
|
+
wrapComponent(Component: React.ComponentType, context: WrapperContext): React.ComponentType;
|
|
32
|
+
/**
|
|
33
|
+
* 注入属性
|
|
34
|
+
* 按插件注册顺序依次应用所有属性注入器
|
|
35
|
+
* @param props 原始属性
|
|
36
|
+
* @param context 注入上下文
|
|
37
|
+
* @returns 注入后的属性
|
|
38
|
+
*/
|
|
39
|
+
injectProps(props: Record<string, any>, context: InjectionContext): Record<string, any>;
|
|
40
|
+
/**
|
|
41
|
+
* 获取所有插件信息
|
|
42
|
+
* @returns 插件信息数组
|
|
43
|
+
*/
|
|
44
|
+
getPluginInfo(): Array<{
|
|
45
|
+
name: string;
|
|
46
|
+
hasWrapper: boolean;
|
|
47
|
+
hasInjector: boolean;
|
|
48
|
+
}>;
|
|
49
|
+
/**
|
|
50
|
+
* 移除插件
|
|
51
|
+
* @param name 插件名称
|
|
52
|
+
* @returns 是否成功移除
|
|
53
|
+
*/
|
|
54
|
+
removePlugin(name: string): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* 清空所有插件
|
|
57
|
+
*/
|
|
58
|
+
clear(): void;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 获取全局增强插件管理器实例
|
|
62
|
+
* @returns 增强插件管理器实例
|
|
63
|
+
*/
|
|
64
|
+
export declare function getEnhancedPluginManager(): EnhancedPluginManager;
|
|
65
|
+
/**
|
|
66
|
+
* 注册多个增强插件(便捷函数)
|
|
67
|
+
* @param plugins 增强插件数组
|
|
68
|
+
*/
|
|
69
|
+
export declare function registerEnhancedPlugins(plugins: EnhancedRuntimePlugin[]): void;
|
|
70
|
+
/**
|
|
71
|
+
* React Hook:使用增强插件管理器
|
|
72
|
+
* @returns 增强插件管理器实例
|
|
73
|
+
*/
|
|
74
|
+
export declare function useEnhancedPluginManager(): EnhancedPluginManager;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
2
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
/**
|
|
5
|
+
* 增强插件管理器 - 专门负责管理和执行增强运行时插件
|
|
6
|
+
* 标准 MF 插件交由底层 @module-federation/runtime 管理
|
|
7
|
+
*/ export class EnhancedPluginManager {
|
|
8
|
+
/**
|
|
9
|
+
* 注册增强插件
|
|
10
|
+
* @param plugin 增强运行时插件
|
|
11
|
+
*/ register(plugin) {
|
|
12
|
+
// 检查是否已存在同名插件
|
|
13
|
+
const existingIndex = this.plugins.findIndex((p)=>p.name === plugin.name);
|
|
14
|
+
if (existingIndex >= 0) {
|
|
15
|
+
console.warn(`Enhanced plugin "${plugin.name}" already exists, replacing it.`);
|
|
16
|
+
this.plugins[existingIndex] = plugin;
|
|
17
|
+
} else {
|
|
18
|
+
this.plugins.push(plugin);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 注册多个增强插件
|
|
23
|
+
* @param plugins 增强插件数组
|
|
24
|
+
*/ registerPlugins(plugins) {
|
|
25
|
+
plugins.forEach((plugin)=>this.register(plugin));
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 获取所有增强插件列表
|
|
29
|
+
* @returns 增强插件数组
|
|
30
|
+
*/ getPlugins() {
|
|
31
|
+
return [
|
|
32
|
+
...this.plugins
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 应用组件包装器
|
|
37
|
+
* 按插件注册顺序依次应用所有组件包装器
|
|
38
|
+
* @param Component 原始组件
|
|
39
|
+
* @param context 包装器上下文
|
|
40
|
+
* @returns 包装后的组件
|
|
41
|
+
*/ wrapComponent(Component, context) {
|
|
42
|
+
let WrappedComponent = Component;
|
|
43
|
+
// 按数组顺序依次应用所有组件包装器
|
|
44
|
+
for (const plugin of this.plugins){
|
|
45
|
+
if (plugin.wrapComponent) {
|
|
46
|
+
try {
|
|
47
|
+
WrappedComponent = plugin.wrapComponent(WrappedComponent, context);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error(`Error applying wrapper from plugin "${plugin.name}":`, error);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return WrappedComponent;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 注入属性
|
|
57
|
+
* 按插件注册顺序依次应用所有属性注入器
|
|
58
|
+
* @param props 原始属性
|
|
59
|
+
* @param context 注入上下文
|
|
60
|
+
* @returns 注入后的属性
|
|
61
|
+
*/ injectProps(props, context) {
|
|
62
|
+
let injectedProps = _object_spread({}, props);
|
|
63
|
+
// 按数组顺序依次应用所有属性注入器
|
|
64
|
+
for (const plugin of this.plugins){
|
|
65
|
+
if (plugin.injectProps) {
|
|
66
|
+
try {
|
|
67
|
+
injectedProps = plugin.injectProps(injectedProps, context);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
console.error(`Error injecting props from plugin "${plugin.name}":`, error);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return injectedProps;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* 获取所有插件信息
|
|
77
|
+
* @returns 插件信息数组
|
|
78
|
+
*/ getPluginInfo() {
|
|
79
|
+
return this.plugins.map((plugin)=>({
|
|
80
|
+
name: plugin.name,
|
|
81
|
+
hasWrapper: !!plugin.wrapComponent,
|
|
82
|
+
hasInjector: !!plugin.injectProps
|
|
83
|
+
}));
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* 移除插件
|
|
87
|
+
* @param name 插件名称
|
|
88
|
+
* @returns 是否成功移除
|
|
89
|
+
*/ removePlugin(name) {
|
|
90
|
+
const index = this.plugins.findIndex((p)=>p.name === name);
|
|
91
|
+
if (index !== -1) {
|
|
92
|
+
this.plugins.splice(index, 1);
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* 清空所有插件
|
|
99
|
+
*/ clear() {
|
|
100
|
+
this.plugins = [];
|
|
101
|
+
}
|
|
102
|
+
constructor(){
|
|
103
|
+
_define_property(this, "plugins", []);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// 全局增强插件管理器实例
|
|
107
|
+
let globalEnhancedPluginManager = null;
|
|
108
|
+
/**
|
|
109
|
+
* 获取全局增强插件管理器实例
|
|
110
|
+
* @returns 增强插件管理器实例
|
|
111
|
+
*/ export function getEnhancedPluginManager() {
|
|
112
|
+
if (!globalEnhancedPluginManager) {
|
|
113
|
+
globalEnhancedPluginManager = new EnhancedPluginManager();
|
|
114
|
+
}
|
|
115
|
+
return globalEnhancedPluginManager;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 注册多个增强插件(便捷函数)
|
|
119
|
+
* @param plugins 增强插件数组
|
|
120
|
+
*/ export function registerEnhancedPlugins(plugins) {
|
|
121
|
+
getEnhancedPluginManager().registerPlugins(plugins);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* React Hook:使用增强插件管理器
|
|
125
|
+
* @returns 增强插件管理器实例
|
|
126
|
+
*/ export function useEnhancedPluginManager() {
|
|
127
|
+
return getEnhancedPluginManager();
|
|
128
|
+
}
|
package/es2017/runtime-plugin.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { getExtraInfo, getRemoteInfoFromStore, hasConflict } from './mf-global-store';
|
|
3
|
-
import {
|
|
3
|
+
import { createRuntimePluginWrapper } from './types';
|
|
4
4
|
const loadRemotePackagedReactAndRender = async (args)=>{
|
|
5
5
|
var _args_origin_options_shared_reactdom_, _args_origin_options_shared_reactdom, _args_origin_options_shared, _args_origin_options, _remoteInstance_options_shared_reactdom_, _remoteInstance_options_shared_reactdom, _remoteInstance_options_shared, _remoteInstance_options;
|
|
6
6
|
const hostVersion = ((_args_origin_options = args.origin.options) === null || _args_origin_options === void 0 ? void 0 : (_args_origin_options_shared = _args_origin_options.shared) === null || _args_origin_options_shared === void 0 ? void 0 : (_args_origin_options_shared_reactdom = _args_origin_options_shared['react-dom']) === null || _args_origin_options_shared_reactdom === void 0 ? void 0 : (_args_origin_options_shared_reactdom_ = _args_origin_options_shared_reactdom[0]) === null || _args_origin_options_shared_reactdom_ === void 0 ? void 0 : _args_origin_options_shared_reactdom_.version) || React.version;
|
|
@@ -10,28 +10,29 @@ const loadRemotePackagedReactAndRender = async (args)=>{
|
|
|
10
10
|
if (remoteVersion && hostVersion) {
|
|
11
11
|
var _sharedOptions_find;
|
|
12
12
|
const remoteReactDOM = await remoteInstance.loadShare('react-dom', {
|
|
13
|
-
resolver: (sharedOptions)=>(_sharedOptions_find = sharedOptions.find((
|
|
13
|
+
resolver: (sharedOptions)=>(_sharedOptions_find = sharedOptions.find((option)=>option.version === remoteVersion)) !== null && _sharedOptions_find !== void 0 ? _sharedOptions_find : sharedOptions[0]
|
|
14
14
|
});
|
|
15
15
|
var _sharedOptions_find1;
|
|
16
16
|
const remoteReact = await remoteInstance.loadShare('react', {
|
|
17
|
-
resolver: (sharedOptions)=>(_sharedOptions_find1 = sharedOptions.find((
|
|
17
|
+
resolver: (sharedOptions)=>(_sharedOptions_find1 = sharedOptions.find((option)=>option.version === remoteVersion)) !== null && _sharedOptions_find1 !== void 0 ? _sharedOptions_find1 : sharedOptions[0]
|
|
18
18
|
});
|
|
19
19
|
if (!remoteReact || !remoteReactDOM) {
|
|
20
20
|
return null;
|
|
21
21
|
}
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
22
|
+
// 创建 Runtime Plugin 包装器对象,返回原始组件和运行时信息
|
|
23
|
+
// 让 RemoteModule 控制 HOC 的正确层级顺序
|
|
24
|
+
return createRuntimePluginWrapper(// 原始组件
|
|
25
|
+
args.exposeModule.default, // 运行时信息
|
|
26
|
+
{
|
|
27
|
+
remoteVersion,
|
|
28
|
+
hostVersion,
|
|
29
|
+
remoteReact: remoteReact,
|
|
30
|
+
remoteReactDOM: remoteReactDOM
|
|
31
|
+
}, // React 实例信息
|
|
32
|
+
{
|
|
33
|
+
react: remoteReact(),
|
|
34
|
+
reactDOM: remoteReactDOM()
|
|
35
|
+
});
|
|
35
36
|
}
|
|
36
37
|
return null;
|
|
37
38
|
};
|
|
@@ -53,9 +54,6 @@ export const runtimePlugin = ()=>({
|
|
|
53
54
|
var _args_origin_options_shared_reactdom_, _args_origin_options_shared_reactdom, _args_origin_options_shared, _args_origin_options, _extraInfo;
|
|
54
55
|
const hostName = args.origin.name;
|
|
55
56
|
const remoteName = args.remote.name;
|
|
56
|
-
console.error(__webpack_init_sharing__);
|
|
57
|
-
console.error(__webpack_share_scopes__);
|
|
58
|
-
window.a = __webpack_share_scopes__;
|
|
59
57
|
const hostVersion = ((_args_origin_options = args.origin.options) === null || _args_origin_options === void 0 ? void 0 : (_args_origin_options_shared = _args_origin_options.shared) === null || _args_origin_options_shared === void 0 ? void 0 : (_args_origin_options_shared_reactdom = _args_origin_options_shared['react-dom']) === null || _args_origin_options_shared_reactdom === void 0 ? void 0 : (_args_origin_options_shared_reactdom_ = _args_origin_options_shared_reactdom[0]) === null || _args_origin_options_shared_reactdom_ === void 0 ? void 0 : _args_origin_options_shared_reactdom_.version) || React.version;
|
|
60
58
|
const extraInfo = getExtraInfo(hostName, remoteName);
|
|
61
59
|
if ((_extraInfo = extraInfo) === null || _extraInfo === void 0 ? void 0 : _extraInfo.external) {
|
|
@@ -68,17 +66,18 @@ export const runtimePlugin = ()=>({
|
|
|
68
66
|
console.log('[runtime Plugin onLoad] use same external react');
|
|
69
67
|
return args;
|
|
70
68
|
}
|
|
71
|
-
//
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
69
|
+
// 创建 Runtime Plugin 包装器对象,返回原始组件和运行时信息
|
|
70
|
+
// 让 RemoteModule 控制 HOC 的正确层级顺序
|
|
71
|
+
return createRuntimePluginWrapper(// 原始组件
|
|
72
|
+
args.exposeModule.default, // 运行时信息
|
|
73
|
+
{
|
|
74
|
+
remoteReact: remoteReact,
|
|
75
|
+
remoteReactDOM: remoteReactDOM
|
|
76
|
+
}, // React 实例信息
|
|
77
|
+
{
|
|
78
|
+
react: remoteReact(),
|
|
79
|
+
reactDOM: remoteReactDOM()
|
|
80
|
+
});
|
|
82
81
|
}
|
|
83
82
|
}
|
|
84
83
|
const fallBackRender = await loadRemotePackagedReactAndRender(args);
|
package/es2017/types.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { UserOptions } from '@module-federation/runtime-core';
|
|
2
2
|
import type { Remote } from '@module-federation/runtime-core/dist/src/types';
|
|
3
|
+
import * as React from 'react';
|
|
3
4
|
export interface ExtraInfo {
|
|
4
5
|
external?: Record<string, string>;
|
|
5
6
|
legacy?: boolean;
|
|
@@ -18,3 +19,69 @@ export interface MicroMod {
|
|
|
18
19
|
moduleFederatedName: string;
|
|
19
20
|
extraInfo?: ExtraInfo;
|
|
20
21
|
}
|
|
22
|
+
export interface FederationRuntimePlugin {
|
|
23
|
+
name: string;
|
|
24
|
+
afterResolve?: (args: any) => any;
|
|
25
|
+
onLoad?: (args: any) => any;
|
|
26
|
+
beforeRequest?: (args: any) => any;
|
|
27
|
+
}
|
|
28
|
+
export interface WrapperContext {
|
|
29
|
+
remoteName: string;
|
|
30
|
+
moduleName: string;
|
|
31
|
+
props: Record<string, any>;
|
|
32
|
+
React: typeof import('react');
|
|
33
|
+
ReactDOM?: typeof import('react-dom');
|
|
34
|
+
}
|
|
35
|
+
export interface InjectionContext {
|
|
36
|
+
remoteName: string;
|
|
37
|
+
moduleName: string;
|
|
38
|
+
}
|
|
39
|
+
export interface ComponentWrapper {
|
|
40
|
+
(WrappedComponent: React.ComponentType<any>, context: WrapperContext): React.ComponentType<any>;
|
|
41
|
+
}
|
|
42
|
+
export interface PropInjector {
|
|
43
|
+
(props: Record<string, any>, context: InjectionContext): Record<string, any>;
|
|
44
|
+
}
|
|
45
|
+
export interface EnhancedRuntimePlugin {
|
|
46
|
+
name: string;
|
|
47
|
+
wrapComponent?: ComponentWrapper;
|
|
48
|
+
injectProps?: PropInjector;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Runtime Plugin 包装器对象的接口
|
|
52
|
+
* 包含原始组件和运行时信息,让 RemoteModule 控制 HOC 层级顺序
|
|
53
|
+
*/
|
|
54
|
+
export interface RuntimePluginWrapper {
|
|
55
|
+
readonly type: 'runtime-plugin-wrapper';
|
|
56
|
+
readonly originalComponent: React.ComponentType<any>;
|
|
57
|
+
readonly runtimeInfo: {
|
|
58
|
+
readonly remoteVersion?: string;
|
|
59
|
+
readonly hostVersion?: string;
|
|
60
|
+
readonly remoteReact: () => typeof import('react');
|
|
61
|
+
readonly remoteReactDOM: () => typeof import('react-dom');
|
|
62
|
+
readonly mountNode?: HTMLElement;
|
|
63
|
+
readonly containerClassName?: string;
|
|
64
|
+
};
|
|
65
|
+
readonly reactInstances: {
|
|
66
|
+
readonly react: typeof import('react');
|
|
67
|
+
readonly reactDOM?: typeof import('react-dom');
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 类型守卫:检查是否为 Runtime Plugin 包装器
|
|
72
|
+
*/
|
|
73
|
+
export declare function isRuntimePluginWrapper(value: any): value is () => RuntimePluginWrapper;
|
|
74
|
+
/**
|
|
75
|
+
* 创建 Runtime Plugin 包装器的工厂函数
|
|
76
|
+
*/
|
|
77
|
+
export declare function createRuntimePluginWrapper(originalComponent: React.ComponentType<any>, runtimeInfo: {
|
|
78
|
+
remoteVersion?: string;
|
|
79
|
+
hostVersion?: string;
|
|
80
|
+
remoteReact: () => typeof import('react');
|
|
81
|
+
remoteReactDOM: () => typeof import('react-dom');
|
|
82
|
+
mountNode?: HTMLElement;
|
|
83
|
+
containerClassName?: string;
|
|
84
|
+
}, reactInstances: {
|
|
85
|
+
react: typeof import('react');
|
|
86
|
+
reactDOM?: typeof import('react-dom');
|
|
87
|
+
}): () => RuntimePluginWrapper;
|
package/es2017/types.js
CHANGED
|
@@ -1 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* 类型守卫:检查是否为 Runtime Plugin 包装器
|
|
4
|
+
*/ export function isRuntimePluginWrapper(value) {
|
|
5
|
+
return value && value.__mf_fallback_render;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* 创建 Runtime Plugin 包装器的工厂函数
|
|
9
|
+
*/ export function createRuntimePluginWrapper(originalComponent, runtimeInfo, reactInstances) {
|
|
10
|
+
const wrapper = ()=>({
|
|
11
|
+
type: 'runtime-plugin-wrapper',
|
|
12
|
+
originalComponent,
|
|
13
|
+
runtimeInfo,
|
|
14
|
+
reactInstances
|
|
15
|
+
});
|
|
16
|
+
wrapper.__mf_fallback_render = true;
|
|
17
|
+
return wrapper;
|
|
18
|
+
} // 不再需要联合类型,两种插件分开管理
|
package/esm/RemoteModule.js
CHANGED
|
@@ -6,10 +6,13 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
|
6
6
|
import { loadRemote } from "@module-federation/runtime";
|
|
7
7
|
import * as React from "react";
|
|
8
8
|
import { useMemo, useState, useLayoutEffect, forwardRef } from "react";
|
|
9
|
+
import * as ReactDOM from "react-dom";
|
|
9
10
|
import { ErrorBoundary } from "react-error-boundary";
|
|
10
11
|
import { FallBack } from "./FallBack";
|
|
11
12
|
import { setFederatedModulePublicPath } from "./set-public-path";
|
|
12
13
|
import { getMicroMod } from "./mf-global-store";
|
|
14
|
+
import { useEnhancedPluginManager } from "./plugin-manager";
|
|
15
|
+
import { isRuntimePluginWrapper } from "./types";
|
|
13
16
|
var useMountNode = function(mountNodeConfig) {
|
|
14
17
|
var _useState = _sliced_to_array(useState(undefined), 2), resolvedNode = _useState[0], setResolvedNode = _useState[1];
|
|
15
18
|
// 解析各种类型的 mountNode
|
|
@@ -40,6 +43,7 @@ var RemoteModuleInner = function(param, ref) {
|
|
|
40
43
|
var _microMod, _runtime, _runtime1;
|
|
41
44
|
var microMod = getMicroMod(scope);
|
|
42
45
|
var resolvedMountNode = useMountNode(mountNode);
|
|
46
|
+
var enhancedPluginManager = useEnhancedPluginManager();
|
|
43
47
|
if ((_microMod = microMod) === null || _microMod === void 0 ? void 0 : _microMod.publicPath) {
|
|
44
48
|
setFederatedModulePublicPath(microMod.moduleFederatedName, microMod.publicPath);
|
|
45
49
|
}
|
|
@@ -49,7 +53,7 @@ var RemoteModuleInner = function(param, ref) {
|
|
|
49
53
|
var Component = useMemo(function() {
|
|
50
54
|
if (!module || !scope) return null;
|
|
51
55
|
return /*#__PURE__*/ React.lazy(/*#__PURE__*/ _async_to_generator(function() {
|
|
52
|
-
var _typedRemoteModule, remoteModule,
|
|
56
|
+
var _typedRemoteModule, remoteModule, _remoteModule, originalComponent, runtimeInfo, reactInstances, remoteReact, remoteReactDOM, PluginWrappedComponent, FinalComponent, typedRemoteModule, BaseComponent, FinalComponent1, react, reactDOM, PluginWrappedComponent1;
|
|
53
57
|
return _ts_generator(this, function(_state) {
|
|
54
58
|
switch(_state.label){
|
|
55
59
|
case 0:
|
|
@@ -59,48 +63,80 @@ var RemoteModuleInner = function(param, ref) {
|
|
|
59
63
|
];
|
|
60
64
|
case 1:
|
|
61
65
|
remoteModule = _state.sent();
|
|
62
|
-
//
|
|
63
|
-
if (
|
|
64
|
-
|
|
66
|
+
// 使用清晰的对象结构检查是否为 Runtime Plugin 包装器
|
|
67
|
+
if (isRuntimePluginWrapper(remoteModule)) {
|
|
68
|
+
_remoteModule = remoteModule(), originalComponent = _remoteModule.originalComponent, runtimeInfo = _remoteModule.runtimeInfo, reactInstances = _remoteModule.reactInstances;
|
|
69
|
+
remoteReact = reactInstances.react;
|
|
70
|
+
remoteReactDOM = reactInstances.reactDOM;
|
|
71
|
+
PluginWrappedComponent = enhancedPluginManager.wrapComponent(originalComponent, {
|
|
72
|
+
remoteName: scope,
|
|
73
|
+
moduleName: module,
|
|
74
|
+
props: componentProps || {},
|
|
75
|
+
React: remoteReact,
|
|
76
|
+
ReactDOM: remoteReactDOM
|
|
77
|
+
});
|
|
78
|
+
FinalComponent = FallBack({
|
|
79
|
+
Original: PluginWrappedComponent,
|
|
80
|
+
remoteVersion: runtimeInfo.remoteVersion ? function() {
|
|
81
|
+
return runtimeInfo.remoteVersion;
|
|
82
|
+
} : undefined,
|
|
83
|
+
hostVersion: runtimeInfo.hostVersion ? function() {
|
|
84
|
+
return runtimeInfo.hostVersion;
|
|
85
|
+
} : undefined,
|
|
86
|
+
remoteReactDOM: runtimeInfo.remoteReactDOM,
|
|
87
|
+
remoteReact: runtimeInfo.remoteReact,
|
|
88
|
+
mountNode: resolvedMountNode,
|
|
89
|
+
containerClassName: fallbackContainerClassName
|
|
90
|
+
});
|
|
65
91
|
return [
|
|
66
92
|
2,
|
|
67
93
|
{
|
|
68
|
-
default:
|
|
94
|
+
default: FinalComponent
|
|
69
95
|
}
|
|
70
96
|
];
|
|
71
97
|
}
|
|
72
98
|
typedRemoteModule = remoteModule;
|
|
73
99
|
if (!((_typedRemoteModule = typedRemoteModule) === null || _typedRemoteModule === void 0 ? void 0 : _typedRemoteModule.default)) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
default: remoteModule
|
|
78
|
-
}
|
|
79
|
-
];
|
|
100
|
+
BaseComponent = remoteModule;
|
|
101
|
+
} else {
|
|
102
|
+
BaseComponent = typedRemoteModule.default;
|
|
80
103
|
}
|
|
104
|
+
// 如果需要 runtime 处理(不同 React 版本或 mountNode)
|
|
81
105
|
if (runtime) {
|
|
82
106
|
react = runtime.react, reactDOM = runtime.reactDOM;
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
|
|
107
|
+
PluginWrappedComponent1 = enhancedPluginManager.wrapComponent(BaseComponent, {
|
|
108
|
+
remoteName: scope,
|
|
109
|
+
moduleName: module,
|
|
110
|
+
props: componentProps || {},
|
|
111
|
+
React: react,
|
|
112
|
+
ReactDOM: reactDOM
|
|
113
|
+
});
|
|
114
|
+
FinalComponent1 = FallBack({
|
|
115
|
+
Original: PluginWrappedComponent1,
|
|
116
|
+
remoteReact: function() {
|
|
117
|
+
return react;
|
|
118
|
+
},
|
|
119
|
+
remoteReactDOM: function() {
|
|
120
|
+
return reactDOM;
|
|
121
|
+
},
|
|
122
|
+
mountNode: resolvedMountNode,
|
|
123
|
+
containerClassName: fallbackContainerClassName
|
|
124
|
+
});
|
|
125
|
+
} else {
|
|
126
|
+
// 没有 runtime 需求时,直接应用插件包装器
|
|
127
|
+
FinalComponent1 = enhancedPluginManager.wrapComponent(BaseComponent, {
|
|
128
|
+
remoteName: scope,
|
|
129
|
+
moduleName: module,
|
|
130
|
+
props: componentProps || {},
|
|
131
|
+
React: React,
|
|
132
|
+
ReactDOM: ReactDOM
|
|
133
|
+
});
|
|
99
134
|
}
|
|
135
|
+
console.log("FinalComponent", FinalComponent1);
|
|
100
136
|
return [
|
|
101
137
|
2,
|
|
102
138
|
{
|
|
103
|
-
default:
|
|
139
|
+
default: FinalComponent1
|
|
104
140
|
}
|
|
105
141
|
];
|
|
106
142
|
}
|
|
@@ -112,7 +148,9 @@ var RemoteModuleInner = function(param, ref) {
|
|
|
112
148
|
(_runtime = runtime) === null || _runtime === void 0 ? void 0 : _runtime.react,
|
|
113
149
|
(_runtime1 = runtime) === null || _runtime1 === void 0 ? void 0 : _runtime1.reactDOM,
|
|
114
150
|
resolvedMountNode,
|
|
115
|
-
fallbackContainerClassName
|
|
151
|
+
fallbackContainerClassName,
|
|
152
|
+
enhancedPluginManager,
|
|
153
|
+
componentProps
|
|
116
154
|
]);
|
|
117
155
|
var Loading = LoadingComponent || /*#__PURE__*/ React.createElement("div", null, "Loading...");
|
|
118
156
|
var ErrorFallback = function(param) {
|
|
@@ -120,6 +158,11 @@ var RemoteModuleInner = function(param, ref) {
|
|
|
120
158
|
return ErrorComponent || /*#__PURE__*/ React.createElement("div", null, "远程模块加载失败: ", error.message);
|
|
121
159
|
};
|
|
122
160
|
if (!Component) return Loading;
|
|
161
|
+
// 应用增强插件的属性注入
|
|
162
|
+
var injectedProps = enhancedPluginManager.injectProps(componentProps || {}, {
|
|
163
|
+
remoteName: scope,
|
|
164
|
+
moduleName: module
|
|
165
|
+
});
|
|
123
166
|
return /*#__PURE__*/ React.createElement(ErrorBoundary, {
|
|
124
167
|
resetKeys: [
|
|
125
168
|
module,
|
|
@@ -132,7 +175,7 @@ var RemoteModuleInner = function(param, ref) {
|
|
|
132
175
|
fallback: Loading
|
|
133
176
|
}, /*#__PURE__*/ React.createElement(Component, _object_spread({
|
|
134
177
|
ref: ref
|
|
135
|
-
},
|
|
178
|
+
}, injectedProps), children)));
|
|
136
179
|
};
|
|
137
180
|
// 使用 forwardRef 包装组件以支持 ref
|
|
138
181
|
export var RemoteModule = /*#__PURE__*/ forwardRef(RemoteModuleInner);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|