共享配置

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

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

  • 对于 npm 作用域模块,请使用 @scope/eslint-config 命名模块或为其添加前缀,例如 @scope/eslint-config@scope/eslint-config-myconfig

    ¥For an npm 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
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 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": ">= 9"
    }
}

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

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

使用可共享配置

¥Using a Shareable Config

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

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

// eslint.config.js
import myconfig from "eslint-config-myconfig";

export default [
    ...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 myconfig from "eslint-config-myconfig";

export default [
    ...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 myconfig from "eslint-config-myconfig";
import mySpecialConfig from "eslint-config-myconfig/my-special-config.js";

export default [
    ...myconfig,
    mySpecialConfig,

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

进阶读物

¥Further Reading