React 項(xiàng)目中引入 Stylelint 做樣式表代碼規(guī)范

在團(tuán)隊(duì)協(xié)作開發(fā)項(xiàng)目的時(shí)候赞厕,我們會必不可少的需要寫一些 css 樣式表耙册。css 雖然不算嚴(yán)格的編程語言荧飞,但它在前端開發(fā)體系中卻占據(jù)著重要地位掌呜。css 樣式表若是寫的混亂沒有規(guī)則滓玖,那么對于團(tuán)隊(duì)其他成員或后來者或維護(hù)者一定是一個(gè)令人頭痛的問題,尤其是具有代碼強(qiáng)迫癥的人群质蕉。因此寫出一手漂亮的势篡,有規(guī)則的 css 樣式表就顯得極為重要。

我們知道在?React 項(xiàng)目中引入 TSLint 做代碼規(guī)范 這篇文章中介紹了如何使用?TSLint 做代碼規(guī)范模暗,但 TSLint 并不能對樣式表起到代碼規(guī)范作用殊霞。那么在團(tuán)隊(duì)協(xié)作時(shí)如何才能寫出統(tǒng)一的規(guī)范化的樣式表呢?我們知道?Stylelint?是一個(gè)強(qiáng)大的汰蓉、現(xiàn)代化的 CSS 檢測工具,它通過定義一系列的編碼風(fēng)格規(guī)則幫助我們避免在樣式表中出現(xiàn)錯(cuò)誤.棒卷。本篇文章所要介紹的就是如何在項(xiàng)目中引入?Stylelint?顾孽。

注意:本篇文章以?Create React App?項(xiàng)目為例,至于如何創(chuàng)建一個(gè)?Create React App?項(xiàng)目請查看?React 項(xiàng)目中引入 TSLint 做代碼規(guī)范?這篇文章中的步驟1比规。

步驟1:通過命令安裝 stylelint若厚、stylelint-order、stylelint-scss 安裝包

a蜒什、安裝 stylelint:npm install stylelint --save-dev?

b测秸、安裝 stylelint-order:npm install stylelint-order --save-dev

c、安裝 stylelint-scss:npm install stylelint-scss?--save-dev?

注意1:a 中通過命令安裝的?stylelint 是使用?Stylelint 做樣式表代碼規(guī)范所需要的一個(gè)安裝包? -----?必須安裝

注意2:b 中通過命令安裝的?stylelint-order 是用來指定樣式排序,比如聲明的塊內(nèi)(插件包)屬性的順序霎冯,例如:先寫定位铃拇,再寫盒模型,再寫內(nèi)容區(qū)樣式沈撞,最后寫 CSS3 相關(guān)屬性?----?推薦安裝

注意3慷荔、c 中通過命令安裝的?stylelint-scss 是用來執(zhí)行各種各樣的 SCSS 語法特性檢測規(guī)則(插件包) ----?如果是寫sass樣式表,必須安裝缠俺,否則不用安裝

注意4显晶、除了 b 和 c 這兩個(gè)插件包,你還可以 點(diǎn)擊此處 選擇其他你需要的插件包

步驟2:在項(xiàng)目根目錄中創(chuàng)建 stylelint.config.js 配置文件

步驟3壹士、根據(jù) 配置 | Configuration 中配置?stylelint.config.js 文件中的配置項(xiàng)

stylelint.config.js 文件中是一個(gè)大的 json 對象磷雇,其中的配置項(xiàng)有:

配置項(xiàng)?rules:規(guī)則決定檢測器要查找什么和要解決什么

配置項(xiàng)?extends:的值是個(gè)“定位器” (或 “定位器” 數(shù)組),也是最終被 require() 的躏救,因此唯笙,可以使用 Node 的 require.resolve() 算法適應(yīng)任何格式

配置項(xiàng)?plugins:插件是由社區(qū)創(chuàng)建的規(guī)則或規(guī)則集,支持方法論落剪、工具集睁本,非標(biāo)準(zhǔn) 的 CSS 特性,或非常特定的用例

配置項(xiàng)?processors:Processors 是 stylelint 的鉤子函數(shù)忠怖,可以以它的方式修改代碼呢堰,也可以在它們退出時(shí)修改結(jié)果

配置項(xiàng)?ignoreFiles:提供一個(gè) glob 或 globs 數(shù)組,忽略特定的文件

