Vue3 -- 项目配置之stylelint【企业级项目配置保姆级教程3】

stylelint为css的lint工具。可格式化css代码,检查css语法错误与不合理的写法,指定css书写顺序等。

配置stylelint:

我们项目使用scss作为预处理器

安装依赖:

javascript 复制代码
pnpm add sass sass-loader stylelint postcss postcss-scss postcss-html stylelint-config-prettier stylelint-config-recess-order stylelint-config-recommended-scss stylelint-config-standard stylelint-config-standard-vue stylelint-scss stylelint-order stylelint-config-standard-scss -D

创建.stylelintrc.cjs文件:

在根目录下创建.stylelintrc.cjs文件并配置:

javascript 复制代码
// @see: https://stylelint.io

module.exports = {
    root: true,
    // 继承某些已有的规则
    extends: [
        "stylelint-config-standard", // 配置 stylelint 拓展插件
        "stylelint-config-html/vue", // 配置 vue 中 template 样式格式化
        "stylelint-config-standard-scss", // 配置 stylelint scss 插件
        "stylelint-config-recommended-vue/scss", // 配置 vue 中 scss 样式格式化
        "stylelint-config-recess-order" // 配置 stylelint css 属性书写顺序插件,
    ],
    overrides: [
        // 扫描 .vue/html 文件中的 <style> 标签内的样式
        {
            files: ["**/*.{vue,html}"],
            customSyntax: "postcss-html"
        }
    ],
    rules: {
        "function-url-quotes": "always", // URL 的引号 "always(必须加上引号)"|"never(没有引号)"
        "color-hex-length": "long", // 指定 16 进制颜色的简写或扩写 "short(16进制简写)"|"long(16进制扩写)"
        "rule-empty-line-before": "never", // 要求或禁止在规则之前的空行 "always(规则之前必须始终有一个空行)"|"never(规则前绝不能有空行)"|"always-multi-line(多行规则之前必须始终有一个空行)"|"never-multi-line(多行规则之前绝不能有空行)"
        "font-family-no-missing-generic-family-keyword": null, // 禁止在字体族名称列表中缺少通用字体族关键字
        "scss/at-import-partial-extension": null, // 解决不能使用 @import 引入 scss 文件
        "property-no-unknown": null, // 禁止未知的属性
        "no-empty-source": null, // 禁止空源码
        "selector-class-pattern": null, // 强制选择器类名的格式
        "value-no-vendor-prefix": null, // 关闭 vendor-prefix (为了解决多行省略 -webkit-box)
        "no-descending-specificity": null, // 不允许较低特异性的选择器出现在覆盖较高特异性的选择器
        "value-keyword-case": null, // 解决在 scss 中使用 v-bind 大写单词报错
        "selector-pseudo-class-no-unknown": [
            true,
            {
                ignorePseudoClasses: ["global", "v-deep", "deep"]
            }
        ]
    },
    ignoreFiles: ["**/*.js", "**/*.jsx", "**/*.tsx", "**/*.ts"]
};

创建.stylelintignore文件:

在根目录下创建.stylelintignore文件:

javascript 复制代码
/node_modules/*
/dist/*
/html/*
/public/*

package.json文件添加运行脚本:

配置统一的prettier来格式化我买的html、css、js代码:

javascript 复制代码
"format": "prettier --write \"./**/*.{html,vue,ts,js,json,md}\"",
"lint:eslint": "eslint src/**/*.{ts,vue} --cache --fix",
"lint:style": "stylelint src/**/*.{css,scss,vue} --cache --fix",

运行脚本命令:

javascript 复制代码
pnpm run format

运行前:乱七八糟的样子

运行后:(你就是我的风景~~)

相关推荐
第九序言T14 小时前
Python 数据处理进阶(十八)批量处理同结构文件:一个文件夹一次跑完
开发语言·python·自动化·pandas·数据处理
学长毕设15 小时前
基于springboot的食品仓库管理系统
java·spring boot·后端
滴滴答答哒15 小时前
ASP.NET Core + FaceRecognitionDotNet 人脸识别实战:DI 单例、特征向量持久化与 1:N 比对
后端·asp.net
龙亘川15 小时前
构建城市公共安全数字底座:亘川智城智慧安防系统全维度解读
java·大数据·开发语言·人工智能·信息可视化
程序喵大人15 小时前
【C++入门】内存模型与资源管理 - 09 shared_ptr 和 weak_ptr 解决共享,也带来代价
开发语言·c++·资源管理·智能指针·内存模型
Raas10016 小时前
AI网关部署方案?从部署到上线全流程
java·开发语言·人工智能·gateway·ai网关·企业级ai网关·企业级ai产品
专注仿真17 小时前
线性回归和逻辑回归算法详解
开发语言·人工智能·python·大模型训练·控制算法
jimy117 小时前
C++ 中的 `T&&`:右值引用与转发引用
开发语言·c++
captain37618 小时前
MyBatis操作数据库(入门2)
java·数据库·spring boot·后端·spring·mybatis
呆呆虫1218 小时前
用 orjson 加速 SerpBase 响应解析:基准对比与注意事项
开发语言·php