包详细信息

eslint-loader

webpack-contrib4.2mMIT不推荐使用4.0.2

This loader has been deprecated. Please use eslint-webpack-plugin

A ESlint loader for webpack

eslint, lint, linter, loader

自述文件

npm node deps tests coverage chat size

eslint-loader

A ESlint loader for webpack

Install

npm install eslint-loader --save-dev

Note: You also need to install eslint from npm, if you haven't already:

npm install eslint --save-dev

Usage

In your webpack configuration:

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
          // eslint options (if necessary)
        },
      },
    ],
  },
  // ...
};

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

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: ['babel-loader', 'eslint-loader'],
      },
    ],
  },
  // ...
};

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

module.exports = {
  // ...
  module: {
    rules: [
      {
        enforce: 'pre',
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
      },
    ],
  },
  // ...
};

Options

You can pass eslint options using standard webpack loader options.

Note: That the config option you provide will be passed to the CLIEngine. This is a different set of options than what you'd specify in package.json or .eslintrc. See the eslint docs for more detail.

cache

  • Type: Boolean|String
  • 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.

This can either be a boolean value or the cache directory path(ex: './.eslint-loader-cache').

If cache: true is used, the cache is written to the ./node_modules/.cache/eslint-loader directory. This is the recommended usage.

module.exports = {
  entry: '...',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
          cache: true,
        },
      },
    ],
  },
};

eslintPath

  • Type: String
  • Default: eslint

Path to eslint instance that will be used for linting. If the eslintPath is a folder like a official eslint, or specify a formatter option. Now you dont have to install eslint.

module.exports = {
  entry: '...',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
          eslintPath: path.join(__dirname, 'reusable-eslint'),
        },
      },
    ],
  },
};

fix

  • Type: Boolean
  • Default: false

This option will enable ESLint autofix feature.

Be careful: this option will change source files.

module.exports = {
  entry: '...',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
          fix: true,
        },
      },
    ],
  },
};

formatter

  • Type: String|Function
  • Default: stylish

This option 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: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
          // several examples !

          // default value
          formatter: '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

  • Type: Boolean
  • Default: false

Will always return errors, if this option is set to true.

module.exports = {
  entry: '...',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
          emitError: true,
        },
      },
    ],
  },
};

emitWarning

  • Type: Boolean
  • Default: false

Will always return warnings, if option is set to true. If you're using hot module replacement, you may wish to enable this in development, or else updates will be skipped when there's an eslint error.

module.exports = {
  entry: '...',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
          emitWarning: true,
        },
      },
    ],
  },
};

failOnError

  • Type: Boolean
  • Default: false

Will cause the module build to fail if there are any errors, if option is set to true.

module.exports = {
  entry: '...',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
          failOnError: true,
        },
      },
    ],
  },
};

failOnWarning

  • Type: Boolean
  • Default: false

Will cause the module build to fail if there are any warnings, if option is set to true.

module.exports = {
  entry: '...',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
          failOnWarning: true,
        },
      },
    ],
  },
};

quiet

  • Type: Boolean
  • Default: false

Will process and report errors only and ignore warnings, if this option is set to true.

module.exports = {
  entry: '...',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
          quiet: true,
        },
      },
    ],
  },
};

outputReport

  • Type: Boolean|Object
  • Default: false

Write the output of the errors to a file, for example a checkstyle xml file for use for reporting on Jenkins CI.

The filePath is an absolute path or relative to the webpack config: output.path. You can pass in a different formatter for the output file, if none is passed in the default/configured formatter will be used.

module.exports = {
  entry: '...',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
          outputReport: {
            filePath: 'checkstyle.xml',
            formatter: 'checkstyle',
          },
        },
      },
    ],
  },
};

Gotchas

NoEmitOnErrorsPlugin

NoEmitOnErrorsPlugin is now automatically enabled in webpack 4, when mode is either unset, or set to production. So even ESLint warnings will fail the build. No matter what error settings are used for eslint-loader, except if emitWarning enabled.

Defining configFile or using eslint -c path/.eslintrc

Bear in mind that when you define configFile, eslint doesn't automatically look for .eslintrc files in the directory of the file to be linted. More information is available in official eslint documentation in section Using Configuration Files. See #129.

Changelog

Changelog

License

MIT

更新日志

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

4.0.2 (2020-04-24)

Bug Fixes

4.0.1 (2020-04-24)

Refactor

4.0.0 (2020-04-03)

Breaking Changes

  • drop support for Node < 10.13.0
  • minimum supported eslint version is 6

3.0.4 (2020-04-02)

Bug Fixes

3.0.3 (2019-12-06)

Bug Fixes

3.0.2 (2019-09-27)

Bug Fixes

3.0.1 (2019-09-25)

