@ice/mf-runtime 1.0.1 → 1.0.2-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/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
- registerPlugins([
13
+ mfRegisterPlugins([
12
14
  runtimePlugin()
13
15
  ]);
14
16
  }
@@ -0,0 +1,79 @@
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;
@@ -0,0 +1,134 @@
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 plugin 增强运行时插件
120
+ */ export function registerEnhancedPlugin(plugin) {
121
+ getEnhancedPluginManager().register(plugin);
122
+ }
123
+ /**
124
+ * 注册多个增强插件(便捷函数)
125
+ * @param plugins 增强插件数组
126
+ */ export function registerEnhancedPlugins(plugins) {
127
+ getEnhancedPluginManager().registerPlugins(plugins);
128
+ }
129
+ /**
130
+ * React Hook:使用增强插件管理器
131
+ * @returns 增强插件管理器实例
132
+ */ export function useEnhancedPluginManager() {
133
+ return getEnhancedPluginManager();
134
+ }
@@ -1,3 +1,4 @@
1
+ import { loadShare } from '@module-federation/runtime';
1
2
  import * as React from 'react';
2
3
  import { getExtraInfo, getRemoteInfoFromStore, hasConflict } from './mf-global-store';
3
4
  import { FallBack } from './FallBack';
@@ -9,11 +10,11 @@ const loadRemotePackagedReactAndRender = async (args)=>{
9
10
  const remoteVersion = remoteInstance ? (_remoteInstance_options = remoteInstance.options) === null || _remoteInstance_options === void 0 ? void 0 : (_remoteInstance_options_shared = _remoteInstance_options.shared) === null || _remoteInstance_options_shared === void 0 ? void 0 : (_remoteInstance_options_shared_reactdom = _remoteInstance_options_shared['react-dom']) === null || _remoteInstance_options_shared_reactdom === void 0 ? void 0 : (_remoteInstance_options_shared_reactdom_ = _remoteInstance_options_shared_reactdom[0]) === null || _remoteInstance_options_shared_reactdom_ === void 0 ? void 0 : _remoteInstance_options_shared_reactdom_.version : false;
10
11
  if (remoteVersion && hostVersion) {
11
12
  var _sharedOptions_find;
12
- const remoteReactDOM = await remoteInstance.loadShare('react-dom', {
13
+ const remoteReactDOM = await loadShare('react-dom', {
13
14
  resolver: (sharedOptions)=>(_sharedOptions_find = sharedOptions.find((i)=>i.version === remoteVersion)) !== null && _sharedOptions_find !== void 0 ? _sharedOptions_find : sharedOptions[0]
14
15
  });
15
16
  var _sharedOptions_find1;
16
- const remoteReact = await remoteInstance.loadShare('react', {
17
+ const remoteReact = await loadShare('react', {
17
18
  resolver: (sharedOptions)=>(_sharedOptions_find1 = sharedOptions.find((i)=>i.version === remoteVersion)) !== null && _sharedOptions_find1 !== void 0 ? _sharedOptions_find1 : sharedOptions[0]
18
19
  });
19
20
  if (!remoteReact || !remoteReactDOM) {
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,29 @@ 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
+ }
33
+ export interface InjectionContext {
34
+ remoteName: string;
35
+ moduleName: string;
36
+ }
37
+ export interface ComponentWrapper {
38
+ (WrappedComponent: React.ComponentType<any>, context: WrapperContext): React.ComponentType<any>;
39
+ }
40
+ export interface PropInjector {
41
+ (props: Record<string, any>, context: InjectionContext): Record<string, any>;
42
+ }
43
+ export interface EnhancedRuntimePlugin {
44
+ name: string;
45
+ wrapComponent?: ComponentWrapper;
46
+ injectProps?: PropInjector;
47
+ }
package/es2017/types.js CHANGED
@@ -1 +1,2 @@
1
- export { };
1
+ import * as React from 'react';
2
+ // 不再需要联合类型,两种插件分开管理
@@ -10,6 +10,7 @@ import { ErrorBoundary } from "react-error-boundary";
10
10
  import { FallBack } from "./FallBack";
11
11
  import { setFederatedModulePublicPath } from "./set-public-path";
12
12
  import { getMicroMod } from "./mf-global-store";
13
+ import { useEnhancedPluginManager } from "./plugin-manager";
13
14
  var useMountNode = function(mountNodeConfig) {
14
15
  var _useState = _sliced_to_array(useState(undefined), 2), resolvedNode = _useState[0], setResolvedNode = _useState[1];
15
16
  // 解析各种类型的 mountNode
@@ -40,6 +41,7 @@ var RemoteModuleInner = function(param, ref) {
40
41
  var _microMod, _runtime, _runtime1;
41
42
  var microMod = getMicroMod(scope);
42
43
  var resolvedMountNode = useMountNode(mountNode);
44
+ var enhancedPluginManager = useEnhancedPluginManager();
43
45
  if ((_microMod = microMod) === null || _microMod === void 0 ? void 0 : _microMod.publicPath) {
44
46
  setFederatedModulePublicPath(microMod.moduleFederatedName, microMod.publicPath);
45
47
  }
@@ -49,7 +51,7 @@ var RemoteModuleInner = function(param, ref) {
49
51
  var Component = useMemo(function() {
50
52
  if (!module || !scope) return null;
51
53
  return /*#__PURE__*/ React.lazy(/*#__PURE__*/ _async_to_generator(function() {
52
- var _typedRemoteModule, remoteModule, ComponentFromPlugin, typedRemoteModule, react, reactDOM;
54
+ var _typedRemoteModule, remoteModule, ComponentFromPlugin, typedRemoteModule, BaseComponent, WrappedComponent, FinalComponent, react, reactDOM;
53
55
  return _ts_generator(this, function(_state) {
54
56
  switch(_state.label){
55
57
  case 0:
@@ -71,36 +73,35 @@ var RemoteModuleInner = function(param, ref) {
71
73
  }
72
74
  typedRemoteModule = remoteModule;
73
75
  if (!((_typedRemoteModule = typedRemoteModule) === null || _typedRemoteModule === void 0 ? void 0 : _typedRemoteModule.default)) {
74
- return [
75
- 2,
76
- {
77
- default: remoteModule
78
- }
79
- ];
76
+ BaseComponent = remoteModule;
77
+ } else {
78
+ BaseComponent = typedRemoteModule.default;
80
79
  }
80
+ WrappedComponent = enhancedPluginManager.wrapComponent(BaseComponent, {
81
+ remoteName: scope,
82
+ moduleName: module,
83
+ props: componentProps || {}
84
+ });
81
85
  if (runtime) {
82
86
  react = runtime.react, reactDOM = runtime.reactDOM;
83
- return [
84
- 2,
85
- {
86
- default: FallBack({
87
- Original: typedRemoteModule.default,
88
- remoteReact: function() {
89
- return react;
90
- },
91
- remoteReactDOM: function() {
92
- return reactDOM;
93
- },
94
- mountNode: resolvedMountNode,
95
- containerClassName: fallbackContainerClassName
96
- })
97
- }
98
- ];
87
+ FinalComponent = FallBack({
88
+ Original: WrappedComponent,
89
+ remoteReact: function() {
90
+ return react;
91
+ },
92
+ remoteReactDOM: function() {
93
+ return reactDOM;
94
+ },
95
+ mountNode: resolvedMountNode,
96
+ containerClassName: fallbackContainerClassName
97
+ });
98
+ } else {
99
+ FinalComponent = WrappedComponent;
99
100
  }
100
101
  return [
101
102
  2,
102
103
  {
103
- default: typedRemoteModule.default
104
+ default: FinalComponent
104
105
  }
105
106
  ];
106
107
  }
@@ -112,7 +113,9 @@ var RemoteModuleInner = function(param, ref) {
112
113
  (_runtime = runtime) === null || _runtime === void 0 ? void 0 : _runtime.react,
113
114
  (_runtime1 = runtime) === null || _runtime1 === void 0 ? void 0 : _runtime1.reactDOM,
114
115
  resolvedMountNode,
115
- fallbackContainerClassName
116
+ fallbackContainerClassName,
117
+ enhancedPluginManager,
118
+ componentProps
116
119
  ]);
117
120
  var Loading = LoadingComponent || /*#__PURE__*/ React.createElement("div", null, "Loading...");
118
121
  var ErrorFallback = function(param) {
@@ -120,6 +123,11 @@ var RemoteModuleInner = function(param, ref) {
120
123
  return ErrorComponent || /*#__PURE__*/ React.createElement("div", null, "远程模块加载失败: ", error.message);
121
124
  };
122
125
  if (!Component) return Loading;
126
+ // 应用增强插件的属性注入
127
+ var injectedProps = enhancedPluginManager.injectProps(componentProps || {}, {
128
+ remoteName: scope,
129
+ moduleName: module
130
+ });
123
131
  return /*#__PURE__*/ React.createElement(ErrorBoundary, {
124
132
  resetKeys: [
125
133
  module,
@@ -132,7 +140,7 @@ var RemoteModuleInner = function(param, ref) {
132
140
  fallback: Loading
133
141
  }, /*#__PURE__*/ React.createElement(Component, _object_spread({
134
142
  ref: ref
135
- }, componentProps), children)));
143
+ }, injectedProps), children)));
136
144
  };
137
145
  // 使用 forwardRef 包装组件以支持 ref
138
146
  export var RemoteModule = /*#__PURE__*/ forwardRef(RemoteModuleInner);
@@ -0,0 +1 @@
1
+ export {};