StyleLint配置指南

基础包

stylelint 有力的,现代的 lint 工具,帮助你在 style 中避免错误, 按照约定转换会规则。

stylelint-config-standard stylelint 配置共享库,可以通过 rules 覆盖规则

stylelint-order 一个为 stylelint 规则排序的插件包

stylelint-config-sass-guidelines 如果你写 SCSS 可以用这个包

stylelint-scss 一个SCSS的规则集合

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
{
"extends": "stylelint-config-sass-guidelines",
"plugins": [
"stylelint-scss",
"stylelint-order"
],
"rules": {
"order/properties-order": [
"position",
"top",
"right",
"bottom",
"left",
"z-index",
"display",
"justify-content",
"align-items",
"float",
"clear",
"overflow",
"overflow-x",
"overflow-y",
"margin",
"margin",
"margin-top",
"margin-right",
"margin-bottom",
"margin-left",
"border",
"border-style",
"border-width",
"border-color",
"border-top",
"border-top-style",
"border-top-width",
"border-top-color",
"border-right",
"border-right-style",
"border-right-width",
"border-right-color",
"border-bottom",
"border-bottom-style",
"border-bottom-width",
"border-bottom-color",
"border-left",
"border-left-style",
"border-left-width",
"border-left-color",
"border-radius",
"padding",
"padding-top",
"padding-right",
"padding-bottom",
"padding-left",
"width",
"min-width",
"max-width",
"height",
"min-height",
"max-height",
"font-size",
"font-family",
"font-weight",
"text-align",
"text-justify",
"text-indent",
"text-overflow",
"text-decoration",
"white-space",
"color",
"background",
"background-position",
"background-repeat",
"background-size",
"background-color",
"background-clip",
"opacity",
"filter",
"list-style",
"outline",
"visibility",
"box-shadow",
"text-shadow",
"resize",
"transition"
]
}
}

忽略配置

  • 忽略整个文件,在首行加入 /* stylelint-disable */
1
2
/* stylelint-disable */
html {}
  • 忽略多行
1
2
3
4
5
6
/* stylelint-disable */
html {}
.div {
color: red;
}
/* stylelint-enable */
  • 忽略一行, 在样式前加入 /* stylelint-disable-next-line */ 以忽略该行
1
2
3
4
#id {
/* stylelint-disable-next-line */
color: pink !important;
}
  • .stylelintrc.json 配置文件

自动格式化

  • 安裝 StyleLint

  • 在 settings.json 文件设置

1
2
3
4
5
{
"editor.codeActionsOnSave": {
"source.fixAll.stylelint": true
}
}

与 Prettier 结合

stylelint-prettier 让 Prettier 作为 StyleLint 的规则,并让 StyleLint 统一报错

stylelint-config-prettier 关闭所有可能冲突的配置

配置文件

1
2
3
4
5
6
7
8
9
{
"extends": [
"stylelint-config-sass-guidelines",
"stylelint-prettier/recommended"
],
"plugins": [
"stylelint-scss"
]
}
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2025 SunZhiqi

此时无声胜有声!

支付宝
微信