✏️
ougege
  • README
  • Docs
    • index
    • Articles
      • AI
        • 体验Chrome AI
        • 体验Cloudflare Workers AI
        • 体验deepseek
      • CSS
        • CSS优化-PurgeCSS
        • 实用效果
        • 开发常用样式
      • Deepin
        • deepin20安装mysql
        • deepin使用tensorflow入门机器学习
        • deepin安装cuda和cuDNN
        • deepin安装lamp
        • deepin安装nvidia驱动
        • deepin安装oh my Zsh
        • deepin安装p7zip
        • deepin换源
        • 安装deepin系统后要做的事
      • Docker
        • CI/CD搭建配置
        • deepin搭建docker环境
        • docker安装和使用gitlab
        • docker搭建nginx+php环境
      • Essay
        • IOS申请邓白氏编码
        • Markdown-Mermaid
        • Markdown Use
        • webview白屏的问题查找和修复
        • 前端开发对接问题和解决办法汇总
        • 国务院机构改革方案
        • 国家级智库
        • 实用网站推荐
        • 常用Markdown数学公式语法
        • 强烈推荐前端要安装的vscode扩展
        • 新建销售计划-页面卡死问题分析
        • 海淘入坑手册
        • 竞品研究
        • 足球知识速成
      • Git
        • GitBook安装和常用命令
        • GitKraken免费版本
        • Git安装和配置
        • Git异常处理
        • Git Worktree使用
        • 前端工程化相关的实用git命令
      • JS
        • ESM模块导出方式对比
        • Emoji多端统一处理
        • JS发布订阅模式
        • JS性能优化
        • JS标准内置对象
        • JS链式调用原理
        • Promise介绍和使用
        • Range的使用
        • Vue+Oauth登录实现
        • Vue实现富文本插入Emoji
        • chrome扩展入门
        • es5新特性
        • es6常用特性
        • es常用片段
        • uniapp使用eslint校验代码
        • 与移动端通信
        • 优秀js库moment
        • 使用vue-socketio
        • 实现一个中间件
        • 小程序webview调试
        • 常用snippets
        • 常用正则
        • 常用的设计模式
        • 微信jssdk封装使用
        • 浏览器宏任务和微任务
        • 浏览器的5种Observer
        • 深入理解赋值、浅拷贝、深拷贝
        • 解析vue指令clickoutside源码
        • 键盘事件与KeyBoardWrapper交互
        • 高德地图常用方法封装
        • 高阶函数片段
      • Network
        • 使用Lighthouse分析前端性能
        • 前后端启用https
        • 宝塔nginx安装ngx_brotli
        • 比较gz与br加载速度
        • 浏览器https提示不安全
        • 浏览器提示HSTS
        • 简单使用tcpdump
        • 静态资源gzip优化
      • Node
        • CommonJS模块导出方式对比
        • Taro command not found 多平台解决方案
        • koa使用和API实现
        • node安装报错Unexpected-token
        • 使用nvm和nrm
        • 使用uniapp给小程序添加云函数
        • 使用verdaccio搭建本地npm仓库
        • 使用vue-cli搭建vue项目
        • 安装Node.js和npm配置
        • 编译成cjs和mjs的思路解析
        • 让你的npmPackage同时支持cjs和mjs
        • 通过GithubAction将内容部署到vps
      • Python
        • Python源管理
        • Python版本管理
        • mitmproxy抓包
        • 微信公众平台开发爬坑经历
      • Shell
        • Ubuntu安装deepin桌面环境
        • Ubuntu安装flatpak软件
        • Ubuntu安装wireshark
        • Ubuntu常见问题汇总
        • dell G3装系统无法识别第二块硬盘
        • linux下virtualbox用gho还原系统
        • mysql常用命令
        • navicat连接一键集成环境的mysql
        • nginx常用命令
        • pm2常用命令
        • virtualbox虚拟机和宿主机互相复制粘贴
        • vps内资源通过mega快传到本地
        • vps报错temporary failure in name resolution
        • vscode修改文件监控数
        • windows+linux双系统引导修复
        • zsh常用插件和命令
        • 一键搭建ChatGPT web版
        • 使用V2ray,CloudFlare Warp解锁GPT
        • 使用vscode进行java开发
        • 利用zx和SSHKey发布代码到服务器
        • 反爬虫一些方案总结和尝试
        • 安装1Panel
        • 安装Bt面板
        • 安装Ubuntu22.04后要做的事
        • 无显示器linux设置默认分辨率
        • 特别实用的shell命令
        • 解决linux安装xmind缺少依赖报错
      • Standards
        • CSS格式化之stylelint
        • CSS规范
        • HTML规范
        • JS规范
        • commit规范
        • 使用husky+commitlint规范代码提交
        • 使用semantic-release自动管理版本号
        • 命名规范
        • 图片规范
        • 版本编号规范
      • Wall
        • 科学上网-Cloudflare-Pages
        • 科学上网-Cloudflare-Warp
        • 科学上网-Geph
        • 科学上网-RackNerd
        • 科学上网-Slicehosting
        • 科学上网-Surfshark
        • 科学上网-Tor
        • 科学上网-XX-NET
        • 科学上网-heroku
        • 科学上网-shadowsock
        • 科学上网-v2ray使用
        • 科学上网-v2ray搭建
        • 科学上网-浏览器代理
        • 科学上网-让终端走代理
      • Windows
        • SourceTree破解免登录(windows版)
        • git bash交互提示符不工作
        • nexus 7 2013 wifi 刷机
        • tree命令生成文件目录
        • 利用charles抓包app
        • 安装Openssl
        • 安装msi文件报错2503和2502
        • 神器vimium使用说明
        • 自用host
        • 解决win10扩展出来的屏幕模糊
        • 解决安装Adobe Air时发生错误
    • Snippets
      • zsh
        • docker
        • extract
        • git-commit
        • git
        • mysql-macports
        • npm
        • nvm
        • pip
        • pm2
        • systemd
        • ubuntu
        • vscode
