Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. # eslint-loader [![Build Status](https://travis-ci.org/webpack-contrib/eslint-loader.svg?branch=master)](https://travis-ci.org/webpack-contrib/eslint-loader)
  2. > eslint loader for webpack
  3. ## Install
  4. ```console
  5. $ npm install eslint-loader --save-dev
  6. ```
  7. **NOTE**: You also need to install `eslint` from npm, if you haven't already:
  8. ```console
  9. $ npm install eslint --save-dev
  10. ```
  11. ## Usage
  12. In your webpack configuration
  13. ```js
  14. module.exports = {
  15. // ...
  16. module: {
  17. rules: [
  18. {
  19. test: /\.js$/,
  20. exclude: /node_modules/,
  21. loader: "eslint-loader",
  22. options: {
  23. // eslint options (if necessary)
  24. }
  25. },
  26. ],
  27. },
  28. // ...
  29. }
  30. ```
  31. When using with transpiling loaders (like `babel-loader`), make sure they are in correct order
  32. (bottom to top). Otherwise files will be checked after being processed by `babel-loader`
  33. ```js
  34. module.exports = {
  35. // ...
  36. module: {
  37. rules: [
  38. {
  39. test: /\.js$/,
  40. exclude: /node_modules/,
  41. use: [
  42. "babel-loader",
  43. "eslint-loader",
  44. ],
  45. },
  46. ],
  47. },
  48. // ...
  49. }
  50. ```
  51. To be safe, you can use `enforce: "pre"` section to check source files, not modified
  52. by other loaders (like `babel-loader`)
  53. ```js
  54. module.exports = {
  55. // ...
  56. module: {
  57. rules: [
  58. {
  59. enforce: "pre",
  60. test: /\.js$/,
  61. exclude: /node_modules/,
  62. loader: "eslint-loader",
  63. },
  64. {
  65. test: /\.js$/,
  66. exclude: /node_modules/,
  67. loader: "babel-loader",
  68. },
  69. ],
  70. },
  71. // ...
  72. }
  73. ```
  74. ### Options
  75. You can pass [eslint options](http://eslint.org/docs/developer-guide/nodejs-api#cliengine)
  76. using standard webpack [loader options](https://webpack.js.org/configuration/module/#useentry).
  77. Note that the config option you provide will be passed to the `CLIEngine`.
  78. This is a different set of options than what you'd specify in `package.json` or `.eslintrc`.
  79. See the [eslint docs](http://eslint.org/docs/developer-guide/nodejs-api#cliengine) for more detail.
  80. #### `fix` (default: false)
  81. This option will enable
  82. [ESLint autofix feature](http://eslint.org/docs/user-guide/command-line-interface#fix).
  83. **Be careful: this option will change source files.**
  84. #### `cache` (default: false)
  85. This option will enable caching of the linting results into a file.
  86. This is particularly useful in reducing linting time when doing a full build.
  87. This can either be a `boolean` value or the cache directory path(ex: `'./.eslint-loader-cache'`).
  88. If `cache: true` is used, the cache file is written to the `./node_modules/.cache` directory.
  89. This is the recommended usage.
  90. #### `formatter` (default: eslint stylish formatter)
  91. Loader accepts a function that will have one argument: an array of eslint messages (object).
  92. The function must return the output as a string.
  93. You can use official eslint formatters.
  94. ```js
  95. module.exports = {
  96. entry: "...",
  97. module: {
  98. rules: [
  99. {
  100. test: /\.js$/,
  101. exclude: /node_modules/,
  102. loader: "eslint-loader",
  103. options: {
  104. // several examples !
  105. // default value
  106. formatter: require("eslint/lib/formatters/stylish"),
  107. // community formatter
  108. formatter: require("eslint-friendly-formatter"),
  109. // custom formatter
  110. formatter: function(results) {
  111. // `results` format is available here
  112. // http://eslint.org/docs/developer-guide/nodejs-api.html#executeonfiles()
  113. // you should return a string
  114. // DO NOT USE console.*() directly !
  115. return "OUTPUT"
  116. }
  117. }
  118. },
  119. ],
  120. },
  121. }
  122. ```
  123. #### `eslintPath` (default: "eslint")
  124. Path to `eslint` instance that will be used for linting.
  125. If the `eslintPath` is a folder like a official eslint, or specify a `formatter` option. now you dont have to install `eslint` .
  126. ```js
  127. module.exports = {
  128. entry: "...",
  129. module: {
  130. rules: [
  131. {
  132. test: /\.js$/,
  133. exclude: /node_modules/,
  134. loader: "eslint-loader",
  135. options: {
  136. eslintPath: path.join(__dirname, "reusable-eslint"),
  137. }
  138. },
  139. ],
  140. },
  141. }
  142. ```
  143. #### Errors and Warning
  144. **By default the loader will auto adjust error reporting depending
  145. on eslint errors/warnings counts.**
  146. You can still force this behavior by using `emitError` **or** `emitWarning` options:
  147. ##### `emitError` (default: `false`)
  148. Loader will always return errors if this option is set to `true`.
  149. ```js
  150. module.exports = {
  151. entry: "...",
  152. module: {
  153. rules: [
  154. {
  155. test: /\.js$/,
  156. exclude: /node_modules/,
  157. loader: "eslint-loader",
  158. options: {
  159. emitError: true,
  160. }
  161. },
  162. ],
  163. },
  164. }
  165. ```
  166. ##### `emitWarning` (default: `false`)
  167. Loader 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.
  168. #### `quiet` (default: `false`)
  169. Loader will process and report errors only and ignore warnings if this option is set to true
  170. ```js
  171. module.exports = {
  172. entry: "...",
  173. module: {
  174. rules: [
  175. {
  176. test: /\.js$/,
  177. exclude: /node_modules/,
  178. loader: "eslint-loader",
  179. options: {
  180. quiet: true,
  181. }
  182. },
  183. ],
  184. },
  185. }
  186. ```
  187. ##### `failOnWarning` (default: `false`)
  188. Loader will cause the module build to fail if there are any eslint warnings.
  189. ```js
  190. module.exports = {
  191. entry: "...",
  192. module: {
  193. rules: [
  194. {
  195. test: /\.js$/,
  196. exclude: /node_modules/,
  197. loader: "eslint-loader",
  198. options: {
  199. failOnWarning: true,
  200. }
  201. },
  202. ],
  203. },
  204. }
  205. ```
  206. ##### `failOnError` (default: `false`)
  207. Loader will cause the module build to fail if there are any eslint errors.
  208. ```js
  209. module.exports = {
  210. entry: "...",
  211. module: {
  212. rules: [
  213. {
  214. test: /\.js$/,
  215. exclude: /node_modules/,
  216. loader: "eslint-loader",
  217. options: {
  218. failOnError: true,
  219. }
  220. },
  221. ],
  222. },
  223. }
  224. ```
  225. ##### `outputReport` (default: `false`)
  226. Write the output of the errors to a file, for example a checkstyle xml file for use for reporting on Jenkins CI
  227. The `filePath` is relative to the webpack config: output.path
  228. You can pass in a different formatter for the output file, if none is passed in the default/configured formatter will be used
  229. ```js
  230. module.exports = {
  231. entry: "...",
  232. module: {
  233. rules: [
  234. {
  235. test: /\.js$/,
  236. exclude: /node_modules/,
  237. loader: "eslint-loader",
  238. options: {
  239. outputReport: {
  240. filePath: 'checkstyle.xml',
  241. formatter: require('eslint/lib/formatters/checkstyle')
  242. }
  243. }
  244. },
  245. ],
  246. },
  247. }
  248. ```
  249. ## Gotchas
  250. ### NoErrorsPlugin
  251. `NoErrorsPlugin` prevents webpack from outputting anything into a bundle. So even ESLint warnings
  252. will fail the build. No matter what error settings are used for `eslint-loader`.
  253. So if you want to see ESLint warnings in console during development using `WebpackDevServer`
  254. remove `NoErrorsPlugin` from webpack config.
  255. ### Defining `configFile` or using `eslint -c path/.eslintrc`
  256. Bear in mind that when you define `configFile`, `eslint` doesn't automatically look for
  257. `.eslintrc` files in the directory of the file to be linted.
  258. More information is available in official eslint documentation in section [_Using Configuration Files_](http://eslint.org/docs/user-guide/configuring#using-configuration-files).
  259. See [#129](https://github.com/webpack-contrib/eslint-loader/issues/129).
  260. ---
  261. ## [Changelog](CHANGELOG.md)
  262. ## [License](LICENSE)