postcss-reduce-idents
Advanced tools
Comparing version 2.3.0 to 2.3.1
@@ -0,1 +1,5 @@ | ||
# 2.3.1 | ||
* Performance tweaks: now performs one AST pass instead of three. | ||
# 2.3.0 | ||
@@ -2,0 +6,0 @@ |
@@ -23,21 +23,7 @@ 'use strict'; | ||
function transformAtRule(css, atRuleRegex, propRegex, encoder) { | ||
var cache = {}; | ||
var ruleCache = []; | ||
var declCache = []; | ||
// Encode at rule names and cache the result | ||
css.walk(function (node) { | ||
if (node.type === 'atrule' && atRuleRegex.test(node.name)) { | ||
if (!cache[node.params]) { | ||
cache[node.params] = { | ||
ident: encoder(node.params, Object.keys(cache).length), | ||
count: 0 | ||
}; | ||
} | ||
node.params = cache[node.params].ident; | ||
ruleCache.push(node); | ||
} else if (node.type === 'decl' && propRegex.test(node.prop)) { | ||
declCache.push(node); | ||
} | ||
}); | ||
function transformAtRule(_ref) { | ||
var cache = _ref.cache; | ||
var ruleCache = _ref.ruleCache; | ||
var declCache = _ref.declCache; | ||
// Iterate each property and change their names | ||
@@ -59,4 +45,4 @@ declCache.forEach(function (decl) { | ||
Object.keys(cache).forEach(function (key) { | ||
var k = cache[key]; | ||
if (k.ident === rule.params && !k.count) { | ||
var cached = cache[key]; | ||
if (cached.ident === rule.params && !cached.count) { | ||
rule.params = key; | ||
@@ -68,44 +54,26 @@ } | ||
function transformDecl(css, propOneRegex, propTwoRegex, encoder) { | ||
var cache = {}; | ||
var declOneCache = []; | ||
var declTwoCache = []; | ||
css.walkDecls(function (decl) { | ||
if (propOneRegex.test(decl.prop)) { | ||
decl.value = (0, _postcssValueParser2.default)(decl.value).walk(function (node) { | ||
if (node.type === 'word' && !isNum(node)) { | ||
if (!cache[node.value]) { | ||
cache[node.value] = { | ||
ident: encoder(node.value, Object.keys(cache).length), | ||
count: 0 | ||
}; | ||
} | ||
node.value = cache[node.value].ident; | ||
} else if (node.type === 'space') { | ||
node.value = ' '; | ||
} | ||
}); | ||
declOneCache.push(decl); | ||
} else if (propTwoRegex.test(decl.prop)) { | ||
declTwoCache.push(decl); | ||
} | ||
}); | ||
function transformDecl(_ref2) { | ||
var cache = _ref2.cache; | ||
var declOneCache = _ref2.declOneCache; | ||
var declTwoCache = _ref2.declTwoCache; | ||
declTwoCache.forEach(function (decl) { | ||
decl.value = (0, _postcssValueParser2.default)(decl.value).walk(function (node) { | ||
if (node.type === 'function') { | ||
if (node.value === 'counter' || node.value === 'counters') { | ||
(0, _postcssValueParser.walk)(node.nodes, function (n) { | ||
if (n.type === 'word' && n.value in cache) { | ||
cache[n.value].count++; | ||
n.value = cache[n.value].ident; | ||
} else if (n.type === 'div') { | ||
n.before = n.after = ''; | ||
} | ||
}); | ||
} | ||
return false; | ||
var type = node.type; | ||
var value = node.value; | ||
if (type === 'function' && (value === 'counter' || value === 'counters')) { | ||
(0, _postcssValueParser.walk)(node.nodes, function (child) { | ||
if (child.type === 'word' && child.value in cache) { | ||
cache[child.value].count++; | ||
child.value = cache[child.value].ident; | ||
} else if (child.type === 'div') { | ||
child.before = child.after = ''; | ||
} | ||
}); | ||
} | ||
if (node.type === 'space') { | ||
if (type === 'space') { | ||
node.value = ' '; | ||
} | ||
return false; | ||
}).toString(); | ||
@@ -117,4 +85,4 @@ }); | ||
Object.keys(cache).forEach(function (key) { | ||
var k = cache[key]; | ||
if (k.ident === node.value && !k.count) { | ||
var cached = cache[key]; | ||
if (cached.ident === node.value && !cached.count) { | ||
node.value = key; | ||
@@ -128,18 +96,94 @@ } | ||
function addToCache(value, encoder, cache) { | ||
if (cache[value]) { | ||
return; | ||
} | ||
cache[value] = { | ||
ident: encoder(value, Object.keys(cache).length), | ||
count: 0 | ||
}; | ||
} | ||
function cacheAtRule(node, encoder, _ref3) { | ||
var cache = _ref3.cache; | ||
var ruleCache = _ref3.ruleCache; | ||
var params = node.params; | ||
addToCache(params, encoder, cache); | ||
node.params = cache[params].ident; | ||
ruleCache.push(node); | ||
} | ||
exports.default = _postcss2.default.plugin('postcss-reduce-idents', function () { | ||
var opts = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var _ref4 = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var encoder = opts.encoder || _encode2.default; | ||
var _ref4$counter = _ref4.counter; | ||
var counter = _ref4$counter === undefined ? true : _ref4$counter; | ||
var _ref4$counterStyle = _ref4.counterStyle; | ||
var counterStyle = _ref4$counterStyle === undefined ? true : _ref4$counterStyle; | ||
var _ref4$encoder = _ref4.encoder; | ||
var encoder = _ref4$encoder === undefined ? _encode2.default : _ref4$encoder; | ||
var _ref4$keyframes = _ref4.keyframes; | ||
var keyframes = _ref4$keyframes === undefined ? true : _ref4$keyframes; | ||
return function (css) { | ||
if (opts.counter !== false) { | ||
transformDecl(css, /counter-(reset|increment)/, /content/, encoder); | ||
} | ||
if (opts.keyframes !== false) { | ||
transformAtRule(css, /keyframes/, /animation/, encoder); | ||
} | ||
if (opts.counterStyle !== false) { | ||
transformAtRule(css, /counter-style/, /(list-style|system)/, encoder); | ||
} | ||
// Encode at rule names and cache the result | ||
var counterCache = { | ||
cache: {}, | ||
declOneCache: [], | ||
declTwoCache: [] | ||
}; | ||
var counterStyleCache = { | ||
cache: {}, | ||
ruleCache: [], | ||
declCache: [] | ||
}; | ||
var keyframesCache = { | ||
cache: {}, | ||
ruleCache: [], | ||
declCache: [] | ||
}; | ||
css.walk(function (node) { | ||
var name = node.name; | ||
var prop = node.prop; | ||
var type = node.type; | ||
if (type === 'atrule') { | ||
if (counterStyle && /counter-style/.test(name)) { | ||
cacheAtRule(node, encoder, counterStyleCache); | ||
} | ||
if (keyframes && /keyframes/.test(name)) { | ||
cacheAtRule(node, encoder, keyframesCache); | ||
} | ||
} | ||
if (type === 'decl') { | ||
if (counter) { | ||
if (/counter-(reset|increment)/.test(prop)) { | ||
node.value = (0, _postcssValueParser2.default)(node.value).walk(function (child) { | ||
if (child.type === 'word' && !isNum(child)) { | ||
addToCache(child.value, encoder, counterCache.cache); | ||
child.value = counterCache.cache[child.value].ident; | ||
} else if (child.type === 'space') { | ||
child.value = ' '; | ||
} | ||
}); | ||
counterCache.declOneCache.push(node); | ||
} else if (/content/.test(prop)) { | ||
counterCache.declTwoCache.push(node); | ||
} | ||
} | ||
if (counterStyle && /(list-style|system)/.test(prop)) { | ||
counterStyleCache.declCache.push(node); | ||
} | ||
if (keyframes && /animation/.test(prop)) { | ||
keyframesCache.declCache.push(node); | ||
} | ||
} | ||
}); | ||
counter && transformDecl(counterCache); | ||
counterStyle && transformAtRule(counterStyleCache); | ||
keyframes && transformAtRule(keyframesCache); | ||
}; | ||
}); | ||
module.exports = exports['default']; |
{ | ||
"name": "postcss-reduce-idents", | ||
"version": "2.3.0", | ||
"version": "2.3.1", | ||
"description": "Reduce custom identifiers with PostCSS.", | ||
@@ -12,4 +12,5 @@ "main": "dist/index.js", | ||
"pretest": "eslint src", | ||
"prepublish": "de dist && BABEL_ENV=publish babel src --out-dir dist --ignore /__tests__/", | ||
"test": "ava src/__tests__/*.js" | ||
"prepublish": "del-cli dist && BABEL_ENV=publish babel src --out-dir dist --ignore /__tests__/", | ||
"test": "ava src/__tests__/*.js", | ||
"test-012": "ava src/__tests__/*.js" | ||
}, | ||
@@ -23,12 +24,15 @@ "keywords": [ | ||
"devDependencies": { | ||
"ava": "^0.11.0", | ||
"babel-cli": "^6.4.0", | ||
"babel-core": "^6.4.0", | ||
"babel-plugin-add-module-exports": "^0.1.2", | ||
"ava": "^0.16.0", | ||
"babel-cli": "^6.3.17", | ||
"babel-core": "^6.3.26", | ||
"babel-plugin-add-module-exports": "^0.2.1", | ||
"babel-preset-es2015": "^6.3.13", | ||
"babel-preset-es2015-loose": "^7.0.0", | ||
"babel-preset-stage-0": "^6.3.13", | ||
"del-cli": "^0.1.2", | ||
"eslint": "^1.10.3", | ||
"eslint-config-cssnano": "^1.0.0" | ||
"babel-register": "^6.9.0", | ||
"del-cli": "^0.2.0", | ||
"eslint": "^3.0.0", | ||
"eslint-config-cssnano": "^3.0.0", | ||
"eslint-plugin-babel": "^3.3.0", | ||
"eslint-plugin-import": "^2.0.1" | ||
}, | ||
@@ -47,3 +51,3 @@ "homepage": "https://github.com/ben-eb/postcss-reduce-idents", | ||
"ava": { | ||
"require": "babel-core/register" | ||
"require": "babel-register" | ||
}, | ||
@@ -50,0 +54,0 @@ "eslintConfig": { |
13523
181
13