Powered by GitBook
On this page
  • 介绍
  • 安装
  • 使用
  • 实现ide保存自动格式化
  • 参考

Was this helpful?

  1. Docs
  2. Articles
  3. Standards

CSS格式化之stylelint

介绍

在团队协作中,我们需要统一团队的代码风格,所以需要一个代码格式化工具。这里我们介绍使用 stylelint 来格式化 css 代码。

安装

分别配置 package.json 和 .stylelintrc.cjs ::: code-group

"devDependencies": {
  "postcss": "^8.4.12",
  "postcss-html": "^1.3.0",
  "stylelint": "^14.6.0",
  "stylelint-config-html": "^1.0.0",
  "stylelint-config-prettier": "^9.0.3",
  "stylelint-config-recommended": "^7.0.0",
  "stylelint-config-recommended-less": "^1.0.4",
  "stylelint-config-recommended-scss": "^7.0.0",
  "stylelint-config-recommended-vue": "^1.4.0",
  "stylelint-config-standard": "^25.0.0",
  "stylelint-config-standard-scss": "^4.0.0",
  "stylelint-less": "^1.0.5",
  "stylelint-order": "^5.0.0"
}
"scripts": {
  // 正常模式
  "lint:stylelint": "stylelint \"src/**/*.{vue,css,scss}\" --fix",
  // 以下为忽略特定目录检查
  "lint:stylelint": "stylelint \"src/**/*.{vue,css,scss}\" --ignore-pattern 'src/uni_modules/**/*' --fix"
}
module.exports = {
  // 扩展标准样式lint配置
  extends: [
    'stylelint-config-standard', // 标准配置
    'stylelint-config-prettier', // Prettier配置
    'stylelint-config-html/vue', // HTML和Vue配置
    'stylelint-config-recommended-vue/scss', // 推荐的Vue和SCSS配置
    'stylelint-config-recommended-less', // 推荐的Less配置
    'stylelint-config-recommended-scss' // 推荐的SCSS配置
  ],
  // 插件
  plugins: ['stylelint-order'], // 插件:样式顺序
  // 覆盖配置
  overrides: [
    {
      'files': ['**/*.vue'], // 匹配.vue文件
      'customSyntax': 'postcss-html' // 使用postcss-html自定义语法
    }
  ],
  // 忽略文件
  ignoreFiles: ['**/*.js', '**/*.jsx', '**/*.tsx', '**/*.ts', '**/*.json'], // 忽略指定文件类型
  // 规则
  rules: {
    indentation: 2, // 缩进为2
    'selector-pseudo-element-no-unknown': [ // 选择器伪元素未知
      true,
      {
        ignorePseudoElements: ['v-deep'] // 忽略v-deep伪元素
      }
    ],
    'number-leading-zero': 'never', // 数字前导零永远不需要
    'no-descending-specificity': null, // 禁止特异性降低
    'font-family-no-missing-generic-family-keyword': null, // 字体组中不缺少通用字体关键字
    'selector-type-no-unknown': null, // 选择器类型未知
    'at-rule-no-unknown': null, // @规则未知
    'no-duplicate-selectors': null, // 禁止重复选择器
    'no-empty-source': null, // 禁止空源
    // 禁止空块
    'block-no-empty': true,
    'selector-pseudo-class-no-unknown': [ // 选择器伪类未知
      true,
      { ignorePseudoClasses: ['global'] } // 忽略全局伪类
    ],
    'max-nesting-depth': null, // 最大嵌套深度
    'max-line-length': null, // 最大行长度
    'selector-max-compound-selectors': null, // 选择器最大复合选择器
    'selector-no-qualifying-type': null, // 选择器无资格类型
    'selector-class-pattern': null, // 选择器类模式
    'function-parentheses-newline-inside': null, // 函数括号内换行
    'alpha-value-notation': 'number', // 透明值表示为数字
    // 禁止空第一行
    'no-empty-first-line': true,
    // 限制单行声明块中声明的最大数量:取消限制,防止normalize这样第三方压缩样式报错
    'declaration-block-single-line-max-declarations': null,
    // 禁止字体系列中出现重复名称。(同上)
    'font-family-no-duplicate-names': null,
    // 防止rpx报错
    'unit-no-unknown': [
      true,
      {
        ignoreUnits: ['rpx'], // 忽略rpx单位
      },
    ],
    // 兼容自定义属性(防止蓝湖,月维生成样式报错)
    'custom-property-pattern': null,
    // 属性顺序
    'order/properties-order': [
      'position',
      'top',
      'right',
      'bottom',
      'left',
      'z-index',
      'display',
      'flex-wrap',
      'justify-content',
      'align-items',
      'float',
      'clear',
      'overflow',
      'overflow-x',
      'overflow-y',
      'padding',
      'padding-top',
      'padding-right',
      'padding-bottom',
      'padding-left',
      'margin',
      'margin-top',
      'margin-right',
      'margin-bottom',
      'margin-left',
      'width',
      'min-width',
      'max-width',
      'height',
      'min-height',
      'max-height',
      'font-size',
      'font-family',
      'font-weight',
      'text-justify',
      'text-align',
      'text-indent',
      'text-overflow',
      'text-decoration',
      'white-space',
      'color',
      'background',
      'background-position',
      'background-repeat',
      'background-size',
      'background-color',
      'background-clip',
      'border',
      'border-style',
      'border-width',
      'border-color',
      'border-top-style',
      'border-top-width',
      'border-top-color',
      'border-right-style',
      'border-right-width',
      'border-right-color',
      'border-bottom-style',
      'border-bottom-width',
      'border-bottom-color',
      'border-left-style',
      'border-left-width',
      'border-left-color',
      'border-radius',
      'opacity',
      'filter',
      'list-style',
      'outline',
      'visibility',
      'box-shadow',
      'text-shadow',
      'resize',
      'transition'
    ]
  }
}

