Skip to content

Commit

Permalink
Add support for custom properties
Browse files Browse the repository at this point in the history
Now that custom properties are more commonly used in
CSS, we can use the postcss-custom-properties plugin
to compile to raw values that are more useful when
collecting stats data.
  • Loading branch information
johno committed Dec 8, 2020
1 parent 76b891b commit 0b25cbe
Show file tree
Hide file tree
Showing 7 changed files with 3,418 additions and 8 deletions.
12 changes: 11 additions & 1 deletion packages/cssstats/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var _ = require('lodash')
var postcss = require('postcss')
var safeParser = require('postcss-safe-parser')
var customProperties = require('postcss-custom-properties')
var bytes = require('bytes')
var gzipSize = require('gzip-size')

Expand All @@ -21,12 +22,21 @@ module.exports = function (src, opts) {
repeatedSelectors: false,
propertyResets: false,
vendorPrefixedProperties: false,
preserveCustomProperties: false
})

function parse(root, result) {
var stats = {}

var string = postcss().process(root).css
const plugins = []
if (!opts.preserveCustomProperties) {
plugins.push(customProperties({ preserve: false }))
}

const parser = postcss()
plugins.forEach(plugin => parser.use(plugin))

var string = parser.process(root).css
stats.size = size(string)
stats.gzipSize = gzipSize.sync(string)
stats.humanizedSize = bytes(stats.size, { decimalPlaces: 0 })
Expand Down
1 change: 1 addition & 0 deletions packages/cssstats/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"is-vendor-prefixed": "^3.5.0",
"lodash": "^4.17.20",
"postcss": "^8.1.4",
"postcss-custom-properties": "^10.0.0",
"postcss-safe-parser": "^5.0.2",
"specificity": "^0.4.1"
}
Expand Down
17 changes: 17 additions & 0 deletions packages/cssstats/test/custom-properties-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const test = require('ava')

const { getStatsForFixture } = require('./util')

test('handles custom properties', (t) => {
const result = getStatsForFixture('github').declarations.properties.color

t.snapshot(result)
})

test('does not handle custom properties when told not to', (t) => {
const result = getStatsForFixture('github', {
preserveCustomProperties: true,
}).declarations.properties.color

t.snapshot(result)
})
39 changes: 39 additions & 0 deletions packages/cssstats/test/fixtures/github.css

Large diffs are not rendered by default.

Loading

0 comments on commit 0b25cbe

Please sign in to comment.