Next.js项目中ESLint的特殊配置需求
解读
在国内一线/二线大厂的 Next.js 面试里,这道题通常出现在「代码工程化」或「性能与质量」环节,面试官想验证三点:
- 你是否真的落地过 Next.js 项目,而不是只会 CRA;
- 你是否能把「框架约定」与「Lint 规则」打通,避免“规则打架”;
- 你是否具备“渐进式”治理能力:既能快速跑通 MVP,又能随着业务膨胀持续收紧质量卡口。
因此,回答必须围绕「框架文件约定、运行时差异、国内部署习惯」展开,给出可复制的配置路径,并体现“可灰度、可回滚、可度量”的工程思维。
知识点
- 框架文件约定:pages/、app/、middleware.ts、next.config.js、*.d.ts、CSS Modules、全局样式、next/image、next/font。
- 运行时差异:Node.js 端(SSR/ISR/Edge Runtime)与浏览器端混写,全局变量不一致(window 不存在于 Node 层)。
- 国内部署习惯:Vercel 受限,多数公司用「自建 K8s + 阿里云 CDN」或「腾讯 TEM」,因此 ESLint 报错必须本地提前解决,不能依赖线上 CI 回滚。
- 共享配置生态:@next/eslint-plugin-next 提供的 40+ 条“框架感知”规则,必须与 @typescript-eslint、react-hooks、jsx-a11y、prettier、simple-import-sort 做优先级排序。
- 灰度卡口:husky + lint-staged 只做增量,老代码用 eslint-config-legacy 兜底;新模块用 eslint-config-strict,通过 eslint-diff 做“增量红线”。
- 性能红线:禁止在页面组件顶层写 heavy barrel import(如 lodash、moment),避免 SSR 阶段打包体积爆炸;需配合 eslint-plugin-tree-shaking 检测副作用。
- 国际化合规:国内出海业务需满足 GDPR/COPPA,jsx-a11y 规则必须全开,且不能用 eslint-disable-next-line 大面积豁免。
- Monorepo 场景:Next.js 作为子包时,parserOptions.project 需指向根目录 tsconfig.eslint.json,避免“找不到 tsconfig”导致规则降级为 js 版本,漏检泛型约束。
答案
-
安装顺序(锁定版本,避免幽灵依赖) pnpm add -D eslint @next/eslint-plugin-next @typescript-eslint/{parser,eslint-plugin} eslint-config-prettier eslint-plugin-react-hooks eslint-plugin-jsx-a11y eslint-plugin-simple-import-sort lint-staged husky
-
根目录新建 .eslintrc.js,采用「分层 extends」模式,保证 @next/core-web-vitals 最后加载,覆盖掉 react-hooks 冲突: module.exports = { root: true, parser: '@typescript-eslint/parser', parserOptions: { tsconfigRootDir: __dirname, project: ['./tsconfig.eslint.json'], // 独立 tsconfig,排除 next-env.d.ts ecmaFeatures: { jsx: true }, }, env: { node: true, browser: true, es2022: true }, plugins: ['@typescript-eslint', 'react-hooks', 'jsx-a11y', 'simple-import-sort'], extends: [ 'eslint:recommended', 'plugin:@typescript-eslint/recommended-requiring-type-checking', 'plugin:react-hooks/recommended', 'plugin:jsx-a11y/recommended', 'prettier', // 必须放倒数第二,关闭格式冲突 'plugin:@next/next/recommended', // 放最后,框架优先 ], rules: { // 国内常见痛点:禁止在 SSR 文件里直接访问 window 'no-restricted-globals': ['error', { name: 'window', message: '只能在 useEffect 或事件回调里使用' }], // 强制使用 next/image '@next/next/no-img-element': 'error', // 禁止 barrel import 体积炸弹 'no-restricted-syntax': [ 'error', { selector: "ImportDeclaration[source.value='lodash']", message: '请使用 lodash-es 并按需引入,避免 SSR 阶段打包过大', }, ], // 排序,降低 merge 冲突 'simple-import-sort/imports': 'error', 'simple-import-sort/exports': 'error', // 泛型必须显式,避免隐式 any 穿透到运行时 '@typescript-eslint/explicit-function-return-type': ['error', { allowExpressions: true }], }, overrides: [ // 页面文件允许默认导出 { files: ['pages//*', 'app//'], rules: { 'import/no-default-export': 'off' }, }, // Edge Runtime 文件不允许 Node 内置模块 { files: ['middleware.ts', 'pages/api/edge/**/'], rules: { 'node/no-unsupported-features/node-builtins': 'error' }, }, ], };
-
tsconfig.eslint.json(排除 next-env.d.ts,防止类型冲突) { "extends": "./tsconfig.json", "include": ["/*.ts", "/.tsx", "**/.js", "**/*.jsx"], "exclude": ["node_modules", ".next", "next-env.d.ts"] }
-
增量卡口(package.json) "lint-staged": { "*.{ts,tsx,js,jsx}": "eslint --cache --max-warnings 0" }, "husky": { "hooks": { "pre-commit": "lint-staged" } }
-
老代码兜底 在根目录新增 eslint-config-legacy.js,仅开启 @next/next 安全规则,通过 eslint-diff 只检测本次 MR 变更文件,避免一次性爆炸式整改。
-
CI 阶段(国内 GitLab 自建) 在 .gitlab-ci.yml 中增加: eslint: stage: quality image: node:18-alpine script: - pnpm install --frozen-lockfile - pnpm run lint:diff # 只检测变更,失败即阻断合并 allow_failure: false
-
结果度量 每周跑
pnpm run lint:summary输出规则覆盖率、剩余 any 数量、@next/next 违规清零进度,同步到飞书多维表格,PM 可直观看到“技术债下降曲线”。
拓展思考
- 微前端场景:Next.js 作为子应用时,如何与基座共享一份 eslint-config-monorepo,同时允许子应用覆写部分规则(如强制使用基座统一的 Design Token)?
- RSC(React Server Component)时代:app/ 目录下 server 组件、client 组件、shared 组件三段边界清晰,如何写自定义 eslint rule,在 AST 层面识别 "use client" 指令,禁止在 server 组件里写事件处理器?
- 大仓性能:当 Monorepo 达到 300+ 子包、2000+ 页面,eslint --cache 命中率下降,如何把缓存搬到「阿里云 OSS + 签名 URL」,实现分布式缓存,让 CI 耗时从 8 min 降到 1 min?
- 合规审计:国内出海业务需过 Google Play Family Policy,如何利用 eslint-plugin-a11y 与 eslint-plugin-security,在 MR 阶段自动产出「合规检测报告」,直接同步给法务,减少外部审计轮次?
- 规则自研:基于 @next/eslint-plugin-next 源码,如何写一条 custom rule,检测
getServerSideProps里不能出现import('@/utils/chartlib')这种仅在浏览器侧使用的重型库,从而避免 SSR 阶段打包体积爆炸?