2.2.9 • Published 1 day ago

analyze-css v2.2.9

Weekly downloads
3,835
License
BSD-2-Clause
Repository
github
Last release
1 day ago

analyze-css

NPM version Known Vulnerabilities Coverage Status CodeFactor

Download stats

CSS selectors complexity and performance analyzer. analyze-css is built as a set of rules bound to events fired by CSS parser. Each rule can generate metrics and add "offenders" with more detailed information (see Usage section for an example).

Install

analyze-css comes as a "binary" for command-line and as CommonJS module. Run the following to install them globally:

npm install --global analyze-css

or to install from GitHub's repository:

npm install --global @macbre/analyze-css

Usage

Command line tool

You can use analyze-css "binary" to analyze local CSS files or remote CSS assets:

$ analyze-css --file examples/elecena.css

$ analyze-css --url http://s3.macbre.net/analyze-css/propertyResets.css
$ analyze-css --url https://s3.macbre.net/analyze-css/propertyResets.css --ignore-ssl-errors

You can provide CSS via stdin as well (notice the dash: -):

$ echo ".foo {margin: 0 \!important}" | analyze-css -

This will emit JSON formatted results on stdout. Use --pretty (or -p shortcut) option to make the output readable for human beings.

Basic HTTP authentication can be provided through the options --auth-user and --auth-pass.

