Skip to content

设置 CI 及其他集成

🌐 Setup CI and other integrations

你可以——而且应该——设置你的 CI 流水线来运行 Oxlint,并在出现 lint 错误时使构建失败。

🌐 You can - and should - setup your CI pipeline to run Oxlint and fail the build on lint errors.

本页面还涵盖了你可能想要包含的其他集成,例如 git 预提交钩子。

🌐 This page also covers other integrations you may want to include, like git pre-commit hooks.

CI

这些说明假设你已经通过在你的 package.json 中将 oxlint 添加到开发依赖中来在项目中设置了 Oxlint,并且在仓库中已经有了 Oxlint 配置文件。

🌐 These instructions assume you have already set up Oxlint in your project by adding oxlint to your devDependencies in your package.json, and already have an oxlint configuration file in the repo.

GitHub 操作

🌐 GitHub Actions

首先,如果你的 package.json 中还没有 lint 脚本,先添加一个:

🌐 First, add a lint script to your package.json if you don't have one already:

package.json
json
{
  "scripts": {
    "lint": "oxlint"
  }
}

然后创建 .github/workflows/oxlint.yml

🌐 Then create .github/workflows/oxlint.yml:

.github/workflows/oxlint.yml
yaml
name: Lint

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

      # alternatively use npm install / yarn install here
      - run: pnpm install --frozen-lockfile
      - run: pnpm run lint

或者,你可以使用 GitHub 格式选项输出,以获得更好的警告/错误注释

🌐 You can, alternatively, output using the github format option, for better warning/error annotations:

package.json
json
{
  "scripts": {
    "lint:github": "oxlint --format=github"
  }
}

GitLab 持续集成

🌐 GitLab CI

如果你使用 GitLab CI,你可以通过 --format=gitlabGitLab 的代码质量功能 设置 Oxlint,以在合并请求中获取针对 lint 违规的内联注释。

🌐 If you use GitLab CI, you can set up Oxlint with --format=gitlab and GitLab's Code Quality feature to get inline annotations for lint violations in merge requests.

要设置此项,你可以向你的 package.json 添加一个脚本,以输出 GitLab 格式并将其保存到文件中,方法如下:

🌐 To set this up, you can add a script to your package.json to output the gitlab format and save it to a file, like so:

package.json
json
{
  "scripts": {
    "lint:gitlab": "oxlint --format=gitlab > gitlab-oxlint-report.json"
  }
}

然后在你的 .gitlab-ci.yml 中添加一个任务,运行脚本并将报告作为代码质量工件上传:

🌐 And then add a job to your .gitlab-ci.yml, to run the script and upload the report as a Code Quality artifact:

.gitlab-ci.yml
yml
oxlint:
  image: node:lts
  stage: test
  before_script:
    # alternatively use pnpm install / yarn install here
    - npm install
  script:
    - npm run lint:gitlab
  artifacts:
    reports:
      codequality:
        # This is relative to your repository root, so adjust if your repo has a different structure or you put the report in a different location
        - gitlab-oxlint-report.json

如果你不想使用代码质量功能,你可以在 CI 任务中直接运行 oxlint,而不使用 --format=gitlab

🌐 If you do not want to use the Code Quality feature, you can simply run oxlint without --format=gitlab in the CI job instead.

如果你想使用它们,应该确保启用了类型感知规则,并考虑缓存 node_modules 来加快依赖的安装速度。

🌐 You should ensure type-aware rules are enabled if you want to use them, and consider caching node_modules to speed up the installation of dependencies.

Git 钩子

🌐 Git hooks

lint-staged

对于使用 lint-staged 的 JS/TS 项目,你可以将 oxlint 设置为预提交钩子,方法如下:

🌐 For JS/TS projects using lint-staged, you can set up oxlint to run as a pre-commit hook as follows:

package.json
json
{
  "lint-staged": {
    "*.{js,jsx,ts,tsx,mjs,cjs}": "pnpm run lint"
  }
}

在安装依赖时自动安装 git 钩子,也可以考虑使用 husky

🌐 To automatically install the git hook when installing dependencies, considering also using husky.

预提交

🌐 pre-commit

如果你使用 pre-commit 来管理 git 钩子,你可以按如下方式设置 Oxlint:

🌐 If you use pre-commit to manage git hooks, you can set up Oxlint as follows:

.pre-commit-config.yaml
yaml
repos:
  - repo: https://github.com/oxc-project/mirrors-oxlint
    rev: v0.0.0
    hooks:
      - id: oxlint
        verbose: true

v0.0.0 替换为最新版本。

🌐 Replace v0.0.0 with the latest version.

其他集成

🌐 Other integrations

无插件

🌐 Unplugin

Unplugin 通过一个 第三方包 支持

🌐 Unplugin is supported via a third-party package

Vite 插件

🌐 Vite plugin

Vite 插件可以通过第三方包支持

🌐 A Vite plugin is supported via a third-party package