Wang's blog Wang's blog
首页
  • 前端文章

    • HTML教程
    • CSS
    • JavaScript
  • 前端框架

    • Vue
    • React
    • VuePress
    • Electron
  • 后端技术

    • Npm
    • Node
    • TypeScript
  • 编程规范

    • 规范
  • 我的笔记
  • Git
  • GitHub
  • VSCode
  • Mac工具
  • 数据库
  • Google
  • 服务器
  • Python爬虫
  • 前端教程
更多
收藏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Wang Mings

跟随大神,成为大神!
首页
  • 前端文章

    • HTML教程
    • CSS
    • JavaScript
  • 前端框架

    • Vue
    • React
    • VuePress
    • Electron
  • 后端技术

    • Npm
    • Node
    • TypeScript
  • 编程规范

    • 规范
  • 我的笔记
  • Git
  • GitHub
  • VSCode
  • Mac工具
  • 数据库
  • Google
  • 服务器
  • Python爬虫
  • 前端教程
更多
收藏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • CSS

  • Npm

  • Vue

  • HTML

  • Node

  • Yaml

  • React

  • 框架

  • 规范

    • 模板
    • JS规范
    • CSS规范
    • html规范
    • ESLint规范
    • readme文件
    • 命名规范
    • Git操作规范
    • 测试与发布
    • stylelint配置使用
      • 自定义css属性顺序规则
      • 忽略stylelint对css的检验
    • Typescript错误忽略
  • Electron

  • JS演示

  • VuePress

  • JavaScript

  • TypeScript

  • 微信小程序

  • TypeScript-axios

  • 前端
  • 规范
wangmings
2022-07-19
目录

stylelint配置使用

# stylelint 配置使用

# 安装 stylelint 和 Standard 规范

npm install --save-dev stylelint stylelint-config-standard stylelint-config-standard-scss postcss postcss-html postcss-sass stylelint-config-html
1

# 然后再根目录创建 .stylelintrc.js 文件, 并写下以下内容

{
 "extends": [
    "stylelint-config-standard",
    "stylelint-config-standard-scss",
    "stylelint-config-html/html",
    "stylelint-config-html/vue",
  ],
}
1
2
3
4
5
6
7
8

# 在 package.json 文件的 scripts 加上命令, 规则检查自动修复css

"style": "stylelint \"src/**/*.(vue|scss|css)\" --fix",
1

# vscode配置

  1. 安裝 styleLint插件

  2. 在 settings.json 文件设置, win + , 快捷键可以快速打开

{
  "editor.codeActionsOnSave": {
    "source.fixAll.stylelint": true
  },
  "stylelint.snippet": [
        "css",
        "less",
        "postcss",
        "vue",
        "sass"
    ],
    "stylelint.validate": [
        "css",
        "less",
        "postcss",
        "vue",
        "sass"
    ],
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  1. 设置完之后,在vscode就可以有提示了,也保存自动修复了

# 自定义css属性顺序规则

除了使用设定默认的 standard 规则外,我们还可以在 .stylelintrc.js 内添加自己喜欢的规则

  1. 先安装 stylelint-order
npm install stylelint-order --save-dev
1
  1. 在 .stylelintrc.js 设置代码如下
module.exports = {
    "plugins": [
        "stylelint-order"
    ],
    "rules": {
        "order/properties-order": [
            "width",
            "height"
        ]
    }
}
1
2
3
4
5
6
7
8
9
10
11

先引入 plugins 插件, 在 order/properties-order 里面写css属性的先后顺序,

  1. 完整的 .stylelintrc.js 如下
module.exports = {
  "extends": [
    "stylelint-config-standard",
    "stylelint-config-standard-scss",
    "stylelint-config-html/html",
    "stylelint-config-html/vue",
  ],
  "plugins": [
    "stylelint-order"
  ],
  "rules": {
    // scss 变量名忽略警告
    "scss/dollar-variable-pattern": [/./, {"ignore": "global"}],
    // mixin变量名支持全部字符
    "scss/at-mixin-pattern": /.+/,
    // 类选择器的命名规则
    "selector-class-pattern": ".",
    // 指定字符串使用单引号或双引号 "single"|"double"
    "string-quotes": "single",
    // 颜色指定大写
    "color-hex-case": "upper",
    // 禁止空块
    'block-no-empty': true,
    // 颜色6位长度
    "color-hex-length": "long",
    // 兼容自定义标签名
    "selector-type-no-unknown": [true, {
      "ignoreTypes": []
    }],
    // 忽略伪类选择器 ::v-deep
    "selector-pseudo-element-no-unknown": [true, {
      "ignorePseudoElements": ["/./","v-deep", '-webkit-']
    }],
    // 禁止低优先级的选择器出现在高优先级的选择器之后。
    "no-descending-specificity": null,
    // 不验证@未知的名字,为了兼容scss的函数
    "at-rule-no-unknown": null,
    // 禁止空注释
    "comment-no-empty": true,
    // 禁止简写属性的冗余值
    "shorthand-property-no-redundant-values": true,
    "selector-pseudo-class-no-unknown": [true, {
      "ignorePseudoClasses": ['/./', '-webkit-']
    }],
    // 禁止值的浏览器引擎前缀
    "value-no-vendor-prefix": [true,
    {
      "ignoreValues": "box"
    }],
    // 禁止属性的浏览器引擎前缀
    "property-no-vendor-prefix": [
      true,
      {
        "ignoreProperties": [ /./]
      }
    ],
    // 禁止小于 1 的小数有一个前导零
    "number-leading-zero": "never",
    // 禁止空第一行
    "no-empty-first-line": true,
    // 属性的排序
    "order/properties-order": [
      "position",
      "top",
      "right",
      "bottom",
      "left",
      "z-index",
      "display",
      "justify-content",
      "align-items",
      "float",
      "clear",
      "overflow",
      "overflow-x",
      "overflow-y",
      "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"
    ],
  }
}
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

# 忽略stylelint对css的检验

  1. 忽略整个文件,在首行加入 /* stylelint-disable */
/* stylelint-disable */
html {}
1
2
  1. 忽略多行
/* stylelint-disable */
html {}
.div {
    color: red;
}
/* stylelint-enable */
1
2
3
4
5
6
  1. 忽略一行, 在样式前加入 /* stylelint-disable-next-line */ 以忽略该行
#id {
  /* stylelint-disable-next-line */
  color: pink !important;
}
1
2
3
4
  1. 在 .stylelintrc.js 內设定需要忽略的文件
{
  ignoreFiles: ["dist/**/*", "src/assets/scss/abc.scss"]
}
1
2
3

stylelint 官网 (opens new window)

stylelint 中文文档 (opens new window)

stylelint-scss 规则文档 (opens new window)

stylelint-config-standard-scss 的规则 (opens new window)

编辑 (opens new window)
测试与发布
Typescript错误忽略

← 测试与发布 Typescript错误忽略→

最近更新
01
theme-vdoing-blog博客静态编译问题
09-16
02
搜索引擎
07-19
03
友情链接
07-19
更多文章>
Theme by Vdoing | Copyright © 2019-2022 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式