postcss-discard-unused 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 1.0.3
2
+
3
+ * Improved performance by reducing the amount of AST iterations.
4
+ * Converted the codebase to ES6.
5
+
1
6
  # 1.0.2
2
7
 
3
8
  * Fixes an integration issue where the module would crash on `undefined`
package/dist/index.js ADDED
@@ -0,0 +1,89 @@
1
+ 'use strict';
2
+
3
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
4
+
5
+ var _uniqs = require('uniqs');
6
+
7
+ var _uniqs2 = _interopRequireDefault(_uniqs);
8
+
9
+ var _postcss = require('postcss');
10
+
11
+ var _postcss2 = _interopRequireDefault(_postcss);
12
+
13
+ var _flatten = require('flatten');
14
+
15
+ var _flatten2 = _interopRequireDefault(_flatten);
16
+
17
+ var comma = _postcss.list.comma;
18
+ var space = _postcss.list.space;
19
+
20
+ var filterAtRule = function filterAtRule(css, properties, atrule) {
21
+ var atRules = [];
22
+ var values = [];
23
+ css.eachInside(function (node) {
24
+ if (node.type === 'decl' && properties.test(node.prop)) {
25
+ return comma(node.value).forEach(function (val) {
26
+ return values.push(space(val));
27
+ });
28
+ }
29
+ if (node.type === 'atrule') {
30
+ if (typeof atrule === 'string' && node.name === atrule) {
31
+ atRules.push(node);
32
+ } else if (atrule instanceof RegExp && atrule.test(node.name)) {
33
+ atRules.push(node);
34
+ }
35
+ }
36
+ });
37
+ values = (0, _uniqs2['default'])((0, _flatten2['default'])(values));
38
+ atRules.forEach(function (node) {
39
+ var hasAtRule = values.some(function (value) {
40
+ return value === node.params;
41
+ });
42
+ if (!hasAtRule) {
43
+ node.removeSelf();
44
+ }
45
+ });
46
+ };
47
+
48
+ var hasFont = function hasFont(fontFamily, cache) {
49
+ return comma(fontFamily).some(function (font) {
50
+ return cache.some(function (c) {
51
+ return ~c.indexOf(font);
52
+ });
53
+ });
54
+ };
55
+
56
+ module.exports = _postcss2['default'].plugin('postcss-discard-unused', function () {
57
+ return function (css) {
58
+ // fonts have slightly different logic
59
+ var atRules = [];
60
+ var values = [];
61
+ css.eachInside(function (node) {
62
+ if (node.type === 'decl' && node.parent.type === 'rule' && /font(|-family)/.test(node.prop)) {
63
+ return values.push(comma(node.value));
64
+ }
65
+ if (node.type === 'atrule' && node.name === 'font-face' && node.nodes) {
66
+ atRules.push(node);
67
+ }
68
+ });
69
+ values = (0, _uniqs2['default'])((0, _flatten2['default'])(values));
70
+ atRules.forEach(function (rule) {
71
+ var families = rule.nodes.filter(function (node) {
72
+ return node.prop === 'font-family';
73
+ });
74
+ // Discard the @font-face if it has no font-family
75
+ if (!families.length) {
76
+ return rule.removeSelf();
77
+ }
78
+ families.forEach(function (family) {
79
+ if (!hasFont(family.value, values)) {
80
+ rule.removeSelf();
81
+ }
82
+ });
83
+ });
84
+
85
+ // keyframes & counter styles
86
+ filterAtRule(css, /list-style|system/, 'counter-style');
87
+ filterAtRule(css, /animation/, /keyframes/);
88
+ };
89
+ });
package/package.json CHANGED
@@ -1,20 +1,32 @@
1
1
  {
2
2
  "name": "postcss-discard-unused",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Discard unused counter styles, keyframes and fonts.",
5
- "main": "index.js",
5
+ "main": "dist/index.js",
6
+ "files": [
7
+ "bin",
8
+ "LICENSE-MIT",
9
+ "dist"
10
+ ],
6
11
  "scripts": {
7
- "test": "tape test.js | tap-spec"
12
+ "prepublish": "babel src --out-dir dist --ignore /__tests__/",
13
+ "test-unformatted": "babel-tape-runner \"src/**/__tests__/*.js\"",
14
+ "test": "npm run test-unformatted | faucet"
8
15
  },
9
16
  "keywords": [
10
17
  "css",
18
+ "minify",
19
+ "optimise",
11
20
  "postcss",
12
- "postcss-plugin"
21
+ "postcss-plugin",
22
+ "unused"
13
23
  ],
14
24
  "license": "MIT",
15
25
  "devDependencies": {
16
- "tap-spec": "^2.2.2",
17
- "tape": "^3.5.0"
26
+ "babel": "^5.8.21",
27
+ "babel-tape-runner": "^1.2.0",
28
+ "faucet": "0.0.1",
29
+ "tape": "^4.1.0"
18
30
  },
19
31
  "homepage": "https://github.com/ben-eb/postcss-discard-unused",
20
32
  "author": {
@@ -25,7 +37,7 @@
25
37
  "repository": "ben-eb/postcss-discard-unused",
26
38
  "dependencies": {
27
39
  "flatten": "0.0.1",
28
- "postcss": "^4.1.11",
40
+ "postcss": "^4.1.16",
29
41
  "uniqs": "^2.0.0"
30
42
  }
31
43
  }
package/.npmignore DELETED
@@ -1,5 +0,0 @@
1
- .editorconfig
2
- .gitignore
3
- .jshintrc
4
- .travis.yml
5
- test.js
package/index.js DELETED
@@ -1,62 +0,0 @@
1
- 'use strict';
2
-
3
- var uniqs = require('uniqs');
4
- var postcss = require('postcss');
5
- var flatten = require('flatten');
6
- var comma = postcss.list.comma;
7
- var space = postcss.list.space;
8
-
9
- function filterAtRule (css, properties, atrule) {
10
- var cache = [];
11
- css.eachDecl(properties, function (decl) {
12
- comma(decl.value).forEach(function (val) {
13
- cache.push(space(val));
14
- });
15
- });
16
- cache = uniqs(flatten(cache));
17
- css.eachAtRule(atrule, function (rule) {
18
- var hasAtRule = cache.some(function (c) {
19
- return c === rule.params;
20
- });
21
- if (!hasAtRule) {
22
- rule.removeSelf();
23
- }
24
- });
25
- }
26
-
27
- module.exports = postcss.plugin('postcss-discard-unused', function () {
28
- return function (css) {
29
- // fonts have slightly different logic
30
- var cache = [];
31
- css.eachRule(function (rule) {
32
- rule.eachDecl(/font(|-family)/, function (decl) {
33
- cache.push(comma(decl.value));
34
- });
35
- });
36
- cache = uniqs(flatten(cache));
37
- css.eachAtRule('font-face', function (rule) {
38
- if (!rule.nodes) { return; }
39
- var fontFamilies = rule.nodes.filter(function (node) {
40
- return node.prop === 'font-family';
41
- });
42
- // Discard the @font-face if it has no font-family
43
- if (!fontFamilies.length) {
44
- return rule.removeSelf();
45
- }
46
- fontFamilies.forEach(function (family) {
47
- var hasFont = comma(family.value).some(function (font) {
48
- return cache.some(function (c) {
49
- return ~c.indexOf(font);
50
- });
51
- });
52
- if (!hasFont) {
53
- rule.removeSelf();
54
- }
55
- });
56
- });
57
-
58
- // keyframes & counter styles
59
- filterAtRule(css, /list-style|system/, 'counter-style');
60
- filterAtRule(css, /animation/, /keyframes/);
61
- };
62
- });