postcss-colormin 5.2.0 → 5.2.4

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/dist/index.js CHANGED
@@ -36,16 +36,15 @@ function walk(parent, callback) {
36
36
  */
37
37
 
38
38
 
39
- function hasTransparentBug(browser) {
40
- return ~['ie 8', 'ie 9'].indexOf(browser);
41
- }
39
+ const browsersWithTransparentBug = new Set(['ie 8', 'ie 9']);
40
+ const mathFunctions = new Set(['calc', 'min', 'max', 'clamp']);
42
41
 
43
42
  function isMathFunctionNode(node) {
44
43
  if (node.type !== 'function') {
45
44
  return false;
46
45
  }
47
46
 
48
- return ['calc', 'min', 'max', 'clamp'].includes(node.value.toLowerCase());
47
+ return mathFunctions.has(node.value.toLowerCase());
49
48
  }
50
49
 
51
50
  function transform(value, options) {
@@ -76,22 +75,32 @@ function transform(value, options) {
76
75
  return parsed.toString();
77
76
  }
78
77
 
79
- function pluginCreator() {
78
+ function addPluginDefaults(options, browsers) {
79
+ const defaults = {
80
+ // Does the browser support 4 & 8 character hex notation
81
+ transparent: browsers.some(b => browsersWithTransparentBug.has(b)) === false,
82
+ // Does the browser support "transparent" value properly
83
+ alphaHex: (0, _caniuseApi.isSupported)('css-rrggbbaa', browsers),
84
+ name: true
85
+ };
86
+ return { ...defaults,
87
+ ...options
88
+ };
89
+ }
90
+
91
+ function pluginCreator(config = {}) {
80
92
  return {
81
93
  postcssPlugin: 'postcss-colormin',
82
94
 
83
95
  prepare(result) {
84
- const resultOpts = result.opts || {};
96
+ const resultOptions = result.opts || {};
85
97
  const browsers = (0, _browserslist.default)(null, {
86
- stats: resultOpts.stats,
98
+ stats: resultOptions.stats,
87
99
  path: __dirname,
88
- env: resultOpts.env
100
+ env: resultOptions.env
89
101
  });
90
- const options = {
91
- supportsTransparent: browsers.some(hasTransparentBug) === false,
92
- supportsAlphaHex: (0, _caniuseApi.isSupported)('css-rrggbbaa', browsers)
93
- };
94
- const cache = {};
102
+ const cache = new Map();
103
+ const options = addPluginDefaults(config, browsers);
95
104
  return {
96
105
  OnceExit(css) {
97
106
  css.walkDecls(decl => {
@@ -111,14 +120,14 @@ function pluginCreator() {
111
120
  browsers
112
121
  });
113
122
 
114
- if (cache[cacheKey]) {
115
- decl.value = cache[cacheKey];
123
+ if (cache.has(cacheKey)) {
124
+ decl.value = cache.get(cacheKey);
116
125
  return;
117
126
  }
118
127
 
119
128
  const newValue = transform(value, options);
120
129
  decl.value = newValue;
121
- cache[cacheKey] = newValue;
130
+ cache.set(cacheKey, newValue);
122
131
  });
123
132
  }
124
133
 
@@ -5,31 +5,30 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = minifyColor;
7
7
 
8
- var _color = require("./lib/color");
8
+ var _colord = require("colord");
9
9
 
10
- var _getShortestString = _interopRequireDefault(require("./lib/getShortestString"));
10
+ var _names = _interopRequireDefault(require("colord/plugins/names"));
11
+
12
+ var _minify = _interopRequireDefault(require("colord/plugins/minify"));
11
13
 
12
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
15
 
16
+ (0, _colord.extend)([_names.default, _minify.default]);
14
17
  /**
15
18
  * Performs color value minification
16
19
  *
17
20
  * @param {string} input - CSS value
18
- * @param {boolean} options.supportsAlphaHex - Does the browser support 4 & 8 character hex notation
19
- * @param {boolean} options.supportsTransparent – Does the browser support "transparent" value properly
21
+ * @param {boolean} options - object with colord.minify() options
20
22
  */
23
+
21
24
  function minifyColor(input, options = {}) {
22
- const settings = {
23
- supportsAlphaHex: false,
24
- supportsTransparent: true,
25
- ...options
26
- };
27
- const instance = (0, _color.process)(input);
25
+ const instance = (0, _colord.colord)(input);
28
26
 
29
27
  if (instance.isValid()) {
30
- // Try to shorten the string if it is a valid CSS color value.
31
- // Fall back to the original input if it's smaller or has equal length/
32
- return (0, _getShortestString.default)([input.toLowerCase(), instance.toShortString(settings)]);
28
+ // Try to shorten the string if it is a valid CSS color value
29
+ const minified = instance.minify(options); // Fall back to the original input if it's smaller or has equal length
30
+
31
+ return minified.length < input.length ? minified : input.toLowerCase();
33
32
  } else {
34
33
  // Possibly malformed, so pass through
35
34
  return input;
package/package.json CHANGED
@@ -1,17 +1,12 @@
1
1
  {
2
2
  "name": "postcss-colormin",
3
- "version": "5.2.0",
3
+ "version": "5.2.4",
4
4
  "description": "Minify colors in your CSS files with PostCSS.",
5
5
  "main": "dist/index.js",
6
6
  "files": [
7
7
  "dist",
8
8
  "LICENSE-MIT"
9
9
  ],
10
- "scripts": {
11
- "prebuild": "del-cli dist",
12
- "build": "cross-env BABEL_ENV=publish babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\"",
13
- "prepublish": "yarn build"
14
- },
15
10
  "keywords": [
16
11
  "color",
17
12
  "colors",
@@ -32,8 +27,8 @@
32
27
  "dependencies": {
33
28
  "browserslist": "^4.16.6",
34
29
  "caniuse-api": "^3.0.0",
35
- "colord": "^2.0.1",
36
- "postcss-value-parser": "^4.1.0"
30
+ "colord": "^2.9.1",
31
+ "postcss-value-parser": "^4.2.0"
37
32
  },
38
33
  "bugs": {
39
34
  "url": "https://github.com/cssnano/cssnano/issues"
@@ -47,5 +42,9 @@
47
42
  "peerDependencies": {
48
43
  "postcss": "^8.2.15"
49
44
  },
50
- "gitHead": "9b3c54fd94f3e2bdb503d1e21f171d7fe02f33ca"
51
- }
45
+ "scripts": {
46
+ "prebuild": "rimraf dist",
47
+ "build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\""
48
+ },
49
+ "readme": "# [postcss][postcss]-colormin\n\n> Minify colors in your CSS files with PostCSS.\n\n## Install\n\nWith [npm](https://npmjs.org/package/postcss-colormin) do:\n\n```\nnpm install postcss-colormin --save\n```\n\n\n## Example\n\n```js\nvar postcss = require('postcss')\nvar colormin = require('postcss-colormin');\n\nvar css = 'h1 {color: rgba(255, 0, 0, 1)}';\nconsole.log(postcss(colormin()).process(css).css);\n\n// => 'h1 {color:red}'\n```\n\nFor more examples see the [tests](src/__tests__/index.js).\n\n\n## Usage\n\nSee the [PostCSS documentation](https://github.com/postcss/postcss#usage) for\nexamples for your environment.\n\n\n## Contributors\n\nSee [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).\n\n\n## License\n\nMIT © [Ben Briggs](http://beneb.info)\n\n\n[postcss]: https://github.com/postcss/postcss\n"
50
+ }
package/dist/lib/color.js DELETED
@@ -1,101 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- Object.defineProperty(exports, "process", {
7
- enumerable: true,
8
- get: function () {
9
- return _colord.colord;
10
- }
11
- });
12
- Object.defineProperty(exports, "getFormat", {
13
- enumerable: true,
14
- get: function () {
15
- return _colord.getFormat;
16
- }
17
- });
18
-
19
- var _colord = require("colord");
20
-
21
- var _names = _interopRequireDefault(require("colord/plugins/names"));
22
-
23
- var _getShortestString = _interopRequireDefault(require("./getShortestString"));
24
-
25
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
-
27
- let minifierPlugin = Colord => {
28
- /**
29
- * Shortens a color to 3 or 4 digit hexadecimal string if it's possible.
30
- * Returns the original (6 or 8 digit) hex if the it can't be shortened.
31
- */
32
- Colord.prototype.toShortHex = function ({
33
- formatAlpha
34
- }) {
35
- let hex = this.toHex();
36
- let [, r1, r2, g1, g2, b1, b2, a1, a2] = hex.split(''); // Check if the string can be shorten
37
-
38
- if (r1 === r2 && g1 === g2 && b1 === b2) {
39
- if (this.alpha() === 1) {
40
- // Express as 3 digit hexadecimal string if the color doesn't have an alpha channel
41
- return '#' + r1 + g1 + b1;
42
- } else if (formatAlpha && a1 === a2) {
43
- // Format 4 digit hex
44
- return '#' + r1 + g1 + b1 + a1;
45
- }
46
- }
47
-
48
- return hex;
49
- };
50
- /**
51
- * Returns the shortest representation of a color.
52
- */
53
-
54
-
55
- Colord.prototype.toShortString = function ({
56
- supportsTransparent,
57
- supportsAlphaHex
58
- }) {
59
- let {
60
- r,
61
- g,
62
- b
63
- } = this.toRgb();
64
- let a = this.alpha(); // RGB[A] and HSL[A] functional notations
65
-
66
- let options = [this.toRgbString(), // e.g. "rgb(128, 128, 128)" or "rgba(128, 128, 128, 0.5)"
67
- this.toHslString() // e.g. "hsl(180, 50%, 50%)" or "hsla(180, 50%, 50%, 0.5)"
68
- ]; // Hexadecimal notations
69
-
70
- if (supportsAlphaHex && a < 1) {
71
- let alphaHex = this.toShortHex({
72
- formatAlpha: true
73
- }); // e.g. "#7777" or "#80808080"
74
- // Output 4 or 8 digit hex only if the color conversion is lossless
75
-
76
- if ((0, _colord.colord)(alphaHex).alpha() === a) {
77
- options.push(alphaHex);
78
- }
79
- } else if (a === 1) {
80
- options.push(this.toShortHex({
81
- formatAlpha: false
82
- })); // e.g. "#777" or "#808080"
83
- } // CSS keyword
84
-
85
-
86
- if (supportsTransparent && r === 0 && g === 0 && b === 0 && a === 0) {
87
- options.push('transparent');
88
- } else if (a === 1) {
89
- let name = this.toName(); // e.g. "gray"
90
-
91
- if (name) {
92
- options.push(name);
93
- }
94
- } // Find the shortest option available
95
-
96
-
97
- return (0, _getShortestString.default)(options);
98
- };
99
- };
100
-
101
- (0, _colord.extend)([_names.default, minifierPlugin]);
@@ -1,25 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- /**
9
- * Returns the shortest string in array
10
- */
11
- const getShortestString = strings => {
12
- let shortest = null;
13
-
14
- for (let string of strings) {
15
- if (shortest === null || string.length < shortest.length) {
16
- shortest = string;
17
- }
18
- }
19
-
20
- return shortest;
21
- };
22
-
23
- var _default = getShortestString;
24
- exports.default = _default;
25
- module.exports = exports.default;