Parses stylesheets and returns an object with statistics. This is the core module used in cssstats.com
npm i --save cssstats
var 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
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 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
total
number - total number of declarationsunique
number - total unique 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
- 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