-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (71 loc) · 2.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const _ = require('lodash')
const postcss = require('postcss')
const safeParser = require('postcss-safe-parser')
const bytes = require('bytes')
const gzipSize = require('gzip-size')
const size = require('./lib/size')
const rules = require('./lib/rules')
const selectors = require('./lib/selectors')
const declarations = require('./lib/declarations')
const mediaQueries = require('./lib/media-queries')
const fontFaces = require('./lib/font-faces')
module.exports = function (src, opts) {
opts = opts || {}
opts = _.defaults(opts, {
safe: true,
mediaQueries: true,
importantDeclarations: false,
specificityGraph: false,
sortedSpecificityGraph: false,
repeatedSelectors: false,
propertyResets: false,
vendorPrefixedProperties: false
})
function parse (root, result) {
const stats = {}
const string = postcss().process(root).css
stats.size = size(string)
stats.gzipSize = gzipSize.sync(string)
stats.humanizedSize = bytes(stats.size, { decimalPlaces: 0 })
stats.humanizedGzipSize = bytes(stats.gzipSize, { decimalPlaces: 0 })
stats.rules = rules(root, opts)
stats.selectors = selectors(root, opts)
stats.declarations = declarations(root, opts)
stats.mediaQueries = mediaQueries(root, opts)
stats.fontFaces = fontFaces(root, opts)
// Push message to PostCSS when used as a plugin
if (result && result.messages) {
result.messages.push({
type: 'cssstats',
plugin: 'postcss-cssstats',
stats: stats
})
}
stats.toJSON = function () {
// Remove methods when using JSON.stringify
delete stats.selectors.getSpecificityGraph
delete stats.selectors.getRepeatedValues
delete stats.selectors.getSortedSpecificity
delete stats.declarations.getPropertyResets
delete stats.declarations.getUniquePropertyCount
delete stats.declarations.getPropertyValueCount
delete stats.declarations.getVendorPrefixed
delete stats.declarations.getAllFontSizes
delete stats.declarations.getAllFontFamilies
return stats
}
// Return stats for default usage
return stats
}
if (typeof src === 'string') {
// Default behavior
const root = postcss().process(src, { parser: safeParser }).root
const result = parse(root, {})
return result
} else if (typeof src === 'object' || typeof src === 'undefined') {
// Return a PostCSS plugin
return parse
} else {
throw new TypeError('cssstats expects a string or to be used as a PostCSS plugin')
}
}