@ice/mf-runtime 0.0.3 → 0.0.5
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 +128 -0
- package/es2017/RemoteModule.d.ts +4 -2
- package/es2017/RemoteModule.js +6 -2
- package/esm/RemoteModule.d.ts +4 -2
- package/esm/RemoteModule.js +6 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -207,6 +207,26 @@ RemoteConfig 配置:
|
|
|
207
207
|
|
|
208
208
|
### RemoteModule
|
|
209
209
|
|
|
210
|
+
RemoteModule 组件支持泛型,可以用来约束远程组件的 props 类型:
|
|
211
|
+
|
|
212
|
+
```tsx
|
|
213
|
+
// 定义远程组件的 props 类型
|
|
214
|
+
interface RemoteButtonProps {
|
|
215
|
+
onClick: () => void;
|
|
216
|
+
text: string;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// 使用带类型的 RemoteModule
|
|
220
|
+
<RemoteModule<RemoteButtonProps>
|
|
221
|
+
module="Button"
|
|
222
|
+
scope="remote_app"
|
|
223
|
+
componentProps={{
|
|
224
|
+
onClick: () => console.log('clicked'),
|
|
225
|
+
text: '点击我'
|
|
226
|
+
}}
|
|
227
|
+
/>
|
|
228
|
+
```
|
|
229
|
+
|
|
210
230
|
组件属性:
|
|
211
231
|
|
|
212
232
|
| 参数 | 说明 | 类型 | 默认值 |
|
|
@@ -218,9 +238,117 @@ RemoteConfig 配置:
|
|
|
218
238
|
| LoadingComponent | 加载状态组件 | `ReactNode` | `<div>Loading...</div>` |
|
|
219
239
|
| ErrorComponent | 错误回退组件 | `ReactNode` | `<div>远程模块加载失败: {error.message}</div>` |
|
|
220
240
|
| onError | 错误处理回调函数 | `(error: Error, info: { componentStack: string }) => void` | - |
|
|
241
|
+
| componentProps | 传递给远程模块的属性 | `T extends object` | `{}` |
|
|
242
|
+
| children | 传递给远程模块的子元素 | `ReactNode` | `null` |
|
|
243
|
+
|
|
244
|
+
类型定义:
|
|
245
|
+
|
|
246
|
+
```tsx
|
|
247
|
+
// RemoteModule 的类型定义
|
|
248
|
+
interface RemoteModuleOptions<T = any> {
|
|
249
|
+
module: string;
|
|
250
|
+
scope: string;
|
|
251
|
+
runtime?: {
|
|
252
|
+
react: typeof import('react');
|
|
253
|
+
reactDOM: typeof import('react-dom');
|
|
254
|
+
};
|
|
255
|
+
publicPath?: string;
|
|
256
|
+
LoadingComponent?: React.ReactNode;
|
|
257
|
+
ErrorComponent?: React.ReactNode;
|
|
258
|
+
onError?: (error: Error, info: { componentStack: string }) => void;
|
|
259
|
+
componentProps?: T;
|
|
260
|
+
children?: React.ReactNode;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// 组件声明
|
|
264
|
+
const RemoteModule = <T extends object = any>(props: RemoteModuleOptions<T>) => { ... }
|
|
265
|
+
```
|
|
221
266
|
|
|
222
267
|
|
|
223
268
|
## 注意事项
|
|
224
269
|
|
|
225
270
|
1. host name 请保证全局唯一。
|
|
226
271
|
2. remote name 的 package.json name 请保证全局唯一,否则会出现意料之外的缓存问题。
|
|
272
|
+
|
|
273
|
+
## 使用示例
|
|
274
|
+
|
|
275
|
+
### 传递属性和子元素
|
|
276
|
+
|
|
277
|
+
当远程模块需要接收属性或子元素时,可以使用 `componentProps` 和 `children`:
|
|
278
|
+
|
|
279
|
+
```tsx
|
|
280
|
+
import { RemoteModule } from '@ice/mf-runtime';
|
|
281
|
+
|
|
282
|
+
interface UserProfileProps {
|
|
283
|
+
userId: string;
|
|
284
|
+
showAvatar: boolean;
|
|
285
|
+
onUserClick: (user: any) => void;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
interface CardProps {
|
|
289
|
+
title: string;
|
|
290
|
+
children?: React.ReactNode;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function App() {
|
|
294
|
+
return (
|
|
295
|
+
<div>
|
|
296
|
+
{/* 传递属性示例 */}
|
|
297
|
+
<RemoteModule<UserProfileProps>
|
|
298
|
+
module="UserProfile"
|
|
299
|
+
scope="remote_app"
|
|
300
|
+
componentProps={{
|
|
301
|
+
userId: "123",
|
|
302
|
+
showAvatar: true,
|
|
303
|
+
onUserClick: (user) => console.log('用户点击:', user)
|
|
304
|
+
}}
|
|
305
|
+
/>
|
|
306
|
+
|
|
307
|
+
{/* 传递子元素示例 */}
|
|
308
|
+
<RemoteModule<CardProps>
|
|
309
|
+
module="Card"
|
|
310
|
+
scope="remote_app"
|
|
311
|
+
componentProps={{
|
|
312
|
+
title: "我的卡片"
|
|
313
|
+
}}
|
|
314
|
+
>
|
|
315
|
+
<h2>这是卡片内容</h2>
|
|
316
|
+
<p>可以传入任意的子元素</p>
|
|
317
|
+
</RemoteModule>
|
|
318
|
+
</div>
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
在远程模块中的使用方式:
|
|
324
|
+
|
|
325
|
+
```tsx
|
|
326
|
+
// 远程模块: UserProfile.tsx
|
|
327
|
+
interface UserProfileProps {
|
|
328
|
+
userId: string;
|
|
329
|
+
showAvatar: boolean;
|
|
330
|
+
onUserClick: (user: any) => void;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export default function UserProfile(props: UserProfileProps) {
|
|
334
|
+
const { userId, showAvatar, onUserClick } = props;
|
|
335
|
+
// ... 组件实现
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// 远程模块: Card.tsx
|
|
339
|
+
interface CardProps {
|
|
340
|
+
title: string;
|
|
341
|
+
children?: React.ReactNode;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export default function Card({ title, children }: CardProps) {
|
|
345
|
+
return (
|
|
346
|
+
<div className="card">
|
|
347
|
+
<div className="card-header">{title}</div>
|
|
348
|
+
<div className="card-body">
|
|
349
|
+
{children}
|
|
350
|
+
</div>
|
|
351
|
+
</div>
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
```
|
package/es2017/RemoteModule.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
interface RemoteModuleOptions {
|
|
2
|
+
interface RemoteModuleOptions<T = any> {
|
|
3
3
|
module: string;
|
|
4
4
|
scope: string;
|
|
5
5
|
runtime?: {
|
|
@@ -12,6 +12,8 @@ interface RemoteModuleOptions {
|
|
|
12
12
|
onError?: (error: Error, info: {
|
|
13
13
|
componentStack: string;
|
|
14
14
|
}) => void;
|
|
15
|
+
componentProps?: T;
|
|
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: <T extends object = any>({ module, scope, runtime, publicPath, LoadingComponent, ErrorComponent, onError, componentProps, children, }: RemoteModuleOptions<T>) => 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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
interface RemoteModuleOptions {
|
|
2
|
+
interface RemoteModuleOptions<T = any> {
|
|
3
3
|
module: string;
|
|
4
4
|
scope: string;
|
|
5
5
|
runtime?: {
|
|
@@ -12,6 +12,8 @@ interface RemoteModuleOptions {
|
|
|
12
12
|
onError?: (error: Error, info: {
|
|
13
13
|
componentStack: string;
|
|
14
14
|
}) => void;
|
|
15
|
+
componentProps?: T;
|
|
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: <T extends object = any>({ module, scope, runtime, publicPath, LoadingComponent, ErrorComponent, onError, componentProps, children, }: RemoteModuleOptions<T>) => 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
|
}
|