stylelint是一个强大、现代的检测工具,它可以帮助我们在样式中强制执行规范,减少错误。
我们使用Eslint和Prettier规范Js代码还不够,在前端项目中style样式也是重要的一部分,各式各样的样式代码格式不仅不容易阅读,也容易导致错误,所以需要使用Stylelint来规范样式代码。
https://github.com/m-alfred/webpack5
https://github.com/m-alfred/webpack5/blob/master/.stylelintrc.json
yarn add --dev stylelint stylelint-config-standard
stylelint支持以下格式配置文件
.stylelintrc.json
{
"extends": [
"stylelint-config-standard"
]
}
默认情况下未打开任何规则,也没有默认值。 您必须明确配置每个规则才能将其打开。
rules是一个规则的键值对对象
{
"rules": {
"color-no-invalid-hex": true
}
}
每个规则配置值可以是以下格式:
通过以下配置
block-no-empty
comment-empty-line-before
max-empty-lines
和unit-allowed-list
{
"rules": {
"block-no-empty": null,
"comment-empty-line-before": [
"always",
{
"ignore": ["stylelint-commands", "after-comment"]
}
],
"max-empty-lines": 2,
"unit-allowed-list": ["em", "rem", "%", "s"]
}
}
默认忽略node_modules
目录
举例,忽略所有js文件:
{
"ignoreFiles": ["**/*.js"]
}
我们可以通过注释忽略代码块或者整个文件的stylelint规则。 https://stylelint.io/user-guide/ignore-code
/* stylelint-disable */
a {}
/* stylelint-enable */
#id { /* stylelint-disable-line */
color: pink !important; /* stylelint-disable-line declaration-no-important */
}
#id {
/* stylelint-disable-next-line declaration-no-important */
color: pink !important;
}
package.json中添加
"lint:style": "stylelint --fix 'src/**/*.less'",
检测多个类型类型文件
"lint:style": "stylelint --fix 'src/**/*.{css,less}'",
使用stylelint-config-rational-order
对style属性进行分组排序
yarn add --dev stylelint-order stylelint-config-rational-order
.stylelintrc.json
{
"extends": [
"stylelint-config-standard",
"stylelint-config-rational-order"
],
"rules": {
"order/order": [
[
{
"type": "at-rule",
"name": "import"
},
"dollar-variables",
"at-variables",
"custom-properties",
"at-rules",
"declarations",
{
"type": "at-rule",
"name": "supports"
},
{
"type": "at-rule",
"name": "media"
},
"rules"
],
{
"severity": "warning"
}
],
"no-invalid-double-slash-comments": null,
"no-descending-specificity": null,
"no-empty-source": null,
"property-no-vendor-prefix": [true, { "ignoreProperties": [
"background-clip",
"box-orient"
] }],
"number-leading-zero": "never",
"number-no-trailing-zeros": true,
"value-list-comma-space-after": "always",
"declaration-colon-space-after": "always",
"value-list-max-empty-lines": 0,
"shorthand-property-no-redundant-values": true,
"declaration-block-no-duplicate-properties": true,
"declaration-block-no-redundant-longhand-properties": true,
"declaration-block-semicolon-newline-after": "always",
"block-closing-brace-newline-after": "always",
"media-feature-colon-space-after": "always",
"media-feature-range-operator-space-after": "always",
"at-rule-name-space-after": "always",
"indentation": 2,
"no-eol-whitespace": true,
"string-no-newline": null
}
}
rules中:
vscode扩展中搜索@id:stylelint.vscode-stylelint
,安装并启用。
"css.validate": false,
"less.validate": false,
"scss.validate": false,
"stylelint.enable": true",
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.fixAll.stylelint": true
}
at-rule由一个@关键字和后续的一个区块组成,如果没有区块,则以分号结束。
@charset "utf-8"
@import "mystyle.css"
;@media
@page
@counter-style
@fontface
@support
可选值: "custom-properties" | "dollar-variables" | "at-variables" | "declarations" | "rules" | "at-rules" | "less-mixins"
常出现在less的mixins上,mixins会被格式化自动放置到其他属性声明后面,导致其他属性被mixins中的属性覆盖
https://stylelint.io/user-guide/rules/list