Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin/model): support sort #12838

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docs/docs/docs/max/data-flow.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ translated_at: '2024-03-18T00:49:20.502Z'

`@umi/max` has a built-in **data flow management** [plugin](https://github.com/umijs/umi/blob/master/packages/plugins/src/model.ts), which is a lightweight data management solution based on the `hooks` paradigm. It can be used to manage global shared data in Umi projects.

## Configuration

e.g.

```ts
export default {
model: {
extraModels: ['src/models/userModel.ts'],
sort: (a, b) => a.namespace.localeCompare(b.namespace),
},
};
```

### extraModels

- Type: `string[]`
- Default: `[]`

Configure `extraModels` to automatically add these Model files to the data stream management.

### sort

- Type: `(a: Model, b: Model) => number`
- Default: `(a, b) => a.namespace.localeCompare(b.namespace)`

Configure `sort` to sort the Model based on the return value of the `sort` function.

## Getting Started

### Creating a Model
Expand Down
27 changes: 27 additions & 0 deletions docs/docs/docs/max/data-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ toc: content

`@umi/max` 内置了**数据流管理**[插件](https://github.com/umijs/umi/blob/master/packages/plugins/src/model.ts),它是一种基于 `hooks` 范式的轻量级数据管理方案,可以在 Umi 项目中管理全局的共享数据。

## 配置

e.g.

```ts
export default {
model: {
extraModels: ['src/models/userModel.ts'],
sort: (a, b) => a.namespace.localeCompare(b.namespace),
},
};
```

### extraModels

- Type: `string[]`
- Default: `[]`

配置 `extraModels` 后,插件会自动将这些 Model 文件添加到数据流管理中。

### sort

- Type: `(a: Model, b: Model) => number`
- Default: `(a, b) => a.namespace.localeCompare(b.namespace)`

配置 `sort` 后,插件会根据 `sort` 函数返回的值对 Model 进行排序。

## 开始使用

### 创建 Model
Expand Down
4 changes: 4 additions & 0 deletions packages/plugins/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default (api: IApi) => {
return zod
.object({
extraModels: zod.array(zod.string()),
sort: zod.function().optional(),
})
.partial();
},
Expand All @@ -22,6 +23,9 @@ export default (api: IApi) => {

api.onGenerateFiles(async () => {
const models = await getAllModels(api);
if (api.userConfig.model.sort) {
models.sort(api.userConfig.model.sort);
}

// model.ts
api.writeTmpFile({
Expand Down
Loading