@ice/mf-runtime 0.0.2 → 0.0.4

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/README.md CHANGED
@@ -186,7 +186,6 @@ function App() {
186
186
  );
187
187
  }
188
188
  ```
189
-
190
189
  ## API
191
190
 
192
191
  ### init(options)
@@ -217,6 +216,10 @@ RemoteConfig 配置:
217
216
  | runtime | React 运行时配置 | `{ react: React, reactDOM: ReactDOM }` | - |
218
217
  | publicPath | 远程模块静态资源地址 | `string` | - |
219
218
  | LoadingComponent | 加载状态组件 | `ReactNode` | `<div>Loading...</div>` |
219
+ | ErrorComponent | 错误回退组件 | `ReactNode` | `<div>远程模块加载失败: {error.message}</div>` |
220
+ | onError | 错误处理回调函数 | `(error: Error, info: { componentStack: string }) => void` | - |
221
+ | componentProps | 传递给远程模块的属性 | `object` | `{}` |
222
+ | children | 传递给远程模块的子元素 | `ReactNode` | `null` |
220
223
 
221
224
 
222
225
  ## 注意事项
@@ -224,3 +227,74 @@ RemoteConfig 配置:
224
227
  1. host name 请保证全局唯一。
225
228
  2. remote name 的 package.json name 请保证全局唯一,否则会出现意料之外的缓存问题。
226
229
 
230
+ ## 使用示例
231
+
232
+ ### 传递属性和子元素
233
+
234
+ 当远程模块需要接收属性或子元素时,可以使用 `componentProps` 和 `children`:
235
+
236
+ ```tsx
237
+ import { RemoteModule } from '@ice/mf-runtime';
238
+
239
+ function App() {
240
+ return (
241
+ <div>
242
+ {/* 传递属性示例 */}
243
+ <RemoteModule
244
+ module="UserProfile"
245
+ scope="remote_app"
246
+ componentProps={{
247
+ userId: "123",
248
+ showAvatar: true,
249
+ onUserClick: (user) => console.log('用户点击:', user)
250
+ }}
251
+ />
252
+
253
+ {/* 传递子元素示例 */}
254
+ <RemoteModule
255
+ module="Card"
256
+ scope="remote_app"
257
+ componentProps={{
258
+ title: "我的卡片"
259
+ }}
260
+ >
261
+ <h2>这是卡片内容</h2>
262
+ <p>可以传入任意的子元素</p>
263
+ </RemoteModule>
264
+ </div>
265
+ );
266
+ }
267
+ ```
268
+
269
+ 在远程模块中的使用方式:
270
+
271
+ ```tsx
272
+ // 远程模块: UserProfile.tsx
273
+ interface UserProfileProps {
274
+ userId: string;
275
+ showAvatar: boolean;
276
+ onUserClick: (user: any) => void;
277
+ }
278
+
279
+ export default function UserProfile(props: UserProfileProps) {
280
+ const { userId, showAvatar, onUserClick } = props;
281
+ // ... 组件实现
282
+ }
283
+
284
+ // 远程模块: Card.tsx
285
+ interface CardProps {
286
+ title: string;
287
+ children?: React.ReactNode;
288
+ }
289
+
290
+ export default function Card({ title, children }: CardProps) {
291
+ return (
292
+ <div className="card">
293
+ <div className="card-header">{title}</div>
294
+ <div className="card-body">
295
+ {children}
296
+ </div>
297
+ </div>
298
+ );
299
+ }
300
+ ```
@@ -8,6 +8,12 @@ interface RemoteModuleOptions {
8
8
  };
9
9
  publicPath?: string;
10
10
  LoadingComponent?: React.ReactNode;
11
+ ErrorComponent?: React.ReactNode;
12
+ onError?: (error: Error, info: {
13
+ componentStack: string;
14
+ }) => void;
15
+ componentProps?: any;
16
+ children?: React.ReactNode;
11
17
  }
12
- export declare const RemoteModule: ({ module, scope, runtime, publicPath, LoadingComponent, }: RemoteModuleOptions) => string | number | true | Iterable<React.ReactNode> | React.JSX.Element;
18
+ export declare const RemoteModule: ({ module, scope, runtime, publicPath, LoadingComponent, ErrorComponent, onError, componentProps, children, }: RemoteModuleOptions) => string | number | true | Iterable<React.ReactNode> | React.JSX.Element;
13
19
  export {};
@@ -1,7 +1,10 @@
1
- import { jsx as _jsx } from "@ice/jsx-runtime/jsx-runtime";
1
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
+ import { jsx as _jsx, jsxs as _jsxs } from "@ice/jsx-runtime/jsx-runtime";
2
4
  import { loadRemote } from '@module-federation/runtime';
3
5
  import * as React from 'react';
4
6
  import { useEffect, useState } from 'react';
7
+ import { ErrorBoundary } from 'react-error-boundary';
5
8
  import { FallBack } from './FallBack';
6
9
  import { setFederatedModulePublicPath } from './set-public-path';
7
10
  function useDynamicImport({ module, scope }) {
@@ -23,7 +26,7 @@ function useDynamicImport({ module, scope }) {
23
26
  ]);
24
27
  return component;
25
28
  }
26
- export const RemoteModule = ({ module, scope, runtime, publicPath, LoadingComponent })=>{
29
+ export const RemoteModule = ({ module, scope, runtime, publicPath, LoadingComponent, ErrorComponent, onError, componentProps = {}, children = null })=>{
27
30
  var _remoteModule;
28
31
  if (publicPath) {
29
32
  setFederatedModulePublicPath(scope, publicPath);
@@ -53,10 +56,22 @@ export const RemoteModule = ({ module, scope, runtime, publicPath, LoadingCompon
53
56
  const Loading = LoadingComponent || /*#__PURE__*/ _jsx("div", {
54
57
  children: "Loading..."
55
58
  });
59
+ const ErrorFallback = ({ error })=>ErrorComponent || /*#__PURE__*/ _jsxs("div", {
60
+ children: [
61
+ "远程模块加载失败: ",
62
+ error.message
63
+ ]
64
+ });
56
65
  if (Component) {
57
- return /*#__PURE__*/ _jsx(React.Suspense, {
58
- fallback: Loading,
59
- children: /*#__PURE__*/ _jsx(Component, {})
66
+ return /*#__PURE__*/ _jsx(ErrorBoundary, {
67
+ FallbackComponent: ErrorFallback,
68
+ onError: onError,
69
+ children: /*#__PURE__*/ _jsx(React.Suspense, {
70
+ fallback: Loading,
71
+ children: /*#__PURE__*/ _jsx(Component, _object_spread_props(_object_spread({}, componentProps), {
72
+ children: children
73
+ }))
74
+ })
60
75
  });
61
76
  }
62
77
  return Loading;
@@ -8,6 +8,12 @@ interface RemoteModuleOptions {
8
8
  };
9
9
  publicPath?: string;
10
10
  LoadingComponent?: React.ReactNode;
11
+ ErrorComponent?: React.ReactNode;
12
+ onError?: (error: Error, info: {
13
+ componentStack: string;
14
+ }) => void;
15
+ componentProps?: any;
16
+ children?: React.ReactNode;
11
17
  }
12
- export declare const RemoteModule: ({ module, scope, runtime, publicPath, LoadingComponent, }: RemoteModuleOptions) => string | number | true | Iterable<React.ReactNode> | React.JSX.Element;
18
+ export declare const RemoteModule: ({ module, scope, runtime, publicPath, LoadingComponent, ErrorComponent, onError, componentProps, children, }: RemoteModuleOptions) => string | number | true | Iterable<React.ReactNode> | React.JSX.Element;
13
19
  export {};
@@ -1,10 +1,13 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
3
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
2
4
  import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
3
5
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
- import { jsx as _jsx } from "@ice/jsx-runtime/jsx-runtime";
6
+ import { jsx as _jsx, jsxs as _jsxs } from "@ice/jsx-runtime/jsx-runtime";
5
7
  import { loadRemote } from "@module-federation/runtime";
6
8
  import * as React from "react";
7
9
  import { useEffect, useState } from "react";
10
+ import { ErrorBoundary } from "react-error-boundary";
8
11
  import { FallBack } from "./FallBack";
9
12
  import { setFederatedModulePublicPath } from "./set-public-path";
10
13
  function useDynamicImport(param) {
@@ -61,7 +64,7 @@ function useDynamicImport(param) {
61
64
  return component;
62
65
  }
63
66
  export var RemoteModule = function(param) {
64
- var module = param.module, scope = param.scope, runtime = param.runtime, publicPath = param.publicPath, LoadingComponent = param.LoadingComponent;
67
+ var module = param.module, scope = param.scope, runtime = param.runtime, publicPath = param.publicPath, LoadingComponent = param.LoadingComponent, ErrorComponent = param.ErrorComponent, onError = param.onError, _param_componentProps = param.componentProps, componentProps = _param_componentProps === void 0 ? {} : _param_componentProps, _param_children = param.children, children = _param_children === void 0 ? null : _param_children;
65
68
  var _remoteModule;
66
69
  if (publicPath) {
67
70
  setFederatedModulePublicPath(scope, publicPath);
@@ -97,10 +100,25 @@ export var RemoteModule = function(param) {
97
100
  var Loading = LoadingComponent || /*#__PURE__*/ _jsx("div", {
98
101
  children: "Loading..."
99
102
  });
103
+ var ErrorFallback = function(param) {
104
+ var error = param.error;
105
+ return ErrorComponent || /*#__PURE__*/ _jsxs("div", {
106
+ children: [
107
+ "远程模块加载失败: ",
108
+ error.message
109
+ ]
110
+ });
111
+ };
100
112
  if (Component) {
101
- return /*#__PURE__*/ _jsx(React.Suspense, {
102
- fallback: Loading,
103
- children: /*#__PURE__*/ _jsx(Component, {})
113
+ return /*#__PURE__*/ _jsx(ErrorBoundary, {
114
+ FallbackComponent: ErrorFallback,
115
+ onError: onError,
116
+ children: /*#__PURE__*/ _jsx(React.Suspense, {
117
+ fallback: Loading,
118
+ children: /*#__PURE__*/ _jsx(Component, _object_spread_props(_object_spread({}, componentProps), {
119
+ children: children
120
+ }))
121
+ })
104
122
  });
105
123
  }
106
124
  return Loading;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ice/mf-runtime",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "ice mf runtime",
5
5
  "files": [
6
6
  "esm",
@@ -48,7 +48,8 @@
48
48
  "@ice/jsx-runtime": "^0.2.0",
49
49
  "@module-federation/runtime": "^0.11.2",
50
50
  "@module-federation/runtime-core": "^0.11.2",
51
- "@swc/helpers": "^0.5.1"
51
+ "@swc/helpers": "^0.5.1",
52
+ "react-error-boundary": "^5.0.0"
52
53
  },
53
54
  "devDependencies": {
54
55
  "@ali/pkg-plugin-dev": "^1.0.0",