postcss-colormin 5.2.0 → 5.2.1
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/minifyColor.js +15 -6
- package/package.json +5 -5
- package/dist/lib/color.js +0 -101
- package/dist/lib/getShortestString.js +0 -25
package/dist/minifyColor.js
CHANGED
|
@@ -5,12 +5,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = minifyColor;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _colord = require("colord");
|
|
9
9
|
|
|
10
|
-
var
|
|
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
|
*
|
|
@@ -18,18 +21,24 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
18
21
|
* @param {boolean} options.supportsAlphaHex - Does the browser support 4 & 8 character hex notation
|
|
19
22
|
* @param {boolean} options.supportsTransparent – Does the browser support "transparent" value properly
|
|
20
23
|
*/
|
|
24
|
+
|
|
21
25
|
function minifyColor(input, options = {}) {
|
|
22
26
|
const settings = {
|
|
23
27
|
supportsAlphaHex: false,
|
|
24
28
|
supportsTransparent: true,
|
|
25
29
|
...options
|
|
26
30
|
};
|
|
27
|
-
const instance = (0,
|
|
31
|
+
const instance = (0, _colord.colord)(input);
|
|
28
32
|
|
|
29
33
|
if (instance.isValid()) {
|
|
30
|
-
// Try to shorten the string if it is a valid CSS color value
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
// Try to shorten the string if it is a valid CSS color value
|
|
35
|
+
const minified = instance.minify({
|
|
36
|
+
alphaHex: settings.supportsAlphaHex,
|
|
37
|
+
transparent: settings.supportsTransparent,
|
|
38
|
+
name: true
|
|
39
|
+
}); // Fall back to the original input if it's smaller or has equal length
|
|
40
|
+
|
|
41
|
+
return minified.length < input.length ? minified : input.toLowerCase();
|
|
33
42
|
} else {
|
|
34
43
|
// Possibly malformed, so pass through
|
|
35
44
|
return input;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-colormin",
|
|
3
|
-
"version": "5.2.
|
|
3
|
+
"version": "5.2.1",
|
|
4
4
|
"description": "Minify colors in your CSS files with PostCSS.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"files": [
|
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
"LICENSE-MIT"
|
|
9
9
|
],
|
|
10
10
|
"scripts": {
|
|
11
|
-
"prebuild": "
|
|
11
|
+
"prebuild": "rimraf dist",
|
|
12
12
|
"build": "cross-env BABEL_ENV=publish babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\"",
|
|
13
|
-
"
|
|
13
|
+
"prepare": "yarn build"
|
|
14
14
|
},
|
|
15
15
|
"keywords": [
|
|
16
16
|
"color",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"browserslist": "^4.16.6",
|
|
34
34
|
"caniuse-api": "^3.0.0",
|
|
35
|
-
"colord": "^2.
|
|
35
|
+
"colord": "^2.9.1",
|
|
36
36
|
"postcss-value-parser": "^4.1.0"
|
|
37
37
|
},
|
|
38
38
|
"bugs": {
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"postcss": "^8.2.15"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "2d84646671c7075f8dae35de310351aac3436bc0"
|
|
51
51
|
}
|
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;
|