:::

使用

使用 npm run lint:stylelint,会自动修复大多数错误,剩下的错误需要按照终端提示修复。当终端无任何提示则表示全局已经没有样式错误。

::: warning 注意 关闭所有规则

/* stylelint-disable */
a {}
/* stylelint-enable */

关闭个别规则

/* stylelint-disable selector-max-id, declaration-no-important */
#id {
  color: pink !important;
}
/* stylelint-enable selector-max-id, declaration-no-important */

注释关闭各个行的规则

#id { /* stylelint-disable-line */
  color: pink !important; /* stylelint-disable-line declaration-no-important */
}

注释关闭下一行的规则

#id {
  /* stylelint-disable-next-line declaration-no-important */
  color: pink !important;
}

:::

实现ide保存自动格式化

  1. vscode 下载 stylelint 插件

  2. 在项目下新建 .vscode 文件夹,新建 settings.json 文件, 添加如下配置

    "editor.codeActionsOnSave": {
      "source.fixAll.stylelint": "explicit", // 开启stylelint自动修复
    },
    "stylelint.validate": [ // 需要格式化的文件类型
      "css",
      "less",
      "postcss",
      "scss",
      "sass",
      "vue"
    ],
    "stylelint.enable": true // 开启stylelint
  3. 然后试着调整样式顺序,然后 ctrl + s 保存,会自动修复

参考

PreviousStandardsNextCSS规范

Last updated 10 months ago

Was this helpful?

Stylelint Wiki
2022 Stylelint 配置详细步骤(css、less、sass、vue适用)
vue2项目配置prettier + eslint + commitlint + stylelint