此版本的 eslint-webpack-plugin 仅支持 webpack 5。对于 webpack 4,请参阅 2.x 分支。
此插件使用 ESlint
在 Webpack 构建过程中查找并修复 JavaScript 代码中的问题。
首先,你需要安装 eslint-webpack-plugin
npm install eslint-webpack-plugin --save-dev
或
yarn add -D eslint-webpack-plugin
或
pnpm add -D eslint-webpack-plugin
[!注意]
如果你尚未安装,还需要从 npm 安装
eslint >= 8
npm install eslint --save-dev
或
yarn add -D eslint
或
pnpm add -D eslint
然后将插件添加到您的 webpack 配置中。例如
const ESLintPlugin = require('eslint-webpack-plugin');
module.exports = {
// ...
plugins: [new ESLintPlugin(options)],
// ...
};
你可以传递 ESLint Node.js API 选项。
[!注意]
你提供的配置选项将传递给
ESLint
类。这与你在package.json
或eslint.config.js
中指定的选项(ESLint v9.0.0 后,之前是.eslintrc
)不同。更多详情请参阅 ESLint 文档。
[!警告]
在 eslint-webpack-plugin version 1 中,选项被传递给现在已弃用的 CLIEngine。
cache
type cache = boolean;
true
缓存默认启用以减少执行时间。
cacheLocation
type cacheLocation = string;
node_modules/.cache/eslint-webpack-plugin/.eslintcache
指定缓存位置的路径。可以是文件或目录。
configType
type configType = 'flat' | 'eslintrc';
flat
指定与 ESLint 一起使用的配置类型。
eslintrc
是大多数 ESLint 版本中可用的经典配置格式。flat
是 ESLint 8.21.0 中引入的新格式。新的配置格式在其自己的文档中进行了说明。
context
type context = string;
compiler.context
代码检查的基本目录。
eslintPath
type eslintPath = string;
eslint
用于代码检查的 eslint
实例路径。
如果 eslintPath
是一个像官方 ESlint 一样的文件夹,或者指定了 formatter
选项,那么现在你不需要安装 eslint
。
extensions
type extensions = string | Array<string>;
'js'
指定应检查的文件扩展名。
exclude
type exclude = string | Array<string>;
'node_modules'
指定要排除的文件/目录。必须是相对于 options.context
的路径。
resourceQueryExclude
type resourceQueryExclude = RegExp | Array<RegExp>;
[]
指定要排除的资源查询。
files
type files = string | Array<string>;
null
指定目录、文件或 glob 模式。必须是相对于 options.context
的路径。目录会递归遍历以查找与 options.extensions
匹配的文件。文件和 glob 模式会忽略 options.extensions
。
fix
type fix = boolean;
false
将启用 ESLint 自动修复功能。
请注意:此选项将修改源文件。
formatter
type formatter = string| (
results: Array<import('eslint').ESLint.LintResult>,
data?: import('eslint').ESLint.LintResultData | undefined
) => string
'stylish'
接受一个函数,该函数将 ESLint 消息数组(对象)作为其参数,并且必须返回一个字符串作为输出。
你可以使用官方的 ESLint 格式化器。
lintDirtyModulesOnly
type lintDirtyModulesOnly = boolean;
false
仅检查已更改的文件,跳过构建开始时的初始代码检查。
threads
type threads = boolean | number;
false
将在线程池中运行代码检查任务。除非你指定一个数字,否则线程池大小是自动的。
默认情况下,插件将根据 ESLint 错误/警告的数量自动调整错误报告。
你仍然可以使用 emitError
或 emitWarning
选项强制执行此行为
emitError
type emitError = boolean;
true
发现的错误将始终发出,要禁用请设置为 false
。
emitWarning
type emitWarning = boolean;
true
发现的警告将始终发出,要禁用请设置为 false
。
failOnError
type failOnError = boolean;
true
如果发现任何错误,将导致模块构建失败,要禁用请设置为 false
。
failOnWarning
type failOnWarning = boolean;
false
如果发现任何警告,并且设置为 true
,将导致模块构建失败。
quiet
type quiet = boolean;
false
如果设置为 true
,将只处理并报告错误,并忽略警告。
outputReport
type outputReport =
| boolean
| {
filePath?: string | undefined;
formatter?:
| (
| string
| ((
results: Array<import('eslint').ESLint.LintResult>,
data?: import('eslint').ESLint.LintResultData | undefined,
) => string)
)
| undefined;
};
false
将 ESLint 结果写入文件,例如用于 Jenkins CI 报告的 checkstyle xml 文件。
formatter
。如果没有传入,将使用默认/配置的格式化器。我们欢迎所有贡献!
如果你是新用户,请花一些时间阅读我们的贡献指南。