本文主要介绍了前端规范之CSS规范(Stylelint),将会对Stylelint的使用进行介绍,欢迎大家交流讨论~

1. Stylelint介绍及安装

1.1 什么是Stylelint

Stylelint是一个强大的,现代的代码检查工具,与ESLint类似,Stylelint能够通过定义一系列的编码风格规则帮助我们避免在样式表中出现错误。
目前在开源社区上,关于CSS Lint的解决方案主要包括了csslint、SCSS-Lint和Stylelint等几种。而由于Stylelint在技术架构上基于AST 的方式扩展CSS,除原生CSS 语法,其也支持 SCSS、Less 这类预处理器,并且也有非常多的第三方插件,因此我们团队选择了Stylelint作为CSS Lint工具。
官方文档:https://stylelint.io/

1.2 安装Stylelint

可以选采用npm安装Stylelint。其中,stylelint-config-standard是Stylelint的标准配置。如果想使用airbnb或prettier的规范,也可以将stylelint-config-standard改为stylelint-config-airbnb或stylelint-config-prettier。

  1. npm install stylelint stylelint-config-standard --save-dev
  2. yarn add stylelint stylelint-config-standard --save-dev

1.3 安装适配预处理语法的插件

如果我们项目中采用了如sass或less等css预处理器,那么可以安装适配预处理语法的插件。以sass为例,需要安装stylelint-scss插件。

  1. npm install stylelint-scss --save-dev
  2. yarn add stylelint-scss --save-dev

1.4 安装CSS属性排序插件

我们也可以选择安装stylelint-order插件。该插件能够强制我们按照某个顺序编写css,比如先写定位,再写盒模型,再写内容区样式,最后写CSS3相关属性,这样可以更好的保证我们代码的可读性。

  1. npm install stylelint-order --save-dev
  2. yarn add stylelint-order --save-dev

2. Stylelint配置

2.1 Stylelint配置方式

安装好Stylelint之后,就需要对Stylelint进行配置。Stylelint的配置方式包括了以下几种:

  • 在package.json中添加stylelint属性并添加规则
  • 在.stylelintrc文件中指定,.stylelintrc文件支持添加一个文件扩展名来区分 JSON,YAML 或 JS 格式,如创建.stylelintrc.json、.stylelintrc.yaml、.stylelintrc.yml或.stylelintrc.js文件
  • 在stylelint.config.js文件中指定,该文件将会exports一个配置对象

