diff --git a/docs/docs/docs/max/data-flow.en-US.md b/docs/docs/docs/max/data-flow.en-US.md index 48ad620e9d07..6c9bf9bed4c5 100644 --- a/docs/docs/docs/max/data-flow.en-US.md +++ b/docs/docs/docs/max/data-flow.en-US.md @@ -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 diff --git a/docs/docs/docs/max/data-flow.md b/docs/docs/docs/max/data-flow.md index 1ce7aa4144d7..8724b8096e92 100644 --- a/docs/docs/docs/max/data-flow.md +++ b/docs/docs/docs/max/data-flow.md @@ -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 diff --git a/packages/plugins/src/model.ts b/packages/plugins/src/model.ts index d377b4f39e8f..af262f981616 100644 --- a/packages/plugins/src/model.ts +++ b/packages/plugins/src/model.ts @@ -13,6 +13,7 @@ export default (api: IApi) => { return zod .object({ extraModels: zod.array(zod.string()), + sort: zod.function().optional(), }) .partial(); }, @@ -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({