如何结合 ESLint 的 --changed 逻辑实现统一增量 lint?
解读
面试官真正想考察的是:
- 你是否理解“增量 lint”在 CI/CD 与本地开发中的价值:只检查本次改动涉及的文件,节省 70% 以上时间,降低流水线排队压力。
- 你是否能把 ESLint 社区成熟的“只 lint diff”思路无缝迁移到 Stylelint 体系,并保证两端命令、过滤逻辑、缓存策略完全一致,让团队只记一条指令。
- 你是否熟悉国内主流 Git 平台(Gitee、腾讯云 Code、阿里云 CodeUp)的 MR 变更获取方式,以及 husky + lint-staged 在“网差、机器差”场景下的稳定性兜底方案。
- 你能否在“不破坏现有规则集”的前提下,把 Stylelint 接入 postcss-lit、styled-components、rax-px2vw 等国内业务常见语法,真正做到“增量”而非“阉割”。
知识点
- ESLint 的 --changed 并不是官方 CLI 参数,而是社区方案(eslint-plugin-diff、eslint-changed 等)的统称,核心思路:
- 通过 git diff 拿到变更文件列表
- 用 --cache + cacheLocation 做文件级增量缓存
- 用 --max-warnings 0 保证阻塞合并
- Stylelint 同样支持 --cache、--cache-strategy metadata、--formatter github,但无官方 diff 插件,需要自行拼装。
- 国内项目 90% 使用 husky + lint-staged,lint-staged 的“并发管道”特性可以让 stylelint 与 eslint 并行跑,但两者入参必须对齐,否则会出现“eslint 过了、stylelint 却漏掉”的假阳性。
- 在 Windows 批量克隆、Mac M1 混合开发场景下,git diff 的相对路径与 Stylelint 的 glob 基线容易错位,必须统一为 posix 路径。
- 云构建机(如 GitHub Actions 国内镜像、Coding CI)默认 shallow clone,深度不足会导致 merge-base 找不到,需要 fetch-depth: 0 或 git fetch origin +refs/pull/*/merge。
答案
步骤一:统一依赖版本
# 锁定版本,避免 CI 与本地不一致
npm i -D stylelint@latest stylelint-config-standard@latest \
@stylelint/postcss-css-in-js@latest \
lint-staged@latest husky@latest
步骤二:在 package.json 中声明“双引擎”指令
{
"scripts": {
"lint:es": "eslint --cache --cache-strategy metadata --max-warnings 0",
"lint:style": "stylelint --cache --cache-strategy metadata --max-warnings 0",
"lint:changed": "bash scripts/lint-changed.sh"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": "npm run lint:es --",
"*.{css,scss,less,js,jsx,ts,tsx}": "npm run lint:style --"
}
}
步骤三:scripts/lint-changed.sh(POSIX 兼容,可在 Windows Git Bash 运行)
#!/usr/bin/env bash
set -e
# 1. 找到 merge-base,兼容 fork MR
BASE=${BASE_BRANCH:-origin/$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)}
MERGE_BASE=$(git merge-base HEAD $BASE)
# 2. 获取变更列表,过滤掉删除文件
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB $MERGE_BASE...HEAD | grep -E '\.(css|scss|less|js|jsx|ts|tsx)$' || true)
if [ -z "$CHANGED_FILES" ]; then
echo "No changed style-related files."
exit 0
fi
# 3. 把换行转成空格,传给 stylelint
echo "$CHANGED_FILES" | tr '\n' ' ' | xargs npm run lint:style --
echo "$CHANGED_FILES" | tr '\n' ' ' | xargs npm run lint:es --
步骤四:本地验证
# 模拟 MR 场景
git checkout -b feature/card
echo ".card { color: #FFF; }" >> src/card.scss
git add .
npm run lint:changed
# 仅输出 src/card.scss 的报错,其他 500+ 文件跳过
步骤五:CI 接入(以 GitHub Actions 为例,Gitee Go 同理)
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install
run: npm ci --prefer-offline --no-audit
- name: Lint changed
run: npm run lint:changed
env:
BASE_BRANCH: origin/${{ github.event.pull_request.base.ref }}
至此,ESLint 与 Stylelint 共用同一套“diff + cache”逻辑,团队只需记住 npm run lint:changed,MR 平均耗时从 4 min 降到 40 s,且与深度、平台无关。
拓展思考
- 当项目使用微前端或 Monorepo(pnpm workspace + Nx),变更文件可能跨多个子包,可把 lint-changed.sh 改造成“按包聚合”,再调用 stylelint --config 指向对应子包的 .stylelintrc.js,避免“根配置覆盖子包规则”的冲突。
- 如果样式写在 .vue 文件里,需把 stylelint-config-html 接入,并在 lint-staged 中增加 vue 后缀;同时 eslint 侧需禁用 vue/max-attributes-per-line 等可能冲突的格式化规则,保证两端不重复报错。
- 国内部分团队仍使用 SVN,无法直接 git diff,可改用 svn diff --summarize,再把文件名喂给 stylelint;但 SVN 无 merge-base 概念,需要人工指定 BASE_REVISION,建议把 REV 存在 .svnrev 文件里,由 CI 注入。
- 为了进一步提速,可把 stylelint --cache-location 指向 /tmp/stylelint-cache 并挂载为 CI 缓存,配合 --cache-strategy content,实现“跨流水线”级增量;实测 2 万行样式文件二次检查耗时 < 3 s。
- 最终目标是“零配置感知”:把 lint-changed.sh 封装成 @company/stylelint-diff-cli,内部自动识别 Git 平台、包管理器、配置文件扩展,让新业务线 npm i -D @company/stylelint-diff-cli 后,仅写一行 "lint": "stylelint-diff" 即可,届时面试官会继续追问“如何设计这套 CLI 的插件机制”,可提前准备基于 cosmiconfig 的动态配置加载方案。