在这里,我们选择了在项目根目录创建.stylelintrc.js来配置Stylelint。
前端规范之CSS规范(Stylelint) - 图1
在.stylelintrc.js文件中,我们可以指定要配置的内容,下面给出了一个配置文件的例子。
其中,该配置文件采用了stylelint-config-standard标准配置,并且添加了stylelint-order插件用于CSS属性排序,在rules中,可以指定声明块内属性的顺序,也可以自定义CSS检查规则。比如定义了color-hex-case为lower,表示CSS文件的颜色值都必须小写,否则会报错。

  1. module.exports = {
  2. plugins: ['stylelint-order'],
  3. extends: ['stylelint-config-standard'],
  4. rules: {
  5. // 指定声明块内属性的字母顺序
  6. 'order/properties-order': [
  7. 'position',
  8. 'top',
  9. 'right',
  10. 'bottom',
  11. 'left',
  12. 'z-index',
  13. 'display',
  14. 'float',
  15. 'width',
  16. 'height',
  17. 'max-width',
  18. 'max-height',
  19. 'min-width',
  20. 'min-height',
  21. 'padding',
  22. 'padding-top',
  23. 'padding-right',
  24. 'padding-bottom',
  25. 'padding-left',
  26. 'margin',
  27. 'margin-top',
  28. 'margin-right',
  29. 'margin-bottom',
  30. 'margin-left',
  31. 'margin-collapse',
  32. 'margin-top-collapse',
  33. 'margin-right-collapse',
  34. 'margin-bottom-collapse',
  35. 'margin-left-collapse',
  36. 'overflow',
  37. 'overflow-x',
  38. 'overflow-y',
  39. 'clip',
  40. 'clear',
  41. 'font',
  42. 'font-family',
  43. 'font-size',
  44. 'font-smoothing',
  45. 'osx-font-smoothing',
  46. 'font-style',
  47. 'font-weight',
  48. 'hyphens',
  49. 'src',
  50. 'line-height',
  51. 'letter-spacing',
  52. 'word-spacing',
  53. 'color',
  54. 'text-align',
  55. 'text-decoration',
  56. 'text-indent',
  57. 'text-overflow',
  58. 'text-rendering',
  59. 'text-size-adjust',
  60. 'text-shadow',
  61. 'text-transform',
  62. 'word-break',
  63. 'word-wrap',
  64. 'white-space',
  65. 'vertical-align',
  66. 'list-style',
  67. 'list-style-type',
  68. 'list-style-position',
  69. 'list-style-image',
  70. 'pointer-events',
  71. 'cursor',
  72. 'background',
  73. 'background-attachment',
  74. 'background-color',
  75. 'background-image',
  76. 'background-position',
  77. 'background-repeat',
  78. 'background-size',
  79. 'border',
  80. 'border-collapse',
  81. 'border-top',
  82. 'border-right',
  83. 'border-bottom',
  84. 'border-left',
  85. 'border-color',
  86. 'border-image',
  87. 'border-top-color',
  88. 'border-right-color',
  89. 'border-bottom-color',
  90. 'border-left-color',
  91. 'border-spacing',
  92. 'border-style',
  93. 'border-top-style',
  94. 'border-right-style',
  95. 'border-bottom-style',
  96. 'border-left-style',
  97. 'border-width',
  98. 'border-top-width',
  99. 'border-right-width',
  100. 'border-bottom-width',
  101. 'border-left-width',
  102. 'border-radius',
  103. 'border-top-right-radius',
  104. 'border-bottom-right-radius',
  105. 'border-bottom-left-radius',
  106. 'border-top-left-radius',
  107. 'border-radius-topright',
  108. 'border-radius-bottomright',
  109. 'border-radius-bottomleft',
  110. 'border-radius-topleft',
  111. 'content',
  112. 'quotes',
  113. 'outline',
  114. 'outline-offset',
  115. 'opacity',
  116. 'filter',
  117. 'visibility',
  118. 'size',
  119. 'zoom',
  120. 'transform',
  121. 'box-align',
  122. 'box-flex',
  123. 'box-orient',
  124. 'box-pack',
  125. 'box-shadow',
  126. 'box-sizing',
  127. 'table-layout',
  128. 'animation',
  129. 'animation-delay',
  130. 'animation-duration',
  131. 'animation-iteration-count',
  132. 'animation-name',
  133. 'animation-play-state',
  134. 'animation-timing-function',
  135. 'animation-fill-mode',
  136. 'transition',
  137. 'transition-delay',
  138. 'transition-duration',
  139. 'transition-property',
  140. 'transition-timing-function',
  141. 'background-clip',
  142. 'backface-visibility',
  143. 'resize',
  144. 'appearance',
  145. 'user-select',
  146. 'interpolation-mode',
  147. 'direction',
  148. 'marks',
  149. 'page',
  150. 'set-link-source',
  151. 'unicode-bidi',
  152. 'speak',
  153. ],
  154. // 颜色值要小写
  155. 'color-hex-case': 'lower','number-leading-zero': 'always',
  156. },
  157. };

2.2 Stylelint配置项

