Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Shareable configs page edits and expansion #16824

Merged
merged 6 commits into from Feb 13, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 32 additions & 23 deletions docs/src/extend/shareable-configs.md
Expand Up @@ -8,17 +8,23 @@ eleventyNavigation:

---

The configuration that you have in your `.eslintrc` file is an important part of your project, and as such, you may want to share it with other projects or people. Shareable configs allow you to publish your configuration settings on [npm](https://www.npmjs.com/) and have others download and use it in their ESLint projects.
To share your `.eslintrc` file, create a **shareable config**. You can publish your shareable config on [npm](https://www.npmjs.com/) so that others can download and use it in their ESLint projects.
bpmutter marked this conversation as resolved.
Show resolved Hide resolved

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

## Creating a Shareable Config

Shareable configs are simply npm packages that export a configuration object. To start, [create a Node.js module](https://docs.npmjs.com/getting-started/creating-node-modules) like you normally would. Make sure the module name begins with `eslint-config-`, such as `eslint-config-myconfig`.
Shareable configs are simply npm packages that export a configuration object. To start, [create a Node.js module](https://docs.npmjs.com/getting-started/creating-node-modules) like you normally would.

The module name must take one of the following forms:

npm [scoped modules](https://docs.npmjs.com/misc/scope) are also supported, by naming or prefixing the module with `@scope/eslint-config`, such as `@scope/eslint-config` or `@scope/eslint-config-myconfig`.
* Begin with `eslint-config-`, such as `eslint-config-myconfig`.
* Be an npm [scoped module](https://docs.npmjs.com/misc/scope). To create a scoped module, name or prefix the module with `@scope/eslint-config`, such as `@scope/eslint-config` or `@scope/eslint-config-myconfig`.

Create a new `index.js` file and export an object containing your settings:
In your module, export the shareable config from the module's [`main`](https://docs.npmjs.com/cli/v9/configuring-npm/package-json#main) entry point file. The default main entry point is `index.js`. For example:

```js
// index.js
module.exports = {

globals: {
Expand All @@ -32,13 +38,13 @@ module.exports = {
};
```

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

## Publishing a Shareable Config

Once your shareable config is ready, you can [publish to npm](https://docs.npmjs.com/getting-started/publishing-npm-packages) to share with others. We recommend using the `eslint` and `eslintconfig` keywords so others can easily find your module.
Once your shareable config is ready, you can [publish it to npm](https://docs.npmjs.com/getting-started/publishing-npm-packages) to share it with others. We recommend using the `eslint` and `eslintconfig` [keywords](https://docs.npmjs.com/cli/v9/configuring-npm/package-json#keywords) in the `package.json` so others can easily find your module.
bpmutter marked this conversation as resolved.
Show resolved Hide resolved

You should declare your dependency on ESLint in `package.json` using the [peerDependencies](https://docs.npmjs.com/files/package.json#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:
You should declare your dependency on ESLint in the `package.json` using the [peerDependencies](https://docs.npmjs.com/files/package.json#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:

```json
{
Expand All @@ -48,7 +54,7 @@ You should declare your dependency on ESLint in `package.json` using the [peerDe
}
```

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 third-party parser or another shareable config, you can specify these packages as `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](custom-parsers) 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:

Expand All @@ -66,59 +72,62 @@ Be sure to replace `eslint-config-myconfig` with the actual name of your module.

## Using a Shareable Config

Shareable configs are designed to work with the `extends` feature of `.eslintrc` files. Instead of using a file path for the value of `extends`, use your module name. For example:
To use a shareable config, include it in the `extends` field of an `.eslintrc` file. For the value, use your module name. For example:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep the "Shareable configs are designed to work with the extends feature of .eslintrc files." sentence so that it isn't expected that they can be used in other ways. For example, we had a bug report that shareable configs don't work with --config (#15697).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the proposed changes are pretty clear that the shareable config should be in the ESLint config file.

Do you think we should add some sentence like: "You cannot use shareable configs with the ESLint CLI --config flag"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should add some sentence like: "You cannot use shareable configs with the ESLint CLI --config flag"?

Yes, I think that would be good.

(although, technically it is possible to use a shareable config package with --config, e.g. eslint --config node_modules/eslint-config-airbnb/index.js)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's still some ambiguity with the current wording. "Include it in the extends field". What is "it"? (We know it's a string, but maybe "it" should "the config name" or something more specific?)


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

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

```json
{
"extends": "myconfig"
}
```

### npm scoped modules
### npm Scoped Modules

npm [scoped modules](https://docs.npmjs.com/misc/scope) are also supported in a number of ways.

By using the module name:
You can use the module name:

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

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

```json
{
"extends": "@scope"
}
```

The module name can also be customized, just note that when using [scoped modules](https://docs.npmjs.com/misc/scope) it is not possible to omit the `eslint-config-` prefix. Doing so would result in package naming conflicts, and thus in resolution errors in most of cases. For example a package named `@scope/eslint-config-myconfig` vs `@scope/myconfig`, since both are valid scoped package names, the configuration should be specified as:
The module name can also be customized, just note that when using [scoped modules](https://docs.npmjs.com/misc/scope) you cannot omit the `eslint-config-` prefix. Doing so results in package naming conflicts, and thus often in resolution errors. For example, if you have a package named `@scope/eslint-config-myconfig`, the configuration must be specified as:

```json
{
"extends": "@scope/eslint-config-myconfig"
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't true, a @scope/eslint-config-myconfig package can be used by specifying "extends": "@scope/myconfig". The only case where "eslint-config" sequence must appear in the config specifier is when specifying a subpath of a @scope/eslint-config package, like in the example on line 150 ("extends": "@scope/eslint-config/my-special-config"), because "@scope/my-special-config" would be interpreted as "@scope/eslint-config-my-special-config".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct. I think this is a case where the old docs were also incorrect, so probably a good idea to clean this up.

Copy link
Contributor Author

@bpmutter bpmutter Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, clarified the text and example in latest version

```

### Overriding Settings from Shareable Configs

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

## Sharing Multiple Configs

It's possible to share multiple configs in the same npm package. You can specify a default config for the package by following the directions in the first section. You can specify additional configs by simply adding a new file to your npm package and then referencing it from your ESLint config.
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](#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.

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:

```js
// my-special-config.js
module.exports = {
rules: {
quotes: [2, "double"]
Expand All @@ -142,13 +151,13 @@ When using [scoped modules](https://docs.npmjs.com/misc/scope) it is not possibl
}
```

Note that you can leave off the `.js` from the filename. In this way, you can add as many additional configs to your package as you'd like.
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 from each other and live in different directories, you can create a single shareable config that handles this scenario.
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.

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

Expand All @@ -165,13 +174,13 @@ myconfig
└── common.js
```

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

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

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

```js
module.exports = {
Expand All @@ -181,13 +190,13 @@ module.exports = {
};
```

Inside your `/lib/ci.js` you have
Inside `/lib/ci.js` you have:

```js
module.exports = require('./ci/backend');
```

Inside your `/lib/ci/common.js`
Inside `/lib/ci/common.js`:

```js
module.exports = {
Expand All @@ -200,7 +209,7 @@ module.exports = {

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.

Now inside your `/lib/ci/backend.js`
Now inside `/lib/ci/backend.js`:

```js
module.exports = {
Expand All @@ -211,7 +220,7 @@ module.exports = {
};
```

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

## Further Reading

Expand Down