步驟4凡泣、根據(jù) Rules 中的各個(gè)配置項(xiàng)進(jìn)行配置?stylelint.config.js 文件中的配置項(xiàng) rules 中的配置項(xiàng)

步驟5枉疼、?步驟4中根據(jù)?Rules?中配置的配置項(xiàng)是最基本的配置項(xiàng),此外還要在配置 scss rules 和 order rules

a鞋拟、因?yàn)樵诓襟E1中安裝了??stylelint-scss 插件骂维,所以要根據(jù) List of rules 配置相應(yīng)的 scss rules

b、因?yàn)樵诓襟E1中安裝了?stylelint-order 插件贺纲,所以要根據(jù) stylelint-order 中的 Rules?配置相應(yīng)的 order rules

步驟6:以 vscode 編輯器為例航闺,需要在 vscode 中安裝 stylelint 插件

步驟7、重啟編輯器修改樣式文件中的聲明的塊內(nèi)屬性的順序猴誊,查看是否會出現(xiàn)錯(cuò)誤警告

備注:我的項(xiàng)目中的 stylelint.config.js 文件 中的配置如下(沒有截圖潦刃,方便拷貝)

module.exports?=?{

??plugins:?[

????'stylelint-scss',

????'stylelint-order'

??],

??rules:?{

????//?Possible?errors

????'color-no-invalid-hex':?true,

????'font-family-no-missing-generic-family-keyword':?true,

????'function-calc-no-unspaced-operator':?true,

????'function-linear-gradient-no-nonstandard-direction':?true,

????'unit-no-unknown':?true,

????'property-no-unknown':?true,

????'declaration-block-no-duplicate-properties':?true,

????'declaration-block-no-shorthand-property-overrides':?true,

????'selector-pseudo-class-no-unknown':?true,

????'selector-pseudo-element-no-unknown':?[

??????true,

??????{

????????ignorePseudoElements:?['ng-deep']

??????}

????],

????'media-feature-name-no-unknown':?true,

????'comment-no-empty':?true,

????'no-duplicate-at-import-rules':?true,

????'no-duplicate-selectors':?true,

????'no-empty-source':?true,

????'no-extra-semicolons':?true,

????'no-invalid-double-slash-comments':?true,

????//?Limit?language?features

????'unit-blacklist':?[],

????'shorthand-property-no-redundant-values':?true,

????'value-no-vendor-prefix':?true,

????'property-blacklist':?[''],

????'property-no-vendor-prefix':?true,

????'declaration-block-no-redundant-longhand-properties':?true,

????'declaration-no-important':?true,

????'declaration-property-unit-blacklist':?{},

????'declaration-property-unit-whitelist':?{

??????'font-size':?['px',?'rem',?'vmin']

????},

????'declaration-property-value-whitelist':?{

??????'float':?['none']

????},

????'selector-max-compound-selectors':?4,

????'selector-max-empty-lines':?0,

????'selector-max-id':?1,

????'selector-no-vendor-prefix':?true,

????'media-feature-name-no-vendor-prefix':?true,

????'at-rule-no-vendor-prefix':?true,

????'at-rule-blacklist':?['extend'],

????//?Stylistic?issues

????'color-hex-case':?'lower',

????'color-hex-length':?'short',

????'font-family-name-quotes':?'always-where-recommended',

????'function-comma-space-after':?'always',

????'function-comma-space-before':?'never',

????'function-name-case':?'lower',

????'function-parentheses-space-inside':?'never',

????'function-url-quotes':?'never',

????'function-whitespace-after':?'always',

????'font-weight-notation':?'numeric',

????'number-leading-zero':?'always',

????'number-no-trailing-zeros':?true,

????'string-quotes':?[

??????'double',

??????{

????????'avoidEscape':?true

??????}

????],

????'length-zero-no-unit':?true,

????'unit-case':?'lower',

????'value-keyword-case':?[

??????'lower',

??????{

????????ignoreProperties:?['family']

??????}

????],

????'value-list-comma-newline-after':?'always-multi-line',

????'value-list-comma-space-after':?'always-single-line',

????'value-list-comma-space-before':?'never',

????'value-list-max-empty-lines':?0,

????'property-case':?'lower',

????'declaration-bang-space-after':?'never',

????'declaration-bang-space-before':?'always',

????'declaration-colon-space-after':?'always-single-line',

????'declaration-colon-space-before':?'never',

????'declaration-empty-line-before':?'never',

????'declaration-block-single-line-max-declarations':?1,

????'declaration-property-value-blacklist':?{

??????'/^border/':?['none']

????},

????'block-closing-brace-empty-line-before':?'never',

????'block-closing-brace-newline-before':?'always',

????'block-opening-brace-newline-after':?'always',

????'block-opening-brace-space-before':?'always',

????'selector-attribute-brackets-space-inside':?'never',

????'selector-attribute-operator-space-after':?'never',

????'selector-attribute-operator-space-before':?'never',

????'selector-attribute-quotes':?'always',

????'selector-combinator-space-after':?'always',

????'selector-combinator-space-before':?'always',

????'selector-descendant-combinator-no-non-space':?true,

????'selector-pseudo-class-case':?'lower',

????'selector-pseudo-class-parentheses-space-inside':?'never',

????'selector-pseudo-element-case':?'lower',

????'selector-pseudo-element-colon-notation':?'single',

????'selector-type-case':?'lower',

????'selector-list-comma-newline-after':?'always-multi-line',

????'selector-list-comma-space-after':?'always-single-line',

????'selector-list-comma-space-before':?'never',

????'rule-empty-line-before':?[

??????'always',

??????{

????????ignore:?['after-comment',?'first-nested']

??????}

????],

????'media-feature-colon-space-after':?'always',

????'media-feature-colon-space-before':?'never',

????'media-feature-name-case':?'lower',

????'media-feature-parentheses-space-inside':?'never',

????'media-feature-range-operator-space-after':?'always',

????'media-feature-range-operator-space-before':?'always',

????'at-rule-empty-line-before':?[

??????'always',

??????{

????????ignore:?['after-comment',?'blockless-after-same-name-blockless'],

????????except:?['first-nested'],

????????ignoreAtRules:?['else']

??????}

????],

????'at-rule-name-space-after':?'always',

????'at-rule-semicolon-space-before':?'never',

????'comment-empty-line-before':?[

??????'always',

??????{

????????except:?['first-nested']

??????}

????],

????'comment-whitespace-inside':?'always',

????indentation:?2,

????'max-empty-lines':?[

??????1,

??????{

????????ignore:?['comments']

??????}

????],

????'no-eol-whitespace':?[

??????true,

??????{

????????ignore:?['empty-lines']

??????}

????],

????//?Sass?rules

????'scss/at-else-closing-brace-newline-after':?'always-last-in-chain',

????'scss/at-else-closing-brace-space-after':?'always-intermediate',

????'scss/at-else-empty-line-before':?'never',

????'scss/at-else-if-parentheses-space-before':?'always',

????'scss/at-function-named-arguments':?'never',

????'scss/at-function-parentheses-space-before':?'always',

????'scss/at-if-closing-brace-newline-after':?'always-last-in-chain',

????'scss/at-if-closing-brace-space-after':?'always-intermediate',

????'scss/at-mixin-argumentless-call-parentheses':?'always',

????'scss/at-mixin-named-arguments':?'never',

????'scss/at-mixin-parentheses-space-before':?'always',

????'scss/at-rule-no-unknown':?true,

????'scss/dollar-variable-colon-newline-after':?'always-multi-line',

????'scss/dollar-variable-colon-space-after':?'always-single-line',

????'scss/dollar-variable-colon-space-before':?'never',

????'scss/dollar-variable-empty-line-before':?[

??????'always',

??????{

????????except:?['after-comment',?'after-dollar-variable',?'first-nested']

??????}

????],

????'scss/dollar-variable-no-missing-interpolation':?true,

????'scss/dollar-variable-pattern':?'^_?[a-z]+[\\w-]*$',

????'scss/at-extend-no-missing-placeholder':?true,

????'scss/at-import-no-partial-leading-underscore':?true,

????'scss/double-slash-comment-empty-line-before':?'always',

????'scss/double-slash-comment-whitespace-inside':?'always',

????'scss/declaration-nested-properties':?'never',

????'scss/operator-no-newline-after':?true,

????'scss/operator-no-newline-before':?true,

????//?'scss/operator-no-unspaced':?true,?//?Causing?url?parsing?error,?temporarily?disabled.

????'scss/selector-no-redundant-nesting-selector':?true,

????'scss/no-duplicate-dollar-variables':?true,

????//?Order?rules

????'order/order':?[

??????'custom-properties',

??????'dollar-variables',

??????'declarations',

??????{

????????type:?'at-rule',

????????hasBlock:?false

??????},

??????'rules',

??????{

????????type:?'at-rule',

????????hasBlock:?true

??????}

????],

????'order/properties-order':?[

??????[

????????'content',

????????'position',?'top',?'right',?'bottom',?'left',

????????'display',?'flex-flow',?'flex-direction',?'flex-wrap',?'justify-content',?'align-content',?'align-items',

????????'flex',?'flex-grow',?'flex-shrink',?'flex-basis',?'align-self',?'order',

????????'margin',?'margin-top',?'margin-right',?'margin-bottom',?'margin-left',

????????'outline',?'outline-width',?'outline-style',?'outline-color',?'outline-offset',

????????'box-shadow',?'box-sizing',

????????'border-radius',?'border-top-right-radius',?'border-bottom-right-radius',?'border-bottom-left-radius',?'border-top-left-radius',

????????'border',?'border-width',?'border-style',?'border-color',

????????'border-top',?'border-top-width',?'border-top-style',?'border-top-color',

????????'border-right',?'border-right-width',?'border-right-style',?'border-right-color',

????????'border-bottom',?'border-bottom-width',?'border-bottom-style',?'border-bottom-color',

????????'border-left',?'border-left-width',?'border-left-style',?'border-left-color',

????????'border-image',?'border-image-source',?'border-image-slice',?'border-image-width',?'border-image-outset',?'border-image-repeat',

????????'padding',?'padding-top',?'padding-right',?'padding-bottom',?'padding-left',

????????'max-width',?'max-height',?'min-width',?'min-height',?'width',?'height',

????????'background',?'background-attachment',?'background-clip',?'background-origin',?'background-size',?'background-color',?'background-image',?'background-position',?'background-repeat',

????????'table-layout',?'border-collapse',?'border-spacing',?'caption-side',?'empty-cells',

????????'font',?'font-style',?'font-variant',?'font-weight',?'font-size',?'line-height',?'font-family'

??????],

??????{

????????unspecified:?'bottom'

??????}

????]

??}

};




