跳转到内容

Biome

此内容尚不支持你的语言。

一个高性能的 JavaScript / TypeScript 格式化工具和 Linter, 类似于 ESLint + Prettier, 但是性能更好

官网: https://biomejs.dev/

对比:

  • 比 Prettier 快 10-100 倍
  • 比 ESLint 快 20 倍
  • 开箱即用, 不需要复杂的配置
  • 支持 JavaScript, TypeScript, JSX, TSX, JSON, CSS, 等等
Terminal window
npm install -g @biomejs/biome
Terminal window
npm install -D @biomejs/biome
Terminal window
npx @biomejs/biome init

这会在项目根目录创建一个 biome.json 文件:

{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false,
"ignore": []
},
"formatter": {
"enabled": true,
"indentStyle": "tab"
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
"quoteStyle": "double",
"trailingCommas": "none"
}
}
}
Terminal window
npx @biomejs/biome check .
Terminal window
npx @biomejs/biome format .
npx @biomejs/biome format --write . # 格式化并写入文件
Terminal window
npx @biomejs/biome check --apply .
npx @biomejs/biome check --apply-unsafe . # 应用不安全的修复
配置项描述可选值默认值
enabled是否启用格式化true, falsetrue
indentStyle缩进风格tab, spacetab
indentWidth缩进宽度数字2
lineWidth行宽数字80
lineEnding行尾lf, crlf, crlf
配置项描述可选值默认值
quoteStyle引号风格double, singledouble
trailingCommas尾逗号all, es5, noneall
semicolons分号always, asNeededalways
arrowParentheses箭头函数参数括号always, asNeededalways
quoteProperties对象属性引号asNeeded, preserveasNeeded
bracketSpacing对象字面量空格true, falsetrue
bracketSameLine最后一个 > 是否在同一行true, falsefalse
配置项描述
recommended启用推荐规则
all启用所有规则
nursery启用 nursery 规则(试验性)

规则配置示例:

{
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"complexity": {
"noUselessConstructor": "error",
"noUselessSwitchCase": "warn"
},
"correctness": {
"noUnusedVariables": "error"
},
"style": {
"noVar": "error",
"useConst": "error"
},
"suspicious": {
"noExplicitAny": "warn"
}
}
}
}
特性BiomePrettier
语言支持JS/TS, JSON, CSS, Vue, 更多很多语言
速度极快
配置简单简单
插件生态较少丰富
自定义程度中等较高
特性BiomeESLint
语言支持JS/TSJS/TS + 插件
速度极快较慢
配置简单复杂
规则数量较少很多
插件生态较少丰富
自动修复支持支持
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
},
"formatter": {
"indentStyle": "space",
"indentWidth": 2,
"lineWidth": 100
},
"javascript": {
"formatter": {
"quoteStyle": "single",
"trailingCommas": "es5",
"semicolons": "asNeeded",
"arrowParentheses": "asNeeded"
}
},
"linter": {
"rules": {
"recommended": true
}
},
"organizeImports": {
"enabled": true
}
}

安装插件: biomejs.biome

.vscode/settings.json 中配置:

{
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"quickfix.biome": "explicit",
"source.organizeImports.biome": "explicit"
}
}

使用 simple-git-hooks 配合 lint-staged:

Terminal window
npm install -D simple-git-hooks lint-staged

package.json:

{
"scripts": {
"lint": "biome check .",
"lint:fix": "biome check --apply .",
"format": "biome format --write ."
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged"
},
"lint-staged": {
"*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc}": [
"biome check --apply --no-errors-on-unmatched"
]
}
}

然后运行:

Terminal window
npx simple-git-hooks
{
"files": {
"ignore": ["dist", "build", "node_modules", "*.config.js"]
}
}

Biome 会自动读取 .gitignore 中的配置, 只需要在 biome.json 中启用:

{
"vcs": {
"enabled": true,
"useIgnoreFile": true
}
}
// biome-ignore lint/suspicious/noExplicitAny: 暂时使用 any
const value: any = getValue();
// biome-ignore format: 需要保持原样
const data = [
1, 2, 3,
4, 5, 6
];
Terminal window
npx @biomejs/biome migrate prettier

这会读取 .prettierrc 并创建/更新 biome.json

PrettierBiome
tabWidth: 2indentWidth: 2
useTabs: falseindentStyle: "space"
singleQuote: truejavascript.formatter.quoteStyle: "single"
trailingComma: "es5"javascript.formatter.trailingCommas: "es5"
semi: falsejavascript.formatter.semicolons: "asNeeded"
arrowParens: "avoid"javascript.formatter.arrowParentheses: "asNeeded"
printWidth: 100lineWidth: 100

Biome 实现了大部分 ESLint 核心规则, 查看: https://biomejs.dev/linter/rules/

ESLintBiome
no-unused-varscorrectness/noUnusedVariables
no-varstyle/noVar
prefer-conststyle/useConst
no-explicit-anysuspicious/noExplicitAny
eqeqeqsuspicious/noDoubleEquals
curlystyle/useBlockStatements

官方基准测试:

工具时间
Biome0.4 秒
Prettier7.0 秒 (慢 17x)
ESLint8.5 秒 (慢 21x)
ESLint + Prettier12.4 秒 (慢 31x)
t1