此版本
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。
cachetype cache = boolean;
true缓存默认启用以减少执行时间。
cacheLocationtype cacheLocation = string;
node_modules/.cache/stylelint-webpack-plugin/.stylelintcache指定缓存位置的路径。这可以是一个文件或一个目录。
configFiletype context = string;
undefined指定 stylelint 使用的配置文件位置。
注意
默认情况下,这由
stylelint处理。
contexttype context = string;
compiler.context一个字符串,指示您文件的根目录。
excludetype exclude = string | Array<string>;
['node_modules', compiler.options.output.path]指定要排除的文件和/或目录。必须是相对于 options.context 的路径。
extensionstype extensions = string | Array<string>;
['css', 'scss', 'sass']指定应检查的扩展名。
filestype files = string | Array<string>;
null指定目录、文件或 glob 模式。必须是相对于 options.context 的路径。目录将被递归遍历,查找与 options.extensions 匹配的文件。文件和 glob 模式会忽略 options.extensions。
fixtype fix = boolean;
false如果为 true,stylelint 将尽可能修复错误。修复会直接作用于实际的源文件。所有未修复的错误都将被报告。请参阅 自动修复错误 文档。
formattertype formatter = string | (
results: Array<import('stylelint').LintResult>
) => string
'string'指定您希望用于格式化结果的格式化器。请参阅 格式化器选项。
lintDirtyModulesOnlytype lintDirtyModulesOnly = boolean;
false仅检查已更改的文件;启动时跳过检查。
stylelintPathtype stylelintPath = string;
stylelint用于检查的 stylelint 实例路径。
threadstype threads = boolean | number;
false设置为 true 可根据 CPU 数量自动选择线程池大小。设置为大于 1 的数字可设置明确的线程池大小。
设置为 false、1 或更小的值可禁用并行并仅在主进程中运行。
默认情况下,插件将根据 Stylelint 错误/警告的数量自动调整错误报告。
您仍然可以通过使用 emitError 或 emitWarning 选项来强制执行此行为
emitErrortype emitError = boolean;
true找到的错误将始终被发出。要禁用,请设置为 false。
emitWarningtype emitWarning = boolean;
true找到的警告将始终被发出。要禁用,请设置为 false。
failOnErrortype failOnError = boolean;
true如果存在任何错误,将导致模块构建失败。要禁用,请设置为 false。
failOnWarningtype failOnWarning = boolean;
false如果存在任何警告,当设置为 true 时,将导致模块构建失败。
quiettype quiet = boolean;
false当设置为 true 时,将只处理和报告错误,并忽略警告。
outputReporttype 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';
}
我们欢迎所有贡献!如果您是新用户,请在提交问题或拉取请求之前花一些时间阅读我们的贡献指南。