forked from cssstats/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (66 loc) · 2.22 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
var _ = require('lodash')
var postcss = require('postcss')
var safeParser = require('postcss-safe-parser');
var gzipSize = require('gzip-size')
var size = require('./lib/size')
var rules = require('./lib/rules')
var selectors = require('./lib/selectors')
var declarations = require('./lib/declarations')
var mediaQueries = require('./lib/media-queries')
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) {
var stats = {}
var string = postcss().process(root).css
stats.size = size(string)
stats.gzipSize = gzipSize.sync(string)
stats.rules = rules(root, opts)
stats.selectors = selectors(root, opts)
stats.declarations = declarations(root, opts)
stats.mediaQueries = mediaQueries(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
var root = postcss().process(src, { parser: safeParser }).root
var 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')
}
}