postcss-colormin 5.0.0-rc.2 → 5.2.0

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
@@ -7,13 +7,15 @@ exports.default = void 0;
7
7
 
8
8
  var _browserslist = _interopRequireDefault(require("browserslist"));
9
9
 
10
+ var _caniuseApi = require("caniuse-api");
11
+
10
12
  var _postcssValueParser = _interopRequireWildcard(require("postcss-value-parser"));
11
13
 
12
- var _colours = _interopRequireDefault(require("./colours"));
14
+ var _minifyColor = _interopRequireDefault(require("./minifyColor"));
13
15
 
14
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
16
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
15
17
 
16
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
18
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17
19
 
18
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
21
 
@@ -46,7 +48,7 @@ function isMathFunctionNode(node) {
46
48
  return ['calc', 'min', 'max', 'clamp'].includes(node.value.toLowerCase());
47
49
  }
48
50
 
49
- function transform(value, isLegacy, colorminCache) {
51
+ function transform(value, options) {
50
52
  const parsed = (0, _postcssValueParser.default)(value);
51
53
  walk(parsed, (node, index, parent) => {
52
54
  if (node.type === 'function') {
@@ -54,7 +56,7 @@ function transform(value, isLegacy, colorminCache) {
54
56
  const {
55
57
  value: originalValue
56
58
  } = node;
57
- node.value = (0, _colours.default)((0, _postcssValueParser.stringify)(node), isLegacy, colorminCache);
59
+ node.value = (0, _minifyColor.default)((0, _postcssValueParser.stringify)(node), options);
58
60
  node.type = 'word';
59
61
  const next = parent.nodes[index + 1];
60
62
 
@@ -68,7 +70,7 @@ function transform(value, isLegacy, colorminCache) {
68
70
  return false;
69
71
  }
70
72
  } else if (node.type === 'word') {
71
- node.value = (0, _colours.default)(node.value, isLegacy, colorminCache);
73
+ node.value = (0, _minifyColor.default)(node.value, options);
72
74
  }
73
75
  });
74
76
  return parsed.toString();
@@ -85,8 +87,10 @@ function pluginCreator() {
85
87
  path: __dirname,
86
88
  env: resultOpts.env
87
89
  });
88
- const isLegacy = browsers.some(hasTransparentBug);
89
- const colorminCache = {};
90
+ const options = {
91
+ supportsTransparent: browsers.some(hasTransparentBug) === false,
92
+ supportsAlphaHex: (0, _caniuseApi.isSupported)('css-rrggbbaa', browsers)
93
+ };
90
94
  const cache = {};
91
95
  return {
92
96
  OnceExit(css) {
@@ -101,14 +105,18 @@ function pluginCreator() {
101
105
  return;
102
106
  }
103
107
 
104
- const cacheKey = `${decl.prop}|${decl.value}`;
108
+ const cacheKey = JSON.stringify({
109
+ value,
110
+ options,
111
+ browsers
112
+ });
105
113
 
106
114
  if (cache[cacheKey]) {
107
115
  decl.value = cache[cacheKey];
108
116
  return;
109
117
  }
110
118
 
111
- const newValue = transform(value, isLegacy, colorminCache);
119
+ const newValue = transform(value, options);
112
120
  decl.value = newValue;
113
121
  cache[cacheKey] = newValue;
114
122
  });
@@ -0,0 +1,101 @@
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]);
@@ -0,0 +1,25 @@
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;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = minifyColor;
7
+
8
+ var _color = require("./lib/color");
9
+
10
+ var _getShortestString = _interopRequireDefault(require("./lib/getShortestString"));
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ /**
15
+ * Performs color value minification
16
+ *
17
+ * @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
20
+ */
21
+ function minifyColor(input, options = {}) {
22
+ const settings = {
23
+ supportsAlphaHex: false,
24
+ supportsTransparent: true,
25
+ ...options
26
+ };
27
+ const instance = (0, _color.process)(input);
28
+
29
+ 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)]);
33
+ } else {
34
+ // Possibly malformed, so pass through
35
+ return input;
36
+ }
37
+ }
38
+
39
+ module.exports = exports.default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-colormin",
3
- "version": "5.0.0-rc.2",
3
+ "version": "5.2.0",
4
4
  "description": "Minify colors in your CSS files with PostCSS.",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -9,7 +9,7 @@
