概述
Biome 是一款面向 Web 项目的快速、一体化工具链,使用 Rust 编写。它用一个工具替代 ESLint 与 Prettier,官方资料表述为速度提升 100 倍,并提供零配置默认值。
主要特性(据资料):
- 单一工具同时完成代码检查(linting)与格式化(formatting)
- 比 ESLint 快 100 倍
- 默认零配置
- 内置导入语句排序
- 以 TypeScript 优先的设计
- 部分兼容 Prettier
- 原生支持 monorepo
- 提供 VS Code 集成
安装
npm install --save-dev @biomejs/biome
快速开始
初始化并运行:
# 创建 biome.json 配置
npx @biomejs/biome init
# 检查项目
npx @biomejs/biome check .
# 自动修复问题
npx @biomejs/biome check --write .
# 仅格式化
npx @biomejs/biome format --write .
# 仅代码检查
npx @biomejs/biome lint .
package.json 脚本示例:
{
"scripts": {
"check": "biome check .",
"check:write": "biome check --write .",
"format": "biome format --write .",
"lint": "biome lint .",
"lint:fix": "biome lint --write ."
}
}
基本配置(biome.json):
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
},
"files": {
"ignoreUnknown": false,
"ignore": ["node_modules", "dist", "build", ".next"]
},
"formatter": {
"enabled": true,
"indentStyle": "space",
"indentWidth": 2,
"lineWidth": 100
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
"quoteStyle": "single",
"semicolons": "asNeeded",
"trailingCommas": "all"
}
}
}
核心命令
check 命令(推荐)
check 同时运行代码检查与格式化:
biome check .
biome check --write .
biome check --write --unsafe .
biome check --write --unsafe --apply-suggested
biome check src/**/*.ts
biome ci .
format 命令
biome format --write .
biome format .
biome format --write src/**/*.{ts,tsx,js,jsx}
lint 命令
biome lint .
biome lint --write .
biome lint --verbose .
biome lint --write --unsafe .
配置说明
格式化配置支持 formatter(enabled、formatWithErrors、indentStyle、indentWidth、lineEnding、lineWidth、attributePosition)以及针对 JavaScript 的 quoteStyle、jsxQuoteStyle、quoteProperties、trailingCommas、semicolons、arrowParentheses、bracketSpacing、bracketSameLine 等选项,JSON 可单独配置 trailingCommas。
代码检查配置按规则类别组织,包括 a11y、complexity、correctness、performance、security、style、suspicious,每类可开启 recommended 并逐条设置 error/warn/off。示例规则如 noAutofocus、useKeyWithClickEvents、noForEach、useLiteralKeys、noUnusedVariables、useExhaustiveDependencies、noAccumulatingSpread、noDangerouslySetInnerHtml、noNonNullAssertion、useConst、useTemplate、noExplicitAny、noArrayIndexKey。
文件忽略与包含通过 files.ignore、files.ignoreUnknown、files.include 配置,并支持用 overrides 针对特定文件(如测试文件、脚本目录)覆盖规则或格式化参数。
VS Code 集成
安装扩展:
code --install-extension biomejs.biome
在 .vscode/settings.json 中将默认格式化器设为 biomejs.biome,并可为 javascript、typescript、typescriptreact、json 分别配置 formatOnSave 与 codeActionsOnSave(quickfix.biome、source.organizeImports.biome)。可通过 biome.lspBin 指定本地二进制路径。
从 ESLint 与 Prettier 迁移
卸载旧依赖并删除配置文件后,可使用迁移工具:
biome migrate prettier --write
biome migrate eslint --write
也可手动将 .prettierrc 与 .eslintrc 配置映射为 biome.json 中的 formatter、javascript.formatter 与 linter.rules 配置。
Git 钩子集成
可使用 Husky + lint-staged、Lefthook,或不依赖第三方库的原生 Git 钩子。示例 lint-staged 配置:
{
"lint-staged": {
"*.{js,ts,jsx,tsx,json}": [
"biome check --write --no-errors-on-unmatched"
]
}
}
CI/CD 集成
资料给出了 GitHub Actions、GitLab CI、CircleCI 的配置示例,核心步骤均为安装依赖后运行 npx @biomejs/biome ci .,并可分别执行 format 与 lint 检查。
导入排序
Biome 内置导入排序,可运行:
biome check --write --organize-imports-enabled=true .
并可通过 organizeImports.enabled 配置开启。
TypeScript 与 monorepo
Biome 提供一等 TypeScript 支持,可检测未使用变量、强制类型导入(useImportType、useExportType)、检测不安全的类型断言等。在 monorepo 中支持根配置与各包通过 extends 继承与覆盖,并可按 packages/* 批量执行命令。
性能与缓存
资料给出的对比为:ESLint + Prettier 处理 10000 个文件约 60 秒,Biome 约 0.6 秒。Biome 具备智能缓存,首次运行与二次运行耗时差异明显,缓存位于 node_modules/.cache/biome。默认使用全部 CPU 核心,可通过 --max-diagnostics 等参数调整输出。
常见项目配置模式
资料提供了 React、Next.js、Node.js 后端的推荐规则配置示例,以及若干实际仓库(ai-code-review、itinerizer-ts 等)使用的 biome.json 模式,常见约定包括 2 空格缩进、lineWidth 100、单引号、启用 organizeImports 等。
最佳实践
优先使用 biome check 而非分开执行 format/lint;启用 --write 自动修复;配置 VS Code 保存时格式化;添加 Git 钩子在提交前把关;CI 中使用 biome ci;从推荐规则起步再自定义;利用导入排序;用 overrides 处理不同类型文件;启用 VCS 集成以遵循 .gitignore;尽量保持配置精简。
故障排查
可用 biome rage 查看运行环境信息,用 biome check --verbose 确认文件是否被忽略;若与 Prettier 冲突,可在 VS Code 中指定默认格式化器并卸载 Prettier;性能问题可清理缓存或降低最大诊断数;规则不生效时注意类别名称需与配置结构匹配。
相关资源
官方文档:https://biomejs.dev VS Code 扩展:https://marketplace.visualstudio.com/items?itemName=biomejs.biome GitHub:https://github.com/biomejs/biome 规则参考:https://biomejs.dev/linter/rules/ 迁移指南:https://biomejs.dev/guides/migrate-eslint-prettier/
关于定价、收费模式、支持的完整语言范围等信息,资料中暂未提供。