@ice/mf-runtime 1.0.3-beta.1 → 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.
@@ -0,0 +1,294 @@
1
+ # 微前端插件系统使用指南
2
+
3
+ ## 概述
4
+
5
+ `@ice/mf-runtime` 提供了一套增强插件系统,允许开发者对远程组件进行功能扩展,如错误边界、日志记录、性能监控等。
6
+
7
+ ## 快速开始
8
+
9
+ ### 1. 安装依赖
10
+
11
+ ```bash
12
+ npm install @ice/mf-runtime
13
+ ```
14
+
15
+ ### 2. 基本使用
16
+
17
+ ```tsx
18
+ import { RemoteModule, registerEnhancedPlugins } from '@ice/mf-runtime';
19
+ import { createEnhancedLoggingPlugin } from '@ice/mf-plugin-example';
20
+
21
+ // 注册插件
22
+ registerEnhancedPlugins([createEnhancedLoggingPlugin()]);
23
+
24
+ // 使用远程组件
25
+ function App() {
26
+ return (
27
+ <RemoteModule
28
+ scope="remote-app"
29
+ module="Button"
30
+ />
31
+ );
32
+ }
33
+ ```
34
+
35
+ ## 插件开发
36
+
37
+ ### 插件接口
38
+
39
+ ```typescript
40
+ export interface EnhancedRuntimePlugin {
41
+ name: string;
42
+ wrapComponent?: ComponentWrapper;
43
+ injectProps?: PropInjector;
44
+ }
45
+
46
+ export interface ComponentWrapper {
47
+ (
48
+ WrappedComponent: React.ComponentType<any>,
49
+ context: WrapperContext
50
+ ): React.ComponentType<any>;
51
+ }
52
+
53
+ export interface PropInjector {
54
+ (
55
+ props: Record<string, any>,
56
+ context: InjectionContext
57
+ ): Record<string, any>;
58
+ }
59
+ ```
60
+
61
+ ### 上下文信息
62
+
63
+ ```typescript
64
+ export interface WrapperContext {
65
+ remoteName: string;
66
+ moduleName: string;
67
+ props: Record<string, any>;
68
+ React: typeof import('react'); // 正确的 React 实例
69
+ ReactDOM?: typeof import('react-dom'); // 正确的 ReactDOM 实例
70
+ }
71
+
72
+ export interface InjectionContext {
73
+ remoteName: string;
74
+ moduleName: string;
75
+ }
76
+ ```
77
+
78
+ ### 创建插件
79
+
80
+ ```typescript
81
+ import type { EnhancedRuntimePlugin, WrapperContext } from '@ice/mf-runtime';
82
+
83
+ export function createMyPlugin(options = {}): EnhancedRuntimePlugin {
84
+ return {
85
+ name: 'my-plugin',
86
+
87
+ wrapComponent: (WrappedComponent, context: WrapperContext) => {
88
+ // 🔑 使用上下文提供的 React 实例
89
+ const { React: ContextReact } = context;
90
+
91
+ const MyWrapper = ContextReact.forwardRef<any, any>((props, ref) => {
92
+ ContextReact.useEffect(() => {
93
+ console.log(`Component ${context.remoteName}/${context.moduleName} mounted`);
94
+ }, []);
95
+
96
+ return ContextReact.createElement(WrappedComponent, { ...props, ref });
97
+ });
98
+
99
+ MyWrapper.displayName = `MyWrapper(${WrappedComponent.displayName || 'Component'})`;
100
+ return MyWrapper;
101
+ },
102
+
103
+ injectProps: (props, context) => {
104
+ return {
105
+ ...props,
106
+ pluginData: 'injected by my plugin'
107
+ };
108
+ }
109
+ };
110
+ }
111
+ ```
112
+
113
+ ## 内置插件示例
114
+
115
+ ### 日志记录插件
116
+
117
+ ```typescript
118
+ import {
119
+ createEnhancedLoggingPlugin,
120
+ developmentEnhancedLoggingPlugin,
121
+ productionEnhancedLoggingPlugin
122
+ } from '@ice/mf-plugin-example';
123
+
124
+ // 自定义配置
125
+ registerEnhancedPlugins([createEnhancedLoggingPlugin({
126
+ level: 'info',
127
+ logComponentRender: true,
128
+ enablePerformanceMonitoring: true
129
+ })]);
130
+
131
+ // 或使用预定义插件
132
+ registerEnhancedPlugins([
133
+ process.env.NODE_ENV === 'development'
134
+ ? developmentEnhancedLoggingPlugin
135
+ : productionEnhancedLoggingPlugin
136
+ ]);
137
+ ```
138
+
139
+ ### 属性注入插件
140
+
141
+ ```typescript
142
+ import { createPropsInjectionPlugin } from '@ice/mf-plugin-example';
143
+
144
+ registerEnhancedPlugins([createPropsInjectionPlugin({
145
+ globalProps: {
146
+ theme: 'dark',
147
+ version: '1.0.0'
148
+ }
149
+ })]);
150
+ ```
151
+
152
+ ## 插件管理
153
+
154
+ ### 注册插件
155
+
156
+ ```typescript
157
+ import { registerEnhancedPlugins } from '@ice/mf-runtime';
158
+
159
+ // 注册多个插件(推荐方式)
160
+ registerEnhancedPlugins([plugin1, plugin2, plugin3]);
161
+
162
+ // 或通过插件管理器注册
163
+ import { getEnhancedPluginManager } from '@ice/mf-runtime';
164
+ const manager = getEnhancedPluginManager();
165
+ manager.register(myPlugin);
166
+ manager.registerPlugins([plugin1, plugin2]);
167
+ ```
168
+
169
+ ### 获取插件管理器
170
+
171
+ ```typescript
172
+ import { getEnhancedPluginManager } from '@ice/mf-runtime';
173
+
174
+ const manager = getEnhancedPluginManager();
175
+
176
+ // 获取所有插件
177
+ const plugins = manager.getPlugins();
178
+
179
+ // 获取插件信息
180
+ const info = manager.getPluginInfo();
181
+
182
+ // 移除插件
183
+ manager.removePlugin('plugin-name');
184
+
185
+ // 清空所有插件
186
+ manager.clear();
187
+ ```
188
+
189
+ ### 在组件中使用
190
+
191
+ ```typescript
192
+ import { useEnhancedPluginManager } from '@ice/mf-runtime';
193
+
194
+ function MyComponent() {
195
+ const pluginManager = useEnhancedPluginManager();
196
+
197
+ // 动态注册插件
198
+ React.useEffect(() => {
199
+ pluginManager.register(createMyPlugin());
200
+ }, []);
201
+
202
+ return <div>My Component</div>;
203
+ }
204
+ ```
205
+
206
+ ## 远程组件配置
207
+
208
+ ### 基本配置
209
+
210
+ ```tsx
211
+ <RemoteModule
212
+ scope="remote-app"
213
+ module="Button"
214
+ componentProps={{ text: 'Click me' }}
215
+ />
216
+ ```
217
+
218
+
219
+ ## 最佳实践
220
+
221
+ ### 1. React 实例一致性
222
+
223
+ 始终使用上下文提供的 React 实例,避免使用 JSX 语法:
224
+
225
+ ```typescript
226
+ // ✅ 正确 - 使用上下文的 React 实例
227
+ wrapComponent: (WrappedComponent, context) => {
228
+ const { React: ContextReact } = context;
229
+ return ContextReact.forwardRef((props, ref) => {
230
+ // 使用 createElement 确保实例一致性
231
+ return ContextReact.createElement(WrappedComponent, { ...props, ref });
232
+ });
233
+ }
234
+
235
+ // ❌ 错误 - 使用导入的 React 实例
236
+ import React from 'react';
237
+ wrapComponent: (WrappedComponent, context) => {
238
+ return React.forwardRef((props, ref) => { // 可能是错误的 React 实例
239
+ return <WrappedComponent {...props} ref={ref} />; // JSX 使用编译时的 React
240
+ });
241
+ }
242
+ ```
243
+
244
+ **为什么不能直接用 JSX?**
245
+
246
+ 在微前端场景中:
247
+ - 主应用和远程模块可能使用不同版本的 React
248
+ - JSX 编译时绑定到特定的 React 实例
249
+ - 使用 `ContextReact.createElement` 确保运行时使用正确的实例
250
+
251
+ ### 2. Ref 转发
252
+
253
+ 确保正确转发 ref:
254
+
255
+ ```typescript
256
+ const MyWrapper = ContextReact.forwardRef<any, any>((props, ref) => {
257
+ return ContextReact.createElement(WrappedComponent, { ...props, ref });
258
+ });
259
+ ```
260
+
261
+ ## 故障排除
262
+
263
+ ### 常见问题
264
+
265
+ 1. **插件不生效**
266
+ - 确保插件已正确注册
267
+ - 检查插件名称是否唯一
268
+ - 验证插件函数是否正确实现
269
+
270
+ 2. **React Hook 错误**
271
+ - 确保使用上下文提供的 React 实例
272
+ - 检查是否在条件语句中使用 Hook
273
+
274
+ 3. **Ref 转发失败**
275
+ - 确保使用 `React.forwardRef`
276
+ - 检查 ref 是否正确传递给底层组件
277
+
278
+ 4. **类型错误**
279
+ - 确保导入正确的类型定义
280
+ - 使用 `React.ComponentType<any>` 处理动态组件类型
281
+
282
+ ### 调试技巧
283
+
284
+ ```typescript
285
+ // 启用调试日志
286
+ registerEnhancedPlugin(createEnhancedLoggingPlugin({
287
+ level: 'debug',
288
+ logComponentRender: true
289
+ }));
290
+
291
+ // 检查插件状态
292
+ const manager = getEnhancedPluginManager();
293
+ console.log('Registered plugins:', manager.getPluginInfo());
294
+ ```
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@ice/mf-runtime",
3
- "version": "1.0.3-beta.1",
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",