此版本
stylelint-webpack-plugin
仅支持 webpack 5。对于 webpack 4,请参阅 2.x 分支。
此插件使用 stylelint
,它可以帮助您避免样式中的错误并强制执行规范。
首先,您需要安装 stylelint-webpack-plugin
npm install stylelint-webpack-plugin --save-dev
或
yarn add -D stylelint-webpack-plugin
或
pnpm add -D stylelint-webpack-plugin
[!注意]
如果您尚未安装,还需要从 npm 安装
stylelint >= 13
npm install stylelint --save-dev
或
yarn add -D stylelint
或
pnpm add -D stylelint
[!注意]
如果您使用的是 Stylelint 13 而非 14+,并且遇到与 Stylelint 相关的类型错误,您可能还需要安装
@types/stylelint
作为开发依赖。
然后将插件添加到您的 webpack 配置中。例如
const StylelintPlugin = require('stylelint-webpack-plugin');
module.exports = {
// ...
plugins: [new StylelintPlugin(options)],
// ...
};
请参阅 stylelint 选项 以获取所有可用选项的完整列表。这些选项会直接传递给 stylelint
。
cache
type cache = boolean;
true
缓存默认启用以减少执行时间。
cacheLocation
type cacheLocation = string;
node_modules/.cache/stylelint-webpack-plugin/.stylelintcache
指定缓存位置的路径。这可以是一个文件或一个目录。
configFile
type context = string;
undefined
指定 stylelint
使用的配置文件位置。
注意
默认情况下,这由
stylelint
处理。
context
type context = string;
compiler.context
一个字符串,指示您文件的根目录。
exclude
type exclude = string | Array<string>;
['node_modules', compiler.options.output.path]
指定要排除的文件和/或目录。必须是相对于 options.context
的路径。
extensions
type extensions = string | Array<string>;
['css', 'scss', 'sass']
指定应检查的扩展名。
files
type files = string | Array<string>;
null
指定目录、文件或 glob 模式。必须是相对于 options.context
的路径。目录将被递归遍历,查找与 options.extensions
匹配的文件。文件和 glob 模式会忽略 options.extensions
。
fix
type fix = boolean;
false
如果为 true
,stylelint
将尽可能修复错误。修复会直接作用于实际的源文件。所有未修复的错误都将被报告。请参阅 自动修复错误 文档。
formatter
type formatter = string | (
results: Array<import('stylelint').LintResult>
) => string
'string'
指定您希望用于格式化结果的格式化器。请参阅 格式化器选项。
lintDirtyModulesOnly
type lintDirtyModulesOnly = boolean;
false
仅检查已更改的文件;启动时跳过检查。
stylelintPath
type stylelintPath = string;
stylelint
用于检查的 stylelint
实例路径。
threads
type threads = boolean | number;
false
设置为 true
可根据 CPU 数量自动选择线程池大小。设置为大于 1 的数字可设置明确的线程池大小。
设置为 false
、1 或更小的值可禁用并行并仅在主进程中运行。
默认情况下,插件将根据 Stylelint 错误/警告的数量自动调整错误报告。
您仍然可以通过使用 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('stylelint').LintResult>) => string)
)
| undefined;
};
false
将错误输出写入文件 - 例如,用于报告的 json
文件。
filePath
相对于 webpack 配置中的 output.path
。
您可以为输出文件传入不同的格式化器。如果没有传入,将使用默认/已配置的格式化器。
{
filePath: 'path/to/file';
formatter: 'json';
}
我们欢迎所有贡献!如果您是新用户,请在提交问题或拉取请求之前花一些时间阅读我们的贡献指南。