Bug Fixes

  • module build failed error at Linter.parseResults (#294) (360e69c)

3.0.0 (2019-08-24)

Bugfix

  • fix corrupted filenames if cwd == "/"
  • cannot use string formatter in outputReport
  • no Output Report in File when build fails under webpack 4
  • add posibility to use absolute file path in outputReport.filePath
  • it should be possible to use absolute file path in outputReport.filePath
  • try load official formatter (#285) (997cce5)
  • emit warning/error if no config was found/given (#286) (4204560)

Features

  • validate schema options

Breaking Changes

  • drop support for Node < 8.9.0
  • minimum supported webpack version is 4
  • minimum supported eslint version is 5

2.2.1 - 2019-07-04

  • Fixed: path to eslint version for cache (#282 - @ricardogobbosouza)

2.2.0 - 2019-07-03

2.1.2 - 2019-01-31

2.1.1 - 2018-09-19

2.1.0 - 2018-07-19

2.0.0 - 2018-02-26

  • 🚨 Drop webpack@1.x support (by @wonism in #212)
  • 🚨 Drop Node 4 supported (by @wonism in #212)
  • ✨ Add webpack@4.x support (by @wonism in #212)
  • 🐛 Respect eslintPath for formatter (by @lmnsg in #195)

1.9.0 - 2017-07-06

  • Added: eslintPath option so you can provide your own eslint path. (#183 - @trungdq88)

1.8.0 - 2017-06-15

  • Added: support for eslint@^4.0.0 (#178 - @Aladdin-ADD)

1.7.1 - 2017-03-31

  • Fixed: Remove duplicate output with webpack 2 (#169 - @jaridmargolin)

1.7.0 - 2017-03-23

  • Fixed: outputReport option writes report for last file checked only (#160 - @deryni)
  • Added: use babel loader fs cache as the default caching engine (#159 - @viankakrisna)

1.6.3 - 2017-02-22

  • Fixed: ignore cache when eslint rules have changed (#151 - @wrakky)

1.6.2 - 2017-02-22

  • Fixed: fallback to OS temp directory if findCacheDir fails (#154 - @viankakrisna)
  • Fixed: loader-utils deprecation warning by upgrading to v1.0.0 (#155 - @fknussel)

1.6.1 - 2016-11-02

  • Fixed: multiples config per instance are now supported (#105 - @jaythomas and @jameslnewell)

1.6.0 - 2016-10-17

  • Added: Option to generate report file (#118 - @vidhill)

1.5.0 - 2016-07-28

  • Added: cache options (#93 - @genintho)

1.4.1 - 2016-06-07

  • Fixed: .eslintignore is not ignored anymore (eslint 3.x regression) (#99 - @waiterZen)

1.4.0 - 2016-06-02

  • Added: support for eslint@^3.0.0 (#96)

1.3.0 - 2016-02-17

  • Added: support for eslint@^2.0.0 (#81)

1.2.1 - 2016-01-26

  • Updated: object-assign dependency (#77)

1.2.0 - 2016-01-02

  • Added: this loader now pass down the input source map to the next chained loader if it exists (#70).

1.1.1 - 2015-10-08

  • Fixed: failOnError and failOnWarning now print messages.

1.1.0 - 2015-10-08

  • Added: fix option to enable ESLint auto fix feature.

1.0.0 - 2015-08-08

  • Added: support for eslint 1.x
  • Removed: support for eslint 1.x-rc*
  • Removed: support for eslint 0.x

0.14.2 - 2015-07-18

  • Fixed: support for eslint 1.x-rc

0.14.1 - 2015-06-15

  • Fixed: support for eslint 0.24.x

0.14.0 - 2015-06-15

  • Added: support for eslint 0.23.x

0.13.0 - 2015-06-14

  • Changed: a file that should be ignored doesn't trigger a warning (#44)

0.12.0 - 2015-06-04

  • Changed: upgrade to eslint 0.22.x
  • Fixed: respect .eslintrc/eslintignore files in directory tree (#21)

0.11.2 - 2015-05-11

  • Fixed: eslint range from 0.17 to 0.21

0.11.1 - 2015-04-27

  • Fixed: eslint range from 0.17 to 0.20

0.11.0 - 2015-04-27

  • Changed: upgrade to eslint 0.20.x

0.10.0 - 2015-04-13

  • Changed: upgrade to eslint 0.19.x

0.9.0 - 2015-03-29

  • Changed: upgrade to eslint 0.18.x

0.8.0 - 2015-03-27

  • Changed: reporter is now formatter option to fit eslint name
  • Changed: plugin is now async as it don't need to be sync
  • Added: options are supported as query strings

0.7.0 - 2015-03-15

  • Changed: upgrade to eslint 0.17.x
  • Added: failOnError option
  • Added: failOnWarning option

0.6.0 - 2015-03-11

  • Changed: reporter now automatically drop lines that contains the filename in the reporter output. That mean you can use official or community reporters without worrying to see lot of lines with <text> as filename :)

0.5.0 - 2015-03-11

  • Changed: upgrade to eslint 0.16.x
  • Changed: emitErrors is now emitError
  • Changed: loader now use webpack.emitError or webpack.emitWarning automatically (according to eslint configuration). You can still override by using emitError or emitWarning options to override this behavior
  • Added: emitWarning can force eslint to report warning instead of the default behavior (see above)
  • Added: quiet option to hide warnings

0.4.0 - 2015-02-23

  • Changed: upgrade to eslint 0.15.x
  • Changed: more readable default reporter
  • Added: reporter options allow to define a custom reporter function

0.3.0 - 2015-02-10

  • Changed: upgrade to eslint 0.14.x

0.2.1 - 2015-01-27

  • Changed: upgrade to eslint 0.13.x

0.2.0 - 2015-01-23

  • Changed: upgrade to eslint 0.12.x
  • Added: enable loading of eslint config from webpack config, .eslintrc, or package.json

0.1.0 - 2014-12-05

✨ Initial release