postcss-colormin 5.1.0 → 5.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/dist/index.js CHANGED
@@ -7,9 +7,11 @@ 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
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
 
@@ -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) {
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) {
54
56
  const {
55
57
  value: originalValue
56
58
  } = node;
57
- node.value = (0, _colours.default)((0, _postcssValueParser.stringify)(node), isLegacy);
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,25 +70,38 @@ function transform(value, isLegacy) {
68
70
  return false;
69
71
  }
70
72
  } else if (node.type === 'word') {
71
- node.value = (0, _colours.default)(node.value, isLegacy);
73
+ node.value = (0, _minifyColor.default)(node.value, options);
72
74
  }
73
75
  });
74
76
  return parsed.toString();
75
77
  }
76
78
 
77
- function pluginCreator() {
79
+ function addPluginDefaults(options, browsers) {
80
+ const defaults = {
81
+ transparent: browsers.some(hasTransparentBug) === false,
82
+ // Does the browser support 4 & 8 character hex notation
83
+ alphaHex: (0, _caniuseApi.isSupported)('css-rrggbbaa', browsers),
84
+ // Does the browser support "transparent" value properly
85
+ name: true
86
+ };
87
+ return { ...defaults,
88
+ ...options
89
+ };
90
+ }
91
+
92
+ function pluginCreator(config = {}) {
78
93
  return {
79
94
  postcssPlugin: 'postcss-colormin',
80
95
 
81
96
  prepare(result) {
82
- const resultOpts = result.opts || {};
97
+ const resultOptions = result.opts || {};
83
98
  const browsers = (0, _browserslist.default)(null, {
84
- stats: resultOpts.stats,
99
+ stats: resultOptions.stats,
85
100
  path: __dirname,
86
- env: resultOpts.env
101
+ env: resultOptions.env
87
102
  });
88
- const isLegacy = browsers.some(hasTransparentBug);
89
103
  const cache = {};
104
+ const options = addPluginDefaults(config, browsers);
90
105
  return {
91
106
  OnceExit(css) {
92
107
  css.walkDecls(decl => {
@@ -100,14 +115,18 @@ function pluginCreator() {
100
115
  return;
101
116
  }
102
117
 
103
- const cacheKey = value + '|' + isLegacy;
118
+ const cacheKey = JSON.stringify({
119
+ value,
120
+ options,
121
+ browsers
122
+ });
104
123
 
105
124
  if (cache[cacheKey]) {
106
125
  decl.value = cache[cacheKey];
107
126
  return;
108
127
  }
109
128
 
110
- const newValue = transform(value, isLegacy);
129
+ const newValue = transform(value, options);
111
130
  decl.value = newValue;
112
131
  cache[cacheKey] = newValue;
113
132
  });
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = minifyColor;
7
+
8
+ var _colord = require("colord");
9
+
10
+ var _names = _interopRequireDefault(require("colord/plugins/names"));
11
+
12
+ var _minify = _interopRequireDefault(require("colord/plugins/minify"));
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ (0, _colord.extend)([_names.default, _minify.default]);
17
+ /**
18
+ * Performs color value minification
19
+ *
20
+ * @param {string} input - CSS value
21
+ * @param {boolean} options - object with colord.minify() options
22
+ */
23
+
24
+ function minifyColor(input, options = {}) {
25
+ const instance = (0, _colord.colord)(input);
26
+
27
+ if (instance.isValid()) {
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();
32
+ } else {
33
+ // Possibly malformed, so pass through
34
+ return input;
35
+ }
36
+ }
37
+
38
+ module.exports = exports.default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-colormin",
3
- "version": "5.1.0",
3
+ "version": "5.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,9 @@
8
8
  "LICENSE-MIT"
9
9
  ],
10
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"
11
+ "prebuild": "rimraf dist",
12
+ "build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\"",
13
+ "prepare": "yarn build"
14
14
  },
15
15
  "keywords": [
16
16
  "color",
@@ -30,9 +30,10 @@
30
30
  },
31
31
  "repository": "cssnano/cssnano",
32
32
  "dependencies": {
33
- "browserslist": "^4.16.0",
34
- "colord": "^1.7.0",
35
- "postcss-value-parser": "^4.1.0"
33
+ "browserslist": "^4.16.6",
34
+ "caniuse-api": "^3.0.0",
35
+ "colord": "^2.9.1",
36
+ "postcss-value-parser": "^4.2.0"
36
37
  },
37
38
  "bugs": {
38
39
  "url": "https://github.com/cssnano/cssnano/issues"
@@ -45,6 +46,5 @@
45
46
  },
46
47
  "peerDependencies": {
47
48
  "postcss": "^8.2.15"
48
- },
49
- "gitHead": "28c247175032fa03f04911cde56ad82d74d211cc"
49
+ }
50
50
  }
package/CHANGELOG.md DELETED
@@ -1,81 +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.1.0](https://github.com/cssnano/cssnano/compare/postcss-colormin@5.0.0...postcss-colormin@5.1.0) (2021-05-19)
7
-
8
-
9
- ### Features
10
-
11
- * **postcss-colormin:** switch to colord and solve multiple issues ([#1107](https://github.com/cssnano/cssnano/issues/1107)) ([a7f0be4](https://github.com/cssnano/cssnano/commit/a7f0be4acc640aab89cace53a720b3d59b6f7b4f)), closes [#819](https://github.com/cssnano/cssnano/issues/819) [#1042](https://github.com/cssnano/cssnano/issues/1042) [#819](https://github.com/cssnano/cssnano/issues/819) [#771](https://github.com/cssnano/cssnano/issues/771)
12
-
13
-
14
-
15
-
16
-
17
- # [5.0.0](https://github.com/cssnano/cssnano/compare/postcss-colormin@5.0.0-rc.2...postcss-colormin@5.0.0) (2021-04-06)
18
-
19
- **Note:** Version bump only for package postcss-colormin
20
-
21
-
22
-
23
-
24
-
25
- # [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)
26
-
27
- **Note:** Version bump only for package postcss-colormin
28
-
29
-
30
-
31
-
32
-
33
- # [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)
34
-
35
- **Note:** Version bump only for package postcss-colormin
36
-
37
-
38
-
39
-
40
-
41
- # 5.0.0-rc.0 (2021-02-19)
42
-
43
-
44
- ### Bug Fixes
45
-
46
- * **postcss-colormin:** fixed plugin running error ([#856](https://github.com/cssnano/cssnano/issues/856)) ([eb37dd5](https://github.com/cssnano/cssnano/commit/eb37dd570a916ce7d6080a782a24d951082c5497))
47
-
48
-
49
- ### chore
50
-
51
- * minimum require version of node is 10.13 ([#871](https://github.com/cssnano/cssnano/issues/871)) ([28bda24](https://github.com/cssnano/cssnano/commit/28bda243e32ce3ba89b3c358a5f78727b3732f11))
52
-
53
-
54
- ### Features
55
-
56
- * migarete to PostCSS 8 ([#975](https://github.com/cssnano/cssnano/issues/975)) ([40b82dc](https://github.com/cssnano/cssnano/commit/40b82dca7f53ac02cd4fe62846dec79b898ccb49))
57
-
58
-
59
- ### BREAKING CHANGES
60
-
61
- * minimum supported `postcss` version is `8.2.1`
62
- * minimum require version of node is 10.13
63
-
64
-
65
-
66
- ## 4.1.9 (2019-02-12)
67
-
68
-
69
- ### Performance Improvements
70
-
71
- * **postcss-colormin:** increase ([#682](https://github.com/cssnano/cssnano/issues/682)) ([73e021e](https://github.com/cssnano/cssnano/commit/73e021e0fd02ab44c8726ae0719c5669a29bc8dc))
72
- * **postcss-colormin:** increase perf ([#696](https://github.com/cssnano/cssnano/issues/696)) ([88c2f2e](https://github.com/cssnano/cssnano/commit/88c2f2e0c20fcd3f3be20b10501e0cec9e453aeb))
73
-
74
-
75
-
76
- ## 4.1.1 (2018-09-24)
77
-
78
-
79
- ### Bug Fixes
80
-
81
- * **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,77 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _colord = require("colord");
9
-
10
- var _names = _interopRequireDefault(require("colord/plugins/names"));
11
-
12
- var _toShorthand = _interopRequireDefault(require("./lib/toShorthand"));
13
-
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- (0, _colord.extend)([_names.default]);
17
-
18
- var _default = (colour, isLegacy = false) => {
19
- if ((0, _colord.getFormat)(colour) === 'rgb') {
20
- /* check that it is a valid CSS value
21
- https://www.w3.org/TR/css-color-4/#rgb-functions */
22
- let percentCount = 0;
23
-
24
- for (const c of colour) {
25
- if (c === '%') {
26
- percentCount++;
27
- }
28
- } // rgb values should either be all percentages or all numbers
29
-
30
-
31
- if (percentCount !== 3 && percentCount !== 0) {
32
- return colour;
33
- }
34
- }
35
-
36
- const parsed = (0, _colord.colord)(colour.toLowerCase());
37
-
38
- if (parsed.isValid()) {
39
- const alpha = parsed.alpha();
40
-
41
- if (alpha === 1) {
42
- const toHex = (0, _toShorthand.default)(parsed.toHex());
43
- const toName = parsed.toName();
44
-
45
- if (toName && toName.length < toHex.length) {
46
- return toName;
47
- } else {
48
- return toHex;
49
- }
50
- } else {
51
- const rgb = parsed.toRgb();
52
-
53
- if (!isLegacy && !rgb.r && !rgb.g && !rgb.b && !alpha) {
54
- return 'transparent';
55
- }
56
-
57
- let hsla = parsed.toHslString();
58
- let rgba = parsed.toRgbString();
59
- const shortestConversion = hsla.length < rgba.length ? hsla : rgba;
60
- let result;
61
-
62
- if (colour.length < shortestConversion.length) {
63
- result = colour;
64
- } else {
65
- result = shortestConversion;
66
- }
67
-
68
- return result;
69
- }
70
- } else {
71
- // Possibly malformed, so pass through
72
- return colour;
73
- }
74
- };
75
-
76
- exports.default = _default;
77
- module.exports = exports.default;
@@ -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;