项目作者: webpack-contrib

项目描述 :
[DEPRECATED] A ESlint loader for webpack
高级语言: JavaScript
项目地址: git://github.com/webpack-contrib/eslint-loader.git
创建时间: 2014-12-05T06:15:25Z
项目社区:https://github.com/webpack-contrib/eslint-loader

开源协议:MIT License

下载





npm
node
deps
tests
coverage
chat
size

eslint-loader

A ESlint loader for webpack

:warning: DEPRECATED

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

Install

  1. npm install eslint-loader --save-dev

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

  1. npm install eslint --save-dev

Usage

In your webpack configuration:

  1. module.exports = {
  2. // ...
  3. module: {
  4. rules: [
  5. {
  6. test: /\.js$/,
  7. exclude: /node_modules/,
  8. loader: 'eslint-loader',
  9. options: {
  10. // eslint options (if necessary)
  11. },
  12. },
  13. ],
  14. },
  15. // ...
  16. };

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:

  1. module.exports = {
  2. // ...
  3. module: {
  4. rules: [
  5. {
  6. test: /\.js$/,
  7. exclude: /node_modules/,
  8. use: ['babel-loader', 'eslint-loader'],
  9. },
  10. ],
  11. },
  12. // ...
  13. };

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

  1. module.exports = {
  2. // ...
  3. module: {
  4. rules: [
  5. {
  6. enforce: 'pre',
  7. test: /\.js$/,
  8. exclude: /node_modules/,
  9. loader: 'eslint-loader',
  10. },
  11. {
  12. test: /\.js$/,
  13. exclude: /node_modules/,
  14. loader: 'babel-loader',
  15. },
  16. ],
  17. },
  18. // ...
  19. };

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.

  1. module.exports = {
  2. entry: '...',
  3. module: {
  4. rules: [
  5. {
  6. test: /\.js$/,
  7. exclude: /node_modules/,
  8. loader: 'eslint-loader',
  9. options: {
  10. cache: true,
  11. },
  12. },
  13. ],
  14. },
  15. };

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.

  1. module.exports = {
  2. entry: '...',
  3. module: {
  4. rules: [
  5. {
  6. test: /\.js$/,
  7. exclude: /node_modules/,
  8. loader: 'eslint-loader',
  9. options: {
  10. eslintPath: path.join(__dirname, 'reusable-eslint'),
  11. },
  12. },
  13. ],
  14. },
  15. };

fix

  • Type: Boolean
  • Default: false

This option will enable ESLint autofix feature.

Be careful: this option will change source files.

  1. module.exports = {
  2. entry: '...',
  3. module: {
  4. rules: [
  5. {
  6. test: /\.js$/,
  7. exclude: /node_modules/,
  8. loader: 'eslint-loader',
  9. options: {
  10. fix: true,
  11. },
  12. },
  13. ],
  14. },
  15. };

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.

  1. module.exports = {
  2. entry: '...',
  3. module: {
  4. rules: [
  5. {
  6. test: /\.js$/,
  7. exclude: /node_modules/,
  8. loader: 'eslint-loader',
  9. options: {
  10. // several examples !
  11. // default value
  12. formatter: 'stylish',
  13. // community formatter
  14. formatter: require('eslint-friendly-formatter'),
  15. // custom formatter
  16. formatter: function (results) {
  17. // `results` format is available here
  18. // http://eslint.org/docs/developer-guide/nodejs-api.html#executeonfiles()
  19. // you should return a string
  20. // DO NOT USE console.*() directly !
  21. return 'OUTPUT';
  22. },
  23. },
  24. },
  25. ],
  26. },
  27. };

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.

  1. module.exports = {
  2. entry: '...',
  3. module: {
  4. rules: [
  5. {
  6. test: /\.js$/,
  7. exclude: /node_modules/,
  8. loader: 'eslint-loader',
  9. options: {
  10. emitError: true,
  11. },
  12. },
  13. ],
  14. },
  15. };

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.

  1. module.exports = {
  2. entry: '...',
  3. module: {
  4. rules: [
  5. {
  6. test: /\.js$/,
  7. exclude: /node_modules/,
  8. loader: 'eslint-loader',
  9. options: {
  10. emitWarning: true,
  11. },
  12. },
  13. ],
  14. },
  15. };

failOnError

  • Type: Boolean
  • Default: false

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

  1. module.exports = {
  2. entry: '...',
  3. module: {
  4. rules: [
  5. {
  6. test: /\.js$/,
  7. exclude: /node_modules/,
  8. loader: 'eslint-loader',
  9. options: {
  10. failOnError: true,
  11. },
  12. },
  13. ],
  14. },
  15. };

failOnWarning

  • Type: Boolean
  • Default: false

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

  1. module.exports = {
  2. entry: '...',
  3. module: {
  4. rules: [
  5. {
  6. test: /\.js$/,
  7. exclude: /node_modules/,
  8. loader: 'eslint-loader',
  9. options: {
  10. failOnWarning: true,
  11. },
  12. },
  13. ],
  14. },
  15. };

quiet

  • Type: Boolean
  • Default: false

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

  1. module.exports = {
  2. entry: '...',
  3. module: {
  4. rules: [
  5. {
  6. test: /\.js$/,
  7. exclude: /node_modules/,
  8. loader: 'eslint-loader',
  9. options: {
  10. quiet: true,
  11. },
  12. },
  13. ],
  14. },
  15. };

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.

  1. module.exports = {
  2. entry: '...',
  3. module: {
  4. rules: [
  5. {
  6. test: /\.js$/,
  7. exclude: /node_modules/,
  8. loader: 'eslint-loader',
  9. options: {
  10. outputReport: {
  11. filePath: 'checkstyle.xml',
  12. formatter: 'checkstyle',
  13. },
  14. },
  15. },
  16. ],
  17. },
  18. };

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