postcss-reduce-idents
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -0,1 +1,6 @@ | ||
# 2.2.0 | ||
* Added options for customising what the module reduces (thanks to @TrySound). | ||
* Replaced regex number test with postcss-value-parser unit method. | ||
# 2.1.0 | ||
@@ -2,0 +7,0 @@ |
128
index.js
@@ -7,18 +7,28 @@ 'use strict'; | ||
function isNum (node) { | ||
return parser.unit(node.value); | ||
} | ||
function transformAtRule (css, atRuleRegex, propRegex) { | ||
var cache = {}; | ||
var ruleCache = []; | ||
var declCache = []; | ||
// Encode at rule names and cache the result | ||
css.walkAtRules(atRuleRegex, function (rule) { | ||
if (!cache[rule.params]) { | ||
cache[rule.params] = { | ||
ident: encode(Object.keys(cache).length), | ||
count: 0 | ||
}; | ||
css.walk(function (node) { | ||
if (node.type === 'atrule' && atRuleRegex.test(node.name)) { | ||
if (!cache[node.params]) { | ||
cache[node.params] = { | ||
ident: encode(Object.keys(cache).length), | ||
count: 0 | ||
}; | ||
} | ||
node.params = cache[node.params].ident; | ||
ruleCache.push(node); | ||
} | ||
rule.params = cache[rule.params].ident; | ||
ruleCache.push(rule); | ||
if (node.type === 'decl' && propRegex.test(node.prop)) { | ||
declCache.push(node); | ||
} | ||
}); | ||
// Iterate each property and change their names | ||
css.walkDecls(propRegex, function (decl) { | ||
declCache.forEach(function (decl) { | ||
decl.value = parser(decl.value).walk(function (node) { | ||
@@ -49,9 +59,10 @@ if (node.type === 'word' && node.value in cache) { | ||
module.exports = postcss.plugin('postcss-reduce-idents', function () { | ||
return function (css) { | ||
var cache = {}; | ||
var declCache = []; | ||
css.walkDecls(/counter-(reset|increment)/, function (decl) { | ||
function transformDecl (css, propOneRegex, propTwoRegex) { | ||
var cache = {}; | ||
var declOneCache = []; | ||
var declTwoCache = []; | ||
css.walkDecls(function (decl) { | ||
if (propOneRegex.test(decl.prop)) { | ||
decl.value = parser(decl.value).walk(function (node) { | ||
if (node.type === 'word' && !/^-?\d*$/.test(node.value)) { | ||
if (node.type === 'word' && !isNum(node)) { | ||
if (!cache[node.value]) { | ||
@@ -69,42 +80,57 @@ cache[node.value] = { | ||
}); | ||
declCache.push(decl); | ||
}); | ||
css.walkDecls('content', function (decl) { | ||
decl.value = parser(decl.value).walk(function (node) { | ||
if (node.type === 'function') { | ||
if (node.value === 'counter' || node.value === 'counters') { | ||
node.nodes.forEach(function (node) { | ||
if (node.type === 'word' && node.value in cache) { | ||
cache[node.value].count++; | ||
node.value = cache[node.value].ident; | ||
} | ||
if (node.type === 'div') { | ||
node.before = ''; | ||
node.after = ''; | ||
} | ||
}); | ||
} | ||
return false; | ||
} | ||
if (node.type === 'space') { | ||
node.value = ' '; | ||
} | ||
}).toString(); | ||
}); | ||
declCache.forEach(function (decl) { | ||
decl.value = decl.value.walk(function (node) { | ||
if (node.type === 'word' && !/^-?\d*$/.test(node.value)) { | ||
Object.keys(cache).forEach(function (key) { | ||
var k = cache[key]; | ||
if (k.ident === node.value && !k.count) { | ||
node.value = key; | ||
declOneCache.push(decl); | ||
} | ||
if (propTwoRegex.test(decl.prop)) { | ||
declTwoCache.push(decl); | ||
} | ||
}); | ||
declTwoCache.forEach(function (decl) { | ||
decl.value = parser(decl.value).walk(function (node) { | ||
if (node.type === 'function') { | ||
if (node.value === 'counter' || node.value === 'counters') { | ||
node.nodes.forEach(function (node) { | ||
if (node.type === 'word' && node.value in cache) { | ||
cache[node.value].count++; | ||
node.value = cache[node.value].ident; | ||
} | ||
if (node.type === 'div') { | ||
node.before = ''; | ||
node.after = ''; | ||
} | ||
}); | ||
} | ||
}).toString(); | ||
}); | ||
// Transform @keyframes, @counter-style | ||
transformAtRule(css, /keyframes/, /animation/); | ||
transformAtRule(css, 'counter-style', /(list-style|system)/); | ||
return false; | ||
} | ||
if (node.type === 'space') { | ||
node.value = ' '; | ||
} | ||
}).toString(); | ||
}); | ||
declOneCache.forEach(function (decl) { | ||
decl.value = decl.value.walk(function (node) { | ||
if (node.type === 'word' && !isNum(node)) { | ||
Object.keys(cache).forEach(function (key) { | ||
var k = cache[key]; | ||
if (k.ident === node.value && !k.count) { | ||
node.value = key; | ||
} | ||
}); | ||
} | ||
}).toString(); | ||
}); | ||
} | ||
module.exports = postcss.plugin('postcss-reduce-idents', function (opts) { | ||
opts = opts || {}; | ||
return function (css) { | ||
if (opts.counter !== false) { | ||
transformDecl(css, /counter-(reset|increment)/, /content/); | ||
} | ||
if (opts.keyframes !== false) { | ||
transformAtRule(css, /keyframes/, /animation/); | ||
} | ||
if (opts.counterStyle !== false) { | ||
transformAtRule(css, /counter-style/, /(list-style|system)/); | ||
} | ||
}; | ||
}); |
{ | ||
"name": "postcss-reduce-idents", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Reduce custom identifiers with PostCSS.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -79,2 +79,29 @@ # [postcss][postcss]-reduce-idents [![Build Status](https://travis-ci.org/ben-eb/postcss-reduce-idents.svg?branch=master)][ci] [![NPM version](https://badge.fury.io/js/postcss-reduce-idents.svg)][npm] [![Dependency Status](https://gemnasium.com/ben-eb/postcss-reduce-idents.svg)][deps] | ||
## API | ||
### reduceIdents([options]) | ||
#### options | ||
##### counter | ||
Type: `boolean` | ||
Default: `true` | ||
Pass `false` to disable reducing `content`, `counter-reset` and `counter-increment` declarations. | ||
##### keyframes | ||
Type: `boolean` | ||
Default: `true` | ||
Pass `false` to disable reducing `keyframes` rules and `animation` declarations. | ||
##### counterStyle | ||
Type: `boolean` | ||
Default: `true` | ||
Pass `false` to disable reducing `counter-style` rules and `list-style` and `system` declarations. | ||
## Contributing | ||
@@ -81,0 +108,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10209
146
120