postcss-colormin 2.1.7 → 2.2.2

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,23 @@
1
+ # 2.2.2
2
+
3
+ * Resolves an issue where the module would mangle the non-standard `composes`
4
+ property when consumed via css-loader.
5
+
6
+ # 2.2.1
7
+
8
+ * Resolves an issue where converting an rgb/hsl function next to another token,
9
+ such as `linear-gradient(rgb(10, 10, 10)0%, blue)` would result in a
10
+ mangled value.
11
+
12
+ # 2.2.0
13
+
14
+ * Adds support for legacy IE versions (< 10).
15
+
16
+ # 2.1.8
17
+
18
+ * Fixes incorrect minification of percentages used by `rgb` functions; i.e.
19
+ `rgb(100%,100%,100%)` was not converted correctly to `#fff`.
20
+
1
21
  # 2.1.7
2
22
 
3
23
  * Fixes another regression where `hsla(0,0%,100%,.5)` was converted to
package/README.md CHANGED
@@ -10,6 +10,7 @@ With [npm](https://npmjs.org/package/postcss-colormin) do:
10
10
  npm install postcss-colormin --save
11
11
  ```
12
12
 
13
+
13
14
  ## Example
14
15
 
15
16
  ```js
@@ -22,17 +23,36 @@ console.log(postcss(colormin()).process(css).css);
22
23
  // => 'h1 {color:red}'
23
24
  ```
24
25
 
25
- For more examples see the [tests](test.js).
26
+ For more examples see the [tests](src/__tests__/index.js).
27
+
28
+
29
+ ## API
30
+
31
+ ### colormin([options])
32
+
33
+ #### options
34
+
35
+ ##### legacy
36
+
37
+ Type: `boolean`
38
+ Default: `false`
39
+
40
+ Set this to `true` to enable IE < 10 compatibility; the browser chokes on the
41
+ `transparent` keyword, so in this mode the conversion from `rgba(0,0,0,0)`
42
+ is turned off.
43
+
26
44
 
27
45
  ## Contributing
28
46
 
29
47
  Pull requests are welcome. If you add functionality, then please add unit tests
30
48
  to cover it.
31
49
 
50
+
32
51
  ## License
33
52
 
34
53
  MIT © [Ben Briggs](http://beneb.info)
35
54
 
55
+
36
56
  [ci]: https://travis-ci.org/ben-eb/postcss-colormin
37
57
  [deps]: https://gemnasium.com/ben-eb/postcss-colormin
38
58
  [npm]: http://badge.fury.io/js/postcss-colormin
package/dist/index.js CHANGED
@@ -1,10 +1,6 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', {
4
- value: true
5
- });
6
-
7
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
3
+ exports.__esModule = true;
8
4
 
9
5
  var _postcss = require('postcss');
10
6
 
@@ -14,63 +10,69 @@ var _postcssValueParser = require('postcss-value-parser');
14
10
 
15
11
  var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
16
12
 
17
- var _libColormin = require('./lib/colormin');
18
-
19
- var _libColormin2 = _interopRequireDefault(_libColormin);
20
-
21
- function extractColorArguments(nodes) {
22
- var result = [];
23
- for (var i = 0, max = nodes.length; i < max; i += 1) {
24
- if (nodes[i].type === 'word') {
25
- var val = parseFloat(nodes[i].value);
26
- if (isNaN(val)) {
27
- return false;
28
- }
29
- result.push(val);
30
- }
31
- }
13
+ var _colormin = require('colormin');
32
14
 
33
- return result;
34
- }
15
+ var _colormin2 = _interopRequireDefault(_colormin);
35
16
 
36
- function reduceColor(decl) {
37
- decl.value = (0, _postcssValueParser2['default'])(decl.value).walk(function (node) {
38
- if (node.type === 'function') {
39
- if (/^(rgb|hsl)a?$/.test(node.value)) {
40
- var args = extractColorArguments(node.nodes);
41
- if (args) {
42
- node.value = (0, _libColormin2['default'])(node.value, args);
43
- node.type = 'word';
44
- }
45
- }
46
- } else if (node.type === 'word') {
47
- node.value = (0, _libColormin2['default'])(node.value);
48
- }
49
- }).toString();
50
- }
17
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
51
18
 
52
19
  function reduceWhitespaces(decl) {
53
- decl.value = (0, _postcssValueParser2['default'])(decl.value).walk(function (node) {
20
+ decl.value = (0, _postcssValueParser2.default)(decl.value).walk(function (node) {
54
21
  if (node.type === 'function' || node.type === 'div') {
55
22
  node.before = node.after = '';
56
23
  }
57
24
  }).toString();
58
25
  }
59
26
 
60
- function transform(decl) {
27
+ function walk(parent, callback) {
28
+ parent.nodes.forEach(function (node, index) {
29
+ var bubble = callback(node, index, parent);
30
+ if (node.nodes && bubble !== false) {
31
+ walk(node, callback);
32
+ }
33
+ });
34
+ }
35
+
36
+ function transform(decl, opts) {
61
37
  if (decl.prop === '-webkit-tap-highlight-color') {
62
38
  if (decl.value === 'inherit' || decl.value === 'transparent') {
63
39
  return;
64
40
  }
65
41
  reduceWhitespaces(decl);
66
- } else if (/^(?!font|filter)/.test(decl.prop)) {
67
- reduceColor(decl);
42
+ return;
43
+ }
44
+ if (/^(composes|font|filter)/i.test(decl.prop)) {
45
+ return;
68
46
  }
47
+ var ast = (0, _postcssValueParser2.default)(decl.value);
48
+ walk(ast, function (node, index, parent) {
49
+ if (node.type === 'function') {
50
+ if (/^(rgb|hsl)a?$/.test(node.value)) {
51
+ var value = node.value;
52
+
53
+ node.value = (0, _colormin2.default)((0, _postcssValueParser.stringify)(node), opts);
54
+ node.type = 'word';
55
+ var next = parent.nodes[index + 1];
56
+ if (node.value !== value && next && next.type === 'word') {
57
+ parent.nodes.splice(index + 1, 0, { type: 'space', value: ' ' });
58
+ }
59
+ } else if (node.value === 'calc') {
60
+ return false;
61
+ }
62
+ } else {
63
+ node.value = (0, _colormin2.default)(node.value, opts);
64
+ }
65
+ });
66
+ decl.value = ast.toString();
69
67
  }
70
68
 
71
- exports['default'] = _postcss2['default'].plugin('postcss-colormin', function () {
69
+ exports.default = _postcss2.default.plugin('postcss-colormin', function () {
70
+ var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
71
+
72
72
  return function (css) {
73
- css.walkDecls(transform);
73
+ return css.walkDecls(function (node) {
74
+ return transform(node, opts);
75
+ });
74
76
  };
75
77
  });
76
78
  module.exports = exports['default'];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-colormin",
3
- "version": "2.1.7",
3
+ "version": "2.2.2",
4
4
  "description": "Minify colors in your CSS files with PostCSS.",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -8,9 +8,10 @@
8
8
  "LICENSE-MIT"
9
9
  ],
10
10
  "scripts": {
11
- "prepublish": "babel src --out-dir dist --ignore /__tests__/",
12
- "test-unformatted": "babel-tape-runner \"src/**/__tests__/*.js\"",
13
- "test": "npm run test-unformatted | tap-spec"
11
+ "pretest": "eslint src",
12
+ "prepublish": "del-cli dist && babel src --out-dir dist --ignore /__tests__/",
13
+ "test": "ava src/__tests__",
14
+ "test-012": "ava src/__tests__"
14
15
  },
15
16
  "keywords": [
16
17
  "color",
@@ -23,10 +24,19 @@
23
24
  ],
24
25
  "license": "MIT",
25
26
  "devDependencies": {
26
- "babel": "^5.8.23",
27
- "babel-tape-runner": "^1.2.0",
28
- "tap-spec": "^4.1.0",
29
- "tape": "^4.2.0"
27
+ "ava": "^0.17.0",
28
+ "babel-cli": "^6.3.17",
29
+ "babel-core": "^6.3.26",
30
+ "babel-plugin-add-module-exports": "^0.2.0",
31
+ "babel-preset-es2015": "^6.3.13",
32
+ "babel-preset-es2015-loose": "^7.0.0",
33
+ "babel-preset-stage-0": "^6.3.13",
34
+ "babel-register": "^6.9.0",
35
+ "del-cli": "^0.2.0",
36
+ "eslint": "^3.0.0",
37
+ "eslint-config-cssnano": "^3.0.0",
38
+ "eslint-plugin-babel": "^3.3.0",
39
+ "eslint-plugin-import": "^2.0.1"
30
40
  },
31
41
  "homepage": "https://github.com/ben-eb/postcss-colormin",
32
42
  "author": {
@@ -36,9 +46,14 @@
36
46
  },
37
47
  "repository": "ben-eb/postcss-colormin",
38
48
  "dependencies": {
39
- "colr-convert": "^1.0.5",
40
- "css-color-names": "0.0.2",
41
- "postcss": "^5.0.4",
42
- "postcss-value-parser": "^3.0.2"
49
+ "colormin": "^1.0.5",
50
+ "postcss": "^5.0.13",
51
+ "postcss-value-parser": "^3.2.3"
52
+ },
53
+ "eslintConfig": {
54
+ "extends": "cssnano"
55
+ },
56
+ "ava": {
57
+ "require": "babel-register"
43
58
  }
44
59
  }
@@ -1,119 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', {
4
- value: true
5
- });
6
-
7
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
8
-
9
- var _colrConvert = require('colr-convert');
10
-
11
- var _colrConvert2 = _interopRequireDefault(_colrConvert);
12
-
13
- var _cssColorNames = require('css-color-names');
14
-
15
- var _cssColorNames2 = _interopRequireDefault(_cssColorNames);
16
-
17
- var _dropLeadingZero = require('./drop-leading-zero');
18
-
19
- var _dropLeadingZero2 = _interopRequireDefault(_dropLeadingZero);
20
-
21
- var _hex = require('./hex');
22
-
23
- var hexes = {};
24
- var round = Math.round;
25
-
26
- Object.keys(_cssColorNames2['default']).forEach(function (keyword) {
27
- return hexes[_cssColorNames2['default'][keyword]] = keyword;
28
- });
29
-
30
- var shorter = function shorter(a, b) {
31
- return a && a.length < b.length ? a : b;
32
- };
33
-
34
- function clamp(max) {
35
- return function (value) {
36
- return Math.max(0, Math.min(value, max));
37
- };
38
- }
39
-
40
- function clampRgb(value) {
41
- return clamp(255)(value);
42
- }
43
-
44
- function clampHsl(value, index) {
45
- if (!index) {
46
- return clamp(360)(value);
47
- }
48
- return index < 4 ? clamp(100)(value) : value;
49
- }
50
-
51
- exports['default'] = function (name, args) {
52
- var word = name.toLowerCase();
53
- if (word === 'rgb' || word === 'rgba' || word === 'hsl' || word === 'hsla') {
54
- if (!args) {
55
- return name;
56
- }
57
- if (word[3] === 'a') {
58
- if (args[1] === 0 && args[2] === 0 && args[3] === 0) {
59
- if (word === 'hsla' || args[0] === 0) {
60
- return 'transparent';
61
- }
62
- }
63
- if (args[3] === 1) {
64
- word = word.slice(0, 3);
65
- } else {
66
- args[3] = (0, _dropLeadingZero2['default'])(args[3]);
67
- }
68
- }
69
- if (word === 'hsl') {
70
- word = 'rgb';
71
- args = _colrConvert2['default'].hsl.rgb(args.map(clampHsl));
72
- args[0] = round(args[0]);
73
- args[1] = round(args[1]);
74
- args[2] = round(args[2]);
75
- }
76
- if (word === 'rgb') {
77
- word = _colrConvert2['default'].rgb.hex(args.map(clampRgb));
78
- } else {
79
- var rgba = undefined,
80
- hsla = undefined;
81
- // alpha conversion
82
- if (word === 'rgba') {
83
- rgba = args.slice(0, 3).map(clampRgb).concat(args[3]);
84
- hsla = _colrConvert2['default'].rgb.hsl(args);
85
- hsla[0] = round(hsla[0]);
86
- hsla[1] = round(hsla[1]);
87
- hsla[2] = round(hsla[2]);
88
- hsla.push(args[3]);
89
- } else {
90
- hsla = args.map(clampHsl);
91
- rgba = _colrConvert2['default'].hsl.rgb(hsla);
92
- rgba[0] = round(rgba[0]);
93
- rgba[1] = round(rgba[1]);
94
- rgba[2] = round(rgba[2]);
95
- rgba.push(args[3]);
96
- hsla[3] = args[3];
97
- }
98
- hsla[1] = hsla[1] + '%';
99
- hsla[2] = hsla[2] + '%';
100
- return shorter('hsla(' + hsla.join() + ')', 'rgba(' + rgba.join() + ')');
101
- }
102
- }
103
-
104
- if ((0, _hex.isHex)(word)) {
105
- word = (0, _hex.toLonghand)(word);
106
- if (word in hexes) {
107
- return shorter(hexes[word], (0, _hex.toShorthand)(word));
108
- }
109
- return (0, _hex.toShorthand)(word);
110
- }
111
-
112
- if (word in _cssColorNames2['default']) {
113
- return shorter((0, _hex.toShorthand)(_cssColorNames2['default'][word]), word);
114
- }
115
-
116
- return name;
117
- };
118
-
119
- module.exports = exports['default'];
@@ -1,23 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', {
4
- value: true
5
- });
6
-
7
- exports['default'] = function (number) {
8
- var value = String(number);
9
-
10
- if (number % 1) {
11
- if (value[0] === '0') {
12
- return value.slice(1);
13
- }
14
-
15
- if (value[0] === '-' && value[1] === '0') {
16
- return '-' + value.slice(2);
17
- }
18
- }
19
-
20
- return value;
21
- };
22
-
23
- module.exports = exports['default'];
package/dist/lib/hex.js DELETED
@@ -1,39 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', {
4
- value: true
5
- });
6
- exports.toShorthand = toShorthand;
7
- exports.toLonghand = toLonghand;
8
- exports.isHex = isHex;
9
-
10
- function toShorthand(hex) {
11
- if (hex.length === 7 && hex[1] === hex[2] && hex[3] === hex[4] && hex[5] === hex[6]) {
12
- return '#' + hex[2] + hex[4] + hex[6];
13
- }
14
- return hex;
15
- }
16
-
17
- ;
18
-
19
- function toLonghand(hex) {
20
- if (hex.length !== 4) {
21
- return hex;
22
- }
23
-
24
- var h = hex.substring(1);
25
- var r = h[0];
26
- var g = h[1];
27
- var b = h[2];
28
- return '#' + r + r + g + g + b + b;
29
- }
30
-
31
- ;
32
-
33
- function isHex(hex) {
34
- if (hex[0] === '#') {
35
- var c = toLonghand(hex).substring(1);
36
- return c.length === 6 && !isNaN(parseInt(c, 16));
37
- }
38
- return false;
39
- }