What is eslint-loader?
eslint-loader is a webpack loader that integrates ESLint into the webpack build process. It allows you to lint your JavaScript code as part of the build process, ensuring that your code adheres to specified coding standards and conventions.
What are eslint-loader's main functionalities?
Linting JavaScript files
This feature allows you to lint JavaScript files using ESLint as part of the webpack build process. The configuration specifies that all `.js` files, excluding those in `node_modules`, should be processed by `eslint-loader`.
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'eslint-loader',
options: {
// eslint options (if necessary)
}
}
]
}
]
}
};
Custom ESLint options
This feature allows you to specify custom ESLint options, such as the path to the ESLint configuration file (`configFile`) and whether to emit warnings instead of errors (`emitWarning`).
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'eslint-loader',
options: {
configFile: 'path/to/.eslintrc',
emitWarning: true
}
}
]
}
]
}
};
Other packages similar to eslint-loader
eslint-webpack-plugin
eslint-webpack-plugin is a modern alternative to eslint-loader. It provides similar functionality by integrating ESLint into the webpack build process but is designed to work with webpack 5 and above. It offers better performance and more features compared to eslint-loader.
stylelint-webpack-plugin
stylelint-webpack-plugin is a webpack plugin that integrates Stylelint into the webpack build process. While it focuses on linting CSS and other style files, it provides similar functionality to eslint-loader for stylesheets.
tslint-loader
tslint-loader is a webpack loader that integrates TSLint into the webpack build process. It is similar to eslint-loader but is specifically designed for linting TypeScript files.
eslint-loader
eslint loader for webpack
Install
$ npm install eslint-loader
Usage
In your webpack configuration
module.exports = {
module: {
loaders: [
{test: /\.js$/, loader: "eslint-loader", exclude: /node_modules/}
]
}
}
Options
You can pass directly some eslint options by adding an eslint
entry in you webpack config:
module.exports = {
eslint: {
configFile: 'path/.eslintrc'
}
}
reporter
(default: eslint stylish reporter)
Loader accepts a function that will have one argument: an array of eslint messages (object).
The function must return the output as a string.
You can use official eslint reporters.
Please note that every lines with the filename will be skipped from output
because of the way the loader use eslint (just with a string of text, not a file - so no filename available)
module.exports = {
entry: "...",
module: {
}
eslint: {
reporter: require("eslint/lib/formatters/stylish"),
reporter: require("eslint-friendly-formatter"),
reporter: function(results) {
return "OUTPUT"
}
}
}
Errors and Warning
By default the loader will auto adjust error reporting depending
on eslint errors/warnings counts.
You can still force this behavior by using emitError
or emitWarning
options:
emitError
(default: false
)
Loader will always returns errors if this option is set to true
.
module.exports = {
entry: "...",
module: {
}
eslint: {
emitErrors: true
}
}
emitWarning
(default: false
)
Loader will always returns warning if option is set to true
.
quiet
(default: false
)
Loader will process and report errors only and ignore warnings if this option is set to true
module.exports = {
entry: "...",
module: {
}
eslint: {
quiet: true
}
}
failOnWarning
(default: false
)
Loader will cause the module build to fail if there are any eslint warnings.
module.exports = {
entry: "...",
module: {
}
eslint: {
failOnWarning: true
}
}
failOnError
(default: false
)
Loader will cause the module build to fail if there are any eslint errors.
module.exports = {
entry: "...",
module: {
}
eslint: {
failOnError: true
}
}