postcss-colormin 5.2.4 → 5.2.5

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/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "postcss-colormin",
3
- "version": "5.2.4",
3
+ "version": "5.2.5",
4
4
  "description": "Minify colors in your CSS files with PostCSS.",
5
- "main": "dist/index.js",
5
+ "main": "src/index.js",
6
6
  "files": [
7
- "dist",
7
+ "src",
8
8
  "LICENSE-MIT"
9
9
  ],
10
10
  "keywords": [
@@ -42,9 +42,5 @@
42
42
  "peerDependencies": {
43
43
  "postcss": "^8.2.15"
44
44
  },
45
- "scripts": {
46
- "prebuild": "rimraf dist",
47
- "build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\""
48
- },
49
45
  "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
46
  }
package/src/index.js ADDED
@@ -0,0 +1,131 @@
1
+ 'use strict';
2
+ const browserslist = require('browserslist');
3
+ const { isSupported } = require('caniuse-api');
4
+ const valueParser = require('postcss-value-parser');
5
+ const minifyColor = require('./minifyColor');
6
+
7
+ function walk(parent, callback) {
8
+ parent.nodes.forEach((node, index) => {
9
+ const bubble = callback(node, index, parent);
10
+
11
+ if (node.nodes && bubble !== false) {
12
+ walk(node, callback);
13
+ }
14
+ });
15
+ }
16
+
17
+ /*
18
+ * IE 8 & 9 do not properly handle clicks on elements
19
+ * with a `transparent` `background-color`.
20
+ *
21
+ * https://developer.mozilla.org/en-US/docs/Web/Events/click#Internet_Explorer
22
+ */
23
+ const browsersWithTransparentBug = new Set(['ie 8', 'ie 9']);
24
+ const mathFunctions = new Set(['calc', 'min', 'max', 'clamp']);
25
+
26
+ function isMathFunctionNode(node) {
27
+ if (node.type !== 'function') {
28
+ return false;
29
+ }
30
+ return mathFunctions.has(node.value.toLowerCase());
31
+ }
32
+
33
+ function transform(value, options) {
34
+ const parsed = valueParser(value);
35
+
36
+ walk(parsed, (node, index, parent) => {
37
+ if (node.type === 'function') {
38
+ if (/^(rgb|hsl)a?$/i.test(node.value)) {
39
+ const { value: originalValue } = node;
40
+
41
+ node.value = minifyColor(valueParser.stringify(node), options);
42
+ node.type = 'word';
43
+
44
+ const next = parent.nodes[index + 1];
45
+
46
+ if (
47
+ node.value !== originalValue &&
48
+ next &&
49
+ (next.type === 'word' || next.type === 'function')
50
+ ) {
51
+ parent.nodes.splice(index + 1, 0, {
52
+ type: 'space',
53
+ value: ' ',
54
+ });
55
+ }
56
+ } else if (isMathFunctionNode(node)) {
57
+ return false;
58
+ }
59
+ } else if (node.type === 'word') {
60
+ node.value = minifyColor(node.value, options);
61
+ }
62
+ });
63
+
64
+ return parsed.toString();
65
+ }
66
+
67
+ function addPluginDefaults(options, browsers) {
68
+ const defaults = {
69
+ // Does the browser support 4 & 8 character hex notation
70
+ transparent:
71
+ browsers.some((b) => browsersWithTransparentBug.has(b)) === false,
72
+ // Does the browser support "transparent" value properly
73
+ alphaHex: isSupported('css-rrggbbaa', browsers),
74
+ name: true,
75
+ };
76
+ return { ...defaults, ...options };
77
+ }
78
+
79
+ function pluginCreator(config = {}) {
80
+ return {
81
+ postcssPlugin: 'postcss-colormin',
82
+
83
+ prepare(result) {
84
+ const resultOptions = result.opts || {};
85
+ const browsers = browserslist(null, {
86
+ stats: resultOptions.stats,
87
+ path: __dirname,
88
+ env: resultOptions.env,
89
+ });
90
+
91
+ const cache = new Map();
92
+ const options = addPluginDefaults(config, browsers);
93
+
94
+ return {
95
+ OnceExit(css) {
96
+ css.walkDecls((decl) => {
97
+ if (
98
+ /^(composes|font|filter|-webkit-tap-highlight-color)/i.test(
99
+ decl.prop
100
+ )
101
+ ) {
102
+ return;
103
+ }
104
+
105
+ const value = decl.value;
106
+
107
+ if (!value) {
108
+ return;
109
+ }
110
+
111
+ const cacheKey = JSON.stringify({ value, options, browsers });
112
+
113
+ if (cache.has(cacheKey)) {
114
+ decl.value = cache.get(cacheKey);
115
+
116
+ return;
117
+ }
118
+
119
+ const newValue = transform(value, options);
120
+
121
+ decl.value = newValue;
122
+ cache.set(cacheKey, newValue);
123
+ });
124
+ },
125
+ };
126
+ },
127
+ };
128
+ }
129
+
130
+ pluginCreator.postcss = true;
131
+ module.exports = pluginCreator;
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+ const { colord, extend } = require('colord');
3
+ const namesPlugin = require('colord/plugins/names');
4
+ const minifierPlugin = require('colord/plugins/minify');
5
+
6
+ extend([namesPlugin, minifierPlugin]);
7
+
8
+ /**
9
+ * Performs color value minification
10
+ *
11
+ * @param {string} input - CSS value
12
+ * @param {boolean} options - object with colord.minify() options
13
+ */
14
+ module.exports = function minifyColor(input, options = {}) {
15
+ const instance = colord(input);
16
+
17
+ if (instance.isValid()) {
18
+ // Try to shorten the string if it is a valid CSS color value
19
+ const minified = instance.minify(options);
20
+
21
+ // Fall back to the original input if it's smaller or has equal length
22
+ return minified.length < input.length ? minified : input.toLowerCase();
23
+ } else {
24
+ // Possibly malformed, so pass through
25
+ return input;
26
+ }
27
+ };
package/dist/index.js DELETED
@@ -1,143 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _browserslist = _interopRequireDefault(require("browserslist"));
9
-
10
- var _caniuseApi = require("caniuse-api");
11
-
12
- var _postcssValueParser = _interopRequireWildcard(require("postcss-value-parser"));
13
-
14
- var _minifyColor = _interopRequireDefault(require("./minifyColor"));
15
-
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); }
17
-
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; }
19
-
20
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
-
22
- function walk(parent, callback) {
23
- parent.nodes.forEach((node, index) => {
24
- const bubble = callback(node, index, parent);
25
-
26
- if (node.nodes && bubble !== false) {
27
- walk(node, callback);
28
- }
29
- });
30
- }
31
- /*
32
- * IE 8 & 9 do not properly handle clicks on elements
33
- * with a `transparent` `background-color`.
34
- *
35
- * https://developer.mozilla.org/en-US/docs/Web/Events/click#Internet_Explorer
36
- */
37
-
38
-
39
- const browsersWithTransparentBug = new Set(['ie 8', 'ie 9']);
40
- const mathFunctions = new Set(['calc', 'min', 'max', 'clamp']);
41
-
42
- function isMathFunctionNode(node) {
43
- if (node.type !== 'function') {
44
- return false;
45
- }
46
-
47
- return mathFunctions.has(node.value.toLowerCase());
48
- }
49
-
50
- function transform(value, options) {
51
- const parsed = (0, _postcssValueParser.default)(value);
52
- walk(parsed, (node, index, parent) => {
53
- if (node.type === 'function') {
54
- if (/^(rgb|hsl)a?$/i.test(node.value)) {
55
- const {
56
- value: originalValue
57
- } = node;
58
- node.value = (0, _minifyColor.default)((0, _postcssValueParser.stringify)(node), options);
59
- node.type = 'word';
60
- const next = parent.nodes[index + 1];
61
-
62
- if (node.value !== originalValue && next && (next.type === 'word' || next.type === 'function')) {
63
- parent.nodes.splice(index + 1, 0, {
64
- type: 'space',
65
- value: ' '
66
- });
67
- }
68
- } else if (isMathFunctionNode(node)) {
69
- return false;
70
- }
71
- } else if (node.type === 'word') {
72
- node.value = (0, _minifyColor.default)(node.value, options);
73
- }
74
- });
75
- return parsed.toString();
76
- }
77
-
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 = {}) {
92
- return {
93
- postcssPlugin: 'postcss-colormin',
94
-
95
- prepare(result) {
96
- const resultOptions = result.opts || {};
97
- const browsers = (0, _browserslist.default)(null, {
98
- stats: resultOptions.stats,
99
- path: __dirname,
100
- env: resultOptions.env
101
- });
102
- const cache = new Map();
103
- const options = addPluginDefaults(config, browsers);
104
- return {
105
- OnceExit(css) {
106
- css.walkDecls(decl => {
107
- if (/^(composes|font|filter|-webkit-tap-highlight-color)/i.test(decl.prop)) {
108
- return;
109
- }
110
-
111
- const value = decl.value;
112
-
113
- if (!value) {
114
- return;
115
- }
116
-
117
- const cacheKey = JSON.stringify({
118
- value,
119
- options,
120
- browsers
121
- });
122
-
123
- if (cache.has(cacheKey)) {
124
- decl.value = cache.get(cacheKey);
125
- return;
126
- }
127
-
128
- const newValue = transform(value, options);
129
- decl.value = newValue;
130
- cache.set(cacheKey, newValue);
131
- });
132
- }
133
-
134
- };
135
- }
136
-
137
- };
138
- }
139
-
140
- pluginCreator.postcss = true;
141
- var _default = pluginCreator;
142
- exports.default = _default;
143
- module.exports = exports.default;
@@ -1,38 +0,0 @@
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;