New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

postcss-discard-unused

Package Overview
Dependencies
Maintainers
2
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-discard-unused - npm Package Compare versions

Comparing version 2.2.1 to 2.2.2

5

CHANGELOG.md

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

# 2.2.2
* Removed a dependency on `flatten`.
* Performance tweaks; now performs a single AST pass instead of four.
# 2.2.1

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

170

dist/index.js
'use strict';
exports.__esModule = true;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

@@ -11,8 +13,2 @@

var _postcss2 = _interopRequireDefault(_postcss);
var _flatten = require('flatten');
var _flatten2 = _interopRequireDefault(_flatten);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -23,16 +19,20 @@

function filterAtRule(css, declRegex, atruleRegex) {
var atRules = [];
var values = [];
css.walk(function (node) {
if (node.type === 'decl' && declRegex.test(node.prop)) {
return comma(node.value).forEach(function (val) {
return values.push(space(val));
});
}
if (node.type === 'atrule' && atruleRegex.test(node.name)) {
atRules.push(node);
}
});
values = (0, _uniqs2.default)((0, _flatten2.default)(values));
var atrule = 'atrule';
var decl = 'decl';
var rule = 'rule';
function addValues(cache, _ref) {
var value = _ref.value;
return comma(value).reduce(function (memo, val) {
return [].concat(memo, space(val));
}, cache);
}
function filterAtRule(_ref2) {
var atRules = _ref2.atRules;
var values = _ref2.values;
values = (0, _uniqs2.default)(values);
atRules.forEach(function (node) {

@@ -48,26 +48,7 @@ var hasAtRule = values.some(function (value) {

function hasFont(fontFamily, cache) {
return comma(fontFamily).some(function (font) {
return cache.some(function (c) {
return ~c.indexOf(font);
});
});
}
function filterNamespace(_ref3) {
var atRules = _ref3.atRules;
var rules = _ref3.rules;
function filterNamespace(css) {
var atRules = [];
var rules = [];
css.walk(function (node) {
var type = node.type;
var selector = node.selector;
var name = node.name;
if (type === 'rule' && /\|/.test(selector)) {
return rules.push(selector.split('|')[0]);
}
if (type === 'atrule' && name === 'namespace') {
atRules.push(node);
}
});
rules = (0, _uniqs2.default)((0, _flatten2.default)(rules));
rules = (0, _uniqs2.default)(rules);
atRules.forEach(function (atRule) {

@@ -82,4 +63,4 @@ var _atRule$params$split$ = atRule.params.split(' ').filter(Boolean);

}
var hasRule = rules.some(function (rule) {
return rule === param || rule === '*';
var hasRule = rules.some(function (r) {
return r === param || r === '*';
});

@@ -92,26 +73,28 @@ if (!hasRule) {

function hasFont(fontFamily, cache) {
return comma(fontFamily).some(function (font) {
return cache.some(function (c) {
return ~c.indexOf(font);
});
});
}
// fonts have slightly different logic
function filterFont(css) {
var atRules = [];
var values = [];
css.walk(function (node) {
if (node.type === 'decl' && node.parent.type === 'rule' && /font(|-family)/.test(node.prop)) {
return values.push(comma(node.value));
}
if (node.type === 'atrule' && node.name === 'font-face' && node.nodes) {
atRules.push(node);
}
});
values = (0, _uniqs2.default)((0, _flatten2.default)(values));
atRules.forEach(function (rule) {
var families = rule.nodes.filter(function (node) {
return node.prop === 'font-family';
function filterFont(_ref4) {
var atRules = _ref4.atRules;
var values = _ref4.values;
values = (0, _uniqs2.default)(values);
atRules.forEach(function (r) {
var families = r.nodes.filter(function (_ref5) {
var prop = _ref5.prop;
return prop === 'font-family';
});
// Discard the @font-face if it has no font-family
if (!families.length) {
return rule.remove();
return r.remove();
}
families.forEach(function (family) {
if (!hasFont(family.value, values)) {
rule.remove();
r.remove();
}

@@ -122,3 +105,3 @@ });

module.exports = _postcss2.default.plugin('postcss-discard-unused', function (opts) {
exports.default = (0, _postcss.plugin)('postcss-discard-unused', function (opts) {
var _fontFace$counterStyl = _extends({

@@ -137,15 +120,50 @@ fontFace: true,

return function (css) {
if (fontFace) {
filterFont(css);
}
if (counterStyle) {
filterAtRule(css, /list-style|system/, /counter-style/);
}
if (keyframes) {
filterAtRule(css, /animation/, /keyframes/);
}
if (namespace) {
filterNamespace(css);
}
var counterStyleCache = { atRules: [], values: [] };
var keyframesCache = { atRules: [], values: [] };
var namespaceCache = { atRules: [], rules: [] };
var fontCache = { atRules: [], values: [] };
css.walk(function (node) {
var type = node.type;
var prop = node.prop;
var selector = node.selector;
var name = node.name;
if (type === rule && namespace && ~selector.indexOf('|')) {
namespaceCache.rules = namespaceCache.rules.concat(selector.split('|')[0]);
return;
}
if (type === decl) {
if (counterStyle && /list-style|system/.test(prop)) {
counterStyleCache.values = addValues(counterStyleCache.values, node);
}
if (fontFace && node.parent.type === rule && /font(|-family)/.test(prop)) {
fontCache.values = fontCache.values.concat(comma(node.value));
}
if (keyframes && /animation/.test(prop)) {
keyframesCache.values = addValues(keyframesCache.values, node);
}
return;
}
if (type === atrule) {
if (counterStyle && /counter-style/.test(name)) {
counterStyleCache.atRules.push(node);
}
if (fontFace && name === 'font-face' && node.nodes) {
fontCache.atRules.push(node);
}
if (keyframes && /keyframes/.test(name)) {
keyframesCache.atRules.push(node);
}
if (namespace && name === 'namespace') {
namespaceCache.atRules.push(node);
}
return;
}
});
counterStyle && filterAtRule(counterStyleCache);
fontFace && filterFont(fontCache);
keyframes && filterAtRule(keyframesCache);
namespace && filterNamespace(namespaceCache);
};
});
});
module.exports = exports['default'];
{
"name": "postcss-discard-unused",
"version": "2.2.1",
"version": "2.2.2",
"description": "Discard unused counter styles, keyframes and fonts.",

@@ -12,5 +12,8 @@ "main": "dist/index.js",

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

@@ -27,12 +30,16 @@ "keywords": [

"devDependencies": {
"ava": "^0.11.0",
"all-contributors-cli": "^3.0.5",
"ava": "^0.16.0",
"babel-cli": "^6.4.5",
"babel-core": "^6.4.5",
"babel-plugin-add-module-exports": "^0.1.2",
"babel-plugin-add-module-exports": "^0.2.0",
"babel-preset-es2015": "^6.3.13",
"babel-preset-es2015-loose": "^7.0.0",
"babel-preset-stage-0": "^6.3.13",
"babel-register": "^6.9.0",
"del-cli": "^0.2.0",
"eslint": "^1.10.3",
"eslint-config-cssnano": "^1.0.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 +54,2 @@ "homepage": "https://github.com/ben-eb/postcss-discard-unused",

"dependencies": {
"flatten": "1.0.2",
"postcss": "^5.0.14",

@@ -53,3 +59,3 @@ "uniqs": "^2.0.0"

"ava": {
"require": "babel-core/register"
"require": "babel-register"
},

@@ -56,0 +62,0 @@ "eslintConfig": {

@@ -112,8 +112,14 @@ # [postcss][postcss]-discard-unused [![Build Status](https://travis-ci.org/ben-eb/postcss-discard-unused.svg?branch=master)][ci] [![NPM version](https://badge.fury.io/js/postcss-discard-unused.svg)][npm] [![Dependency Status](https://gemnasium.com/ben-eb/postcss-discard-unused.svg)][deps]

## 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-discard-unused/commits?author=ben-eb) [📖](https://github.com/ben-eb/postcss-discard-unused/commits?author=ben-eb) 👀 [⚠️](https://github.com/ben-eb/postcss-discard-unused/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-discard-unused/commits?author=TrySound) [📖](https://github.com/ben-eb/postcss-discard-unused/commits?author=TrySound) 👀 [⚠️](https://github.com/ben-eb/postcss-discard-unused/commits?author=TrySound) | [<img src="https://avatars.githubusercontent.com/u/770675?v=3" width="100px;"/><br /><sub>Paweł Lesiecki</sub>](https://github.com/plesiecki)<br />[💻](https://github.com/ben-eb/postcss-discard-unused/commits?author=plesiecki) [⚠️](https://github.com/ben-eb/postcss-discard-unused/commits?author=plesiecki) |
| :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the [all-contributors] specification. Contributions of
any kind welcome!
## License

@@ -123,2 +129,4 @@

[all-contributors]: https://github.com/kentcdodds/all-contributors
[ci]: https://travis-ci.org/ben-eb/postcss-discard-unused

@@ -125,0 +133,0 @@ [deps]: https://gemnasium.com/ben-eb/postcss-discard-unused

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc