cssstats v4.0.5
cssstats
Parses stylesheets and returns an object with statistics. This is the core module used in cssstats.com
Installation
npm i --save cssstats
Usage
Node
var fs = require('fs')
var cssstats = require('cssstats')
var css = fs.readFileSync('./styles.css', 'utf8')
var stats = cssstats(css)
PostCSS Plugin
CSS Stats can be used as a PostCSS plugin. The stats will be added to PostCSS's messages array.
var fs = require('fs')
var postcss = require('postcss')
var cssstats = require('cssstats')
var css = fs.readFileSync('./styles.css', 'utf8')
postcss()
.use(cssstats())
.process(css)
.then(function (result) {
result.messages.forEach(function (message) {
console.log(message)
})
})
Options
Options may be passed as a second argument.
var stats = cssstats(css, { mediaQueries: false })
safe
(boolean, default:true
) - enables PostCSS safe mode for parsing CSS with syntax errorsmediaQueries
(boolean, defaulttrue
) - determines whether or not to generate stats for each media query blockimportantDeclarations
(boolean, defaultfalse
) - include an array of declarations with!important
The following options add the results of helper methods to the returned object. This is helpful when using JSON.stringify()
.
specificityGraph
(boolean, defaultfalse
)sortedSpecificityGraph
(boolean, defaultfalse
)repeatedSelectors
(boolean, defaultfalse
)propertyResets
(boolean, defaultfalse
)vendorPrefixedProperties
(boolean, defaultfalse
)
Returned Object
// Example
{
size: n,
gzipSize: n,
rules: {
total: n,
size: {
graph: [n],
max: n,
average: n
}
},
selectors: {
total: n,
id: n,
class: n,
type: n,
pseudoClass: n,
psuedoElement: n,
values: [str],
specificity: {
max: n
average: n
},
getSpecificityGraph(),
getSpecificityValues(),
getRepeatedValues(),
getSortedSpecificity()
},
declarations: {
total: n,
unique: n,
uniqueToTotalRatio: n,
important: [obj],
properties:
prop: [str]
},
getPropertyResets(),
getUniquePropertyCount(),
getPropertyValueCount(),
getVendorPrefixed(),
getAllFontSizes(),
getAllFontFamilies(),
},
mediaQueries: {
total: n,
unique: n,
values: [str],
contents: [
{
value: str,
rules: {
total: n,
size: {
graph: [n],
max: n,
average: n
}
},
selectors: {
total: n,
id: n,
class: n,
type: n,
pseudoClass: n,
pseudoElement: n,
values: [str],
specificity: {
max: n,
average: n
}
},
declarations: {
total: n,
unique: n,
important: [obj],
vendorPrefix: n,
properties: {
prop: [str]
}
}
}
]
}
}
size
number
The size of the file in bytes
gzipSize
number
The size of the stylesheet gzipped in bytes
rules
object
total
number - total number of rulessize
objectsize.graph
array - ruleset sizes (number of declarations per rule) in source ordersize.max
number - maximum ruleset sizesize.average
number - average ruleset size
selectors
object
total
number - total number of selectorstype
number - total number of type selectorsclass
number - total number of class selectorsid
number - total number of id selectorspseudoClass
number - total number of pseudo class selectorspseudoElement
number - total number of pseudo element selectorsvalues
array - array of strings for all selectorsspecificity
objectspecificity.max
number - maximum specificity as a base 10 numberspecificity.average
number - average specificity as a base 10 number
getSpecificityGraph()
function - returns an array of numbers for each selector’s specificity as a base 10 numbergetSpecificityValues()
function - returns an array of selectors with base 10 specificity score in ordergetRepeatedValues()
function - returns an array of strings of repeated selectorsgetSortedSpecificity()
function - returns an array of selectors with base 10 specificity score, sorted from highest to lowest
declarations
object
total
number - total number of declarationsunique
number - total unique declarationsuniqueToTotalRatio
number - ratio of unique declarations to total declarationsproperties
object - object with each unique property and an array of that property’s valuesgetPropertyResets()
function - returns an object with the number of times margin or padding is reset for each propertygetUniquePropertyCount(property)
function - returns the number of unique values for the given propertygetPropertyValueCount(property, value)
function - returns the number of times a declaration occurs for the given property and valuegetVendorPrefixed()
function - returns an array of declarations with vendor prefixed propertiesgetAllFontSizes()
function - returns an array of font sizes from bothfont-size
andfont
shorthand declarationsgetAllFontFamilies()
function - returns an array of font families from bothfont-family
andfont
shorthand declarationsimportant
array (optional) -!important
declaration objects withproperty
andvalue
mediaQueries
object
total
number - total number of media queriesunique
number - total unique media queriesvalues
array - array of values for each media querycontents
array - array of media query blocks with full stats object for each
See the /test/results
folder for example JSON results.
Usage examples
var cssstats = require('cssstats')
var stats = cssstats(css)
Generate a specificity graph
var specificityGraph = stats.selectors.getSpecificityGraph()
Sort selectors by highest specificity
var sortedSelectors = stats.selectors.getSortedSpecificity()
Get total number of unique colors
var uniqueColorsCount = stats.declarations.getUniquePropertyCount('color')
display: none
count
var displayNoneCount = stats.declarations.getPropertyValueCount(
'display',
'none'
)
License
MIT
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
3 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
7 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago