Biome

Biome 是一个 JavaScript、TypeScript、JSX 和 JSON 的快速格式化程序,可节省持续集成 (CI) 和开发者的时间。

¥Biome is a fast formatter for JavaScript, TypeScript, JSX, and JSON that saves CI and developer time.

Good to know: 

This guide assumes you're using create-turbo or a repository with a similar structure.

将 Biome 与 Turborepo 结合使用

¥Using Biome with Turborepo

Biome 是 Turborepo 中使用的大多数工具中的一个罕见例外,因为它的速度非常快。因此,我们建议使用 根任务,而不是在每个包中创建单独的脚本。

¥Biome is a rare exception to most tools that are used with Turborepo because it is so extraordinarily fast. For this reason, we recommend using a Root Task rather than creating separate scripts in each of your packages.

Caching behavior

在项目根目录下使用 Biome 会导致升级 Biome 版本或更改配置时所有任务的缓存未命中。如果你在这些情况下更倾向于更高的缓存命中率而不是更少的配置,你仍然可以像我们指南中的其他建议一样,在单独的脚本中使用 Biome。

¥Using Biome at the root of the project will result in cache misses for all tasks when you upgrade Biome versions or change configuration. If you prefer the tradeoff of higher cache hit ratios in these situations over less configuration, you can still use Biome in separate scripts like the other recommendations in our guides.

初始化 Biome

¥Initialize Biome

首先,在你的仓库中添加 按照安装文档设置 Biome。然后,你将能够在存储库的根目录中创建一个使用 Biome 的脚本:

¥First, follow the installation documentation to set up Biome in your repository. You'll then be able to create a script to use Biome in the root of your repository:

./package.json
{
  "scripts": {
    "format-and-lint": "biome check .",
    "format-and-lint:fix": "biome check . --write"
  }
}

创建根任务

¥Create a root task

实际上,Biome 不太可能成为代码库迭代速度的瓶颈。因此,通过在 根任务 中使用 Biome,我们可以减少代码库中需要管理的配置。

¥In practice, Biome is unlikely to be a bottleneck in the iteration speed of your repository. For this reason, we can have less configuration to manage in our repository by using Biome in a Root Task.

如果你认为将 Biome 拆分成多个任务放在包中运行速度会更快,你可以自由地这样做。我们鼓励你尝试最适合你用例的方案。

¥If you believe Biome may be faster in your repository split up into tasks in packages, you are free to do so. We encourage you to experiment with what's best for your use case.

要创建 根任务,请将脚本注册到 Turborepo:

¥To create a Root Task, register the scripts to Turborepo:

Turborepo logo
./turbo.json
{
  "tasks": {
    "//#format-and-lint": {},
    "//#format-and-lint:fix": {
      "cache": false
    }
  }
}

现在你可以使用 turbo run format-and-lintturbo run format-and-lint:fix 运行这些脚本。

¥You'll now be able to run these scripts using turbo run format-and-lint and turbo run format-and-lint:fix.