9
9
  ],
10
10
  "scripts": {
11
11
  "prebuild": "del-cli dist",
12
- "build": "cross-env BABEL_ENV=publish babel src --config-file ../../babel.config.js --out-dir dist --ignore \"**/__tests__/\" && babel-node script/generate",
12
+ "build": "cross-env BABEL_ENV=publish babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\"",
13
13
  "prepublish": "yarn build"
14
14
  },
15
15
  "keywords": [
@@ -30,8 +30,9 @@
30
30
  },
31
31
  "repository": "cssnano/cssnano",
32
32
  "dependencies": {
33
- "browserslist": "^4.16.0",
34
- "color": "^3.1.1",
33
+ "browserslist": "^4.16.6",
34
+ "caniuse-api": "^3.0.0",
35
+ "colord": "^2.0.1",
35
36
  "postcss-value-parser": "^4.1.0"
36
37
  },
37
38
  "bugs": {
@@ -41,10 +42,10 @@
41
42
  "node": "^10 || ^12 || >=14.0"
42
43
  },
43
44
  "devDependencies": {
44
- "postcss": "^8.2.1"
45
+ "postcss": "^8.2.15"
45
46
  },
46
47
  "peerDependencies": {
47
- "postcss": "^8.2.1"
48
+ "postcss": "^8.2.15"
48
49
  },
49
- "gitHead": "5ba19ca54892f76ba6b2b698b4d88adcabd4451b"
50
+ "gitHead": "9b3c54fd94f3e2bdb503d1e21f171d7fe02f33ca"
50
51
  }
