微调ESLint配置导致检测速度骤降10倍的问题求助
把原ESLint配置改成更新后的版本后,代码检测速度直接慢了10倍。已经确认两种配置下遍历、检测的文件完全一致,而且VSCode里测试,两种配置都能正常捕获规则违规情况,搞不懂为啥更新后的配置这么慢。
原配置
extends: [ 'plugin:@typescript-eslint/recommended', 'plugin:vue/vue3-strongly-recommended', 'plugin:vuejs-accessibility/recommended' ], plugins: ['import'], parser: 'vue-eslint-parser', parserOptions: { parser: { js: '@typescript-eslint/parser', ts: '@typescript-eslint/parser', }, ecmaVersion: 2020, },
更新后的配置
extends: [ 'plugin:@typescript-eslint/recommended', 'plugin:vue/vue3-strongly-recommended', 'plugin:vuejs-accessibility/recommended' ], plugins: ['import'], parser: 'vue-eslint-parser', parserOptions: { 'parser': '@typescript-eslint/parser', ecmaVersion: 2020, project: './tsconfig.eslint.json', sourceType: 'module', tsconfigRootDir: __dirname, 'extraFileExtensions': ['.vue'] },
问题根源
核心原因是更新后的配置新增了project: './tsconfig.eslint.json'。这个选项会触发@typescript-eslint的类型检查类规则——这类规则需要TypeScript完整编译整个项目的类型信息,而非仅做语法层面的检测,计算量呈指数级上升,直接导致检测速度暴跌。
原配置没有project选项,@typescript-eslint仅运行不需要类型信息的语法规则,速度自然快很多。
优化方案
缩小tsconfig.eslint的范围
检查tsconfig.eslint.json,确保只包含需要检测的文件,用exclude排除node_modules、dist等无关目录:{ "extends": "./tsconfig.json", "include": ["src/**/*", "*.vue"], "exclude": ["node_modules", "dist", "**/*.spec.ts"] }禁用不必要的类型检查规则
如果不需要全量类型检查,可以关闭部分规则,或者改用plugin:@typescript-eslint/recommended-type-checked替代plugin:@typescript-eslint/recommended——前者仅启用关键类型检查规则,能大幅降低计算负载。调整parserOptions的文件类型指定方式
改回原配置中按文件类型分开指定parser的写法,避免parser对所有文件做不必要的类型处理:parserOptions: { parser: { js: '@typescript-eslint/parser', ts: '@typescript-eslint/parser', }, ecmaVersion: 2020, project: './tsconfig.eslint.json', sourceType: 'module', tsconfigRootDir: __dirname, extraFileExtensions: ['.vue'] },启用ESLint缓存
在ESLint配置中添加cache: true,或在检测命令后加--cache,仅重新检测修改过的文件,大幅提升后续检测速度:module.exports = { // 其他配置 cache: true, cacheLocation: './node_modules/.cache/eslint' }
内容的提问来源于stack exchange,提问作者Yasin Yaqoobi

