Socket
Socket
Sign inDemoInstall

postcss-reduce-idents

Package Overview
Dependencies
12
Maintainers
2
Versions
48
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.3.1 to 2.4.0

dist/lib/cache.js

5

CHANGELOG.md

@@ -0,1 +1,6 @@

# 2.4.0
* Adds support for reducing `grid` identifiers
(thanks to @sylvainpolletvillard).
# 2.3.1

@@ -2,0 +7,0 @@

184

dist/index.js

@@ -5,6 +5,2 @@ 'use strict';

var _postcssValueParser = require('postcss-value-parser');
var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
var _postcss = require('postcss');

@@ -18,169 +14,51 @@

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _counter = require('./lib/counter');
function isNum(node) {
return (0, _postcssValueParser.unit)(node.value);
}
var _counter2 = _interopRequireDefault(_counter);
function transformAtRule(_ref) {
var cache = _ref.cache;
var ruleCache = _ref.ruleCache;
var declCache = _ref.declCache;
var _counterStyle = require('./lib/counter-style');
// Iterate each property and change their names
declCache.forEach(function (decl) {
decl.value = (0, _postcssValueParser2.default)(decl.value).walk(function (node) {
if (node.type === 'word' && node.value in cache) {
cache[node.value].count++;
node.value = cache[node.value].ident;
} else if (node.type === 'space') {
node.value = ' ';
} else if (node.type === 'div') {
node.before = node.after = '';
}
}).toString();
});
// Ensure that at rules with no references to them are left unchanged
ruleCache.forEach(function (rule) {
Object.keys(cache).forEach(function (key) {
var cached = cache[key];
if (cached.ident === rule.params && !cached.count) {
rule.params = key;
}
});
});
}
var _counterStyle2 = _interopRequireDefault(_counterStyle);
function transformDecl(_ref2) {
var cache = _ref2.cache;
var declOneCache = _ref2.declOneCache;
var declTwoCache = _ref2.declTwoCache;
var _keyframes = require('./lib/keyframes');
declTwoCache.forEach(function (decl) {
decl.value = (0, _postcssValueParser2.default)(decl.value).walk(function (node) {
var type = node.type;
var value = node.value;
var _keyframes2 = _interopRequireDefault(_keyframes);
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 (type === 'space') {
node.value = ' ';
}
return false;
}).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 cached = cache[key];
if (cached.ident === node.value && !cached.count) {
node.value = key;
}
});
}
}).toString();
});
}
var _gridTemplate = require('./lib/grid-template');
function addToCache(value, encoder, cache) {
if (cache[value]) {
return;
}
cache[value] = {
ident: encoder(value, Object.keys(cache).length),
count: 0
};
}
var _gridTemplate2 = _interopRequireDefault(_gridTemplate);
function cacheAtRule(node, encoder, _ref3) {
var cache = _ref3.cache;
var ruleCache = _ref3.ruleCache;
var params = node.params;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
addToCache(params, encoder, cache);
node.params = cache[params].ident;
ruleCache.push(node);
}
exports.default = _postcss2.default.plugin('postcss-reduce-idents', function () {
var _ref4 = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$counter = _ref.counter,
counter = _ref$counter === undefined ? true : _ref$counter,
_ref$counterStyle = _ref.counterStyle,
counterStyle = _ref$counterStyle === undefined ? true : _ref$counterStyle,
_ref$keyframes = _ref.keyframes,
keyframes = _ref$keyframes === undefined ? true : _ref$keyframes,
_ref$gridTemplate = _ref.gridTemplate,
gridTemplate = _ref$gridTemplate === undefined ? true : _ref$gridTemplate,
_ref$encoder = _ref.encoder,
encoder = _ref$encoder === undefined ? _encode2.default : _ref$encoder;
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;
var reducers = [];
counter && reducers.push(_counter2.default);
counterStyle && reducers.push(_counterStyle2.default);
keyframes && reducers.push(_keyframes2.default);
gridTemplate && reducers.push(_gridTemplate2.default);
return function (css) {
// 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;
reducers.forEach(function (reducer) {
return reducer.collect(node, encoder);
});
});
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);
}
}
reducers.forEach(function (reducer) {
return reducer.transform();
});
counter && transformDecl(counterCache);
counterStyle && transformAtRule(counterStyleCache);
keyframes && transformAtRule(keyframesCache);
};
});
module.exports = exports['default'];
{
"name": "postcss-reduce-idents",
"version": "2.3.1",
"version": "2.4.0",
"description": "Reduce custom identifiers with PostCSS.",

@@ -11,3 +11,5 @@ "main": "dist/index.js",

"scripts": {
"pretest": "eslint src",
"contributorAdd": "all-contributors add",
"contributorGenerate": "all-contributors generate",
"pretest": "eslint src --fix",
"prepublish": "del-cli dist && BABEL_ENV=publish babel src --out-dir dist --ignore /__tests__/",

@@ -24,3 +26,4 @@ "test": "ava src/__tests__/*.js",

"devDependencies": {
"ava": "^0.16.0",
"all-contributors-cli": "^3.0.7",
"ava": "^0.17.0",
"babel-cli": "^6.3.17",

@@ -27,0 +30,0 @@ "babel-core": "^6.3.26",

@@ -72,3 +72,3 @@ # [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]

It works for `@keyframes`, `@counter-style` and custom `counter` values. See the
It works for `@keyframes`, `@counter-style`, custom `counter` values and grid area definitions. See the
[documentation][idents] for more information, or the [tests](test.js) for more

@@ -111,3 +111,9 @@ examples.

##### gridTemplate
Type: `boolean`
Default: `true`
Pass `false` to disable reducing `grid-template`, `grid-area` and `grid-template-areas` declarations.
##### encoder

@@ -124,8 +130,14 @@

## Contributing
## Contributors
Pull requests are welcome. If you add functionality, then please add unit tests
to cover it.
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<img src="https://avatars.githubusercontent.com/u/1282980?v=3" width="100px;"/><br /><sub>Ben Briggs</sub>](http://beneb.info)<br />[💻](https://github.com/ben-eb/postcss-reduce-idents/commits?author=ben-eb) [📖](https://github.com/ben-eb/postcss-reduce-idents/commits?author=ben-eb) 👀 [⚠️](https://github.com/ben-eb/postcss-reduce-idents/commits?author=ben-eb) | [<img src="https://avatars.githubusercontent.com/u/5635476?v=3" width="100px;"/><br /><sub>Bogdan Chadkin</sub>](https://github.com/TrySound)<br />[⚠️](https://github.com/ben-eb/postcss-reduce-idents/commits?author=TrySound) [💻](https://github.com/ben-eb/postcss-reduce-idents/commits?author=TrySound) | [<img src="https://avatars.githubusercontent.com/u/13041?v=3" width="100px;"/><br /><sub>Guillermo Rauch</sub>](http://twitter.com/rauchg)<br />[💻](https://github.com/ben-eb/postcss-reduce-idents/commits?author=rauchg) [📖](https://github.com/ben-eb/postcss-reduce-idents/commits?author=rauchg) [⚠️](https://github.com/ben-eb/postcss-reduce-idents/commits?author=rauchg) | [<img src="https://avatars.githubusercontent.com/u/566536?v=3" width="100px;"/><br /><sub>Sylvain Pollet-Villard</sub>](https://github.com/sylvainpolletvillard)<br />[💻](https://github.com/ben-eb/postcss-reduce-idents/commits?author=sylvainpolletvillard) [📖](https://github.com/ben-eb/postcss-reduce-idents/commits?author=sylvainpolletvillard) [⚠️](https://github.com/ben-eb/postcss-reduce-idents/commits?author=sylvainpolletvillard) |
| :---: | :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
## License

@@ -132,0 +144,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc