Index

共享配置

要共享你的 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.

创建可共享配置

🌐 Create a Shareable Config

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

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

虽然你可以按照自己喜欢的任何方式命名包,但我们建议使用以下约定之一以使你的包更易于识别:

🌐 While you can name the package in any way that you’d like, we recommend using one of the following conventions to make your package easier to identify:

  • eslint-config- 开始,例如 eslint-config-myconfig
  • 对于一个 npm 作用域模块,将模块命名或加前缀为 @scope/eslint-config,例如 @scope/eslint-config@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
export default [
	{
		languageOptions: {
			globals: {
				MyGlobal: true,
			},
		},

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

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

🌐 Because 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 关键字,以便其他人可以轻松找到你的模块。

🌐 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.

你应该在 package.json 中使用 peerDependencies 字段声明你对 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": ">= 9"
	}
}

如果你的可共享配置依赖于插件或自定义解析器,你应该在你的 package.json 中将这些包指定为 dependencies

🌐 If your shareable config depends on a plugin or a custom parser, you should specify these packages as dependencies in your package.json.

使用可共享配置

🌐 Use a Shareable Config

要使用可共享的配置,请在 eslint.config.js 文件中导入该包,并使用 extends 将其添加到导出的数组中,如下所示:

🌐 To use a shareable config, import the package inside of an eslint.config.js file and add it into the exported array using extends, like this:

// eslint.config.js
import { defineConfig } from "eslint/config";
import myconfig from "eslint-config-myconfig";

export default defineConfig([
	{
		files: ["**/*.js"],
		extends: [myconfig],
	},
]);

从可共享配置覆盖设置

🌐 Overriding Settings from Shareable Configs

你可以通过在导入可共享配置后将其直接添加到你的 eslint.config.js 文件中来覆盖可共享配置的设置。例如:

🌐 You can override settings from the shareable config by adding them directly into your eslint.config.js file after importing the shareable config. For example:

// eslint.config.js
import { defineConfig } from "eslint/config";
import myconfig from "eslint-config-myconfig";

export default defineConfig([
	{
		files: ["**/*.js"],
		extends: [myconfig],

		// anything from here will override myconfig
		rules: {
			"no-unused-vars": "warn",
		},
	},
]);

共享多个配置

🌐 Sharing Multiple Configs

由于可共享的配置只是 npm 包,你可以从同一个包中导出任意数量的配置。除了在你的 package.json 中使用 main 条目指定默认配置之外,你还可以通过向你的 npm 包添加新文件并随后在你的 eslint.config.js 文件中引用它来指定额外的可共享配置。

🌐 Because shareable configs are just npm packages, you can export as many configs as you’d like from the same package. In addition to specifying a default config using the main entry in your package.json, you can specify additional shareable configs by adding a new file to your npm package and then referencing it from your eslint.config.js file.

例如,你可以在你的 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
export default {
	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:

// eslint.config.js
import { defineConfig } from "eslint/config";
import myconfig from "eslint-config-myconfig";
import mySpecialConfig from "eslint-config-myconfig/my-special-config.js";

export default defineConfig([
	{
		files: ["**/*.js"],
		extends: [myconfig, mySpecialConfig],

		// anything from here will override myconfig
		rules: {
			"no-unused-vars": "warn",
		},
	},
]);

进阶读物

🌐 Further Reading