stylelint在项目细节配置 记录

根据前两篇文章基本适用 .css .less 文件
这篇主要适用检测.jsx 文件中css 样式代码

在项目中实践配置-stylelint: 看这个

安装

npm install stylelint-config-standard stylelint-order --save-dev

配置

文件 stylelint.config.js

module.exports = {
  extends: 'stylelint-config-standard',
  ignoreFiles: ['**/*.js', '**/*.md'],
  plugins: ['stylelint-order'],
  rules: {
    'at-rule-no-unknown': [
      true,
      {
        ignoreAtRules: ['function', 'if', 'each', 'include', 'mixin'],
      },
    ],
    'no-empty-source': null, // 不允许空块
    'block-no-empty': true,
    'rule-empty-line-before': 'always', // 不允许rules前空一行
    'at-rule-empty-line-before': null, // 在 AT规则 前要求或不允许留有空行
    'no-missing-end-of-source-newline': null,
    'selector-list-comma-newline-after': null, // 在选择器列表的逗号后指定一个换行符或禁止留有空格
    'font-family-no-missing-generic-family-keyword': null,
    indentation: 2,

    // order:指定声明块中内容的顺序。
    // properties-order:指定声明块内属性的顺序。
    // properties-alphabetical-order:指定声明块内属性的字母顺序。
    'order/order': [
      'declarations', // 声明
      'custom-properties', // 自定义属性
      'dollar-variables', // 变量
      'rules', // 规则
      'at-rules', // 规则
    ],
    // 根据 Andy Ford 的 "Order of the Day: CSS Properties"
    // 并且可以将 CSS 属性进行分组
    'order/properties-order': [
      // 指定声明块内属性的顺序
      {
        groundName: 'Display & Flow',
        emptyLineBefore: 'never',
        properties: ['display', 'visibility', 'float', 'clear'],
      },
      {
        groundName: 'Positioning',
        emptyLineBefore: 'never',
        properties: [
          'position',
          'top',
          'right',
          'bottom',
          'left',
          'z-index',
          'transform',
        ],
      },
      {
        groupName: 'Flex',
        emptyLineBefore: 'never',
        properties: [
          'flex',
          'flex-direction',
          'flex-grow',
          'flex-shrink',
          'flex-basis',
          'flex-wrap',
          'justify-content',
          'align-items',
        ],
      },
      {
        groupName: 'Dimensions',
        emptyLineBefore: 'never',
        properties: [
          'width',
          'min-width',
          'max-width',
          'height',
          'min-height',
          'max-height',
          'overflow',
        ],
      },
      {
        groupName: 'Margins, Padding, Borders, Outline',
        emptyLineBefore: 'never',
        properties: [
          'margin',
          'margin-top',
          'margin-right',
          'margin-bottom',
          'margin-left',
          'padding',
          'padding-top',
          'padding-right',
          'padding-bottom',
          'padding-left',
          'border-radius',
          'border',
          'border-top',
          'border-right',
          'border-bottom',
          'border-left',
          'border-width',
          'border-top-width',
          'border-right-width',
          'border-bottom-width',
          'border-left-width',
          'border-style',
          'border-top-style',
          'border-right-style',
          'border-bottom-style',
          'border-left-style',
          'border-color',
          'border-top-color',
          'border-right-color',
          'border-bottom-color',
          'border-left-color',
          'outline',
          'list-style',
          'table-layout',
          'border-collapse',
          'border-spacing',
          'empty-cells',
        ],
      },
      {
        groundName: 'Typographic Styles',
        emptyLineBefore: 'never',
        properties: [
          'font',
          'font-family',
          'font-size',
          'line-height',
          'font-weight',
          'text-align',
          'text-indent',
          'text-transform',
          'text-decoration',
          'letter-spacing',
          'word-spacing',
          'white-space',
          'vertical-align',
          'color',
        ],
      },
      {
        groupName: 'Backgrounds',
        emptyLineBefore: 'never',
        properties: [
          'background',
          'background-color',
          'background-image',
          'background-repeat',
          'background-position',
        ],
      },
      {
        groundName: 'Opacity, Cursors, Generated Content, Transition',
        emptyLineBefore: 'never',
        properties: ['opacity', 'cursor', 'content', 'quotes', 'transition'],
      },
    ],
  },
};


