postcss-colormin 4.0.0-rc.2 → 4.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/README.md CHANGED
@@ -34,7 +34,7 @@ examples for your environment.
34
34
 
35
35
  ## Contributors
36
36
 
37
- See [CONTRIBUTORS.md](https://github.com/ben-eb/cssnano/blob/master/CONTRIBUTORS.md).
37
+ See [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).
38
38
 
39
39
 
40
40
  ## License
package/dist/colours.js CHANGED
@@ -18,30 +18,59 @@ var _toShorthand2 = _interopRequireDefault(_toShorthand);
18
18
 
19
19
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
20
 
21
- var shorter = function shorter(a, b) {
22
- return (a && a.length < b.length ? a : b).toLowerCase();
23
- };
21
+ const shorter = (a, b) => (a && a.length < b.length ? a : b).toLowerCase();
24
22
 
25
- exports.default = function (colour) {
26
- var legacy = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
23
+ exports.default = (colour, isLegacy = false, cache = false) => {
24
+ const key = colour + "|" + isLegacy;
25
+
26
+ if (cache && cache[key]) {
27
+ return cache[key];
28
+ }
27
29
 
28
30
  try {
29
- var parsed = (0, _color2.default)(colour.toLowerCase());
30
- var alpha = parsed.alpha();
31
+ const parsed = (0, _color2.default)(colour.toLowerCase());
32
+ const alpha = parsed.alpha();
33
+
31
34
  if (alpha === 1) {
32
- var toHex = (0, _toShorthand2.default)(parsed.hex().toLowerCase());
33
- return shorter(_keywords2.default[toHex], toHex);
35
+ const toHex = (0, _toShorthand2.default)(parsed.hex().toLowerCase());
36
+ const result = shorter(_keywords2.default[toHex], toHex);
37
+
38
+ if (cache) {
39
+ cache[key] = result;
40
+ }
41
+
42
+ return result;
34
43
  } else {
35
- var rgb = parsed.rgb();
36
- if (!legacy && !rgb.color[0] && !rgb.color[1] && !rgb.color[2] && !alpha) {
37
- return 'transparent';
44
+ const rgb = parsed.rgb();
45
+
46
+ if (!isLegacy && !rgb.color[0] && !rgb.color[1] && !rgb.color[2] && !alpha) {
47
+ const result = 'transparent';
48
+
49
+ if (cache) {
50
+ cache[key] = result;
51
+ }
52
+
53
+ return result;
54
+ }
55
+
56
+ let hsla = parsed.hsl().string();
57
+ let rgba = rgb.string();
58
+ let result = hsla.length < rgba.length ? hsla : rgba;
59
+
60
+ if (cache) {
61
+ cache[key] = result;
38
62
  }
39
- var hsla = parsed.hsl().round().string();
40
- var rgba = rgb.string();
41
- return hsla.length < rgba.length ? hsla : rgba;
63
+
64
+ return result;
42
65
  }
43
66
  } catch (e) {
44
67
  // Possibly malformed, so pass through
45
- return colour;
68
+ const result = colour;
69
+
70
+ if (cache) {
71
+ cache[key] = result;
72
+ }
73
+
74
+ return result;
46
75
  }
47
76
  };
package/dist/index.js CHANGED
@@ -1,62 +1,37 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
 
7
- var _browserslist = require('browserslist');
7
+ var _browserslist = require("browserslist");
8
8
 
9
9
  var _browserslist2 = _interopRequireDefault(_browserslist);
10
10
 
11
- var _postcss = require('postcss');
11
+ var _postcss = require("postcss");
12
12
 
13
13
  var _postcss2 = _interopRequireDefault(_postcss);
14
14
 
15
- var _postcssValueParser = require('postcss-value-parser');
15
+ var _postcssValueParser = require("postcss-value-parser");
16
16
 
17
17
  var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
18
18
 
19
- var _colours = require('./colours');
19
+ var _colours = require("./colours");
20
20
 
21
21
  var _colours2 = _interopRequireDefault(_colours);
22
22
 
23
23
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
24
 
25
25
  function walk(parent, callback) {
26
- parent.nodes.forEach(function (node, index) {
27
- var bubble = callback(node, index, parent);
26
+ parent.nodes.forEach((node, index) => {
27
+ const bubble = callback(node, index, parent);
28
+
28
29
  if (node.nodes && bubble !== false) {
29
30
  walk(node, callback);
30
31
  }
31
32
  });
32
33
  }
33
34
 
34
- function transform(legacy, decl) {
35
- if (/^(composes|font|filter|-webkit-tap-highlight-color)/i.test(decl.prop)) {
36
- return;
37
- }
38
- var ast = (0, _postcssValueParser2.default)(decl.value);
39
- walk(ast, function (node, index, parent) {
40
- if (node.type === 'function') {
41
- if (/^(rgb|hsl)a?$/.test(node.value)) {
42
- var value = node.value;
43
-
44
- node.value = (0, _colours2.default)((0, _postcssValueParser.stringify)(node), legacy);
45
- node.type = 'word';
46
- var next = parent.nodes[index + 1];
47
- if (node.value !== value && next && next.type === 'word') {
48
- parent.nodes.splice(index + 1, 0, { type: 'space', value: ' ' });
49
- }
50
- } else if (node.value === 'calc') {
51
- return false;
52
- }
53
- } else if (node.type === 'word') {
54
- node.value = (0, _colours2.default)(node.value, legacy);
55
- }
56
- });
57
- decl.value = ast.toString();
58
- }
59
-
60
35
  /*
61
36
  * IE 8 & 9 do not properly handle clicks on elements
62
37
  * with a `transparent` `background-color`.
@@ -65,17 +40,62 @@ function transform(legacy, decl) {
65
40
  */
66
41
 
67
42
  function hasTransparentBug(browser) {
68
- return ~['ie 8', 'ie 9'].indexOf(browser);
43
+ return ~["ie 8", "ie 9"].indexOf(browser);
69
44
  }
70
45
 
71
- exports.default = _postcss2.default.plugin('postcss-colormin', function () {
72
- return function (css, result) {
73
- var resultOpts = result.opts || {};
74
- var browsers = (0, _browserslist2.default)(null, {
46
+ exports.default = _postcss2.default.plugin("postcss-colormin", () => {
47
+ return (css, result) => {
48
+ const resultOpts = result.opts || {};
49
+ const browsers = (0, _browserslist2.default)(null, {
75
50
  stats: resultOpts.stats,
76
51
  path: __dirname,
77
52
  env: resultOpts.env
78
53
  });
79
- css.walkDecls(transform.bind(null, browsers.some(hasTransparentBug)));
54
+ const isLegacy = browsers.some(hasTransparentBug);
55
+ const colorminCache = {};
56
+ const cache = {};
57
+
58
+ css.walkDecls(decl => {
59
+ if (/^(composes|font|filter|-webkit-tap-highlight-color)/i.test(decl.prop)) {
60
+ return;
61
+ }
62
+
63
+ if (cache[decl.value]) {
64
+ decl.value = cache[decl.value];
65
+
66
+ return;
67
+ }
68
+
69
+ const parsed = (0, _postcssValueParser2.default)(decl.value);
70
+
71
+ walk(parsed, (node, index, parent) => {
72
+ if (node.type === "function") {
73
+ if (/^(rgb|hsl)a?$/i.test(node.value)) {
74
+ const { value } = node;
75
+
76
+ node.value = (0, _colours2.default)((0, _postcssValueParser.stringify)(node), isLegacy, colorminCache);
77
+ node.type = "word";
78
+
79
+ const next = parent.nodes[index + 1];
80
+
81
+ if (node.value !== value && next && (next.type === "word" || next.type === "function")) {
82
+ parent.nodes.splice(index + 1, 0, {
83
+ type: "space",
84
+ value: " "
85
+ });
86
+ }
87
+ } else if (node.value.toLowerCase() === "calc") {
88
+ return false;
89
+ }
90
+ } else if (node.type === "word") {
91
+ node.value = (0, _colours2.default)(node.value, isLegacy, colorminCache);
92
+ }
93
+ });
94
+
95
+ const optimizedValue = parsed.toString();
96
+
97
+ decl.value = optimizedValue;
98
+ cache[decl.value] = optimizedValue;
99
+ });
80
100
  };
81
101
  });
@@ -4,9 +4,10 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
 
7
- exports.default = function (hex) {
7
+ exports.default = hex => {
8
8
  if (hex[1] === hex[2] && hex[3] === hex[4] && hex[5] === hex[6]) {
9
9
  return '#' + hex[2] + hex[4] + hex[6];
10
10
  }
11
+
11
12
  return hex;
12
13
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-colormin",
3
- "version": "4.0.0-rc.2",
3
+ "version": "4.0.3",
4
4
  "description": "Minify colors in your CSS files with PostCSS.",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -22,28 +22,28 @@
22
22
  "license": "MIT",
23
23
  "devDependencies": {
24
24
  "babel-cli": "^6.0.0",
25
- "cross-env": "^3.0.0",
25
+ "cross-env": "^5.0.0",
26
26
  "css-color-names": "0.0.4",
27
27
  "write-file": "^1.0.0"
28
28
  },
29
- "homepage": "https://github.com/ben-eb/cssnano",
29
+ "homepage": "https://github.com/cssnano/cssnano",
30
30
  "author": {
31
31
  "name": "Ben Briggs",
32
32
  "email": "beneb.info@gmail.com",
33
33
  "url": "http://beneb.info"
34
34
  },
35
- "repository": "ben-eb/cssnano",
35
+ "repository": "cssnano/cssnano",
36
36
  "dependencies": {
37
- "browserslist": "^2.0.0",
38
- "color": "^1.0.0",
37
+ "browserslist": "^4.0.0",
38
+ "color": "^3.0.0",
39
39
  "has": "^1.0.0",
40
- "postcss": "^6.0.0",
40
+ "postcss": "^7.0.0",
41
41
  "postcss-value-parser": "^3.0.0"
42
42
  },
43
43
  "bugs": {
44
- "url": "https://github.com/ben-eb/cssnano/issues"
44
+ "url": "https://github.com/cssnano/cssnano/issues"
45
45
  },
46
46
  "engines": {
47
- "node": ">=4"
47
+ "node": ">=6.9.0"
48
48
  }
49
49
  }
package/CHANGELOG.md DELETED
@@ -1,122 +0,0 @@
1
- # 4.0.0-rc.0
2
-
3
- * Breaking: Drops support for Node 0.12, we now require at least Node 4.
4
- * Breaking: Drops the `legacy` option which was used to provide support for
5
- legacy IE versions. It has been superseded by using Browserslist to supply
6
- a list of browsers; we recommend using the config file as the same values will
7
- be propagated to other cssnano plugins which have the same functionality.
8
- See https://github.com/ai/browserslist#config-file for more details.
9
- * Breaking: Update PostCSS to 6.0.0.
10
- * postcss-colormin will now no longer handle integer or whitespace compression;
11
- it's now delegated to postcss-normalize-whitespace & postcss-convert-values.
12
- We re-integrated colormin back into this module so that it would be clearer
13
- what postcss-colormin's responsibilities are; which are to facilitate
14
- conversion between identical colors.
15
-
16
- # 2.2.2
17
-
18
- * Resolves an issue where the module would mangle the non-standard `composes`
19
- property when consumed via css-loader.
20
-
21
- # 2.2.1
22
-
23
- * Resolves an issue where converting an rgb/hsl function next to another token,
24
- such as `linear-gradient(rgb(10, 10, 10)0%, blue)` would result in a
25
- mangled value.
26
-
27
- # 2.2.0
28
-
29
- * Adds support for legacy IE versions (< 10).
30
-
31
- # 2.1.8
32
-
33
- * Fixes incorrect minification of percentages used by `rgb` functions; i.e.
34
- `rgb(100%,100%,100%)` was not converted correctly to `#fff`.
35
-
36
- # 2.1.7
37
-
38
- * Fixes another regression where `hsla(0,0%,100%,.5)` was converted to
39
- `hsla(0,0%,100%,0.5)`.
40
-
41
- # 2.1.6
42
-
43
- * Fixes a regression where codes for `rgba` & `hsla` were not correctly
44
- capped at minimum/maximum values.
45
-
46
- # 2.1.5
47
-
48
- * Fixes several regressions from 2.1.3 - shorthand hex color minification,
49
- incorrect conversion to `transparent` from `rgba(255, 255, 255, 0)`
50
- (thanks to @TrySound).
51
-
52
- # 2.1.4
53
-
54
- * Fixes an error in the last patch where the `lib` directory was ignored by npm.
55
-
56
- # 2.1.3
57
-
58
- * Updates postcss-value-parser to version 3 (thanks to @TrySound).
59
- * Removes the dependency on colormin, conversion is now done in-module.
60
-
61
- # 2.1.2
62
-
63
- * Removed an unnecessary `trim` method that was used to work around a now
64
- resolved issue in PostCSS (thanks to @TrySound).
65
-
66
- # 2.1.1
67
-
68
- * Fixed a regression that was compressing space around forward slashes in
69
- calc functions.
70
-
71
- # 2.1.0
72
-
73
- * Better support for minifying colors in legacy CSS gradients, switched to
74
- postcss-value-parser (thanks to @TrySound).
75
-
76
- # 2.0.0
77
-
78
- * Upgraded to PostCSS 5.
79
-
80
- # 1.2.7
81
-
82
- * Fixes an issue where IE filter properties were being converted
83
- erroneously (thanks to @faddee).
84
-
85
- # 1.2.6
86
-
87
- * Fixed a crash when specifying `inherit` as a value
88
- to `-webkit-tap-highlight-color`.
89
-
90
- # 1.2.5
91
-
92
- * Speed up node iteration by calling `eachDecl` once rather than twice.
93
-
94
- # 1.2.4
95
-
96
- * Fixed an issue caused by upgrading colormin to use ES6.
97
-
98
- # 1.2.3
99
-
100
- * Fixed an issue where `-webkit-tap-highlight-color` was being incorrectly
101
- transformed to `transparent`. This is not supported in Safari.
102
-
103
- # 1.2.2
104
-
105
- * Fixed a bug where the module crashed on parsing comma separated values for
106
- properties such as `box-shadow`.
107
-
108
- # 1.2.1
109
-
110
- * Extracted each color logic into a function for better readability.
111
-
112
- # 1.2.0
113
-
114
- * Now uses the PostCSS `4.1` plugin API.
115
-
116
- # 1.1.0
117
-
118
- * Now supports optimisation of colors in gradient values.
119
-
120
- # 1.0.0
121
-
122
- * Initial release.