Parses stylesheets and returns an object with statistics. This is the core module used in cssstats.com
npm i --save cssstatsvar fs = require('fs')
var cssstats = require('cssstats')
var css = fs.readFileSync('./styles.css', 'utf8')
var stats = cssstats(css)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 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)
// 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,
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]
}
}
}
]
}
}The size of the file in bytes
The size of the stylesheet gzipped in bytes
totalnumber - total number of rulessizeobjectsize.grapharray - ruleset sizes (number of declarations per rule) in source ordersize.maxnumber - maximum ruleset sizesize.averagenumber - average ruleset size
totalnumber - total number of selectorstypenumber - total number of type selectorsclassnumber - total number of class selectorsidnumber - total number of id selectorspseudoClassnumber - total number of pseudo class selectorspseudoElementnumber - total number of pseudo element selectorsvaluesarray - array of strings for all selectorsspecificityobjectspecificity.maxnumber - maximum specificity as a base 10 numberspecificity.averagenumber - 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
totalnumber - total number of declarationsuniquenumber - total unique declarationspropertiesobject - 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-sizeandfontshorthand declarationsgetAllFontFamilies()function - returns an array of font families from bothfont-familyandfontshorthand declarationsimportantarray (optional) -!importantdeclaration objects withpropertyandvalue
totalnumber - total number of media queriesuniquenumber - total unique media queriesvaluesarray - array of values for each media querycontentsarray - array of media query blocks with full stats object for each
See the /test/results folder for example JSON results.
var cssstats = require('cssstats')
var stats = cssstats(css)Generate a specificity graph
var specificityGraph = stats.selectors.getSpecificityGraph()var sortedSelectors = stats.selectors.getSortedSpecificity()var uniqueColorsCount = stats.declarations.getUniquePropertyCount('color')var displayNoneCount = stats.declarations.getPropertyValueCount('display', 'none')MIT
- 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