Перейти до вмісту

Use Biome in big projects

Цей контент ще не доступний вашою мовою.

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.

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.

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.

  • Каталогapp
    • Каталогbackend
      • biome.json
      • package.json
    • Каталогfrontend
      • biome.json
      • Каталогlegacy-app
        • package.json
      • Каталогnew-app
        • package.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.

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.

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

Let’s assume that we have these requirements:

  • legacy-app have to format using spaces;
  • backend and new-app have to format using tabs;
  • all apps have to format using line width 120;
  • backend app needs some extra linting;

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
}
}

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"
}
}

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"
}
}

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 are particular repositories where multiple libraries are stored and maintained in one big repository. Each library represents a self-contained package, which can contain different configurations.

Since v2, Biome supports monorepos out of the box, and you’ll need to set up the project in the following way.

  1. Create a biome.json file at the root of the monorepo. We will use the recommended rules, and customise the formatter options.:

    biome.json
    {
    "linter": {
    "enabled": true,
    "rules": {
    "recommended": true
    }
    },
    "formatter": {
    "lineWidth": 120,
    "indentStyle": "space",
    "indentWidth": 6,
    }
    }

    This file is called root configuration and it sets the base options inside the project. However, nested configurations can decide to adhere to these options or not. Let’s see how.

  2. Create nested configuration files, one in each package, where is needed. These nested configuration files must have the field "root" set to false. Failing to do so will result into an error. Also, we want these packages the follow the formatting standards set up in the root configuration. In order to so, we will use a new microsyntax available in Biome v2, which is "extends": "//". This syntax tells Biome to extends from the root configuration, regardless of where the nested configuration is.

    Let’s create two configuration files, one inside packages/logger and one inside packages/generate. In the former we will disable noConsole, and in packages/generate we will disable the formatter because those are files that are code-generated:

    packages/logger/biome.json
    {
    "root": false,
    "extends": "//",
    "linter": {
    "rules": {
    "suspicious": {
    "noConsole": "off"
    }
    }
    }
    }
    packages/generate/biome.json
    {
    "root": false,
    "extends": "//",
    "formatter": {
    "enabled": false
    }
    }

    However, when you use the microsyntax extends: "//", you can **omit "root": true, because it implies that this configuration isn’t a a root configuration, and it wants extends from the root configuration:

    packages/generate/biome.json
    {
    "root": false,
    "extends": "//",
    "formatter": {
    "enabled": false
    }
  3. Now, let’s suppose we have a new package in packages/analytics, maintained by another team that has different different coding standards. To change these options, you just need to omit "extends": "//" from the configuration file, and change the formatting options:

    packages/analytics/biome.json
    {
    "root": false,
    "formatter": {
    "lineWidth": 100,
    }
    }
  4. Now that everything is set up, you have few options. You can run biome commands from the root of the project, or from the single packages. Biome will respect all settings!