@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/esm/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
- registerPlugins([
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,201 @@
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 plugins 增强插件数组
193
+ */ export function registerEnhancedPlugins(plugins) {
194
+ getEnhancedPluginManager().registerPlugins(plugins);
195
+ }
196
+ /**
197
+ * React Hook:使用增强插件管理器
198
+ * @returns 增强插件管理器实例
199
+ */ export function useEnhancedPluginManager() {
200
+ return getEnhancedPluginManager();
201
+ }
@@ -2,10 +2,10 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
3
  import * as React from "react";
4
4
  import { getExtraInfo, getRemoteInfoFromStore, hasConflict } from "./mf-global-store";
5
- import { FallBack } from "./FallBack";
5
+ import { createRuntimePluginWrapper } from "./types";
6
6
  var loadRemotePackagedReactAndRender = function() {
7
7
  var _ref = _async_to_generator(function(args) {
8
- 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, hostVersion, remoteInstance, remoteVersion, _sharedOptions_find, remoteReactDOM, _sharedOptions_find1, remoteReact, wrappedRender;
8
+ 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, hostVersion, remoteInstance, remoteVersion, _sharedOptions_find, remoteReactDOM, _sharedOptions_find1, remoteReact;
9
9
  return _ts_generator(this, function(_state) {
10
10
  switch(_state.label){
11
11
  case 0:
@@ -23,8 +23,8 @@ var loadRemotePackagedReactAndRender = function() {
23
23
  4,
24
24
  remoteInstance.loadShare("react-dom", {
25
25
  resolver: function(sharedOptions) {
26
- return (_sharedOptions_find = sharedOptions.find(function(i) {
27
- return i.version === remoteVersion;
26
+ return (_sharedOptions_find = sharedOptions.find(function(option) {
27
+ return option.version === remoteVersion;
28
28
  })) !== null && _sharedOptions_find !== void 0 ? _sharedOptions_find : sharedOptions[0];
29
29
  }
30
30
  })
@@ -35,8 +35,8 @@ var loadRemotePackagedReactAndRender = function() {
35
35
  4,
36
36
  remoteInstance.loadShare("react", {
37
37
  resolver: function(sharedOptions) {
38
- return (_sharedOptions_find1 = sharedOptions.find(function(i) {
39
- return i.version === remoteVersion;
38
+ return (_sharedOptions_find1 = sharedOptions.find(function(option) {
39
+ return option.version === remoteVersion;
40
40
  })) !== null && _sharedOptions_find1 !== void 0 ? _sharedOptions_find1 : sharedOptions[0];
41
41
  }
42
42
  })
@@ -49,26 +49,22 @@ var loadRemotePackagedReactAndRender = function() {
49
49
  null
50
50
  ];
51
51
  }
52
- wrappedRender = function(mountNode, containerClassName) {
53
- return FallBack({
54
- Original: args.exposeModule.default,
55
- remoteVersion: function() {
56
- return remoteVersion;
57
- },
58
- hostVersion: function() {
59
- return hostVersion;
60
- },
61
- remoteReactDOM: remoteReactDOM,
62
- remoteReact: remoteReact,
63
- mountNode: mountNode,
64
- containerClassName: containerClassName
65
- });
66
- };
67
- // 添加标记,用于在 RemoteModule 中识别
68
- wrappedRender.__ICE_MF_RUNTIME_WRAPPER__ = true;
52
+ // 创建 Runtime Plugin 包装器对象,返回原始组件和运行时信息
53
+ // 让 RemoteModule 控制 HOC 的正确层级顺序
69
54
  return [
70
55
  2,
71
- wrappedRender
56
+ createRuntimePluginWrapper(// 原始组件
57
+ args.exposeModule.default, // 运行时信息
58
+ {
59
+ remoteVersion: remoteVersion,
60
+ hostVersion: hostVersion,
61
+ remoteReact: remoteReact,
62
+ remoteReactDOM: remoteReactDOM
63
+ }, // React 实例信息
64
+ {
65
+ react: remoteReact(),
66
+ reactDOM: remoteReactDOM()
67
+ })
72
68
  ];
73
69
  case 3:
74
70
  return [
@@ -99,7 +95,7 @@ export var runtimePlugin = function() {
99
95
  },
100
96
  onLoad: function onLoad(args) {
101
97
  return _async_to_generator(function() {
102
- var _args_origin_options_shared_reactdom_, _args_origin_options_shared_reactdom, _args_origin_options_shared, _args_origin_options, _extraInfo, hostName, remoteName, hostVersion, extraInfo, externalReact, externalReactDOM, remoteReact, remoteReactDOM, wrappedRender, fallBackRender;
98
+ var _args_origin_options_shared_reactdom_, _args_origin_options_shared_reactdom, _args_origin_options_shared, _args_origin_options, _extraInfo, hostName, remoteName, hostVersion, extraInfo, externalReact, externalReactDOM, remoteReact, remoteReactDOM, fallBackRender;
103
99
  return _ts_generator(this, function(_state) {
104
100
  switch(_state.label){
105
101
  case 0:
@@ -124,20 +120,20 @@ export var runtimePlugin = function() {
124
120
  args
125
121
  ];
126
122
  }
127
- wrappedRender = function(mountNode, containerClassName) {
128
- return FallBack({
129
- Original: args.exposeModule.default,
130
- remoteReactDOM: remoteReactDOM,
131
- remoteReact: remoteReact,
132
- mountNode: mountNode,
133
- containerClassName: containerClassName
134
- });
135
- };
136
- // 添加标记,用于在 RemoteModule 中识别
137
- wrappedRender.__ICE_MF_RUNTIME_WRAPPER__ = true;
123
+ // 创建 Runtime Plugin 包装器对象,返回原始组件和运行时信息
124
+ // 让 RemoteModule 控制 HOC 的正确层级顺序
138
125
  return [
139
126
  2,
140
- wrappedRender
127
+ createRuntimePluginWrapper(// 原始组件
128
+ args.exposeModule.default, // 运行时信息
129
+ {
130
+ remoteReact: remoteReact,
131
+ remoteReactDOM: remoteReactDOM
132
+ }, // React 实例信息
133
+ {
134
+ react: remoteReact(),
135
+ reactDOM: remoteReactDOM()
136
+ })
141
137
  ];
142
138
  }
143
139
  }
package/esm/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/esm/types.js CHANGED
@@ -1 +1,20 @@
1
- export { };
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
+ var wrapper = function() {
11
+ return {
12
+ type: "runtime-plugin-wrapper",
13
+ originalComponent: originalComponent,
14
+ runtimeInfo: runtimeInfo,
15
+ reactInstances: reactInstances
16
+ };
17
+ };
18
+ wrapper.__mf_fallback_render = true;
19
+ return wrapper;
20
+ } // 不再需要联合类型,两种插件分开管理
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@ice/mf-runtime",
3
- "version": "1.0.2",
3
+ "version": "1.0.3-beta.2",
4
4
  "description": "ice mf runtime",
5
5
  "files": [
6
6
  "esm",
7
7
  "es2017",
8
8
  "cjs",
9
- "dist"
9
+ "dist",
10
+ "docs"
10
11
  ],
11
12
  "main": "esm/index.js",
12
13
  "module": "esm/index.js",
@@ -34,6 +35,9 @@
34
35
  "start": "ice-pkg start",
35
36
  "build": "ice-pkg build",
36
37
  "prepublishOnly": "npm run build",
38
+ "test": "vitest",
39
+ "test:run": "vitest run",
40
+ "test:coverage": "vitest run --coverage",
37
41
  "eslint": "eslint --cache --ext .js,.jsx,.ts,.tsx ./",
38
42
  "eslint:fix": "npm run eslint -- --fix",
39
43
  "stylelint": "stylelint \"**/*.{css,scss,less}\"",
@@ -57,12 +61,19 @@
57
61
  "@applint/spec": "^1.2.3",
58
62
  "@ice/pkg": "^1.0.0",
59
63
  "@ice/runtime": "^1.0.0",
64
+ "@ice/stark-app": "^1.5.0",
65
+ "@testing-library/jest-dom": "^6.0.0",
66
+ "@testing-library/react": "^14.0.0",
60
67
  "@types/react": "^18.0.0",
61
68
  "@types/react-dom": "^18.0.0",
69
+ "@vitest/coverage-v8": "^1.0.0",
62
70
  "eslint": "^8.0.0",
71
+ "jsdom": "^23.0.0",
63
72
  "react": "^18.0.0",
64
73
  "react-dom": "^18.0.0",
65
- "stylelint": "^15.0.0"
74
+ "stylelint": "^15.0.0",
75
+ "typescript": "^5.0.0",
76
+ "vitest": "^1.0.0"
66
77
  },
67
78
  "publishConfig": {
68
79
  "access": "public",