@ice/mf-runtime 1.0.2 → 1.0.3-beta.2
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/docs/PLUGIN_SYSTEM_GUIDE.md +294 -0
- 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 +4 -2
- package/es2017/plugin-manager.d.ts +74 -0
- package/es2017/plugin-manager.js +128 -0
- package/es2017/runtime-plugin.js +29 -27
- 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 +4 -2
- package/esm/plugin-manager.d.ts +74 -0
- package/esm/plugin-manager.js +201 -0
- package/esm/runtime-plugin.js +33 -37
- package/esm/types.d.ts +67 -0
- package/esm/types.js +20 -1
- package/package.json +14 -3
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
|
+
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
3
|
+
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
4
|
+
import { EnhancedPluginManager, getEnhancedPluginManager, registerEnhancedPlugins } from '../plugin-manager';
|
|
5
|
+
import * as React from 'react';
|
|
6
|
+
describe('EnhancedPluginManager', ()=>{
|
|
7
|
+
let manager;
|
|
8
|
+
beforeEach(()=>{
|
|
9
|
+
manager = new EnhancedPluginManager();
|
|
10
|
+
});
|
|
11
|
+
describe('Plugin Registration', ()=>{
|
|
12
|
+
it('should register an enhanced plugin', ()=>{
|
|
13
|
+
const enhancedPlugin = {
|
|
14
|
+
name: 'test-enhanced',
|
|
15
|
+
wrapComponent: (Component)=>Component,
|
|
16
|
+
injectProps: (props)=>props
|
|
17
|
+
};
|
|
18
|
+
manager.register(enhancedPlugin);
|
|
19
|
+
const plugins = manager.getPlugins();
|
|
20
|
+
expect(plugins).toHaveLength(1);
|
|
21
|
+
expect(plugins[0].name).toBe('test-enhanced');
|
|
22
|
+
});
|
|
23
|
+
it('should replace existing plugin with same name', ()=>{
|
|
24
|
+
const plugin1 = {
|
|
25
|
+
name: 'test-plugin',
|
|
26
|
+
wrapComponent: (Component)=>Component
|
|
27
|
+
};
|
|
28
|
+
const plugin2 = {
|
|
29
|
+
name: 'test-plugin',
|
|
30
|
+
injectProps: (props)=>props
|
|
31
|
+
};
|
|
32
|
+
manager.register(plugin1);
|
|
33
|
+
manager.register(plugin2);
|
|
34
|
+
const plugins = manager.getPlugins();
|
|
35
|
+
expect(plugins).toHaveLength(1);
|
|
36
|
+
expect(plugins[0]).toBe(plugin2);
|
|
37
|
+
});
|
|
38
|
+
it('should register multiple plugins', ()=>{
|
|
39
|
+
const plugins = [
|
|
40
|
+
{
|
|
41
|
+
name: 'plugin1',
|
|
42
|
+
wrapComponent: (Component)=>Component
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: 'plugin2',
|
|
46
|
+
injectProps: (props)=>props
|
|
47
|
+
}
|
|
48
|
+
];
|
|
49
|
+
manager.registerPlugins(plugins);
|
|
50
|
+
expect(manager.getPlugins()).toHaveLength(2);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
describe('Component Wrapping', ()=>{
|
|
54
|
+
it('should wrap component with single plugin', ()=>{
|
|
55
|
+
const TestComponent = ()=>React.createElement('div', null, 'test');
|
|
56
|
+
const mockWrapper = vi.fn((Component)=>Component);
|
|
57
|
+
const plugin = {
|
|
58
|
+
name: 'wrapper-plugin',
|
|
59
|
+
wrapComponent: mockWrapper
|
|
60
|
+
};
|
|
61
|
+
manager.register(plugin);
|
|
62
|
+
const context = {
|
|
63
|
+
remoteName: 'test-remote',
|
|
64
|
+
moduleName: 'test-module',
|
|
65
|
+
props: {
|
|
66
|
+
test: true
|
|
67
|
+
},
|
|
68
|
+
React: React,
|
|
69
|
+
ReactDOM: undefined
|
|
70
|
+
};
|
|
71
|
+
const wrappedComponent = manager.wrapComponent(TestComponent, context);
|
|
72
|
+
expect(mockWrapper).toHaveBeenCalledWith(TestComponent, context);
|
|
73
|
+
expect(wrappedComponent).toBe(TestComponent);
|
|
74
|
+
});
|
|
75
|
+
it('should apply multiple wrappers in order', ()=>{
|
|
76
|
+
const TestComponent = ()=>React.createElement('div', null, 'original');
|
|
77
|
+
const Wrapper1 = ()=>React.createElement('div', null, 'wrapper1');
|
|
78
|
+
const Wrapper2 = ()=>React.createElement('div', null, 'wrapper2');
|
|
79
|
+
const plugin1 = {
|
|
80
|
+
name: 'wrapper1',
|
|
81
|
+
wrapComponent: ()=>Wrapper1
|
|
82
|
+
};
|
|
83
|
+
const plugin2 = {
|
|
84
|
+
name: 'wrapper2',
|
|
85
|
+
wrapComponent: ()=>Wrapper2
|
|
86
|
+
};
|
|
87
|
+
// 注册顺序很重要
|
|
88
|
+
manager.register(plugin1);
|
|
89
|
+
manager.register(plugin2);
|
|
90
|
+
const context = {
|
|
91
|
+
remoteName: 'test-remote',
|
|
92
|
+
moduleName: 'test-module',
|
|
93
|
+
props: {},
|
|
94
|
+
React: React,
|
|
95
|
+
ReactDOM: undefined
|
|
96
|
+
};
|
|
97
|
+
const result = manager.wrapComponent(TestComponent, context);
|
|
98
|
+
// plugin2 应该最后执行,所以返回 Wrapper2
|
|
99
|
+
expect(result).toBe(Wrapper2);
|
|
100
|
+
});
|
|
101
|
+
it('should handle wrapper errors gracefully', ()=>{
|
|
102
|
+
const TestComponent = ()=>React.createElement('div', null, 'test');
|
|
103
|
+
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(()=>{});
|
|
104
|
+
const plugin = {
|
|
105
|
+
name: 'error-plugin',
|
|
106
|
+
wrapComponent: ()=>{
|
|
107
|
+
throw new Error('Wrapper error');
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
manager.register(plugin);
|
|
111
|
+
const context = {
|
|
112
|
+
remoteName: 'test-remote',
|
|
113
|
+
moduleName: 'test-module',
|
|
114
|
+
props: {},
|
|
115
|
+
React: React,
|
|
116
|
+
ReactDOM: undefined
|
|
117
|
+
};
|
|
118
|
+
const result = manager.wrapComponent(TestComponent, context);
|
|
119
|
+
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Error applying wrapper from plugin "error-plugin"'), expect.any(Error));
|
|
120
|
+
expect(result).toBe(TestComponent); // 应该返回原始组件
|
|
121
|
+
consoleSpy.mockRestore();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
describe('Props Injection', ()=>{
|
|
125
|
+
it('should inject props with single plugin', ()=>{
|
|
126
|
+
const mockInjector = vi.fn((props)=>_object_spread_props(_object_spread({}, props), {
|
|
127
|
+
injected: true
|
|
128
|
+
}));
|
|
129
|
+
const plugin = {
|
|
130
|
+
name: 'injector-plugin',
|
|
131
|
+
injectProps: mockInjector
|
|
132
|
+
};
|
|
133
|
+
manager.register(plugin);
|
|
134
|
+
const context = {
|
|
135
|
+
remoteName: 'test-remote',
|
|
136
|
+
moduleName: 'test-module'
|
|
137
|
+
};
|
|
138
|
+
const result = manager.injectProps({
|
|
139
|
+
original: true
|
|
140
|
+
}, context);
|
|
141
|
+
expect(mockInjector).toHaveBeenCalledWith({
|
|
142
|
+
original: true
|
|
143
|
+
}, context);
|
|
144
|
+
expect(result).toEqual({
|
|
145
|
+
original: true,
|
|
146
|
+
injected: true
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
it('should apply multiple injectors in order', ()=>{
|
|
150
|
+
const plugin1 = {
|
|
151
|
+
name: 'injector1',
|
|
152
|
+
injectProps: (props)=>_object_spread_props(_object_spread({}, props), {
|
|
153
|
+
step1: true
|
|
154
|
+
})
|
|
155
|
+
};
|
|
156
|
+
const plugin2 = {
|
|
157
|
+
name: 'injector2',
|
|
158
|
+
injectProps: (props)=>_object_spread_props(_object_spread({}, props), {
|
|
159
|
+
step2: true
|
|
160
|
+
})
|
|
161
|
+
};
|
|
162
|
+
manager.register(plugin1);
|
|
163
|
+
manager.register(plugin2);
|
|
164
|
+
const context = {
|
|
165
|
+
remoteName: 'test-remote',
|
|
166
|
+
moduleName: 'test-module'
|
|
167
|
+
};
|
|
168
|
+
const result = manager.injectProps({
|
|
169
|
+
original: true
|
|
170
|
+
}, context);
|
|
171
|
+
expect(result).toEqual({
|
|
172
|
+
original: true,
|
|
173
|
+
step1: true,
|
|
174
|
+
step2: true
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
it('should handle injection errors gracefully', ()=>{
|
|
178
|
+
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(()=>{});
|
|
179
|
+
const plugin = {
|
|
180
|
+
name: 'error-plugin',
|
|
181
|
+
injectProps: ()=>{
|
|
182
|
+
throw new Error('Injection error');
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
manager.register(plugin);
|
|
186
|
+
const context = {
|
|
187
|
+
remoteName: 'test-remote',
|
|
188
|
+
moduleName: 'test-module'
|
|
189
|
+
};
|
|
190
|
+
const result = manager.injectProps({
|
|
191
|
+
original: true
|
|
192
|
+
}, context);
|
|
193
|
+
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Error injecting props from plugin "error-plugin"'), expect.any(Error));
|
|
194
|
+
expect(result).toEqual({
|
|
195
|
+
original: true
|
|
196
|
+
}); // 应该返回原始props
|
|
197
|
+
consoleSpy.mockRestore();
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
describe('Plugin Management', ()=>{
|
|
201
|
+
it('should get plugin info', ()=>{
|
|
202
|
+
const wrapperPlugin = {
|
|
203
|
+
name: 'wrapper-plugin',
|
|
204
|
+
wrapComponent: (Component)=>Component
|
|
205
|
+
};
|
|
206
|
+
const injectorPlugin = {
|
|
207
|
+
name: 'injector-plugin',
|
|
208
|
+
injectProps: (props)=>props
|
|
209
|
+
};
|
|
210
|
+
const hybridPlugin = {
|
|
211
|
+
name: 'hybrid-plugin',
|
|
212
|
+
wrapComponent: (Component)=>Component,
|
|
213
|
+
injectProps: (props)=>props
|
|
214
|
+
};
|
|
215
|
+
manager.register(wrapperPlugin);
|
|
216
|
+
manager.register(injectorPlugin);
|
|
217
|
+
manager.register(hybridPlugin);
|
|
218
|
+
const info = manager.getPluginInfo();
|
|
219
|
+
expect(info).toHaveLength(3);
|
|
220
|
+
expect(info[0]).toEqual({
|
|
221
|
+
name: 'wrapper-plugin',
|
|
222
|
+
hasWrapper: true,
|
|
223
|
+
hasInjector: false
|
|
224
|
+
});
|
|
225
|
+
expect(info[1]).toEqual({
|
|
226
|
+
name: 'injector-plugin',
|
|
227
|
+
hasWrapper: false,
|
|
228
|
+
hasInjector: true
|
|
229
|
+
});
|
|
230
|
+
expect(info[2]).toEqual({
|
|
231
|
+
name: 'hybrid-plugin',
|
|
232
|
+
hasWrapper: true,
|
|
233
|
+
hasInjector: true
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
it('should remove plugin', ()=>{
|
|
237
|
+
const plugin = {
|
|
238
|
+
name: 'test-plugin',
|
|
239
|
+
wrapComponent: (Component)=>Component
|
|
240
|
+
};
|
|
241
|
+
manager.register(plugin);
|
|
242
|
+
expect(manager.getPlugins()).toHaveLength(1);
|
|
243
|
+
const removed = manager.removePlugin('test-plugin');
|
|
244
|
+
expect(removed).toBe(true);
|
|
245
|
+
expect(manager.getPlugins()).toHaveLength(0);
|
|
246
|
+
});
|
|
247
|
+
it('should return false when removing non-existent plugin', ()=>{
|
|
248
|
+
const removed = manager.removePlugin('non-existent');
|
|
249
|
+
expect(removed).toBe(false);
|
|
250
|
+
});
|
|
251
|
+
it('should clear all plugins', ()=>{
|
|
252
|
+
manager.register({
|
|
253
|
+
name: 'plugin1',
|
|
254
|
+
wrapComponent: (Component)=>Component
|
|
255
|
+
});
|
|
256
|
+
manager.register({
|
|
257
|
+
name: 'plugin2',
|
|
258
|
+
injectProps: (props)=>props
|
|
259
|
+
});
|
|
260
|
+
expect(manager.getPlugins()).toHaveLength(2);
|
|
261
|
+
manager.clear();
|
|
262
|
+
expect(manager.getPlugins()).toHaveLength(0);
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
describe('Global Enhanced Plugin Manager', ()=>{
|
|
266
|
+
beforeEach(()=>{
|
|
267
|
+
// 清理全局状态
|
|
268
|
+
getEnhancedPluginManager().clear();
|
|
269
|
+
});
|
|
270
|
+
it('should provide singleton instance', ()=>{
|
|
271
|
+
const manager1 = getEnhancedPluginManager();
|
|
272
|
+
const manager2 = getEnhancedPluginManager();
|
|
273
|
+
expect(manager1).toBe(manager2);
|
|
274
|
+
});
|
|
275
|
+
it('should register multiple plugins via convenience function', ()=>{
|
|
276
|
+
const plugins = [
|
|
277
|
+
{
|
|
278
|
+
name: 'plugin1',
|
|
279
|
+
wrapComponent: (Component)=>Component
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
name: 'plugin2',
|
|
283
|
+
injectProps: (props)=>props
|
|
284
|
+
}
|
|
285
|
+
];
|
|
286
|
+
registerEnhancedPlugins(plugins);
|
|
287
|
+
const manager = getEnhancedPluginManager();
|
|
288
|
+
expect(manager.getPlugins()).toHaveLength(2);
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -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
|
}
|
|
@@ -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
|
};
|
|
@@ -65,17 +66,18 @@ export const runtimePlugin = ()=>({
|
|
|
65
66
|
console.log('[runtime Plugin onLoad] use same external react');
|
|
66
67
|
return args;
|
|
67
68
|
}
|
|
68
|
-
//
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
+
});
|
|
79
81
|
}
|
|
80
82
|
}
|
|
81
83
|
const fallBackRender = await loadRemotePackagedReactAndRender(args);
|