A webpack loader that integrates ESLint into the webpack build process for automatic JavaScript code linting during compilation.
npx @tessl/cli install tessl/npm-eslint-loader@4.0.0ESLint Loader is a webpack loader that integrates ESLint into the webpack build process, enabling automatic JavaScript code linting during compilation. It provides comprehensive ESLint integration with webpack bundling workflows, supporting features like caching for improved performance, configurable error and warning reporting, autofix capabilities, and flexible configuration options for customizing ESLint behavior within webpack builds.
Note: This package has been deprecated in favor of eslint-webpack-plugin for newer webpack versions.
npm install eslint-loader --save-devThe package exports a single webpack loader function:
// CommonJS (most common usage in webpack configs)
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader'
}
]
}
};For direct import (rarely needed):
const eslintLoader = require('eslint-loader');ES6 module import:
import eslintLoader from 'eslint-loader';// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader'
}
]
}
};// webpack.config.js
module.exports = {
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader'
},
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
]
}
};// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader',
options: {
cache: true,
fix: true,
emitWarning: true,
failOnError: false
}
}
]
}
};ESLint Loader is built around several key components that work together to integrate ESLint into webpack's build process:
getOptions() validates and processes configuration optionscreateEngine() provides cached ESLint CLIEngine instanceLinter class coordinates the linting workflowcacheLoader() checks for existing lint resultsLinter.lint() runs ESLint on source contentThe main export is a webpack loader function that processes JavaScript files through ESLint.
/**
* Main webpack loader function for ESLint integration
* @param {string} content - Source code content of the file being processed
* @param {Object} map - Source map from previous loaders in the chain
* @returns {void} - Uses webpack callback to pass through content and map
* @this {LoaderContext} - Webpack loader context with emitError, emitWarning, cacheable methods
*/
function loader(content, map);The loader function:
/**
* Processes and validates webpack loader options
* @param {Object} loaderContext - Webpack loader context
* @returns {Object} - Validated and processed options with ESLint formatter
*/
function getOptions(loaderContext);Processes loader options by:
/**
* Creates or retrieves cached ESLint CLIEngine instance
* @param {Object} options - ESLint configuration options
* @returns {Object} - Object containing CLIEngine constructor and engine instance
*/
function createEngine(options);Returns object with:
CLIEngine: ESLint CLIEngine constructor from specified eslintPathengine: Cached ESLint engine instance (cached by options hash for performance)/**
* Main ESLint processing class that handles linting workflow
* @param {Object} loaderContext - Webpack loader context
* @param {Object} options - Processed loader options
*/
class Linter {
constructor(loaderContext, options);
/**
* Execute ESLint on source content
* @param {string} content - Source code to lint
* @returns {Object} - ESLint results object
*/
lint(content);
/**
* Process and report ESLint results
* @param {Object} data - ESLint results from lint()
* @returns {void}
*/
printOutput(data);
/**
* Filter results based on quiet option
* @param {Object} data - ESLint results
* @returns {Object} - Filtered results
*/
filter(data);
/**
* Apply ESLint autofix if enabled
* @param {Object} res - ESLint results with fixable issues
* @returns {void}
*/
autoFix(res);
/**
* Generate report file if outputReport is configured
* @param {Array} results - ESLint results array
* @param {string} messages - Formatted lint messages
* @returns {void}
*/
reportOutput(results, messages);
/**
* Determine appropriate webpack emitter (error/warning)
* @param {Object} lintResults - Object with errorCount property
* @returns {Function} - Webpack emitError or emitWarning function
*/
getEmitter(lintResults);
}/**
* Handles caching of ESLint results for performance optimization
* @param {Linter} linter - Linter instance
* @param {string} content - Source content to cache
* @param {Object} map - Source map from webpack
* @returns {void} - Uses webpack async callback
*/
function cacheLoader(linter, content, map);Caching workflow:
/**
* Filesystem cache implementation for ESLint results
* @param {CacheParams} params - Cache configuration and data
* @returns {Promise<ESLintResult>} - Cached or computed lint results
*/
function cache(params);Cache features:
/**
* Custom error class for ESLint-related errors
* @param {string} messages - Formatted error messages from ESLint
*/
class ESLintError extends Error {
constructor(messages);
}Properties:
name: Always "ESLintError"stack: Set to false to prevent stack trace pollutionmessage: Contains formatted ESLint error messagesAll ESLint CLIEngine options are supported, plus these loader-specific options:
interface CacheOptions {
/** Enable caching of linting results to improve performance */
cache?: boolean | string;
}Usage:
cache: true - Uses default cache directory ./node_modules/.cache/eslint-loadercache: './.eslint-loader-cache' - Uses custom cache directory pathinterface ESLintIntegrationOptions {
/** Path to ESLint instance to use for linting */
eslintPath?: string;
/** Enable ESLint autofix feature (modifies source files) */
fix?: boolean;
/** ESLint output formatter (string name or custom function) */
formatter?: string | ((results: ESLintResult[]) => string);
}interface ErrorWarningOptions {
/** Force emit errors regardless of ESLint result severity */
emitError?: boolean;
/** Force emit warnings regardless of ESLint result severity */
emitWarning?: boolean;
/** Fail module build if ESLint reports errors */
failOnError?: boolean;
/** Fail module build if ESLint reports warnings */
failOnWarning?: boolean;
/** Report errors only, suppress warnings */
quiet?: boolean;
}interface OutputReportOptions {
/** Write ESLint output to file for CI/reporting */
outputReport?: boolean | {
/** Output file path (relative to webpack output.path) */
filePath: string;
/** Custom formatter for report file */
formatter?: string | ((results: ESLintResult[]) => string);
};
}interface ESLintLoaderOptions extends ESLintIntegrationOptions, CacheOptions, ErrorWarningOptions, OutputReportOptions {
// All ESLint CLIEngine options are also supported:
// configFile, baseConfig, rules, globals, extensions, etc.
}All options are validated against a JSON schema during loader initialization:
interface OptionsSchema {
/** Cache configuration - boolean or directory path string */
cache?: boolean | string;
/** Path to ESLint package - defaults to 'eslint' */
eslintPath?: string;
/** Formatter function or name - defaults to 'stylish' */
formatter?: string | ((results: ESLintResult[]) => string);
/** Enable ESLint autofix feature */
fix?: boolean;
/** Always emit webpack errors regardless of ESLint severity */
emitError?: boolean;
/** Always emit webpack warnings regardless of ESLint severity */
emitWarning?: boolean;
/** Fail webpack build on ESLint errors */
failOnError?: boolean;
/** Fail webpack build on ESLint warnings */
failOnWarning?: boolean;
/** Suppress warnings, show errors only */
quiet?: boolean;
/** Report output configuration */
outputReport?: boolean | {
/** Output file path (relative to webpack output.path) */
filePath: string;
/** Custom formatter for report file */
formatter?: string | ((results: ESLintResult[]) => string);
};
}Default Values:
eslintPath: 'eslint'formatter: 'stylish' (ESLint's default formatter)cache: falsefix: falsefalseValidation Rules:
cache: Must be boolean or non-empty stringeslintPath: Must be string (validated at runtime for valid ESLint package)formatter: String name or function (validated against ESLint's available formatters)outputReport.filePath: Required when outputReport is objectinterface ESLintResult {
/** Absolute path to the linted file */
filePath: string;
/** Array of lint messages (errors/warnings) */
messages: ESLintMessage[];
/** Total number of error-level messages */
errorCount: number;
/** Total number of warning-level messages */
warningCount: number;
/** Number of errors that can be automatically fixed */
fixableErrorCount: number;
/** Number of warnings that can be automatically fixed */
fixableWarningCount: number;
/** Fixed source code if autofix was applied */
output?: string;
/** Original source code */
src?: string;
}
interface ESLintMessage {
/** ESLint rule that triggered this message */
ruleId: string;
/** Message severity: 1 = warning, 2 = error */
severity: 1 | 2;
/** Human-readable description of the issue */
message: string;
/** Line number where issue occurs (1-based) */
line: number;
/** Column number where issue occurs (1-based) */
column: number;
/** AST node type that triggered the rule */
nodeType: string;
/** Source code excerpt where issue occurs */
source: string;
/** Autofix information if available */
fix?: ESLintFix;
}
interface ESLintFix {
/** Character range to replace [start, end] */
range: [number, number];
/** Replacement text for the range */
text: string;
}interface LoaderContext {
/** Webpack loader context with emit methods */
emitError: (error: Error) => void;
emitWarning: (warning: Error) => void;
/** Make loader result cacheable */
cacheable: () => void;
/** Async callback for returning results */
async: () => (error?: Error, content?: string, map?: Object) => void;
/** Absolute path to current resource being processed */
resourcePath: string;
/** Webpack compiler instance */
_compiler: {
options: {
output: {
path: string;
};
};
};
}interface EngineResult {
/** ESLint CLIEngine constructor */
CLIEngine: typeof import('eslint').CLIEngine;
/** Cached ESLint engine instance */
engine: import('eslint').CLIEngine;
}
interface CacheParams {
/** Cache directory path */
cacheDirectory: string | boolean;
/** Cache identifier for versioning */
cacheIdentifier: string;
/** Enable compression for cache files */
cacheCompression: boolean;
/** Loader options for cache key generation */
options: ESLintLoaderOptions;
/** Source code to cache */
source: string;
/** Transform function to generate cacheable result */
transform: () => ESLintResult;
}// webpack.config.js
module.exports = {
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader',
options: {
emitWarning: true, // Prevents HMR from breaking on lint errors
cache: true
}
}
]
}
};// webpack.config.js
module.exports = {
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader',
options: {
failOnError: true,
failOnWarning: true,
cache: true
}
}
]
}
};// webpack.config.js
module.exports = {
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader',
options: {
cache: true,
outputReport: {
filePath: 'eslint-report.xml',
formatter: 'checkstyle'
}
}
}
]
}
};// webpack.config.js
const path = require('path');
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader',
options: {
eslintPath: path.join(__dirname, 'custom-eslint-config'),
formatter: require('eslint-friendly-formatter')
}
}
]
}
};The loader integrates with webpack's error handling system:
loaderContext.emitError() (stops compilation by default)loaderContext.emitWarning() (allows compilation to continue)The emitError and emitWarning options override the default behavior based on ESLint severity levels.
This package is deprecated. For webpack 5 and newer ESLint versions, use eslint-webpack-plugin instead:
npm uninstall eslint-loader
npm install eslint-webpack-plugin --save-dev// webpack.config.js
const ESLintPlugin = require('eslint-webpack-plugin');
module.exports = {
plugins: [
new ESLintPlugin({
// options
})
]
};