提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
6.7 KiB

  1. <div align="center">
  2. <a href="https://github.com/eslint/eslint"><img width="200" height="200" src="https://cdn.worldvectorlogo.com/logos/eslint.svg"></a>
  3. <a href="https://github.com/webpack/webpack"><img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg"></a>
  4. </div>
  5. [![npm][npm]][npm-url]
  6. [![node][node]][node-url]
  7. [![tests][tests]][tests-url]
  8. [![coverage][cover]][cover-url]
  9. [![chat][chat]][chat-url]
  10. [![size][size]][size-url]
  11. # eslint-webpack-plugin
  12. > This is eslint-webpack-plugin 3.0 which works only with webpack 5. For the webpack 4, see the [2.x branch](https://github.com/webpack-contrib/eslint-webpack-plugin/tree/2.x).
  13. This plugin uses [`eslint`](https://eslint.org/) to find and fix problems in your JavaScript code
  14. ## Getting Started
  15. To begin, you'll need to install `eslint-webpack-plugin`:
  16. ```console
  17. npm install eslint-webpack-plugin --save-dev
  18. ```
  19. or
  20. ```console
  21. yarn add -D eslint-webpack-plugin
  22. ```
  23. or
  24. ```console
  25. pnpm add -D eslint-webpack-plugin
  26. ```
  27. > **Note**
  28. >
  29. > You also need to install `eslint >= 7` from npm, if you haven't already:
  30. ```console
  31. npm install eslint --save-dev
  32. ```
  33. or
  34. ```console
  35. yarn add -D eslint
  36. ```
  37. or
  38. ```console
  39. pnpm add -D eslint
  40. ```
  41. Then add the plugin to your webpack config. For example:
  42. ```js
  43. const ESLintPlugin = require('eslint-webpack-plugin');
  44. module.exports = {
  45. // ...
  46. plugins: [new ESLintPlugin(options)],
  47. // ...
  48. };
  49. ```
  50. ## Options
  51. You can pass [eslint options](https://eslint.org/docs/developer-guide/nodejs-api#-new-eslintoptions).
  52. > **Note**
  53. >
  54. > The config option you provide will be passed to the `ESLint` class.
  55. > This is a different set of options than what you'd specify in `package.json` or `.eslintrc`.
  56. > See the [eslint docs](https://eslint.org/docs/developer-guide/nodejs-api#-new-eslintoptions) for more details.
  57. > **Warning**:
  58. >
  59. > In eslint-webpack-plugin version 1 the options were passed to the now deprecated [CLIEngine](https://eslint.org/docs/developer-guide/nodejs-api#cliengine).
  60. ### `context`
  61. - Type:
  62. ```ts
  63. type context = string;
  64. ```
  65. - Default: `compiler.context`
  66. A string indicating the root of your files.
  67. ### `eslintPath`
  68. - Type:
  69. ```ts
  70. type eslintPath = string;
  71. ```
  72. - Default: `eslint`
  73. 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 don't have to install `eslint`.
  74. ### `extensions`
  75. - Type:
  76. ```ts
  77. type extensions = string | Array<string>;
  78. ```
  79. - Default: `'js'`
  80. Specify extensions that should be checked.
  81. ### `exclude`
  82. - Type:
  83. ```ts
  84. type exclude = string | Array<string>;
  85. ```
  86. - Default: `'node_modules'`
  87. Specify the files and/or directories to exclude. Must be relative to `options.context`.
  88. ### `resourceQueryExclude`
  89. - Type:
  90. ```ts
  91. type resourceQueryExclude = RegExp | Array<RegExp>;
  92. ```
  93. - Default: `[]`
  94. Specify the resource query to exclude.
  95. ### `files`
  96. - Type:
  97. ```ts
  98. type files = string | Array<string>;
  99. ```
  100. - Default: `null`
  101. Specify directories, files, or globs. Must be relative to `options.context`.
  102. Directories are traversed recursively looking for files matching `options.extensions`.
  103. File and glob patterns ignore `options.extensions`.
  104. ### `fix`
  105. - Type:
  106. ```ts
  107. type fix = boolean;
  108. ```
  109. - Default: `false`
  110. Will enable [ESLint autofix feature](https://eslint.org/docs/developer-guide/nodejs-api#-eslintoutputfixesresults).
  111. **Be careful: this option will change source files.**
  112. ### `formatter`
  113. - Type:
  114. ```ts
  115. type formatter = string| (
  116. results: Array<import('eslint').ESLint.LintResult>,
  117. data?: import('eslint').ESLint.LintResultData | undefined
  118. ) => string
  119. ```
  120. - Default: `'stylish'`
  121. 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](https://eslint.org/docs/user-guide/formatters/).
  122. ### `lintDirtyModulesOnly`
  123. - Type:
  124. ```ts
  125. type lintDirtyModulesOnly = boolean;
  126. ```
  127. - Default: `false`
  128. Lint only changed files, skip lint on start.
  129. ### `threads`
  130. - Type:
  131. ```ts
  132. type threads = boolean | number;
  133. ```
  134. - Default: `false`
  135. Will run lint tasks across a thread pool. The pool size is automatic unless you specify a number.
  136. ### Errors and Warning
  137. **By default the plugin will auto adjust error reporting depending on eslint errors/warnings counts.**
  138. You can still force this behavior by using `emitError` **or** `emitWarning` options:
  139. #### `emitError`
  140. - Type:
  141. ```ts
  142. type emitError = boolean;
  143. ```
  144. - Default: `true`
  145. The errors found will always be emitted, to disable set to `false`.
  146. #### `emitWarning`
  147. - Type:
  148. ```ts
  149. type emitWarning = boolean;
  150. ```
  151. - Default: `true`
  152. The warnings found will always be emitted, to disable set to `false`.
  153. #### `failOnError`
  154. - Type:
  155. ```ts
  156. type failOnError = boolean;
  157. ```
  158. - Default: `true`
  159. Will cause the module build to fail if there are any errors, to disable set to `false`.
  160. #### `failOnWarning`
  161. - Type:
  162. ```ts
  163. type failOnWarning = boolean;
  164. ```
  165. - Default: `false`
  166. Will cause the module build to fail if there are any warnings, if set to `true`.
  167. #### `quiet`
  168. - Type:
  169. ```ts
  170. type quiet = boolean;
  171. ```
  172. - Default: `false`
  173. Will process and report errors only and ignore warnings, if set to `true`.
  174. #### `outputReport`
  175. - Type:
  176. ```ts
  177. type outputReport =
  178. | boolean
  179. | {
  180. filePath?: string | undefined;
  181. formatter?:
  182. | (
  183. | string
  184. | ((
  185. results: Array<import('eslint').ESLint.LintResult>,
  186. data?: import('eslint').ESLint.LintResultData | undefined
  187. ) => string)
  188. )
  189. | undefined;
  190. };
  191. ```
  192. - Default: `false`
  193. Write the output of the errors to a file, for example a checkstyle xml file for use for reporting on Jenkins CI.
  194. The `filePath` is an absolute path or relative to the webpack config: `output.path`.
  195. You can pass in a different `formatter` for the output file,
  196. if none is passed in the default/configured formatter will be used.
  197. ## Changelog
  198. [Changelog](CHANGELOG.md)
  199. ## License
  200. [MIT](./LICENSE)
  201. [npm]: https://img.shields.io/npm/v/eslint-webpack-plugin.svg
  202. [npm-url]: https://npmjs.com/package/eslint-webpack-plugin
  203. [node]: https://img.shields.io/node/v/eslint-webpack-plugin.svg
  204. [node-url]: https://nodejs.org
  205. [tests]: https://github.com/webpack-contrib/eslint-webpack-plugin/workflows/eslint-webpack-plugin/badge.svg
  206. [tests-url]: https://github.com/webpack-contrib/eslint-webpack-plugin/actions
  207. [cover]: https://codecov.io/gh/webpack-contrib/eslint-webpack-plugin/branch/master/graph/badge.svg
  208. [cover-url]: https://codecov.io/gh/webpack-contrib/eslint-webpack-plugin
  209. [chat]: https://badges.gitter.im/webpack/webpack.svg
  210. [chat-url]: https://gitter.im/webpack/webpack
  211. [size]: https://packagephobia.now.sh/badge?p=eslint-webpack-plugin
  212. [size-url]: https://packagephobia.now.sh/result?p=eslint-webpack-plugin