eslint-loader 0,1 editorconfig eslint travis-ci npm

eslint loader (for webpack)

2 years after

eslint-loader Build Status

eslint loader for webpack

Install

$ npm install eslint-loader --save-dev

Usage

In your webpack configuration

module.exports = {
  // ...
  module: {
    loaders: [
      {test: /\.js$/, loader: "eslint-loader", exclude: /node_modules/}
    ]
  }
  // ...
}

When using with transpiling loaders (like babel-loader), make sure they are in correct order (bottom to top). Otherwise files will be check after being processed by babel-loader

module.exports = {
  // ...
  module: {
    loaders: [
      {test: /\.js$/, loaders: [ "babel-loader", "eslint-loader" ], exclude: /node_modules/},
    ]
  }
  // ...
}

To be safe, you can use preLoaders section to check source files, not modified by other loaders (like babel-loader)

module.exports = {
  // ...
  module: {
    preLoaders: [
      {test: /\.js$/, loader: "eslint-loader", exclude: /node_modules/}
    ]
  }
  // ...
}

[email protected] has breaking changes. preLoaders is removed from the webpack^2.1.0-beta.23. so move it to loaders and using [enforce: "pre"] instead.

module.exports = {
  // ...
  module: {
    loaders: [
      {enforce: "pre", test: /\.js$/,  loader: "eslint-loader", exclude: /node_modules/}
      // ... other loader
    ]
  }
  // ...
}

Options

You can pass eslint options directly by

  • Adding a query string to the loader for this loader usage only
{
  module: {
    preLoaders: [
      {
        test: /\.js$/,
        loader: "eslint-loader?{rules:{semi:0}}",
        exclude: /node_modules/,
      },
    ],
  },
}
  • Adding an eslint entry in your webpack config for global options:
module.exports = {
  eslint: {
    configFile: 'path/.eslintrc'
  }
}

Note that you can use both methods in order to benefit from global & specific options

fix (default: false)

This option will enable ESLint autofix feature.

Be careful, this option might cause webpack to enter an infinite build loop if some issues cannot be fixed properly.

cache (default: false)

This option will enable caching of the linting results into a file. This is particularly useful in reducing linting time when doing a full build.

The cache file is written to the ./node_modules/.cache directory, thanks to the usage of the find-cache-dir module.

formatter (default: eslint stylish formatter)

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 formatters.

module.exports = {
  entry: "...",
  module: {
    // ...
  }
  eslint: {
    // several examples !

    // default value
    formatter: require("eslint/lib/formatters/stylish"),

    // community formatter
    formatter: require("eslint-friendly-formatter"),

    // custom formatter
    formatter: function(results) {
      // `results` format is available here
      // http://eslint.org/docs/developer-guide/nodejs-api.html#executeonfiles()

      // you should return a string
      // DO NOT USE console.*() directly !
      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 return errors if this option is set to true.

module.exports = {
  entry: "...",
  module: {
    // ...
  },
  eslint: {
    emitError: true
  }
}
emitWarning (default: false)

Loader will always return warnings 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
  }
}

Gotchas

NoErrorsPlugin

NoErrorsPlugin prevents Webpack from outputting anything into a bundle. So even ESLint warnings will fail the build. No matter what error settings are used for eslint-loader.

So if you want to see ESLint warnings in console during development using WebpackDevServer remove NoErrorsPlugin from webpack config.

Changelog

License

Related Repositories

eslint-plugin-import

eslint-plugin-import

ESLint plugin with rules that help validate proper imports. ...

vue-loader-example

vue-loader-example

Example using vue-loader with Webpack. ...

bootstrap-sass-loader

bootstrap-sass-loader

Webpack Loader for the Sass version Twitter Bootstrap ...

standard-loader

standard-loader

webpack loader for linting your code with https://github.com/feross/standard ...

standard-loader

standard-loader

webpack loader for linting your code with https://github.com/feross/standard ...


Top Contributors

MoOx kentcdodds amilajack JamesHenry nkbt farneman chentsulin coryhouse dfournier Semigradsky yyx990803 lasfin existentialism lancefisher mgtitimoli maxhauser werme shinnn genintho yamalight vinodc alexsunxl bjornbytes waiterZen

Dependencies

package version
loader-fs-cache ^1.0.0
loader-utils ^1.0.2
object-assign ^4.0.1
object-hash ^1.1.4
rimraf ^2.6.1
dev ava ^0.17.0
eslint ^4.0.0
eslint-friendly-formatter ^2.0.4
npmpub ^3.0.1
webpack ^3.3.0
peer eslint >=1.6.0 <5.0.0

Releases

-   1.5.0 zip tar
-   1.4.1 zip tar
-   1.4.0 zip tar
-   1.3.0 zip tar
-   1.2.1 zip tar
-   1.2.0 zip tar
-   1.1.1 zip tar
-   1.1.0 zip tar
-   1.0.0 zip tar
-   0.14.2 zip tar
-   0.14.1 zip tar
-   0.14.0 zip tar
-   0.13.0 zip tar
-   0.12.0 zip tar
-   0.11.2 zip tar
-   0.11.1 zip tar
-   0.11.0 zip tar
-   0.10.0 zip tar
-   0.9.0 zip tar
-   0.8.0 zip tar
-   0.7.0 zip tar
-   0.6.0 zip tar
-   0.5.0 zip tar
-   0.4.0 zip tar
-   0.3.0 zip tar
-   0.2.1 zip tar
-   0.2.0 zip tar
-   0.1.0 zip tar