完成以上配置 基本可以规范样式编写
特殊情况:
styled-component
style jsx
行内样式


stylelint-processor-arbitrary-tags:用户指定标签内的Lint。
stylelint-processor-html:HTML <style>标记内的Lint 。
stylelint-processor-markdown:Markdown的围栏代码块中的 Lint 。

处理 style-component 校验

介绍文章

npm install --save-dev  stylelint-processor-styled-components 
  stylelint-config-styled-components 

配置

{
  "processors": ["stylelint-processor-styled-components"],
  "extends": [
    "stylelint-config-recommended",
    "stylelint-config-styled-components"
  ]
}

style jsx 内部校验

stylelint-processor-arbitrary-tags (需要正则检测)
stylelint-processor-html(检测html 内部style)
styled-jsx-plugin-stylelint (转译css-in-js的插件 在babel中配置)
npm install --save styled-jsx-plugin-stylelint

css命名规则

selector-type-case 命名大小写

其他:
- styled-jsx-plugin-stylelint 支持jsx 写法插件
- styled-jsx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
**`stylelint`是一个强大的CSS代码检查工具,它可以帮助开发者确保代码的一致性并遵循最佳实践**。下面将深入探讨如何配置`stylelint`以满足不同的需求: 1. **配置文件格式** - **JSON**: `.stylelintrc.json`是最常见的配置格式之一,它可以包含从简单的规则列表到复杂的配置结构。这种格式适合初学者和需要基本配置的场景。 - **YML**: YAML 文件(如`.stylelintrc.yml`或`stylelint.config.yml`)提供了一种更灵活的配置方式,特别是在处理更复杂或分层的配置结构时。 - **JS/TS**: JavaScript或TypeScript文件(如`.stylelintrc.js`或`stylelint.config.js`)提供了最大的灵活性和复杂度,允许执行代码以动态生成配置。 2. **配置属性** - **rules**: 这是`stylelint`配置中最重要的部分,指定哪些规则应该被应用来检查CSS代码。每个规则都可以被设置为"warning"、"error",或者完全禁用。 - **ignoreFiles**: 在项目中,有些文件可能不需要进行`stylelint`检查,例如编译后的CSS文件或第三方库。`ignoreFiles`选项允许你指定一个正则表达式,以忽略与该模式匹配的文件。 - **extends**: 通过`extends`属性,可以轻松地继承预定义的规则集,如`stylelint-config-standard`。这可以减少配置的复杂性,同时保持代码一致性。 3. **自定义配置** - **custom syntax**: Stylelint 不仅支持 CSS,还支持 SCSS、Sass、Less 和 PostCSS,以及 CSS-in-JS 和 Vue 单文件组件 (SFC) 的语法检查。这为使用不同技术和框架的开发者提供了极大的灵活性。 - **custom rules**: 除了内置的规则外,`stylelint`还允许你创建自定义规则。这对于团队中有特定编码标准的情况非常有用,可以确保所有成员都遵循相同的指导原则。 4. **编辑器集成** - **自动修复**: 大多数现代编辑器,如VSCode,都支持`stylelint`的集成。通过配置编辑器,可以在保存文件时自动修复`stylelint`发现的问题。这不仅提高了代码质量,还节省了手动修复错误的时间。 5. **工作流集成** - **CI/CD**: 在持续集成/持续部署(CI/CD)工作流程中集成`stylelint`可确保代码库中的CSS代码始终保持一致和干净。借助CI工具(如GitHub Actions、Travis CI等),可以自动化`stylelint`检查并阻止不符合规定的代码被合并进主分支。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值