@ice/mf-runtime 1.0.3-beta.2 → 1.0.3-beta.3

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