如何加速超大规模文件项目中的ESLint运行速度?
大Git LFS仓库中VSCode保存.ts/.js文件卡顿问题排查与解决
我维护一个约20GB的Git仓库(基于Git LFS存储数据),仓库内大部分是JSON/CSV/YAML文件,零散分布少量.ts和.js文件。随着仓库规模增长,在Mac上使用VSCode按CMD+s保存.ts/.js文件时,底部粉色进度条会持续运行3分钟以上才能完成,严重影响开发效率。已多次重启电脑和VSCode,问题仍未解决,仅在仓库规模变大后出现该现象。
当前配置文件
.eslintrc.json
{ "env": { "browser": true, "es2021": true }, "parserOptions": { "ecmaVersion": "latest", "sourceType": "module", "project": ["./tsconfig.json"] }, "ignorePatterns": ["**/*.js", "import/github/**/*/data"], "parser": "@typescript-eslint/parser", "plugins": [ "@typescript-eslint", "import", "simple-import-sort", "typescript-sort-keys", "sort-keys", "prettier" ], "extends": ["prettier"], "rules": { "curly": 2, "@typescript-eslint/quotes": [ "error", "single", { "avoidEscape": true, "allowTemplateLiterals": true } ], "@typescript-eslint/no-unnecessary-condition": 0, "@typescript-eslint/array-type": [ 2, { "default": "generic" } ], "@typescript-eslint/await-thenable": "off", "@typescript-eslint/consistent-type-definitions": [2, "type"], "@typescript-eslint/consistent-type-exports": "off", "@typescript-eslint/method-signature-style": "off", "@typescript-eslint/naming-convention": 0, "@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/no-for-in-array": "off", "@typescript-eslint/no-namespace": "off", "@typescript-eslint/no-non-null-assertion": "off", "@typescript-eslint/no-require-imports": "off", "@typescript-eslint/no-this-alias": "off", "@typescript-eslint/no-unsafe-argument": "off", "@typescript-eslint/no-unsafe-assignment": "off", "@typescript-eslint/no-unsafe-member-access": "off", "@typescript-eslint/no-unsafe-return": "off", "@typescript-eslint/no-useless-empty-export": "off", "@typescript-eslint/prefer-function-type": "off", "no-array-constructor": "off", "@typescript-eslint/no-array-constructor": "off", "no-throw-literal": "off", "@typescript-eslint/no-throw-literal": "off", "lines-between-class-members": "off", "@typescript-eslint/lines-between-class-members": "off", "object-curly-spacing": "off", "@typescript-eslint/object-curly-spacing": [2, "always"], "padding-line-between-statements": "off", "@typescript-eslint/padding-line-between-statements": [ "error", { "blankLine": "always", "prev": "*", "next": ["type"] } ], "space-before-blocks": "off", "@typescript-eslint/space-before-blocks": ["error", "always"], "@typescript-eslint/type-annotation-spacing": [ "error", { "after": true } ], "import/first": "error", "import/newline-after-import": "error", "import/no-duplicates": "error", "typescript-sort-keys/interface": "off", "typescript-sort-keys/string-enum": "off", "sort-keys": 0, "sort-keys/sort-keys-fix": "off", "prettier/prettier": 2, "@typescript-eslint/no-unused-vars": "off", "default-case": "off", "default-case-last": "off" } }
.prettierrc.json
{ "semi": false, "trailingComma": "all", "singleQuote": true, "printWidth": 72, "tabWidth": 2, "useTabs": false, "arrowParens": "avoid", "quoteProps": "as-needed", "bracketSpacing": true, "proseWrap": "always", "endOfLine": "lf", "singleAttributePerLine": true, "prettierPath": "./node_modules/prettier", "importOrder": ["^\\w(.*)$", "^@(.*)$", "~(.*)$", "\\..(.*)$", "\\.(.*)$"], "importOrderSeparation": true, "importOrderSortSpecifiers": true }
tsconfig.json
{ "compilerOptions": { "module": "ESNext", "target": "ESNext", "lib": ["es2020", "dom", "dom.iterable"], "outDir": "host", "rootDir": ".", "sourceMap": true, "moduleResolution": "node", "strictNullChecks": false, "strict": false, "esModuleInterop": true, "noUncheckedIndexedAccess": false, "baseUrl": ".", "noErrorTruncation": true, "types": ["node"], "typeRoots": ["node_modules/@types"], "noImplicitAny": false, "declaration": true, "resolveJsonModule": true, "paths": { "~/*": ["./*"] } }, "ts-node": { "files": true }, "include": ["*.ts", "**/*.ts", "*.d.ts"], "exclude": ["node_modules", "host"] }
问题原因分析
- TypeScript编译器扫描范围过大:当前
tsconfig.json的include覆盖所有TS文件,加上resolveJsonModule开启后,编译器会处理仓库内所有导入的JSON文件,大体积仓库导致编译检查耗时剧增。 - ESLint/Prettier未完全排除无关文件:ESLint的忽略规则仅排除了部分JS文件和特定目录,未覆盖所有大型数据文件,保存时会遍历大量非代码文件进行检查。
- VSCode索引与Git LFS拖慢后台进程:VSCode会自动索引仓库内所有文件,Git LFS存储的大文件拖慢索引速度,保存时后台可能同步LFS状态或重新索引。
- ESLint无缓存机制:未开启缓存时,每次保存都会重新检查所有符合规则的文件,而非仅修改的文件。
可行解决办法
1. 缩小TypeScript编译范围
修改tsconfig.json的include和exclude,明确只包含需要编译的TS文件,排除所有数据文件:
{ "include": ["src/**/*.ts", "*.ts", "*.d.ts"], // 根据实际TS文件位置调整 "exclude": [ "node_modules", "host", "**/*.json", "**/*.csv", "**/*.yaml", "**/*.yml", "import/github/**/*/data" ] }
若不需要导入JSON文件,可关闭resolveJsonModule:
"resolveJsonModule": false,
2. 优化ESLint规则与缓存
扩展.eslintrc.json的ignorePatterns,排除所有非TS/JS的大型文件:
"ignorePatterns": [ "**/*.js", "**/*.json", "**/*.csv", "**/*.yaml", "**/*.yml", "import/github/**/*/data" ]
开启ESLint缓存,减少重复检查:
"cache": true, "cacheLocation": "./node_modules/.cache/eslint"
或在VSCode设置中搜索eslint.cache,勾选启用。
3. 调整VSCode索引与Git LFS设置
- 排除大文件目录索引:打开VSCode设置(CMD+,),搜索
files.exclude和search.exclude,添加排除项:"files.exclude": { "**/*.json": true, "**/*.csv": true, "**/*.yaml": true, "**/*.yml": true, "import/github/**/*/data": true }, "search.exclude": { "**/*.json": true, "**/*.csv": true, "**/*.yaml": true, "**/*.yml": true, "import/github/**/*/data": true } - 禁用Git LFS自动同步:执行Git命令排除指定目录的LFS自动拉取:
git config lfs.fetchexclude "import/github/**/*/data/**"
4. 优化Prettier处理
创建.prettierignore文件,排除非代码文件:
*.json *.csv *.yaml *.yml import/github/**/*/data
5. 拆分仓库(长期方案)
将大型数据文件与代码仓库分离,单独用Git LFS管理数据仓库,代码仓库仅保留TS/JS文件和配置文件,通过子模块或外部存储引用数据,从根源减少代码仓库体积。
内容的提问来源于stack exchange,提问作者Lance Pollard
相关产品推荐
相关产品推荐

