gatsby-plugin-eslint
Replaces Gatsby's ESLint Webpack configs, giving you full control to customize linting with the rules and developer experience you specifically need to maintain code quality.
This will COMPLETELY OVERWRITE any ESLint Webpack Plugins that Gatsby uses.
The installation instructions will help you reactivate the two required rules as of writing:
Installation
npm install --save-dev gatsby-plugin-eslint eslint-webpack-plugin
or
yarn add --dev gatsby-plugin-eslint eslint-webpack-plugin
Default Settings
- Lints development mode in the
'develop'
stage. Add other Webpack Config Stages into stages
array to enable linting during other Gatsby stages (eg. build-javascript
) - Lint
.js
, .jsx
, .ts
, and .tsx
files. - Excludes
node_modules
, bower_components
, .cache
, and public
folders from linting - Otherwise uses
eslint-webpack-plugin
option defaults
Usage
-
Create eslint.config.mjs
file in project root.
export default [
{
files: ['src/**/*.js'],
rules: {
'no-anonymous-exports-page-templates': 'warn',
'limited-exports-page-templates': 'warn'
}
}
];
-
Add plugin into gatsby-config.js
const path = require('path');
const gatsbyRequiredRules = path.join(
process.cwd(),
'node_modules',
'gatsby',
'dist',
'utils',
'eslint-rules'
);
module.exports = {
plugins: [
{
resolve: 'gatsby-plugin-eslint',
options: {
configType: 'flat',
eslintPath: 'eslint/use-at-your-own-risk',
rulePaths: [gatsbyRequiredRules]
}
}
]
};
-
Additionally as of writing, Gatsby's default ESLint config may be copied over if you would still like to take advavntage of those rules.
Configuring ESLint (To Be Updated For Flat Config)
Mix and match your own ESLint plugins and rules depending on the React/Javascript/Typescript patterns you want to enforce. Here are three ways you can get started:
-
Follow eslint-plugin-react
plugin installation procedures:
npm install --save-dev eslint-plugin-react babel-eslint
or
yarn add --dev eslint-plugin-react babel-eslint
-
Add .eslintrc
file to project root:
{
"parser": "babel-eslint",
"settings": {
"react": {
"version": "detect"
}
},
"env": {
"node": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended"
],
"rules": {
"no-anonymous-exports-page-templates": "warn",
"limited-exports-page-templates": "warn"
}
}
-
Follow @typescript-eslint/eslint-plugin
plugin installation procedures:
npm install --save-dev typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin
or
yarn add --dev typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin
-
Add .eslintrc
file to project root:
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
],
"rules": {
"no-anonymous-exports-page-templates": "warn",
"limited-exports-page-templates": "warn"
}
}