共享配置(已弃用)

要共享你的 ESLint 配置,请创建一个可共享的配置。你可以在 npm 上发布你的可共享配置,以便其他人可以下载并在他们的 ESLint 项目中使用它。

¥To share your ESLint configuration, create a shareable config. You can publish your shareable config on npm so that others can download and use it in their ESLint projects.

本页说明如何创建和发布可共享配置。

¥This page explains how to create and publish a shareable config.

创建可共享的配置

¥Creating a Shareable Config

可共享配置只是导出配置对象的 npm 包。首先,像往常一样使用 创建一个 Node.js 模块

¥Shareable configs are simply npm packages that export a configuration object. To start, create a Node.js module like you normally would.

模块名称必须采用以下形式之一:

¥The module name must take one of the following forms:

  • eslint-config- 开头,例如 eslint-config-myconfig

    ¥Begin with eslint-config-, such as eslint-config-myconfig.

  • 成为 npm 作用域模块。要创建作用域模块,请使用 @scope/eslint-config 命名或为模块添加前缀,例如 @scope/eslint-config@scope/eslint-config-myconfig

    ¥Be an npm scoped module. To create a scoped module, name or prefix the module with @scope/eslint-config, such as @scope/eslint-config or @scope/eslint-config-myconfig.

在你的模块中,从模块的 main 入口点文件导出可共享配置。默认主入口点是 index.js。例如:

¥In your module, export the shareable config from the module’s main entry point file. The default main entry point is index.js. For example:

// index.js
module.exports = {
	globals: {
		MyGlobal: true,
	},

	rules: {
		semi: [2, "always"],
	},
};

由于 index.js 文件只是 JavaScript,你可以从文件中读取这些设置或动态生成它们。

¥Since the index.js file is just JavaScript, you can read these settings from a file or generate them dynamically.

发布可共享配置

¥Publishing a Shareable Config

一旦你的可共享配置准备就绪,你就可以 发布到 npm 与他人共享它。我们建议使用 package.json 文件中的 eslinteslintconfig keywords,以便其他人可以轻松找到你的模块。

¥Once your shareable config is ready, you can publish it to npm to share it with others. We recommend using the eslint and eslintconfig keywords in the package.json file so others can easily find your module.

你应该使用 peerDependencies 字段在 package.json 中声明对 ESLint 的依赖。为面向未来的兼容性声明依赖的推荐方法是使用 “> =” 范围语法,使用所需的最低 ESLint 版本。例如:

¥You should declare your dependency on ESLint in the package.json using the peerDependencies field. The recommended way to declare a dependency for future-proof compatibility is with the “>=” range syntax, using the lowest required ESLint version. For example:

{
	"peerDependencies": {
		"eslint": ">= 3"
	}
}

如果你的可共享配置依赖于插件,你还应该将其指定为 peerDependency(插件将相对于终端用户的项目加载,因此终端用户需要安装他们需要的插件)。但是,如果你的可共享配置依赖于 自定义解析器 或另一个可共享配置,则可以在 package.json 中将这些包指定为 dependencies

¥If your shareable config depends on a plugin, you should also specify it as a peerDependency (plugins will be loaded relative to the end user’s project, so the end user is required to install the plugins they need). However, if your shareable config depends on a custom parser or another shareable config, you can specify these packages as dependencies in the package.json.

你还可以在发布之前通过全局链接模块在计算机上测试可共享配置。类型:

¥You can also test your shareable config on your computer before publishing by linking your module globally. Type:

npm link

然后,在想要使用可共享配置的项目中,输入:

¥Then, in your project that wants to use your shareable config, type:

npm link eslint-config-myconfig

请务必将 eslint-config-myconfig 替换为模块的实际名称。

¥Be sure to replace eslint-config-myconfig with the actual name of your module.

使用可共享配置

¥Using a Shareable Config

要使用可共享配置,请在配置文件的 extends 字段中包含配置名称。对于值,请使用你的模块名称。例如:

¥To use a shareable config, include the config name in the extends field of a configuration file. For the value, use your module name. For example:

{
	"extends": "eslint-config-myconfig"
}

你也可以省略 eslint-config-,ESLint 会自动采用该版本:

¥You can also omit the eslint-config- and it is automatically assumed by ESLint:

{
	"extends": "myconfig"
}

你不能将可共享配置与 ESLint CLI --config 标志一起使用。

¥You cannot use shareable configs with the ESLint CLI --config flag.

npm Scoped 模块

¥npm Scoped Modules

npm 作用域模块 也以多种方式受支持。

¥npm scoped modules are also supported in a number of ways.

你可以使用模块名称:

¥You can use the module name:

