Skip to content

在大型项目中使用 Biome

Biome 可以提供一些工具来帮助你在大型项目中正确使用它,例如 monorepo 或包含多个项目的工作区。

¥Biome can provide some tools that can help you to use it properly in big projects, such as monorepo or workspaces that contain multiple projects.

¥Use multiple configuration files

当你使用 Biome 的功能时 - 使用 CLI 或 LSP - 该工具使用当前工作目录查找最近的配置文件。

¥When you use Biome’s features - either with the CLI or LSP - the tool looks for the nearest configuration file using the current working directory.

如果 Biome 在那里找不到配置文件,它会开始向上遍历文件系统的目录,直到找到一个。

¥If Biome doesn’t find the configuration file there, it starts walking upwards the directories of the file system, until it finds one.

你可以利用此功能根据项目/文件夹应用不同的设置。

¥You can leverage this feature to apply different settings based on the project/folder.

假设我们有一个包含后端应用和新前端应用的项目。

¥Let’s suppose we have a project that contains a backend app and new frontend app.

  • Directory

    app

    • Directory

      backend

      • biome.json

      • package.json

    • Directory

      frontend

      • biome.json

      • Directory

        legacy-app

        • package.json
      • Directory

        new-app

        • package.json

这意味着当你从文件 app/backend/package.json 运行脚本时,Biome 将使用配置文件 app/backend/biome.json

¥This means that when you run a script from the file app/backend/package.json, Biome will use the configuration file app/backend/biome.json.

当你从 app/frontend/legacy-app/package.jsonapp/frontend/new-app/package.json 运行脚本时,Biome 将使用配置文件 app/frontend/biome.json

¥When you run a script from app/frontend/legacy-app/package.json or app/frontend/new-app/package.json, Biome will use the configuration file app/frontend/biome.json.

¥Share the configuration

可以使用 extends 配置选项来跨文件细分选项。

¥It’s possible to use the extends configuration option to breakdown options across files.

假设我们有这些要求:

¥Let’s assume that we have these requirements:

  • legacy-app 必须使用空格格式化;

    ¥legacy-app have to format using spaces;

  • backendnew-app 必须使用制表符进行格式化;

    ¥backend and new-app have to format using tabs;

  • 所有应用都必须使用行宽 120 进行格式化;

    ¥all apps have to format using line width 120;

  • backend 应用需要一些额外的 linting;

    ¥backend app needs some extra linting;

我们首先在 app/biome.json 处创建一个新的配置文件,并将共享选项放在那里:

¥We start by creating a new configuration file at app/biome.json, and put there the shared options:

app/biome.json
{
"formatter": {
"enabled": true,
"lineWidth": 120
}
}

现在让我们将 app/frontend/biome.json 移动到 app/frontend/legacy-app/,因为在那里我们需要使用不同的格式。

¥Now let’s move app/frontend/biome.json to app/frontend/legacy-app/, because that’s where we need to use a different formatting.

app/frontend/legacy-app/biome.json
{
"formatter": {
"indentStyle": "space"
}
}

然后,我们告诉 Biome 使用 extends 属性从主 app/biome.json 文件继承所有选项:

¥Then, we tell Biome to inherit all the options from the main app/biome.json file, using the extends property:

app/frontend/legacy-app/biome.json
{
"extends": ["../../biome.json"],
"formatter": {
"indentStyle": "space"
}
}

让我们跳转到 app/backend/biome.json,我们需要在其中启用 linting:

¥Let’s jump to app/backend/biome.json, where we need to enable the linting:

app/backend/biome.json
{
"extends": ["../biome.json"],
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}

Monorepos 是特定的存储库,其中多个库存储并维护在一个大存储库中。每个库代表一个独立的项目,可以包含不同的配置。

¥Monorepos are particular repositories where multiple libraries are stored and maintained in one big repository. Each library represents a self-contained project, which can contain different configurations.

由于解析嵌套配置文件的一些限制,Biome 不能很好地支持 monorepos,你 帮助并关注相关问题

¥Biome doesn’t support monorepos very well due to some limitations in resolving nested configuration files, you help and follow the relative issue.

为了在当前限制的情况下获得最佳的开发者体验,建议在 monorepo 的根目录中使用 biome.json,并使用 overrides 配置来更改 Biome 在某些包中的行为。

¥In order to have the best developer experience despite the current limitation, it’s advised to have a biome.json at the root of the monorepo, and use the overrides configuration to change the behaviour of Biome in certain packages.

在下面的示例中,我们禁用了包 packages/logger 内的规则 suspicious/noConsoleLog

¥In the following example we disable the rule suspicious/noConsoleLog inside the package packages/logger.

biome.jsonc
{
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"overrides": [{
"include": ["packages/logger/**"],
"linter": {
"rules": {
"suspicious": {
"noConsoleLog": "off"
}
}
}
}]
}