參考網(wǎng)址:

stylelint 英文:https://stylelint.io/? ??

stylelint 中文:https://cloud.tencent.com/developer/chapter/18030

stylelint-order:?https://github.com/hudochenkov/stylelint-order

stylelint-scss:https://github.com/kristerkari/stylelint-scss

https://www.cnblogs.com/jiaoshou/p/11284204.html

https://www.cnblogs.com/xiaohuochai/p/9078154.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市懈叹,隨后出現(xiàn)的幾起案子乖杠,更是在濱河造成了極大的恐慌,老刑警劉巖澄成,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件胧洒,死亡現(xiàn)場離奇詭異畏吓,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)卫漫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進(jìn)店門菲饼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人汛兜,你說我怎么就攤上這事巴粪。” “怎么了粥谬?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵肛根,是天一觀的道長。 經(jīng)常有香客問我漏策,道長派哲,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任掺喻,我火速辦了婚禮芭届,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘感耙。我一直安慰自己褂乍,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布即硼。 她就那樣靜靜地躺著逃片,像睡著了一般。 火紅的嫁衣襯著肌膚如雪只酥。 梳的紋絲不亂的頭發(fā)上褥实,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天,我揣著相機(jī)與錄音裂允,去河邊找鬼损离。 笑死,一個(gè)胖子當(dāng)著我的面吹牛绝编,可吹牛的內(nèi)容都是我干的僻澎。 我是一名探鬼主播,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼十饥,長吁一口氣:“原來是場噩夢啊……” “哼怎棱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起绷跑,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎凡资,沒想到半個(gè)月后砸捏,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體谬运,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年垦藏,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了梆暖。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,505評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡掂骏,死狀恐怖轰驳,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情弟灼,我是刑警寧澤级解,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站田绑,受9級特大地震影響勤哗,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜掩驱,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一芒划、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧欧穴,春花似錦民逼、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至焚辅,卻和暖如春映屋,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背同蜻。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工棚点, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人湾蔓。 一個(gè)月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓瘫析,卻偏偏與公主長得像,于是被迫代替她去往敵國和親默责。 傳聞我的和親對象是個(gè)殘疾皇子贬循,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評論 2 359