UNPKG

8.3 kBMarkdownView Raw
1analyze-css
2===========
3
4[![NPM version](https://badge.fury.io/js/analyze-css.png)](http://badge.fury.io/js/analyze-css)
5[![Known Vulnerabilities](https://snyk.io/test/github/macbre/analyze-css/badge.svg)](https://snyk.io/test/github/macbre/analyze-css)
6
7[![Download stats](https://nodei.co/npm/analyze-css.png?downloads=true&downloadRank=true)](https://nodei.co/npm/analyze-css/)
8
9CSS 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).
10
11- [Analyze CSS Demo](http://www.testmycss.com/)
12
13## Install
14
15analyze-css comes as a "binary" for command-line and as CommonJS module. Run the following to install them globally:
16
17```
18npm install --global analyze-css
19```
20
21## Usage
22
23### Command line tool
24
25You can use analyze-css "binary" to analyze local CSS files or remote CSS assets:
26
27```
28analyze-css --file examples/elecena.css
29
30analyze-css --url http://s3.macbre.net/analyze-css/propertyResets.css
31analyze-css --url https://s3.macbre.net/analyze-css/propertyResets.css --ignore-ssl-errors
32```
33
34You can provide CSS via stdin as well (notice the dash: ``-``):
35
36```
37echo ".foo {margin: 0 \!important}" | analyze-css -
38```
39
40This will emit JSON formatted results on ``stdout``. Use ``--pretty`` (or ``-p`` shortcut) option to make the output readable for human beings.
41
42Basic HTTP authentication can be provided through the options `--auth-user` and `--auth-pass`.
43
44HTTP proxy (e.g. `http://localhost:8080`) can be provided via:
45
46* `--proxy` or `-x` option
47* `HTTP_PROXY` env variable
48
49### CommonJS module
50
51```js
52var analyzer = require('analyze-css');
53
54new analyzer('.foo {margin: 0 !important}', function(err, results) {
55 console.error(err);
56 console.log(results); // example? see below
57});
58```
59
60```js
61// options can be provided
62var opts = {
63 'noOffenders': true
64};
65
66new analyzer('.foo {margin: 0 !important}', opts, function(err, results) {
67 console.error(err);
68 console.log(results); // example? see below
69});```
70```
71
72### [grunt task](https://www.npmjs.org/package/grunt-contrib-analyze-css)
73
74> Created by @DeuxHuitHuit
75
76```
77npm i grunt-contrib-analyze-css
78```
79
80It uses configurable threshold and compares the analyze-css result with it.
81
82### Results
83
84```json
85{
86 "generator": "analyze-css v0.10.2",
87 "metrics": {
88 "base64Length": 11332,
89 "redundantBodySelectors": 0,
90 "redundantChildNodesSelectors": 1,
91 "colors": 106,
92 "comments": 1,
93 "commentsLength": 68,
94 "complexSelectors": 37,
95 "duplicatedSelectors": 7,
96 "duplicatedProperties": 24,
97 "emptyRules": 0,
98 "expressions": 0,
99 "oldIEFixes": 51,
100 "imports": 0,
101 "importants": 3,
102 "mediaQueries": 0,
103 "notMinified": 0,
104 "multiClassesSelectors": 74,
105 "parsingErrors": 0,
106 "oldPropertyPrefixes": 79,
107 "propertyResets": 0,
108 "qualifiedSelectors": 28,
109 "specificityIdAvg": 0.04,
110 "specificityIdTotal": 25,
111 "specificityClassAvg": 1.27,
112 "specificityClassTotal": 904,
113 "specificityTagAvg": 0.79,
114 "specificityTagTotal": 562,
115 "selectors": 712,
116 "selectorLengthAvg": 1.5722460658082975,
117 "selectorsByAttribute": 92,
118 "selectorsByClass": 600,
119 "selectorsById": 25,
120 "selectorsByPseudo": 167,
121 "selectorsByTag": 533,
122 "length": 55173,
123 "rules": 433,
124 "declarations": 1288
125 },
126 "offenders": {
127 "importants": [
128 ".foo {margin: 0 !important}"
129 ]
130 }
131}
132```
133
134## Metrics
135
136* **base64Length**: total length of base64-encoded data in CSS source (will warn about base64-encoded data bigger than 4 kB)
137* **redundantBodySelectors**: number of redundant body selectors (e.g. ``body .foo``, ``section body h2``, but not ``body > h1``)
138* **redundantChildNodesSelectors**: number of redundant child nodes selectors (e.g. ``ul li``, ``table tr``)
139* **colors**: number of unique colors used in CSS
140* **comments**: number of comments in CSS source
141* **commentsLength**: length of comments content in CSS source
142* **complexSelectors**: number of complex selectors (consisting of more than three expressions, e.g. ``header ul li .foo``)
143* **duplicatedSelectors**: number of CSS selectors defined more than once in CSS source
144* **duplicatedProperties**: number of CSS property definitions duplicated within a selector
145* **emptyRules**: number of rules with no properties (e.g. ``.foo { }``)
146* **expressions**: number of rules with CSS expressions (e.g. ``expression( document.body.clientWidth > 600 ? "600px" : "auto" )``)
147* **oldIEFixes**: number of fixes for old versions of Internet Explorer (e.g. ``* html .foo {}`` and ``.foo { *zoom: 1 }``, [read](http://blogs.msdn.com/b/ie/archive/2005/09/02/460115.aspx) [more](http://www.impressivewebs.com/ie7-ie8-css-hacks/))
148* **imports** number of ``@import`` rules
149* **importants**: number of properties with value forced by ``!important``
150* **mediaQueries**: number of media queries (e.g. ``@media screen and (min-width: 1370px)``)
151* **notMinified**: set to 1 if the provided CSS is not minified
152* **multiClassesSelectors**: reports selectors with multiple classes (e.g. ``span.foo.bar``)
153* **parsingErrors**: number of CSS parsing errors
154* **oldPropertyPrefixes**: number of properties with no longer needed vendor prefix, powered by data provided by [autoprefixer](https://github.com/ai/autoprefixer) (e.g. ``--moz-border-radius``)
155* **propertyResets**: number of [accidental property resets](http://css-tricks.com/accidental-css-resets/)
156* **qualifiedSelectors**: number of [qualified selectors](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Writing_efficient_CSS) (e.g. ``header#nav``, ``.foo#bar``, ``h1.title``)
157* **specificityIdAvg**: average [specificity](http://css-tricks.com/specifics-on-css-specificity/) for ID
158* **specificityIdTotal**: total [specificity](http://css-tricks.com/specifics-on-css-specificity/) for ID
159* **specificityClassAvg**: average [specificity](http://css-tricks.com/specifics-on-css-specificity/) for class, pseudo-class or attribute
160* **specificityClassTotal**: total [specificity](http://css-tricks.com/specifics-on-css-specificity/) for class, pseudo-class or attribute
161* **specificityTagAvg**: average [specificity](http://css-tricks.com/specifics-on-css-specificity/) for element
162* **specificityTagTotal**: total [specificity](http://css-tricks.com/specifics-on-css-specificity/) for element
163* **selectors**: number of selectors (e.g. ``.foo, .bar { color: red }`` is counted as two selectors - ``.foo`` and ``.bar``)
164* **selectorLengthAvg**: average length of selector (e.g. for ``.foo .bar, #test div > span { color: red }`` will be set as 2.5)
165* **selectorsByAttribute**: number of selectors by attribute (e.g. ``.foo[value=bar]``)
166* **selectorsByClass**: number of selectors by class
167* **selectorsById**: number of selectors by ID
168* **selectorsByPseudo**: number of pseudo-selectors (e,g. ``:hover``)
169* **selectorsByTag**: number of selectors by tag name
170* **length**: length of CSS source (in bytes)
171* **rules**: number of rules (e.g. ``.foo, .bar { color: red }`` is counted as one rule)
172* **declarations**: number of declarations (e.g. ``.foo, .bar { color: red }`` is counted as one declaration - ``color: red``)
173
174## Read more
175
176* [Writing Efficient CSS](http://developer.mozilla.org/en/Writing_Efficient_CSS) (by Mozilla)
177* [Optimize browser rendering](https://developers.google.com/speed/docs/best-practices/rendering) (by Google)
178* [Profiling CSS for fun and profit. Optimization notes.](http://perfectionkills.com/profiling-css-for-fun-and-profit-optimization-notes/)
179* [CSS specificity](http://css-tricks.com/specifics-on-css-specificity/)
180* [CSS Selector Performance has changed! (For the better)](http://calendar.perfplanet.com/2011/css-selector-performance-has-changed-for-the-better/) (by Nicole Sullivan)
181* [CSS Performance](http://dl.dropboxusercontent.com/u/39519/talks/cssperf/index.html) (by Paul Irish)
182* [GitHub's CSS Performance](https://speakerdeck.com/jonrohan/githubs-css-performance) (by Joh Rohan)
183
184## Dev hints
185
186Running tests and linting the code:
187
188```
189npm test && npm run-script lint
190```
191
192Turning on debug mode (i.e. verbose logging to stderr via [debug module](https://npmjs.org/package/debug)):
193
194```
195DEBUG=analyze-css* analyze-css ...
196```