Back to directory
ruilisi avatar
ruilisi / css-checker

css-checker

Reduce Similar & Duplicated CSS Classes with Diff in Seconds!

591

Stars

13

Forks

11

Watchers

MIT

License

CSS-CHECKER

CSS Checker - Less is More

status

中文文档

Purpose

css-checker checks your CSS styles for duplications and finds the diff among CSS classes with high similarity in seconds. It is designed to avoid redundantly or similar css and styled components between files and to work well for both local developments, and for automation like CI.

Similarity check, duplication check, colors check, long lines warning are supported by default. Styled components check, Unused CSS check can be enabled optionally. CSS checker can help reduce CSS code for developers in seconds.

See more on Wiki

Install

Using Go:

go install github.com/ruilisi/css-checker@latest

(With go version before 1.17, use go get github.com/ruilisi/css-checker). Or download from releases

Using npm:

npm install -g css-checker-kit

Usage

Run

  • cd PROJECT_WITH_CSS_FILES and just run:
css-checker
  • (Beta Feature: styled components check): css-checker -styled

DEMO

(Check and show the diff among similar classes (>=80%). Colors, long scripts that are used more than once will also be pointed out by default. Check css-checker -help for customized options.)

  • Colors with rgb/rgba/hsl/hsla/hex will be converted to rbga and compared together.

  • (Alpha Feature: Find classes that are not referred to by your code): css-checker -unused

Run with path

  • css-checker -path=YOUR_PROJECT_PATH

File Ignores

  • CSS-Checker ignores paths in .gitignore by default (You can disable this to read all files by using -unrestricted=true).
  • For adding extra paths to ignore, using: -ignores=node_modules,packages .

Config File

  • css-checker.yaml: CSS-Checker read this yaml file in your project path for settings, you can use parameters in Basic Commands sections to set up this file (without the leading '-').
  • A sample yaml file named 'css-checker.example.yaml' is also provided in this project, move it to your project path with the name 'css-checker.yaml' and it will work.
  • To specify your config file, use -config=YOUR_CONFIG_FILE_PATH.

Advanced Features

  • Run with styled components check only (without checks for css): css-checker -css=false -styled
  • Find classes that not referred by your code: css-checker -unused (Alpha)

Basic commands

  • colors: whether to check colors (default true)
  • css: whether to check css files (default true as you expected)
  • config: set configuration file path (string, default './css-checker.yaml')
  • ignores: paths and files to be ignored (e.g. node_modules,*.example.css) (string, default '')
  • length-threshold: Min length of a single style value (no including the key) that to be considered as long script line (default 20)
  • long-line: whether to check duplicated long script lines (default true)
  • path: set path to files, default to be current folder (default ".")
  • sections: whether to check css class duplications (default true)
  • sim: whether to check similar css classes (default true)
  • sim-threshold: Threshold for Similarity Check ($\geq20$ && $\lt100$) (int only, e.g. 80 for 80%, checks for identical classes defined in sections) (default 80)
  • styled: checks for styled components (default false)
  • unrestricted: search all files (gitignore)
  • unused: whether to check unused classes (Beta)
  • version: prints current version and exits
  • to-file: wherther generate a html file (default true)
  • file-name: if to-file is true, set the html file name(default css-checker.html)

Outputs:

image.png

How to get similarities between classes?

  1. Hash each line of class (aka. section in our code), Generate map: LineHash -> Section.
  2. Convert map LineHash -> Section => [SectionIndex1][SectionIndex2] -> Duplicated Hashes, section stands for css class.
  3. In map: [SectionIndex1][SectionIndex2] -> Duplicated Hashes, number of the duplicated hashes stands for duplicated lines between classes.

Similarity Check

Check similarities ($\geq(sim-threshold)$ && $\lt100$) between classes. This will print the same line in between classes.

  • $sim-threshold$: using -sim-threshold= params or setting sim-threshold: in config yaml file, default 80, min 20.

image.png

Duplicated CSS Classes

Similar to Similarity Check but put those classes that are totally identical to each other.

Long Script Line Check

Long scripts can be saved as variables to make your life easier. This will only alert when long lines are used for more than once.

image.png

Colors Check

Check colors in HEX/RGB/RGBA/HSL/HSLA that are used more than once in your code. As for supporting of different themes and possibly future updates of your color set, you may consider putting them as CSS variables.

image.png

Build & Release

  • make test-models
  • make build
  • make release

Q&A

Ugly output in PowerShell

From PowerShell, paste the following script and run it to activate ANSI escape sequences, then restart your PowerShell.

Set-ItemProperty HKCU:\Console VirtualTerminalLevel -Type DWORD 1

Authors

  • Xiemala Team. It helps in removing hundreds of similar CSS classes for developers in this project.

Releases

Mar 20, 2022

Download .zip

Changelog

b13abbd 0.3.6 88a756c Add MIT License 321bf95 chore: bump to 0.4.0 c236e52 chore: lint 8863f2a chore: update readme 20f98dc chore: updates to wiki a845df0 doc: logo, ci status, link btw CN,...

Mar 11, 2022

Download .zip

Changelog

b13abbd 0.3.6 88a756c Add MIT License 8ead454 chore: bump to 0.4.0 8863f2a chore: update readme a845df0 doc: logo, ci status, link btw CN,EN 7a5869d feat: check unused css 70f0026 feat: def...

Changelog

8863f2a chore: update readme a845df0 doc: logo, ci status, link btw CN,EN 7a5869d feat: check unused css 66593c4 feat: translate to cn doc 8671f34 npm support

Feb 25, 2022

Download .zip

Changelog

9439d15 chore: better doc in readme b71d85a chore: readme b055a7c ci: drone.yml init, go mod 771ce17 doc: install & Run 5740f5d feat: -ignore 84fb9bc feat: add make file, release, git tag,...

Feb 23, 2022

Download .zip

Changelog

9439d15 chore: better doc in readme b055a7c ci: drone.yml init, go mod 5740f5d feat: -ignore 84fb9bc feat: add make file, release, git tag, doc 623ea3f feat: similarity check