Monorepo中如何共享ESLint配置

解读

在国内前端团队普遍采用 Monorepo(如基于 pnpm workspace、Nx、Turborepo、Rush 或 Lerna)的背景下,面试官真正想考察的是:

  1. 你是否理解“配置只写一次、处处生效”对大型仓库的意义;
  2. 你是否能把 ESLint 的扁平配置(flat config)或传统 .eslintrc 与 Monorepo 的“包依赖提升”“路径解析”“CI 缓存”结合起来,给出可落地的方案;
  3. 你是否知道如何避免“幽灵依赖”“重复安装”“规则版本漂移”等国内镜像源环境下高频出现的问题;
  4. 你是否能在不破坏各子包独立发布诉求的前提下,让 WebStorm / VS Code 在 Windows、macOS、Linux 三端都能实时索引到共享配置。

知识点

  1. 扁平配置(eslint.config.js)与继承模型(extends / root / overrides)的差异;
  2. pnpm 的“虚拟存储 + 依赖提升”与 Yarn Berry PnP 对模块解析的影响;
  3. workspace protocol(workspace:*)与“发包时不把本地包打进去”的国内私服(Verdaccio、cnpm、Nexus)适配;
  4. ESLint 的 resolvePluginsRelativeTo、eslint-config-xxx 与 eslint-plugin-xxx 的版本对齐策略;
  5. VS Code 的 eslint.workingDirectories、.vscode/settings.json 与 eslint.options.configFile 的优先级;
  6. Git Hooks 侧:husky + lint-staged 在 Turborepo pipeline 中的缓存键设计;
  7. CI 侧:如何在 GitHub Enterprise 或阿里云效流水线里利用 --cache --cache-strategy content 把 ESLint 缓存落到 OSS;
  8. 子包差异化诉求:某些包需要 React,某些包需要 Node,某些包需要 Vitest,如何基于 files 字段与 overrides 做“条件规则”。

答案

一、整体思路

“把配置当包”——在 Monorepo 根目录建一个 packages/eslint-config 内部包,通过 workspace protocol 发布到私服,所有子包只依赖这一个包,实现“单点升级、全仓生效”。

二、目录结构(以 pnpm + flat config 为例)

monorepo/
├─ packages/
│  ├─ eslint-config/
│  │  ├─ package.json
│  │  ├─ index.js          // 统一导出 flat config 数组
│  │  └─ rules/
│  │     ├─ javascript.js
│  │     ├─ typescript.js
│  │     ├─ react.js
│  │     └─ node.js
│  ├─ foo/
│  │  ├─ package.json
│  │  └─ src/
│  └─ bar/
│     ├─ package.json
│     └─ src/
├─ pnpm-workspace.yaml
├─ eslint.config.js        // 根目录只做“引用”
└─ tsconfig.eslint.json

三、eslint-config 包实现细节

  1. package.json
{
  "name": "@scope/eslint-config",
  "version": "1.0.0",
  "main": "index.js",
  "publishConfig": {
    "registry": "https://your-registry.cn"
  },
  "peerDependencies": {
    "eslint": "^8.57.0",
    "@typescript-eslint/eslint-plugin": "^7.0.0",
    "@typescript-eslint/parser": "^7.0.0"
  },
  "dependencies": {
    "globals": "^15.0.0",
    "eslint-plugin-import-x": "^0.5.0"
  }
}
  1. index.js(flat config)
import js from '@eslint/js';
import ts from '@typescript-eslint/eslint-plugin';
import tsParser from '@typescript-eslint/parser';
import importX from 'eslint-plugin-import-x';
import { node } from './rules/node.js';
import { react } from './rules/react.js';

export default [
  js.configs.recommended,
  {
    files: ['**/*.{js,mjs,cjs,ts,tsx}'],
    languageOptions: {
      parser: tsParser,
      parserOptions: {
        project: './tsconfig.eslint.json',
        tsconfigRootDir: process.cwd()
      },
      globals: {
        ...require('globals').browser,
        ...require('globals').node
      }
    },
    plugins: {
      '@typescript-eslint': ts,
      'import-x': importX
    },
    rules: {
      ...ts.configs.recommended.rules,
      'import-x/no-duplicates': 'error'
    }
  },
  node,
  react
];
  1. 发包脚本
    在 CI 里用 pnpm --filter @scope/eslint-config publish --no-git-checks 推到私服,确保版本号走 changeset 自动打 Tag,避免人工改号出错。

四、子包使用

  1. 子包 package.json
{
  "name": "@scope/foo",
  "devDependencies": {
    "@scope/eslint-config": "workspace:*",
    "eslint": "^8.57.0"
  },
  "scripts": {
    "lint": "eslint src --cache --cache-location node_modules/.cache/eslint"
  }
}
  1. 子包 eslint.config.js(仅一行)
export { default } from '@scope/eslint-config';
  1. VS Code 设置(.vscode/settings.json 放到根,避免每个子包重复)
{
  "eslint.experimental.useFlatConfig": true,
  "eslint.workingDirectories": ["packages/foo", "packages/bar"]
}

五、根目录统一 lint

根 eslint.config.js

import config from '@scope/eslint-config';
export default config;

配合 turbo.json

{
  "pipeline": {
    "lint": {
      "dependsOn": ["^build"],
      "outputs": ["node_modules/.cache/eslint"]
    }
  }
}

CI 命令

pnpm install --frozen-lockfile
pnpm turbo run lint --filter=...[HEAD^1]

缓存键用 pnpm-lock.yaml + eslint.config.js + turbo.json 的哈希,落到阿里云 OSS,第二次构建 5 秒完成。

六、常见坑与对策

  1. 幽灵依赖:pnpm 严格,子包如果没显式依赖 eslint-plugin-react,就算根有也找不到;必须在 @scope/eslint-config 的 dependencies 里声明,或者把插件打平到 peer + dev。
  2. Windows 路径:flat config 里 __dirname 与 process.cwd() 混用导致找不到 tsconfig.eslint.json,统一用 fileURLToPath(import.meta.url) 再 path.resolve。
  3. 版本漂移:私服开启 publishConfig.registry 后,子包 workspace:* 在 CI 里被替换成真实版本,若私服缓存旧包,会出现“本地最新、远端旧”的诡异错误;强制 CI 先 pnpm --filter @scope/eslint-config publish --force。
  4. 大仓性能:flat config 默认会把所有规则打平,若规则过多,VS Code 启动会卡;用 eslint.lintTask.options: ["--max-warnings=0"] 并把 eslint.codeActionOnSave.rules 限定为高频规则,其余放 CI。

拓展思考

  1. 若团队未来要迁移到 ESLint 9 的“无插件入口”模型,如何设计 @scope/eslint-config/next 以保证老项目继续用 flat config、新项目用原生 config,而共享层代码不重复?
  2. 在微前端场景下,主应用和子应用分属不同 Monorepo,如何把 @scope/eslint-config 做成“远程规则集”并通过 eslint.config.js 的 import() 动态加载,实现跨仓库统一?
  3. 当仓库同时存在 .js、.ts、.vue、.svelte 四种文件,如何基于 files 字段做“条件规则”而不让 VS Code 的 Language Server 重复初始化四次?
  4. 若把规则校验下沉到 GitHub Actions 的 PR 评论,如何结合 eslint-formatter-compact 与 dangerjs 实现“只评论增量文件”的增量检查,且在国内网络下保证 30 秒内返回结果?