8889841cREADME.md000066600000001446150513525530006040 0ustar00# [postcss][postcss]-colormin > Minify colors in your CSS files with PostCSS. ## Install With [npm](https://npmjs.org/package/postcss-colormin) do: ``` npm install postcss-colormin --save ``` ## Example ```js var postcss = require('postcss') var colormin = require('postcss-colormin'); var css = 'h1 {color: rgba(255, 0, 0, 1)}'; console.log(postcss(colormin()).process(css).css); // => 'h1 {color:red}' ``` For more examples see the [tests](src/__tests__/index.js). ## Usage See the [PostCSS documentation](https://github.com/postcss/postcss#usage) for examples for your environment. ## Contributors See [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md). ## License MIT © [Ben Briggs](http://beneb.info) [postcss]: https://github.com/postcss/postcss src/index.js000066600000010133150513525530007006 0ustar00'use strict'; const browserslist = require('browserslist'); const { isSupported } = require('caniuse-api'); const valueParser = require('postcss-value-parser'); const minifyColor = require('./minifyColor'); /** * @param {{nodes: valueParser.Node[]}} parent * @param {(node: valueParser.Node, index: number, parent: {nodes: valueParser.Node[]}) => false | undefined} callback * @return {void} */ function walk(parent, callback) { parent.nodes.forEach((node, index) => { const bubble = callback(node, index, parent); if (node.type === 'function' && bubble !== false) { walk(node, callback); } }); } /* * IE 8 & 9 do not properly handle clicks on elements * with a `transparent` `background-color`. * * https://developer.mozilla.org/en-US/docs/Web/Events/click#Internet_Explorer */ const browsersWithTransparentBug = new Set(['ie 8', 'ie 9']); const mathFunctions = new Set(['calc', 'min', 'max', 'clamp']); /** * @param {valueParser.Node} node * @return {boolean} */ function isMathFunctionNode(node) { if (node.type !== 'function') { return false; } return mathFunctions.has(node.value.toLowerCase()); } /** * @param {string} value * @param {Record} options * @return {string} */ function transform(value, options) { const parsed = valueParser(value); walk(parsed, (node, index, parent) => { if (node.type === 'function') { if (/^(rgb|hsl)a?$/i.test(node.value)) { const { value: originalValue } = node; node.value = minifyColor(valueParser.stringify(node), options); /** @type {string} */ (node.type) = 'word'; const next = parent.nodes[index + 1]; if ( node.value !== originalValue && next && (next.type === 'word' || next.type === 'function') ) { parent.nodes.splice( index + 1, 0, /** @type {valueParser.SpaceNode} */ ({ type: 'space', value: ' ', }) ); } } else if (isMathFunctionNode(node)) { return false; } } else if (node.type === 'word') { node.value = minifyColor(node.value, options); } }); return parsed.toString(); } /** * @param {Record} options * @param {string[]} browsers * @return {Record} */ function addPluginDefaults(options, browsers) { const defaults = { // Does the browser support 4 & 8 character hex notation transparent: browsers.some((b) => browsersWithTransparentBug.has(b)) === false, // Does the browser support "transparent" value properly alphaHex: isSupported('css-rrggbbaa', browsers), name: true, }; return { ...defaults, ...options }; } /** * @type {import('postcss').PluginCreator>} * @param {Record} config * @return {import('postcss').Plugin} */ function pluginCreator(config = {}) { return { postcssPlugin: 'postcss-colormin', prepare(result) { /** @type {typeof result.opts & browserslist.Options} */ const resultOptions = result.opts || {}; const browsers = browserslist(null, { stats: resultOptions.stats, path: __dirname, env: resultOptions.env, }); const cache = new Map(); const options = addPluginDefaults(config, browsers); return { OnceExit(css) { css.walkDecls((decl) => { if ( /^(composes|font|src$|filter|-webkit-tap-highlight-color)/i.test( decl.prop ) ) { return; } const value = decl.value; if (!value) { return; } const cacheKey = JSON.stringify({ value, options, browsers }); if (cache.has(cacheKey)) { decl.value = cache.get(cacheKey); return; } const newValue = transform(value, options); decl.value = newValue; cache.set(cacheKey, newValue); }); }, }; }, }; } pluginCreator.postcss = true; module.exports = pluginCreator; src/minifyColor.js000066600000001570150513525530010176 0ustar00'use strict'; const { colord, extend } = require('colord'); const namesPlugin = require('colord/plugins/names'); const minifierPlugin = require('colord/plugins/minify'); extend(/** @type {any[]} */ ([namesPlugin, minifierPlugin])); /** * Performs color value minification * * @param {string} input - CSS value * @param {Record} options object with colord.minify() options * @return {string} */ module.exports = function minifyColor(input, options = {}) { const instance = colord(input); if (instance.isValid()) { // Try to shorten the string if it is a valid CSS color value const minified = instance.minify(options); // Fall back to the original input if it's smaller or has equal length return minified.length < input.length ? minified : input.toLowerCase(); } else { // Possibly malformed, so pass through return input; } }; types/index.d.ts000066600000000516150513525530007623 0ustar00export = pluginCreator; /** * @type {import('postcss').PluginCreator>} * @param {Record} config * @return {import('postcss').Plugin} */ declare function pluginCreator(config?: Record): import('postcss').Plugin; declare namespace pluginCreator { const postcss: true; } types/minifyColor.d.ts000066600000000150150513525530011000 0ustar00declare function _exports(input: string, options?: Record): string; export = _exports; LICENSE-MIT000066600000002104150513525530006205 0ustar00Copyright (c) Ben Briggs (http://beneb.info) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package.json000066600000001751150513525530007046 0ustar00{ "name": "postcss-colormin", "version": "5.3.1", "description": "Minify colors in your CSS files with PostCSS.", "main": "src/index.js", "types": "types/index.d.ts", "files": [ "src", "LICENSE-MIT", "types" ], "keywords": [ "color", "colors", "compression", "css", "minify", "postcss", "postcss-plugin" ], "license": "MIT", "homepage": "https://github.com/cssnano/cssnano", "author": { "name": "Ben Briggs", "email": "beneb.info@gmail.com", "url": "http://beneb.info" }, "repository": "cssnano/cssnano", "dependencies": { "browserslist": "^4.21.4", "caniuse-api": "^3.0.0", "colord": "^2.9.1", "postcss-value-parser": "^4.2.0" }, "bugs": { "url": "https://github.com/cssnano/cssnano/issues" }, "engines": { "node": "^10 || ^12 || >=14.0" }, "devDependencies": { "@types/caniuse-api": "^3.0.2", "postcss": "^8.2.15" }, "peerDependencies": { "postcss": "^8.2.15" } }