@ice/mf-runtime 0.0.3 → 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 +74 -0
- package/es2017/RemoteModule.d.ts +3 -1
- package/es2017/RemoteModule.js +6 -2
- package/esm/RemoteModule.d.ts +3 -1
- package/esm/RemoteModule.js +6 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -218,9 +218,83 @@ RemoteConfig 配置:
|
|
|
218
218
|
| LoadingComponent | 加载状态组件 | `ReactNode` | `<div>Loading...</div>` |
|
|
219
219
|
| ErrorComponent | 错误回退组件 | `ReactNode` | `<div>远程模块加载失败: {error.message}</div>` |
|
|
220
220
|
| onError | 错误处理回调函数 | `(error: Error, info: { componentStack: string }) => void` | - |
|
|
221
|
+
| componentProps | 传递给远程模块的属性 | `object` | `{}` |
|
|
222
|
+
| children | 传递给远程模块的子元素 | `ReactNode` | `null` |
|
|
221
223
|
|
|
222
224
|
|
|
223
225
|
## 注意事项
|
|
224
226
|
|
|
225
227
|
1. host name 请保证全局唯一。
|
|
226
228
|
2. remote name 的 package.json name 请保证全局唯一,否则会出现意料之外的缓存问题。
|
|
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
|
+
```
|
package/es2017/RemoteModule.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ interface RemoteModuleOptions {
|
|
|
12
12
|
onError?: (error: Error, info: {
|
|
13
13
|
componentStack: string;
|
|
14
14
|
}) => void;
|
|
15
|
+
componentProps?: any;
|
|
16
|
+
children?: React.ReactNode;
|
|
15
17
|
}
|
|
16
|
-
export declare const RemoteModule: ({ module, scope, runtime, publicPath, LoadingComponent, ErrorComponent, onError, }: 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;
|
|
17
19
|
export {};
|
package/es2017/RemoteModule.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
|
+
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
1
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';
|
|
@@ -24,7 +26,7 @@ function useDynamicImport({ module, scope }) {
|
|
|
24
26
|
]);
|
|
25
27
|
return component;
|
|
26
28
|
}
|
|
27
|
-
export const RemoteModule = ({ module, scope, runtime, publicPath, LoadingComponent, ErrorComponent, onError })=>{
|
|
29
|
+
export const RemoteModule = ({ module, scope, runtime, publicPath, LoadingComponent, ErrorComponent, onError, componentProps = {}, children = null })=>{
|
|
28
30
|
var _remoteModule;
|
|
29
31
|
if (publicPath) {
|
|
30
32
|
setFederatedModulePublicPath(scope, publicPath);
|
|
@@ -66,7 +68,9 @@ export const RemoteModule = ({ module, scope, runtime, publicPath, LoadingCompon
|
|
|
66
68
|
onError: onError,
|
|
67
69
|
children: /*#__PURE__*/ _jsx(React.Suspense, {
|
|
68
70
|
fallback: Loading,
|
|
69
|
-
children: /*#__PURE__*/ _jsx(Component, {})
|
|
71
|
+
children: /*#__PURE__*/ _jsx(Component, _object_spread_props(_object_spread({}, componentProps), {
|
|
72
|
+
children: children
|
|
73
|
+
}))
|
|
70
74
|
})
|
|
71
75
|
});
|
|
72
76
|
}
|
package/esm/RemoteModule.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ interface RemoteModuleOptions {
|
|
|
12
12
|
onError?: (error: Error, info: {
|
|
13
13
|
componentStack: string;
|
|
14
14
|
}) => void;
|
|
15
|
+
componentProps?: any;
|
|
16
|
+
children?: React.ReactNode;
|
|
15
17
|
}
|
|
16
|
-
export declare const RemoteModule: ({ module, scope, runtime, publicPath, LoadingComponent, ErrorComponent, onError, }: 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;
|
|
17
19
|
export {};
|
package/esm/RemoteModule.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
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
6
|
import { jsx as _jsx, jsxs as _jsxs } from "@ice/jsx-runtime/jsx-runtime";
|
|
@@ -62,7 +64,7 @@ function useDynamicImport(param) {
|
|
|
62
64
|
return component;
|
|
63
65
|
}
|
|
64
66
|
export var RemoteModule = function(param) {
|
|
65
|
-
var module = param.module, scope = param.scope, runtime = param.runtime, publicPath = param.publicPath, LoadingComponent = param.LoadingComponent, ErrorComponent = param.ErrorComponent, onError = param.onError;
|
|
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;
|
|
66
68
|
var _remoteModule;
|
|
67
69
|
if (publicPath) {
|
|
68
70
|
setFederatedModulePublicPath(scope, publicPath);
|
|
@@ -113,7 +115,9 @@ export var RemoteModule = function(param) {
|
|
|
113
115
|
onError: onError,
|
|
114
116
|
children: /*#__PURE__*/ _jsx(React.Suspense, {
|
|
115
117
|
fallback: Loading,
|
|
116
|
-
children: /*#__PURE__*/ _jsx(Component, {})
|
|
118
|
+
children: /*#__PURE__*/ _jsx(Component, _object_spread_props(_object_spread({}, componentProps), {
|
|
119
|
+
children: children
|
|
120
|
+
}))
|
|
117
121
|
})
|
|
118
122
|
});
|
|
119
123
|
}
|