Skip to main content

大仓配置

提示

v8 中的新 "项目服务" 不需要对 monorepos 进行额外配置。

¥The new "project service" in v8 requires no additional configuration for monorepos.

如果你使用 parserOptions.projectService,则不需要本指南。

¥If you're using parserOptions.projectService, you don't need this guide.

如果你使用带有 parserOptions.project 的 monorepo,这些文档将帮助你弄清楚如何设置类型化 linting。如果你不想使用类型化 linting,那么你可以在此处停止 - 你不需要做任何特殊的事情。

¥If you're using a monorepo with parserOptions.project, these docs will help you figure out how to setup typed linting. If you don't want to use typed linting, then you can stop here - you don't need to do anything special.

parserOptions.project 配置将根据你使用的 monorepo 设置而有所不同:

¥parserOptions.project configurations will look different based on which monorepo setup you use:

  1. tsconfig.json

    ¥One root tsconfig.json

  2. 每个包一个 tsconfig.json(根目录中可选一个)

    ¥One tsconfig.json per package (and an optional one in the root)

tsconfig.json

¥One root tsconfig.json

如果你只有一个 tsconfig.json 文件并且其 include 路径包含你想要 lint 的所有文件,你可以直接将其与 typescript-eslint 一起使用而无需进一步配置。

¥If you only have one tsconfig.json file and its include paths include all the files you'd like to lint, you can directly use it with typescript-eslint without further configuration.

如果其 include 路径不能包含要进行 lint 的所有文件,我们建议创建一个名为 tsconfig.eslint.json 的新配置,如下所示:

¥If its include paths cannot include all files to be linted, we suggest creating a new config called tsconfig.eslint.json, that looks something like this:

tsconfig.eslint.json
{
// extend your base config to share compilerOptions, etc
"extends": "./tsconfig.json",
"compilerOptions": {
// ensure that nobody can accidentally use this config for a build
"noEmit": true,
},
"include": [
// whatever paths you intend to lint
"src",
"test",
"tools",
],
}

请务必更新你的 ESLint 配置文件以指向这个新的 TSConfig。

¥Be sure to update your ESLint configuration file to point at this new TSConfig.

每个包一个 tsconfig.json(根目录中可选一个)

¥One tsconfig.json per package (and an optional one in the root)

使用类型信息进行代码检查 中引入的 parserOptions.project 选项接受相对路径数组。路径可以作为 节点全局 提供。对于每个正在 linting 的文件,第一个匹配的项目路径将用作其支持 TSConfig。

¥The parserOptions.project option introduced in Linting with Type Information accepts an array of relative paths. Paths may be provided as Node globs. For each file being linted, the first matching project path will be used as its backing TSConfig.

eslint.config.mjs
export default tseslint.config(
eslint.configs.recommended,
tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
project: true,
project: ['./tsconfig.eslint.json', './packages/*/tsconfig.json'],
tsconfigRootDir: import.meta.dirname,
},
},
},
);

parserOptions.project 中的广泛通配符

¥Wide globs in parserOptions.project

parserOptions.project 中使用宽 glob ** 可能会降低 lint 性能。不要使用 ** 递归检查所有文件夹的 glob,而要选择一次使用单个 * 的路径。

¥Using wide globs ** in your parserOptions.project may degrade linting performance. Instead of globs that use ** to recursively check all folders, prefer paths that use a single * at a time.

eslint.config.mjs
export default tseslint.config(
eslint.configs.recommended,
tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
project: ['./tsconfig.eslint.json', './**/tsconfig.json'],
project: ['./tsconfig.eslint.json', './packages/*/tsconfig.json'],
tsconfigRootDir: import.meta.dirname,
},
},
},
);

请参阅 解析器选项 "project" 中的 Glob 模式会减慢 linting 以了解更多详细信息。

¥See Glob pattern in parser's option "project" slows down linting for more details.

关于大型(> 10)多包大仓的重要说明

¥Important note regarding large (> 10) multi-package monorepos

我们收到的报告表明,对于足够大和/或相互依赖的项目,使用此方法可能会遇到 OOM。我们的建议是先设置并测试,因为触发此 OOM 的情况很少。

¥We've had reports that for sufficiently large and/or interdependent projects, you may run into OOMs using this approach. Our advice is to set it up and test first, as there are very few cases that trigger this OOM.

请参阅 #1192 以了解更多信息和讨论。

¥See #1192 for more information and discussion.

如果你确实遇到了 OOM,请对上述问题发表评论并让我们知道你的 repo - 我们拥有的信息越多越好。作为临时解决方法,请考虑以下方法之一:

¥If you do run into an OOM, please comment on the above issue and let us know about your repo - the more information we have, the better. As an interim workaround, consider one of the following:

  • 切换到一个根 tsconfig.eslint.json(参见 tsconfig.json

    ¥Switching to one root tsconfig.eslint.json (see One root tsconfig.json)

  • 使用 shell 脚本一次仅检查一个包,并使用上面的现有配置。

    ¥Using a shell script to only lint one package at a time, using your existing config above.