![](/img/trans.png)
[英]Format SCSS/CSS/LESS with Prettier with rules of Stylelint
[英]How to Edit Auto-Formatting Rules for CSS/SCSS/LESS using Prettier in VSCode?
我正在使用 Prettier - VSCode 的代碼格式化程序擴展來在保存時自動格式化我的代碼。
我習慣於在我的 sass 文件中編寫單行塊(其中只有一個屬性),即
.some-class { background: #f00; }
問題是Prettier擴展將其格式化為多行,即
.some-class {
background: #f00;
}
似乎更漂亮地將 stylelint 用於 css/scss 文件,我發現這些設置可以通過在設置中啟用來覆蓋:
"prettier.requireConfig": true
並使用.prettierrc.js文件,但無法關閉單行塊的 linting。 如果這里有人對此有任何修復,將不勝感激。
謝謝
設置不能被"prettier.requireConfig": true
覆蓋。 Prettier - VSCode 的代碼格式化程序擴展沒有從 VSCode 設置編輯樣式表 linting 的選項。
但是,可以選擇啟用stylelint集成,但這需要stylelint和stylelint-prettier npm 模塊。
Prettier默認使用標准 stylelint 配置進行樣式表 linting 和格式化。
在下面發布解決方案。
為了在 VSCode 中使用 Prettier - Code formatter extension 允許單行塊,請執行以下步驟:
"prettier.stylelintIntegration": true
npm install stylelint stylelint-prettier --save-dev
{
"plugins": ["stylelint-prettier"],
"rules": {
"block-closing-brace-newline-after": "always-multi-line",
"block-closing-brace-empty-line-before": "never",
"block-closing-brace-space-before": "always",
"block-opening-brace-space-after": "always",
"block-opening-brace-space-before": "always",
"block-closing-brace-newline-before": "always-multi-line",
"block-opening-brace-newline-after": "always-multi-line",
"indentation": 4
}
}
您可以添加/自定義更多 stylelint 規則,請在此處查看完整的規則列表。
我花了一些時間來了解如何配置這些選項,如果你是從 stylelint 開始的,我強烈建議你先閱讀它的指南。
我不知道 vscode 有這個功能。 一個簡單的解決方案可能是指定prettier-ignore
?
/* prettier-ignore */
.some-class { background: #f00; }
參考:
這是我找到的最好的解決方案之一。 它擁有您無需擔心格式化代碼的所有內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.