Parses stylesheets and returns an object with statistics
This is the core module used in http://cssstats.com
npm install cssstats
var fs = require('fs')
var cssstats = require('csstats')
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('csstats')
var css = fs.readFileSync('./styles.css', 'utf8')
postcss()
.use(cssstats())
.process(css)
.then(function (result) {
result.messages.forEach(function (message) {
console.log(message)
})
})
npm i -g cssstats
cssstats file path/to/file.css > results.json
You can also pipe CSS to cssstats:
cat some-css-file.css | cssstats
getcss google.com | cssstats
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, deaultfalse
)sortedSpecificityGraph
(boolean, deaultfalse
)repeatedSelectors
(boolean, deaultfalse
)propertyResets
(boolean, deaultfalse
)vendorPrefixedProperties
(boolean, deaultfalse
)
// 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(),
getRepeatedValues(),
getSortedSpecificity()
},
declarations: {
total: n,
important: n,
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,
important: n,
vendorPrefix: n,
properties: {
prop: [str]
}
}
}
]
}
}
The size of the file in bytes
The size of the stylesheet gzipped in bytes
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
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 numbergetRepeatedValues()
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
total
number - total number of 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
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.
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 License