HTTP proxy (e.g. http://localhost:8080) can be provided via:

  • --proxy or -x option
  • HTTP_PROXY env variable

CommonJS module

npm i --save analyze-css
const analyze = require('analyze-css');

(async() => {
  const results = await analyze('.foo {margin: 0 !important}');
  console.log(results); // example? see below
})();
// options can be provided
const opts = {
  'noOffenders': true
};

(async() => {
  const results = await analyze('.foo {margin: 0 !important}', opts);
  console.log(results); // example? see below
})();

grunt task

Created by @DeuxHuitHuit

$ npm i grunt-contrib-analyze-css

It uses configurable threshold and compares the analyze-css result with it.

Results

{
  "generator": "analyze-css v0.10.2",
  "metrics": {
    "base64Length": 11332,
    "redundantBodySelectors": 0,
    "redundantChildNodesSelectors": 1,
    "colors": 106,
    "comments": 1,
    "commentsLength": 68,
    "complexSelectors": 37,
    "duplicatedSelectors": 7,
    "duplicatedProperties": 24,
    "emptyRules": 0,
    "expressions": 0,
    "oldIEFixes": 51,
    "imports": 0,
    "importants": 3,
    "mediaQueries": 0,
    "notMinified": 0,
    "multiClassesSelectors": 74,
    "parsingErrors": 0,
    "oldPropertyPrefixes": 79,
    "propertyResets": 0,
    "qualifiedSelectors": 28,
    "specificityIdAvg": 0.04,
    "specificityIdTotal": 25,
    "specificityClassAvg": 1.27,
    "specificityClassTotal": 904,
    "specificityTagAvg": 0.79,
    "specificityTagTotal": 562,
    "selectors": 712,
    "selectorLengthAvg": 1.5722460658082975,
    "selectorsByAttribute": 92,
    "selectorsByClass": 600,
    "selectorsById": 25,
    "selectorsByPseudo": 167,
    "selectorsByTag": 533,
    "length": 55173,
    "rules": 433,
    "declarations": 1288
  },
  "offenders": {
    "importants": [
      ".foo {margin: 0 !important}"
    ]
  }
}

Metrics

  • base64Length: total length of base64-encoded data in CSS source (will warn about base64-encoded data bigger than 4 kB)
  • redundantBodySelectors: number of redundant body selectors (e.g. body .foo, section body h2, but not body > h1)
  • redundantChildNodesSelectors: number of redundant child nodes selectors (e.g. ul li, table tr)
  • colors: number of unique colors used in CSS
  • comments: number of comments in CSS source
  • commentsLength: length of comments content in CSS source
  • complexSelectors: number of complex selectors (consisting of more than three expressions, e.g. header ul li .foo)
  • duplicatedSelectors: number of CSS selectors defined more than once in CSS source
  • duplicatedProperties: number of CSS property definitions duplicated within a selector
  • emptyRules: number of rules with no properties (e.g. .foo { })
  • expressions: number of rules with CSS expressions (e.g. expression( document.body.clientWidth > 600 ? "600px" : "auto" ))
  • oldIEFixes: number of fixes for old versions of Internet Explorer (e.g. * html .foo {} and .foo { *zoom: 1 }, read more)
  • imports number of @import rules
  • importants: number of properties with value forced by !important
  • mediaQueries: number of media queries (e.g. @media screen and (min-width: 1370px))
  • notMinified: set to 1 if the provided CSS is not minified
  • multiClassesSelectors: reports selectors with multiple classes (e.g. span.foo.bar)
  • parsingErrors: number of CSS parsing errors
  • oldPropertyPrefixes: number of properties with no longer needed vendor prefix, powered by data provided by autoprefixer (e.g. --moz-border-radius)
  • propertyResets: number of accidental property resets
  • qualifiedSelectors: number of qualified selectors (e.g. header#nav, .foo#bar, h1.title)
  • specificityIdAvg: average specificity for ID
  • specificityIdTotal: total specificity for ID
  • specificityClassAvg: average specificity for class, pseudo-class or attribute
  • specificityClassTotal: total specificity for class, pseudo-class or attribute
  • specificityTagAvg: average specificity for element
  • specificityTagTotal: total specificity for element
  • selectors: number of selectors (e.g. .foo, .bar { color: red } is counted as two selectors - .foo and .bar)
  • selectorLengthAvg: average length of selector (e.g. for .foo .bar, #test div > span { color: red } will be set as 2.5)
  • selectorsByAttribute: number of selectors by attribute (e.g. .foo[value=bar])
  • selectorsByClass: number of selectors by class
  • selectorsById: number of selectors by ID
  • selectorsByPseudo: number of pseudo-selectors (e,g. :hover)
  • selectorsByTag: number of selectors by tag name
  • length: length of CSS source (in bytes)
  • rules: number of rules (e.g. .foo, .bar { color: red } is counted as one rule)
  • declarations: number of declarations (e.g. .foo, .bar { color: red } is counted as one declaration - color: red)

Read more

Dev hints

Running tests and linting the code:

$ npm test && npm run-script lint

Turning on debug mode (i.e. verbose logging to stderr via debug module):

$ DEBUG=analyze-css* analyze-css ...

Stargazers over time

Stargazers over time

2.2.9

1 day ago

2.2.8

8 days ago

2.2.7

15 days ago

2.2.6

22 days ago

2.2.5

29 days ago

2.2.4

1 month ago

2.2.3

1 month ago

2.2.2

2 months ago

2.2.1

2 months ago

2.2.0

2 months ago

2.1.132

2 months ago

2.1.131

3 months ago

2.1.130

3 months ago

2.1.129

3 months ago

2.1.128

3 months ago

2.1.127

4 months ago

2.1.126

4 months ago

2.1.125

4 months ago

2.1.103

10 months ago

2.1.102

10 months ago

2.1.105

10 months ago

2.1.104

10 months ago

2.1.107

9 months ago

2.1.106

9 months ago

2.1.109

9 months ago

2.1.108

9 months ago

2.1.110

9 months ago

2.1.112

8 months ago

2.1.111

8 months ago

2.1.114

8 months ago

2.1.113

8 months ago

2.1.116

8 months ago

2.1.115

8 months ago

2.1.118

7 months ago

2.1.117

8 months ago

2.1.119

7 months ago

2.1.121

7 months ago

2.1.120

7 months ago

2.1.123

7 months ago

2.1.122

7 months ago

2.1.124

6 months ago

2.1.99

11 months ago

2.1.101

11 months ago

2.1.100

11 months ago

2.1.98

11 months ago

2.1.96

12 months ago

2.1.97

11 months ago

2.1.95

12 months ago

2.1.94

12 months ago

2.1.93

1 year ago

2.1.92

1 year ago

2.1.90

1 year ago

2.1.91

1 year ago

2.1.89

1 year ago

2.1.87

1 year ago

2.1.88

1 year ago

2.1.86

1 year ago

2.1.85

1 year ago

2.1.83

1 year ago

2.1.84

1 year ago

2.1.81

1 year ago

2.1.82

1 year ago

2.1.80

1 year ago

2.1.78

1 year ago

2.1.79

1 year ago

2.1.76

1 year ago

2.1.77

1 year ago

2.1.75

1 year ago

2.1.49

2 years ago

2.1.47

2 years ago

2.1.48

2 years ago

2.1.45

2 years ago

2.1.46

2 years ago

2.1.43

2 years ago

2.1.44

2 years ago

2.1.41

2 years ago

2.1.42

2 years ago

2.1.40

2 years ago

2.1.58

2 years ago

2.1.59

2 years ago

2.1.56

2 years ago

2.1.57

2 years ago

2.1.54

2 years ago

2.1.55

2 years ago

2.1.52

2 years ago

2.1.53

2 years ago

2.1.50

2 years ago

2.1.51

2 years ago

2.1.69

2 years ago

2.1.67

2 years ago

2.1.68

2 years ago

2.1.65

2 years ago

2.1.66

2 years ago

2.1.63

2 years ago

2.1.64

2 years ago

2.1.61

2 years ago

2.1.62

2 years ago

2.1.60

2 years ago

2.1.74

1 year ago

2.1.72

1 year ago

2.1.73

1 year ago

2.1.70

2 years ago

2.1.71

2 years ago

2.1.27

2 years ago

2.1.28

2 years ago

2.1.25

2 years ago

2.1.26

2 years ago

2.1.23

2 years ago

2.1.24

2 years ago

2.1.22

2 years ago

2.1.29

2 years ago

2.1.38

2 years ago

2.1.39

2 years ago

2.1.36

2 years ago

2.1.37

2 years ago

2.1.34

2 years ago

2.1.35

2 years ago

2.1.32

2 years ago

2.1.33

2 years ago

2.1.30

2 years ago

2.1.31

2 years ago

2.1.16

2 years ago

2.1.17

2 years ago

2.1.14

2 years ago

2.1.15

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.10

2 years ago

2.1.11

2 years ago

2.1.18

2 years ago

2.1.19

2 years ago

2.1.6

2 years ago

2.1.21

2 years ago

2.1.20

2 years ago

2.1.4

2 years ago

2.1.5

2 years ago

2.1.3

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago

0.13.0

3 years ago

0.12.13

3 years ago

0.12.12

4 years ago

0.12.11

4 years ago

0.12.10

4 years ago

0.12.9

4 years ago

0.12.8

4 years ago

0.12.7

5 years ago

0.12.6

6 years ago

0.12.5

7 years ago

0.12.4

7 years ago

0.12.3

8 years ago

0.12.2

8 years ago

0.12.1

8 years ago

0.12.0

8 years ago

0.11.1

8 years ago

0.11.0

8 years ago

0.10.2

9 years ago

0.10.1

9 years ago

0.10.0

9 years ago

0.9.6

9 years ago

0.9.5

9 years ago

0.9.4

9 years ago

0.9.3

9 years ago

0.9.2

9 years ago

0.9.1

9 years ago

0.9.0

9 years ago

0.8.0

10 years ago

0.7.0

10 years ago

0.6.0

10 years ago

0.5.0

10 years ago

0.4.1

10 years ago

0.4.0

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago