webpack中stylelint配置,手动校验样式方案

42 篇文章 0 订阅
4 篇文章 0 订阅

需求描述,配置stylelint去整理css样式,做到格式统一,美观

配置过程

前提条件:脚手架 @vue/cli-service ~5.0.0,vue2

  1. 引入插件 npm install stylelint@14.16.0 stylelint-config-standard@30.0.1 stylelint-order@5.0.0 postcss-html@1.5.0 postcss-less@6.0.0 -D

  2. 创建.stylelintrc.js文件,用于配置stylelint规则

    module.exports = {
    root: true,
    extends: 'stylelint-config-standard',
    // customSyntax: 'postcss-less',
    overrides: [
        {
          files: ["**/*.{html,vue}"],
          customSyntax: "postcss-html"
        },
        {
          files: ["**/*.less"],
          customSyntax: "postcss-less"
        },
    ],
    plugins: ['stylelint-order'],
    rules: {
        // 句尾不强制分号
        'declaration-block-trailing-semicolon': null,
        'media-feature-name-no-vendor-prefix': true,
        // 'at-rule-no-vendor-prefix': true,
        // 'selector-no-vendor-prefix': true,
        // 'property-no-vendor-prefix': false,
        indentation: 4,
        // 'value-no-vendor-prefix': true,
        'no-eol-whitespace': null,
        'alpha-value-notation': 'number',
        'at-rule-no-unknown': [
            true,
            {
                ignoreAtRules: ['mixin', 'include', 'extend']
            }
        ],
        'no-empty-source': null,
        'alpha-value-notation': null,
        'no-duplicate-selectors': null,
        'no-descending-specificity': null,
        'function-linear-gradient-no-nonstandard-direction': null,
        'font-family-no-missing-generic-family-keyword': null,
        'declaration-block-single-line-max-declarations': null,
        'order/properties-order': ['content', 'display', 'position', 'top', 'right', 'bottom', 'left', 'z-index', 'float', 'width', 'height', 'line-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', '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', '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']
    }
    
    
  3. 创建.stylelintignore文件,用于忽略不需要校验的文件

 # .gitignore syntax (uses node-ignore behind the scenes)
/build/
/public/
/config/
/dist/
/node_modules/
  1. 在package.json中配置
"script"{
	"lint:css": "stylelint src/*.{html,vue,css,less} --fix"
}

运行 npx stylelint "**/*.{css,less,vue}" --fix或者npm run lint:css即可校验并修复样式 – fix表示修复

总结

目前的配置只支持在终端输入命令去校验和修复。
stylelint-order 用于样式排序,顺序可以在。stylelintrc.js的order/properties-order数组中配置
postcss-html 用于vue和html文件校验
postcss-less 用于less文件校验

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值