This is going to be a quick one: How to share ESLint configuration between projects.
Step 1: create a separate project
By separate I mean really separate with its own package.json
and own repository.
$ mkdir eslint-config-config-name
cd eslint-config-config-name
npm init
...
About to write to .../package.json:
{
"name": "eslint-config-config-name",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this ok? (yes)
Step 2: create your configuration
Since the main
in package.json
points to index.js
this will be the spot where we put our shareable configuration. Let's start small - you can always extend it further later on as you work your way through the available rues:
module.exports = {
rules: {
'no-var': 'error',
}
}
To make sure you don't forget about installing ESLint in your target project add the following lines to the package.json
of your configuration project:
"peerDependencies": {
"eslint": ">= 4"
}
This will ensure that when the target project doesn't have ESLint as a dependency then an error will be thrown telling that it needs to be present.
Step 3: publish your repository
In order for the configuration to be available for everyone you need to have it somewhere everyone can access it. In that case it is recommended to add the following keywords to your package.json
.
"keywords": [
"eslint",
"eslintconfig"
]
If you choose to publish it to NPM then that's fine, but honestly those config files can get pretty specific for your company therefore I would suggest just tagging and pushing to your Git repository, like so:
$ git init
git add .
git commit -a -m "Initial import"
git tag 1.0.0
git remote add origin url-to-your-repo
git push --set-upstream origin master
Make sure your repository is accessible via HTTP or HTTPS - that's going to be needed for the next step, which is...
Step 4: use the config
In order to use that preset you need to add it as a dependency
or devDependency
to your project:
"devDependencies": {
...
"eslint-config-config-name": "git+https://url-to-your-repo#1.0.0"
}
That way you are not specifying just the latest version but a specific version. This is done so that projects can adopt the changes to your common configuration as they get the time for it.
Next we need to make use of the configuration in our local project. We do that by creating either a local .eslintrc.js
or by adding that configuration to package.json
. Since we have already created an example .eslintrc.json
in the previous step let's see it being used in package.json
:
"eslintConfig": {
"extends": [
"eslint-config-config-name"
]
}
That's it! Now when you npm install
your dependencies then the configuration and all its dependencies
will be installed along with it.
If you would like to see a working example take a look at Vue.js + Electron example on Github that uses the common configuration I prepared for my company.
Happy linting!
No comments:
Post a Comment