DEV Community

smilelk4
smilelk4

Posted on

Auto-Format Stylesheet On Save Using WebStorm with 3 Steps

Intro

Linter tools can help keep code clean and consistent, but can get annoying if the issues aren't caught until committing or pushing to remote repository. This article is to omit such pain by utilizing Stylelint.

Linter complaining

The author used SCSS as the stylesheet format, but the same idea is also applicable with other formats like CSS.

Setup

  • Mac
  • WebStorm
  • SCSS

1. Download dependencies

If using npm:

npm i stylelint stylelint-config-standard-scss -D
Enter fullscreen mode Exit fullscreen mode

If using yarn:

yarn add stylelint stylelint-config-standard-scss -D
Enter fullscreen mode Exit fullscreen mode

2. .stylelintrc

Create .stylelintrc.json at the root of the project, and add below:

{
  "extends": "stylelint-config-standard-scss"
}
Enter fullscreen mode Exit fullscreen mode

3. Setting

  • Go into Webstorm > Preferences
  • Under Tools > File Watchers:
    • Click + to create new
    • Select SCSS
    • Set up the configs as below: File Watcher configs
  1. File type: SCSS style sheet
  2. Scope: Project Files
  3. Program: /<PATH_TO_PROJECT>/node_modules/stylelint/bin/stylelint.js
  4. Arguments: $FileName$ --fix
  5. Output paths to refresh: $FileName$

...and see the lint in action as you save your stylesheet πŸŽ‰πŸŽ‰πŸŽ‰
Image description

Resources

Top comments (1)

Collapse
 
ivanovserge profile image
Sergey Ivanov • Edited

Thanks! It works!