@ice/mf-runtime 1.0.2-beta.1 → 1.0.2-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.
@@ -1,348 +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, registerEnhancedPlugin, 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
- };
85
- var wrappedComponent = manager.wrapComponent(TestComponent, context);
86
- expect(mockWrapper).toHaveBeenCalledWith(TestComponent, context);
87
- expect(wrappedComponent).toBe(TestComponent);
88
- });
89
- it("should apply multiple wrappers in order", function() {
90
- var TestComponent = function() {
91
- return React.createElement("div", null, "original");
92
- };
93
- var Wrapper1 = function() {
94
- return React.createElement("div", null, "wrapper1");
95
- };
96
- var Wrapper2 = function() {
97
- return React.createElement("div", null, "wrapper2");
98
- };
99
- var plugin1 = {
100
- name: "wrapper1",
101
- wrapComponent: function() {
102
- return Wrapper1;
103
- }
104
- };
105
- var plugin2 = {
106
- name: "wrapper2",
107
- wrapComponent: function() {
108
- return Wrapper2;
109
- }
110
- };
111
- // 注册顺序很重要
112
- manager.register(plugin1);
113
- manager.register(plugin2);
114
- var context = {
115
- remoteName: "test-remote",
116
- moduleName: "test-module",
117
- props: {}
118
- };
119
- var result = manager.wrapComponent(TestComponent, context);
120
- // plugin2 应该最后执行,所以返回 Wrapper2
121
- expect(result).toBe(Wrapper2);
122
- });
123
- it("should handle wrapper errors gracefully", function() {
124
- var TestComponent = function() {
125
- return React.createElement("div", null, "test");
126
- };
127
- var consoleSpy = vi.spyOn(console, "error").mockImplementation(function() {});
128
- var plugin = {
129
- name: "error-plugin",
130
- wrapComponent: function() {
131
- throw new Error("Wrapper error");
132
- }
133
- };
134
- manager.register(plugin);
135
- var context = {
136
- remoteName: "test-remote",
137
- moduleName: "test-module",
138
- props: {}
139
- };
140
- var result = manager.wrapComponent(TestComponent, context);
141
- expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Error applying wrapper from plugin "error-plugin"'), expect.any(Error));
142
- expect(result).toBe(TestComponent); // 应该返回原始组件
143
- consoleSpy.mockRestore();
144
- });
145
- });
146
- describe("Props Injection", function() {
147
- it("should inject props with single plugin", function() {
148
- var mockInjector = vi.fn(function(props) {
149
- return _object_spread_props(_object_spread({}, props), {
150
- injected: true
151
- });
152
- });
153
- var plugin = {
154
- name: "injector-plugin",
155
- injectProps: mockInjector
156
- };
157
- manager.register(plugin);
158
- var context = {
159
- remoteName: "test-remote",
160
- moduleName: "test-module"
161
- };
162
- var result = manager.injectProps({
163
- original: true
164
- }, context);
165
- expect(mockInjector).toHaveBeenCalledWith({
166
- original: true
167
- }, context);
168
- expect(result).toEqual({
169
- original: true,
170
- injected: true
171
- });
172
- });
173
- it("should apply multiple injectors in order", function() {
174
- var plugin1 = {
175
- name: "injector1",
176
- injectProps: function(props) {
177
- return _object_spread_props(_object_spread({}, props), {
178
- step1: true
179
- });
180
- }
181
- };
182
- var plugin2 = {
183
- name: "injector2",
184
- injectProps: function(props) {
185
- return _object_spread_props(_object_spread({}, props), {
186
- step2: true
187
- });
188
- }
189
- };
190
- manager.register(plugin1);
191
- manager.register(plugin2);
192
- var context = {
193
- remoteName: "test-remote",
194
- moduleName: "test-module"
195
- };
196
- var result = manager.injectProps({
197
- original: true
198
- }, context);
199
- expect(result).toEqual({
200
- original: true,
201
- step1: true,
202
- step2: true
203
- });
204
- });
205
- it("should handle injection errors gracefully", function() {
206
- var consoleSpy = vi.spyOn(console, "error").mockImplementation(function() {});
207
- var plugin = {
208
- name: "error-plugin",
209
- injectProps: function() {
210
- throw new Error("Injection error");
211
- }
212
- };
213
- manager.register(plugin);
214
- var context = {
215
- remoteName: "test-remote",
216
- moduleName: "test-module"
217
- };
218
- var result = manager.injectProps({
219
- original: true
220
- }, context);
221
- expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Error injecting props from plugin "error-plugin"'), expect.any(Error));
222
- expect(result).toEqual({
223
- original: true
224
- }); // 应该返回原始props
225
- consoleSpy.mockRestore();
226
- });
227
- });
228
- describe("Plugin Management", function() {
229
- it("should get plugin info", function() {
230
- var wrapperPlugin = {
231
- name: "wrapper-plugin",
232
- wrapComponent: function(Component) {
233
- return Component;
234
- }
235
- };
236
- var injectorPlugin = {
237
- name: "injector-plugin",
238
- injectProps: function(props) {
239
- return props;
240
- }
241
- };
242
- var hybridPlugin = {
243
- name: "hybrid-plugin",
244
- wrapComponent: function(Component) {
245
- return Component;
246
- },
247
- injectProps: function(props) {
248
- return props;
249
- }
250
- };
251
- manager.register(wrapperPlugin);
252
- manager.register(injectorPlugin);
253
- manager.register(hybridPlugin);
254
- var info = manager.getPluginInfo();
255
- expect(info).toHaveLength(3);
256
- expect(info[0]).toEqual({
257
- name: "wrapper-plugin",
258
- hasWrapper: true,
259
- hasInjector: false
260
- });
261
- expect(info[1]).toEqual({
262
- name: "injector-plugin",
263
- hasWrapper: false,
264
- hasInjector: true
265
- });
266
- expect(info[2]).toEqual({
267
- name: "hybrid-plugin",
268
- hasWrapper: true,
269
- hasInjector: true
270
- });
271
- });
272
- it("should remove plugin", function() {
273
- var plugin = {
274
- name: "test-plugin",
275
- wrapComponent: function(Component) {
276
- return Component;
277
- }
278
- };
279
- manager.register(plugin);
280
- expect(manager.getPlugins()).toHaveLength(1);
281
- var removed = manager.removePlugin("test-plugin");
282
- expect(removed).toBe(true);
283
- expect(manager.getPlugins()).toHaveLength(0);
284
- });
285
- it("should return false when removing non-existent plugin", function() {
286
- var removed = manager.removePlugin("non-existent");
287
- expect(removed).toBe(false);
288
- });
289
- it("should clear all plugins", function() {
290
- manager.register({
291
- name: "plugin1",
292
- wrapComponent: function(Component) {
293
- return Component;
294
- }
295
- });
296
- manager.register({
297
- name: "plugin2",
298
- injectProps: function(props) {
299
- return props;
300
- }
301
- });
302
- expect(manager.getPlugins()).toHaveLength(2);
303
- manager.clear();
304
- expect(manager.getPlugins()).toHaveLength(0);
305
- });
306
- });
307
- describe("Global Enhanced Plugin Manager", function() {
308
- beforeEach(function() {
309
- // 清理全局状态
310
- getEnhancedPluginManager().clear();
311
- });
312
- it("should provide singleton instance", function() {
313
- var manager1 = getEnhancedPluginManager();
314
- var manager2 = getEnhancedPluginManager();
315
- expect(manager1).toBe(manager2);
316
- });
317
- it("should register plugin via convenience function", function() {
318
- var plugin = {
319
- name: "convenience-plugin",
320
- wrapComponent: function(Component) {
321
- return Component;
322
- }
323
- };
324
- registerEnhancedPlugin(plugin);
325
- var manager = getEnhancedPluginManager();
326
- expect(manager.getPlugins()).toContainEqual(plugin);
327
- });
328
- it("should register multiple plugins via convenience function", function() {
329
- var plugins = [
330
- {
331
- name: "plugin1",
332
- wrapComponent: function(Component) {
333
- return Component;
334
- }
335
- },
336
- {
337
- name: "plugin2",
338
- injectProps: function(props) {
339
- return props;
340
- }
341
- }
342
- ];
343
- registerEnhancedPlugins(plugins);
344
- var manager = getEnhancedPluginManager();
345
- expect(manager.getPlugins()).toHaveLength(2);
346
- });
347
- });
348
- });
@@ -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,79 +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 plugin 增强运行时插件
68
- */
69
- export declare function registerEnhancedPlugin(plugin: EnhancedRuntimePlugin): void;
70
- /**
71
- * 注册多个增强插件(便捷函数)
72
- * @param plugins 增强插件数组
73
- */
74
- export declare function registerEnhancedPlugins(plugins: EnhancedRuntimePlugin[]): void;
75
- /**
76
- * React Hook:使用增强插件管理器
77
- * @returns 增强插件管理器实例
78
- */
79
- export declare function useEnhancedPluginManager(): EnhancedPluginManager;
@@ -1,207 +0,0 @@
1
- import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
2
- import { _ as _create_class } from "@swc/helpers/_/_create_class";
3
- import { _ as _define_property } from "@swc/helpers/_/_define_property";
4
- import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
5
- import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
6
- import * as React from "react";
7
- /**
8
- * 增强插件管理器 - 专门负责管理和执行增强运行时插件
9
- * 标准 MF 插件交由底层 @module-federation/runtime 管理
10
- */ export var EnhancedPluginManager = /*#__PURE__*/ function() {
11
- "use strict";
12
- function EnhancedPluginManager() {
13
- _class_call_check(this, EnhancedPluginManager);
14
- _define_property(this, "plugins", []);
15
- }
16
- _create_class(EnhancedPluginManager, [
17
- {
18
- /**
19
- * 注册增强插件
20
- * @param plugin 增强运行时插件
21
- */ key: "register",
22
- value: function register(plugin) {
23
- // 检查是否已存在同名插件
24
- var existingIndex = this.plugins.findIndex(function(p) {
25
- return p.name === plugin.name;
26
- });
27
- if (existingIndex >= 0) {
28
- console.warn('Enhanced plugin "'.concat(plugin.name, '" already exists, replacing it.'));
29
- this.plugins[existingIndex] = plugin;
30
- } else {
31
- this.plugins.push(plugin);
32
- }
33
- }
34
- },
35
- {
36
- /**
37
- * 注册多个增强插件
38
- * @param plugins 增强插件数组
39
- */ key: "registerPlugins",
40
- value: function registerPlugins(plugins) {
41
- var _this = this;
42
- plugins.forEach(function(plugin) {
43
- return _this.register(plugin);
44
- });
45
- }
46
- },
47
- {
48
- /**
49
- * 获取所有增强插件列表
50
- * @returns 增强插件数组
51
- */ key: "getPlugins",
52
- value: function getPlugins() {
53
- return _to_consumable_array(this.plugins);
54
- }
55
- },
56
- {
57
- /**
58
- * 应用组件包装器
59
- * 按插件注册顺序依次应用所有组件包装器
60
- * @param Component 原始组件
61
- * @param context 包装器上下文
62
- * @returns 包装后的组件
63
- */ key: "wrapComponent",
64
- value: function wrapComponent(Component, context) {
65
- var WrappedComponent = Component;
66
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
67
- try {
68
- // 按数组顺序依次应用所有组件包装器
69
- for(var _iterator = this.plugins[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
70
- var plugin = _step.value;
71
- if (plugin.wrapComponent) {
72
- try {
73
- WrappedComponent = plugin.wrapComponent(WrappedComponent, context);
74
- } catch (error) {
75
- console.error('Error applying wrapper from plugin "'.concat(plugin.name, '":'), error);
76
- }
77
- }
78
- }
79
- } catch (err) {
80
- _didIteratorError = true;
81
- _iteratorError = err;
82
- } finally{
83
- try {
84
- if (!_iteratorNormalCompletion && _iterator.return != null) {
85
- _iterator.return();
86
- }
87
- } finally{
88
- if (_didIteratorError) {
89
- throw _iteratorError;
90
- }
91
- }
92
- }
93
- return WrappedComponent;
94
- }
95
- },
96
- {
97
- /**
98
- * 注入属性
99
- * 按插件注册顺序依次应用所有属性注入器
100
- * @param props 原始属性
101
- * @param context 注入上下文
102
- * @returns 注入后的属性
103
- */ key: "injectProps",
104
- value: function injectProps(props, context) {
105
- var injectedProps = _object_spread({}, props);
106
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
107
- try {
108
- // 按数组顺序依次应用所有属性注入器
109
- for(var _iterator = this.plugins[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
110
- var plugin = _step.value;
111
- if (plugin.injectProps) {
112
- try {
113
- injectedProps = plugin.injectProps(injectedProps, context);
114
- } catch (error) {
115
- console.error('Error injecting props from plugin "'.concat(plugin.name, '":'), error);
116
- }
117
- }
118
- }
119
- } catch (err) {
120
- _didIteratorError = true;
121
- _iteratorError = err;
122
- } finally{
123
- try {
124
- if (!_iteratorNormalCompletion && _iterator.return != null) {
125
- _iterator.return();
126
- }
127
- } finally{
128
- if (_didIteratorError) {
129
- throw _iteratorError;
130
- }
131
- }
132
- }
133
- return injectedProps;
134
- }
135
- },
136
- {
137
- /**
138
- * 获取所有插件信息
139
- * @returns 插件信息数组
140
- */ key: "getPluginInfo",
141
- value: function getPluginInfo() {
142
- return this.plugins.map(function(plugin) {
143
- return {
144
- name: plugin.name,
145
- hasWrapper: !!plugin.wrapComponent,
146
- hasInjector: !!plugin.injectProps
147
- };
148
- });
149
- }
150
- },
151
- {
152
- /**
153
- * 移除插件
154
- * @param name 插件名称
155
- * @returns 是否成功移除
156
- */ key: "removePlugin",
157
- value: function removePlugin(name) {
158
- var index = this.plugins.findIndex(function(p) {
159
- return p.name === name;
160
- });
161
- if (index !== -1) {
162
- this.plugins.splice(index, 1);
163
- return true;
164
- }
165
- return false;
166
- }
167
- },
168
- {
169
- /**
170
- * 清空所有插件
171
- */ key: "clear",
172
- value: function clear() {
173
- this.plugins = [];
174
- }
175
- }
176
- ]);
177
- return EnhancedPluginManager;
178
- }();
179
- // 全局增强插件管理器实例
180
- var globalEnhancedPluginManager = null;
181
- /**
182
- * 获取全局增强插件管理器实例
183
- * @returns 增强插件管理器实例
184
- */ export function getEnhancedPluginManager() {
185
- if (!globalEnhancedPluginManager) {
186
- globalEnhancedPluginManager = new EnhancedPluginManager();
187
- }
188
- return globalEnhancedPluginManager;
189
- }
190
- /**
191
- * 注册增强插件(便捷函数)
192
- * @param plugin 增强运行时插件
193
- */ export function registerEnhancedPlugin(plugin) {
194
- getEnhancedPluginManager().register(plugin);
195
- }
196
- /**
197
- * 注册多个增强插件(便捷函数)
198
- * @param plugins 增强插件数组
199
- */ export function registerEnhancedPlugins(plugins) {
200
- getEnhancedPluginManager().registerPlugins(plugins);
201
- }
202
- /**
203
- * React Hook:使用增强插件管理器
204
- * @returns 增强插件管理器实例
205
- */ export function useEnhancedPluginManager() {
206
- return getEnhancedPluginManager();
207
- }