package/CHANGELOG.md DELETED
@@ -1,62 +0,0 @@
1
- # Change Log
2
-
3
- All notable changes to this project will be documented in this file.
4
- See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
-
6
- # [5.0.0-rc.2](https://github.com/cssnano/cssnano/compare/postcss-colormin@5.0.0-rc.1...postcss-colormin@5.0.0-rc.2) (2021-03-15)
7
-
8
- **Note:** Version bump only for package postcss-colormin
9
-
10
-
11
-
12
-
13
-
14
- # [5.0.0-rc.1](https://github.com/cssnano/cssnano/compare/postcss-colormin@5.0.0-rc.0...postcss-colormin@5.0.0-rc.1) (2021-03-04)
15
-
16
- **Note:** Version bump only for package postcss-colormin
17
-
18
-
19
-
20
-
21
-
22
- # 5.0.0-rc.0 (2021-02-19)
23
-
24
-
25
- ### Bug Fixes
26
-
27
- * **postcss-colormin:** fixed plugin running error ([#856](https://github.com/cssnano/cssnano/issues/856)) ([eb37dd5](https://github.com/cssnano/cssnano/commit/eb37dd570a916ce7d6080a782a24d951082c5497))
28
-
29
-
30
- ### chore
31
-
32
- * minimum require version of node is 10.13 ([#871](https://github.com/cssnano/cssnano/issues/871)) ([28bda24](https://github.com/cssnano/cssnano/commit/28bda243e32ce3ba89b3c358a5f78727b3732f11))
33
-
34
-
35
- ### Features
36
-
37
- * migarete to PostCSS 8 ([#975](https://github.com/cssnano/cssnano/issues/975)) ([40b82dc](https://github.com/cssnano/cssnano/commit/40b82dca7f53ac02cd4fe62846dec79b898ccb49))
38
-
39
-
40
- ### BREAKING CHANGES
41
-
42
- * minimum supported `postcss` version is `8.2.1`
43
- * minimum require version of node is 10.13
44
-
45
-
46
-
47
- ## 4.1.9 (2019-02-12)
48
-
49
-
50
- ### Performance Improvements
51
-
52
- * **postcss-colormin:** increase ([#682](https://github.com/cssnano/cssnano/issues/682)) ([73e021e](https://github.com/cssnano/cssnano/commit/73e021e0fd02ab44c8726ae0719c5669a29bc8dc))
53
- * **postcss-colormin:** increase perf ([#696](https://github.com/cssnano/cssnano/issues/696)) ([88c2f2e](https://github.com/cssnano/cssnano/commit/88c2f2e0c20fcd3f3be20b10501e0cec9e453aeb))
54
-
55
-
56
-
57
- ## 4.1.1 (2018-09-24)
58
-
59
-
60
- ### Bug Fixes
61
-
62
- * **postcss-merge-longhand:** not mangle border output ([#555](https://github.com/cssnano/cssnano/issues/555)) ([9a70605](https://github.com/cssnano/cssnano/commit/9a706050b621e7795a9bf74eb7110b5c81804ffe)), closes [#553](https://github.com/cssnano/cssnano/issues/553) [#554](https://github.com/cssnano/cssnano/issues/554)
package/dist/colours.js DELETED
@@ -1,74 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _color = _interopRequireDefault(require("color"));
9
-
10
- var _keywords = _interopRequireDefault(require("./keywords.json"));
11
-
12
- var _toShorthand = _interopRequireDefault(require("./lib/toShorthand"));
13
-
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- const shorter = (a, b) => (a && a.length < b.length ? a : b).toLowerCase();
17
-
18
- var _default = (colour, isLegacy = false, cache = false) => {
19
- const key = colour + '|' + isLegacy;
20
-
21
- if (cache && cache[key]) {
22
- return cache[key];
23
- }
24
-
25
- try {
26
- const parsed = (0, _color.default)(colour.toLowerCase());
27
- const alpha = parsed.alpha();
28
-
29
- if (alpha === 1) {
30
- const toHex = (0, _toShorthand.default)(parsed.hex().toLowerCase());
31
- const result = shorter(_keywords.default[toHex], toHex);
32
-
33
- if (cache) {
34
- cache[key] = result;
35
- }
36
-
37
- return result;
38
- } else {
39
- const rgb = parsed.rgb();
40
-
41
- if (!isLegacy && !rgb.color[0] && !rgb.color[1] && !rgb.color[2] && !alpha) {
42
- const result = 'transparent';
43
-
44
- if (cache) {
45
- cache[key] = result;
46
- }
47
-
48
- return result;
49
- }
50
-
51
- let hsla = parsed.hsl().string();
52
- let rgba = rgb.string();
53
- let result = hsla.length < rgba.length ? hsla : rgba;
54
-
55
- if (cache) {
56
- cache[key] = result;
57
- }
58
-
59
- return result;
60
- }
61
- } catch (e) {
62
- // Possibly malformed, so pass through
63
- const result = colour;
64
-
65
- if (cache) {
66
- cache[key] = result;
67
- }
68
-
69
- return result;
70
- }
71
- };
72
-
73
- exports.default = _default;
74
- module.exports = exports.default;
@@ -1,33 +0,0 @@
1
- {
2
- "#f0ffff": "azure",
3
- "#f5f5dc": "beige",
4
- "#ffe4c4": "bisque",
5
- "#a52a2a": "brown",
6
- "#ff7f50": "coral",
7
- "#ffd700": "gold",
8
- "#808080": "grey",
9
- "#008000": "green",
10
- "#4b0082": "indigo",
11
- "#fffff0": "ivory",
12
- "#f0e68c": "khaki",
13
- "#faf0e6": "linen",
14
- "#800000": "maroon",
15
- "#000080": "navy",
16
- "#808000": "olive",
17
- "#ffa500": "orange",
18
- "#da70d6": "orchid",
19
- "#cd853f": "peru",
20
- "#ffc0cb": "pink",
21
- "#dda0dd": "plum",
22
- "#800080": "purple",
23
- "#f00": "red",
24
- "#fa8072": "salmon",
25
- "#a0522d": "sienna",
26
- "#c0c0c0": "silver",
27
- "#fffafa": "snow",
28
- "#d2b48c": "tan",
29
- "#008080": "teal",
30
- "#ff6347": "tomato",
31
- "#ee82ee": "violet",
32
- "#f5deb3": "wheat"
33
- }
@@ -1,17 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _default = hex => {
9
- if (hex[1] === hex[2] && hex[3] === hex[4] && hex[5] === hex[6]) {
10
- return '#' + hex[2] + hex[4] + hex[6];
11
- }
12
-
13
- return hex;
14
- };
15
-
16
- exports.default = _default;
17
- module.exports = exports.default;