3.3.1 • Published 4 years ago
@dimanech/cssstat-core v3.3.1
cssstats 
Parses stylesheets and returns an object with statistics. This is the core module used in cssstats.com
Installation
npm i --save cssstatsUsage
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,
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
totalnumber - total number of rulessizeobjectsize.grapharray - ruleset sizes (number of declarations per rule) in source ordersize.maxnumber - maximum ruleset sizesize.averagenumber - average ruleset size
selectors object
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
declarations object
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
mediaQueries object
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.
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