在上面的配置文件中,我们主要定义了一个配置对象,接下来将对常用的配置项进行介绍。
(1)plugins
plugins定义了一个数组,该配置项允许我们使用第三方插件,在该数组中,需要包含“定位器”标识出你要使用的插件,一个“定位器”可以是一个 npm 模块名,一个绝对路径,或一个相对于要调用的配置文件的路径。
一旦声明了插件,在rules中需要为插件的规则添加选项,就像其他标准的规则一样。你需要查看插件的文档去了解规则的名称。

  1. {
  2. "plugins": [
  3. stylelint-order",
  4. "../special-rule.js"
  5. ],
  6. "rules": {
  7. "order/properties-order": [],
  8. "plugin/special-rule": "everything"
  9. }
  10. }

(2)extends
extends定义了一个数组,该配置项允许我们extend一个已存在的配置文件(无论是你自己的还是第三方的配置)。当一个配置继承了里一个配置,它将会添加自己的属性并覆盖原有的属性。比如下面的代码,我们就extend了Stylelint的标准配置。

  1. {
  2. "extends": "stylelint-config-standard",
  3. "rules": {
  4. "indentation": "tab",
  5. "number-leading-zero": null
  6. }
  7. }

如果extends中包含多个配置项,那么数组中的每一项都优先于前一项,也就是说第二项会覆盖第一项,第三项会覆盖第一项和第二项,最后一项将覆盖其它所有项。

  1. {
  2. "extends": [
  3. "stylelint-config-standard",
  4. "./myExtendableConfig"
  5. ],
  6. "rules": {
  7. "indentation": "tab"
  8. }
  9. }

(3)rules
rules定义了一个对象,属性名为规则名称,属性值为规则取值,它告诉Stylelint该检查什么,该怎么报错,所有的规则都是默认关闭的。我们可以通过该选项开启相应规则,进行相应的检测。所有规则必须显式的进行配置,因为没有默认值。
规则名称主要由两个部分组成,第一部分描述该规则应用于什么东西,第二部分表示该规则检查了什么。

  1. "number-leading-zero"
  2. //
  3. // the thing what the rule is checking

当规则名称应用于整个样式表时只包含第二个部分:

  1. "no-eol-whitespace"
  2. "indentation"
  3. //
  4. // what the rules are checking

当规则名称不同时,规则取值也不同。我们可以将某个规则设置为null禁用该规则。

  1. {
  2. "rules": {
  3. "at-rule-blacklist": string|[],
  4. "at-rule-empty-line-before": "always"|"never",
  5. "at-rule-name-case": "lower"|"upper",
  6. "block-no-empty": null,
  7. ...
  8. }
  9. }

除了规则本身的取值之外,Stylelint还支持一些自定义配置,允许给规则传递一个数组,数组第一项是规则取值,第二项是自定义配置。

  1. "selector-pseudo-class-no-unknown": [true, {
  2. "ignorePseudoClasses": ["global"]
  3. }]

通过自定义配置,我们可以指定:

  • severity:错误级别,取值为”warning”或”error”,默认情况下,所有规则的错误级别都为”error”,通过defatuleServerity,可以修改错误级别的默认值 ```bash // error-level severity examples { “indentation”: 2 } { “indentation”: [2] }

// warning-level severity examples { “indentation”: [2, { “severity”: “warning” } ] } { “indentation”: [2, { “except”: [“value”], “severity”: “warning” }] }

  1. - message:当一个规则被触发时,如果你想实现一个自定义的消息,可以给规则的传递"message“作为第二选项,如果提供,将替代提供的任何标准的消息。例如,以下规则配置会使用一些自定义的消息:
  2. ```bash
  3. "color-hex-case": [ "lower", {
  4. "message": "Lowercase letters are easier to distinguish from numbers"
  5. } ],
  6. "indentation": [ 2, {
  7. "ignore": ["block"],
  8. "message": "Please use 2 spaces for indentation. Tabs make The Architect grumpy.",
  9. "severity": "warning"
  10. } ]
  11. }

(4)processors

  • Processors是Stylelint的钩子函数,只能用在命令行和Node API,不适用于PostCSS插件。Processors可以使Stylelint检测非样式表文件中的CSS。例如,可以检测HTML内中