安装依赖
npm install -D stylelint stylelint-config-standard stylelint-config-rational-order stylelint-prettier stylelint-config-prettier postcss-less postcss-scss postcss-html stylelint-config-recommended-vue
新建配置文件
.stylelintrc.js
module.exports = {
extends: ['stylelint-config-standard', 'stylelint-config-rational-order', 'stylelint-prettier/recommended','stylelint-config-recommended-vue'],
"plugins": [
"stylelint-order"
],
"overrides":[
{
"files": ["**/*.html"],
"customSyntax": "postcss-html",
},
{
"files": ["**/*.scss"],
"customSyntax": "postcss-scss",
},
{
"files": ["**/*.less"],
"customSyntax": "postcss-less",
}
],
"rules": {
"color-hex-case": "lower",
'block-no-empty': true,
"color-hex-length": "long",
"selector-type-no-unknown": [true, {
"ignoreTypes": []
}],
"selector-pseudo-element-no-unknown": [true, {
"ignorePseudoElements": ["v-deep"]
}],
"no-descending-specificity": null,
"at-rule-no-unknown": null,
"comment-no-empty": true,
"shorthand-property-no-redundant-values": true,
"value-no-vendor-prefix": true,
"property-no-vendor-prefix": true,
"number-leading-zero": "always",
"no-empty-first-line": true,
}
}
package.json 加入stylelint相关配置
{
"scripts": {
"stylelint-fix": "stylelint \"src/**/*.(vue|scss|css,less)\" --fix",
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"src/**/*.{less,scss,vue}": [
"stylelint --fix"
]
}
webstorm配置stylelint
忽略stylelint对css的检验
- 忽略整个文件,在首行加入
/* stylelint-disable */
html {}
html {}
.div {
color: red;
}
- 忽略一行, 在样式前加入
/* stylelint-disable-next-line */ 以忽略该行
#id {
color: pink !important;
}
- 在 .stylelintrc.js 內设定需要忽略的文件
{
ignoreFiles: ["dist/**/*", "src/assets/scss/abc.scss"]
}
参考
stylelint-config-recommended-vue
Writing custom syntaxes
stylelint 配置使用,自动修复css,书写顺序
Eslint + Prettier + stylelint + Husky + Lint-staged 规范你的工程git提交信息和代码规范
项目中 Prettier + Stylelint + ESlint 配置
|