{
	"extends": "@scope/eslint-config"
}

你也可以省略 eslint-config,ESLint 会自动采用该版本:

¥You can also omit the eslint-config and it is automatically assumed by ESLint:

{
	"extends": "@scope"
}

模块名称也可以自定义。例如,如果你有一个名为 @scope/eslint-config-myconfig 的包,则可以将配置指定为:

¥The module name can also be customized. For example, if you have a package named @scope/eslint-config-myconfig, the configuration can be specified as:

{
	"extends": "@scope/eslint-config-myconfig"
}

你还可以省略 eslint-config 以将配置指定为:

¥You could also omit eslint-config to specify the configuration as:

{
	"extends": "@scope/myconfig"
}

从可共享配置覆盖设置

¥Overriding Settings from Shareable Configs

你可以通过将可共享配置中的设置直接添加到你的 .eslintrc 文件中来覆盖它们。

¥You can override settings from the shareable config by adding them directly into your .eslintrc file.

共享多个配置

¥Sharing Multiple Configs

你可以在同一个 npm 包中共享多个配置。按照 创建可共享的配置 部分中的说明为包指定默认配置。你可以通过将新文件添加到 npm 包,然后从 ESLint 配置中引用它来指定其他可共享配置。

¥You can share multiple configs in the same npm package. Specify a default config for the package by following the directions in the Creating a Shareable Config section. You can specify additional shareable configs by adding a new file to your npm package and then referencing it from your ESLint config.

例如,你可以在 npm 包的根目录中创建一个名为 my-special-config.js 的文件并导出一个配置,例如:

¥As an example, you can create a file called my-special-config.js in the root of your npm package and export a config, such as:

// my-special-config.js
module.exports = {
	rules: {
		quotes: [2, "double"],
	},
};

然后,假设你使用的是包名称 eslint-config-myconfig,你可以通过以下方式访问附加配置:

¥Then, assuming you’re using the package name eslint-config-myconfig, you can access the additional config via:

{
	"extends": "myconfig/my-special-config"
}

使用 作用域模块 时,无法省略 eslint-config 命名空间。这样做会导致如上所述的解析错误。假设包名称为 @scope/eslint-config,可以按如下方式访问附加配置:

¥When using scoped modules it is not possible to omit the eslint-config namespace. Doing so would result in resolution errors as explained above. Assuming the package name is @scope/eslint-config, the additional config can be accessed as:

{
	"extends": "@scope/eslint-config/my-special-config"
}

请注意,你可以从文件名中省略 .js

¥Note that you can leave off the .js from the filename.

重要:我们强烈建议始终为你的插件包含默认配置以避免错误。

¥Important: We strongly recommend always including a default config for your plugin to avoid errors.

本地配置文件解析

¥Local Config File Resolution

如果你需要创建可以相互扩展并位于不同目录中的多个配置,则可以创建一个可共享的配置来处理这种情况。

¥If you need to make multiple configs that can extend each other and live in different directories, you can create a single shareable config that handles this scenario.

例如,假设你使用包名称 eslint-config-myconfig,并且你的包看起来像这样:

¥As an example, let’s assume you’re using the package name eslint-config-myconfig and your package looks something like this:

myconfig
├── index.js
└─┬ lib
  ├── defaults.js
  ├── dev.js
  ├── ci.js
  └─┬ ci
    ├── frontend.js
    ├── backend.js
    └── common.js

index.js 文件中,你可以执行以下操作:

¥In the index.js file, you can do something like this:

module.exports = require("./lib/ci.js");

现在包内有 /lib/defaults.js,其中包含:

¥Now inside the package you have /lib/defaults.js, which contains:

module.exports = {
	rules: {
		"no-console": 1,
	},
};

/lib/ci.js 内部你有:

¥Inside /lib/ci.js you have:

module.exports = require("./ci/backend");

/lib/ci/common.js 内部:

¥Inside /lib/ci/common.js:

module.exports = {
	rules: {
		"no-alert": 2,
	},
	extends: "myconfig/lib/defaults",
};

尽管位于完全不同的目录中,但你会看到所有 extends 都必须使用你希望扩展的配置文件的完整包路径。

¥Despite being in an entirely different directory, you’ll see that all extends must use the full package path to the config file you wish to extend.

现在在 /lib/ci/backend.js 内部:

¥Now inside /lib/ci/backend.js:

module.exports = {
	rules: {
		"no-console": 1,
	},
	extends: "myconfig/lib/ci/common",
};

在最后一个文件中,再次看到,要正确解析你的配置,你需要包含完整的包路径。

¥In the last file, once again see that to properly resolve your config, you need to include the full package path.

进阶读物

¥Further Reading