本文主要介绍了前端规范之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。
npm install stylelint stylelint-config-standard --save-dev
yarn add stylelint stylelint-config-standard --save-dev
1.3 安装适配预处理语法的插件
如果我们项目中采用了如sass或less等css预处理器,那么可以安装适配预处理语法的插件。以sass为例,需要安装stylelint-scss插件。
npm install stylelint-scss --save-dev
yarn add stylelint-scss --save-dev
1.4 安装CSS属性排序插件
我们也可以选择安装stylelint-order插件。该插件能够强制我们按照某个顺序编写css,比如先写定位,再写盒模型,再写内容区样式,最后写CSS3相关属性,这样可以更好的保证我们代码的可读性。
npm install stylelint-order --save-dev
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。
在.stylelintrc.js文件中,我们可以指定要配置的内容,下面给出了一个配置文件的例子。
其中,该配置文件采用了stylelint-config-standard标准配置,并且添加了stylelint-order插件用于CSS属性排序,在rules中,可以指定声明块内属性的顺序,也可以自定义CSS检查规则。比如定义了color-hex-case为lower,表示CSS文件的颜色值都必须小写,否则会报错。
module.exports = {
plugins: ['stylelint-order'],
extends: ['stylelint-config-standard'],
rules: {
// 指定声明块内属性的字母顺序
'order/properties-order': [
'position',
'top',
'right',
'bottom',
'left',
'z-index',
'display',
'float',
'width',
'height',
'max-width',
'max-height',
'min-width',
'min-height',
'padding',
'padding-top',
'padding-right',
'padding-bottom',
'padding-left',
'margin',
'margin-top',
'margin-right',
'margin-bottom',
'margin-left',
'margin-collapse',
'margin-top-collapse',
'margin-right-collapse',
'margin-bottom-collapse',
'margin-left-collapse',
'overflow',
'overflow-x',
'overflow-y',
'clip',
'clear',
'font',
'font-family',
'font-size',
'font-smoothing',
'osx-font-smoothing',
'font-style',
'font-weight',
'hyphens',
'src',
'line-height',
'letter-spacing',
'word-spacing',
'color',
'text-align',
'text-decoration',
'text-indent',
'text-overflow',
'text-rendering',
'text-size-adjust',
'text-shadow',
'text-transform',
'word-break',
'word-wrap',
'white-space',
'vertical-align',
'list-style',
'list-style-type',
'list-style-position',
'list-style-image',
'pointer-events',
'cursor',
'background',
'background-attachment',
'background-color',
'background-image',
'background-position',
'background-repeat',
'background-size',
'border',
'border-collapse',
'border-top',
'border-right',
'border-bottom',
'border-left',
'border-color',
'border-image',
'border-top-color',
'border-right-color',
'border-bottom-color',
'border-left-color',
'border-spacing',
'border-style',
'border-top-style',
'border-right-style',
'border-bottom-style',
'border-left-style',
'border-width',
'border-top-width',
'border-right-width',
'border-bottom-width',
'border-left-width',
'border-radius',
'border-top-right-radius',
'border-bottom-right-radius',
'border-bottom-left-radius',
'border-top-left-radius',
'border-radius-topright',
'border-radius-bottomright',
'border-radius-bottomleft',
'border-radius-topleft',
'content',
'quotes',
'outline',
'outline-offset',
'opacity',
'filter',
'visibility',
'size',
'zoom',
'transform',
'box-align',
'box-flex',
'box-orient',
'box-pack',
'box-shadow',
'box-sizing',
'table-layout',
'animation',
'animation-delay',
'animation-duration',
'animation-iteration-count',
'animation-name',
'animation-play-state',
'animation-timing-function',
'animation-fill-mode',
'transition',
'transition-delay',
'transition-duration',
'transition-property',
'transition-timing-function',
'background-clip',
'backface-visibility',
'resize',
'appearance',
'user-select',
'interpolation-mode',
'direction',
'marks',
'page',
'set-link-source',
'unicode-bidi',
'speak',
],
// 颜色值要小写
'color-hex-case': 'lower','number-leading-zero': 'always',
},
};
2.2 Stylelint配置项
在上面的配置文件中,我们主要定义了一个配置对象,接下来将对常用的配置项进行介绍。
(1)plugins
plugins定义了一个数组,该配置项允许我们使用第三方插件,在该数组中,需要包含“定位器”标识出你要使用的插件,一个“定位器”可以是一个 npm 模块名,一个绝对路径,或一个相对于要调用的配置文件的路径。
一旦声明了插件,在rules中需要为插件的规则添加选项,就像其他标准的规则一样。你需要查看插件的文档去了解规则的名称。
{
"plugins": [
“stylelint-order",
"../special-rule.js"
],
"rules": {
"order/properties-order": [],
"plugin/special-rule": "everything"
}
}
(2)extends
extends定义了一个数组,该配置项允许我们extend一个已存在的配置文件(无论是你自己的还是第三方的配置)。当一个配置继承了里一个配置,它将会添加自己的属性并覆盖原有的属性。比如下面的代码,我们就extend了Stylelint的标准配置。
{
"extends": "stylelint-config-standard",
"rules": {
"indentation": "tab",
"number-leading-zero": null
}
}
如果extends中包含多个配置项,那么数组中的每一项都优先于前一项,也就是说第二项会覆盖第一项,第三项会覆盖第一项和第二项,最后一项将覆盖其它所有项。
{
"extends": [
"stylelint-config-standard",
"./myExtendableConfig"
],
"rules": {
"indentation": "tab"
}
}
(3)rules
rules定义了一个对象,属性名为规则名称,属性值为规则取值,它告诉Stylelint该检查什么,该怎么报错,所有的规则都是默认关闭的。我们可以通过该选项开启相应规则,进行相应的检测。所有规则必须显式的进行配置,因为没有默认值。
规则名称主要由两个部分组成,第一部分描述该规则应用于什么东西,第二部分表示该规则检查了什么。
"number-leading-zero"
// ↑ ↑
// the thing what the rule is checking
当规则名称应用于整个样式表时只包含第二个部分:
"no-eol-whitespace"
"indentation"
// ↑
// what the rules are checking
当规则名称不同时,规则取值也不同。我们可以将某个规则设置为null禁用该规则。
{
"rules": {
"at-rule-blacklist": string|[],
"at-rule-empty-line-before": "always"|"never",
"at-rule-name-case": "lower"|"upper",
"block-no-empty": null,
...
}
}
除了规则本身的取值之外,Stylelint还支持一些自定义配置,允许给规则传递一个数组,数组第一项是规则取值,第二项是自定义配置。
"selector-pseudo-class-no-unknown": [true, {
"ignorePseudoClasses": ["global"]
}]
通过自定义配置,我们可以指定:
- 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” }] }
- message:当一个规则被触发时,如果你想实现一个自定义的消息,可以给规则的传递"message“作为第二选项,如果提供,将替代提供的任何标准的消息。例如,以下规则配置会使用一些自定义的消息:
```bash
"color-hex-case": [ "lower", {
"message": "Lowercase letters are easier to distinguish from numbers"
} ],
"indentation": [ 2, {
"ignore": ["block"],
"message": "Please use 2 spaces for indentation. Tabs make The Architect grumpy.",
"severity": "warning"
} ]
}
(4)processors
- Processors是Stylelint的钩子函数,只能用在命令行和Node API,不适用于PostCSS插件。Processors可以使Stylelint检测非样式表文件中的CSS。例如,可以检测HTML内中