设置 CI 及其他集成
你应该设置 CI 流水线以运行 Oxlint,并在发现格式检查错误时使构建失败。
本页还介绍了您可能希望包含的其他集成,例如 git 提交前钩子。
CI
这些说明假设您已通过在 package.json 的 devDependencies 中添加 oxlint 来在项目中设置 Oxlint,且仓库中已有 Oxlint 配置文件。
GitHub Actions
首先,如果尚未存在,请在您的 package.json 中添加一个 lint 脚本:
{
"scripts": {
"lint": "oxlint"
}
}然后创建 .github/workflows/oxlint.yml:
name: 格式检查
on:
pull_request:
push:
branches: [main]
permissions: {}
jobs:
oxlint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v6
with:
node-version: lts/*
cache: pnpm
# 可选:此处使用 npm install / yarn install
- run: pnpm install --frozen-lockfile
- run: pnpm run lint您也可以选择使用 github 格式输出,以便获得更好的警告/错误注释(更多信息):
{
"scripts": {
"lint:github": "oxlint --format=github"
}
}GitLab CI
如果您使用 GitLab CI,可以使用 --format=gitlab 参数配合 GitLab 的代码质量功能,在合并请求中获得针对格式检查违规的内联注释。
要进行此设置,您可以在 package.json 中添加一个脚本,用于输出 gitlab 格式并保存到文件,如下所示:
{
"scripts": {
"lint:gitlab": "oxlint --format=gitlab > gitlab-oxlint-report.json"
}
}然后在您的 .gitlab-ci.yml 中添加一个作业,用于运行该脚本并将报告作为代码质量工件上传:
oxlint:
image: node:lts
stage: test
before_script:
# 可选:此处使用 pnpm install / yarn install
- npm install
script:
- npm run lint:gitlab
artifacts:
reports:
codequality:
# 此路径相对于您的仓库根目录,因此如果您的仓库结构不同或报告放在其他位置,请相应调整
- gitlab-oxlint-report.json如果您不希望使用代码质量功能,可以在 CI 作业中直接运行 Oxlint 而不使用 --format=gitlab。
您应确保启用了类型感知规则(如需使用),并考虑缓存 node_modules 以加快依赖项安装速度。
Git 钩子
lint-staged
对于使用 lint-staged 的 JS/TS 项目,您可以按以下方式设置 Oxlint 作为提交前钩子:
{
"lint-staged": {
"*.{js,jsx,ts,tsx,mjs,cjs}": "pnpm run lint"
}
}为了在安装依赖时自动安装 git 钩子,建议同时使用 husky。
pre-commit
如果您使用 pre-commit 来管理 git 钩子,可以按如下方式设置 Oxlint:
repos:
- repo: https://github.com/oxc-project/mirrors-oxlint
rev: v0.0.0
hooks:
- id: oxlint
verbose: true将 v0.0.0 替换为最新版本。
其他集成
Unplugin
通过 第三方包 支持 Unplugin。
Vite 插件
通过 第三方包 支持 Vite 插件。
