postcss-colormin
Advanced tools
Comparing version 4.0.2 to 4.0.3
@@ -23,22 +23,55 @@ 'use strict'; | ||
exports.default = (colour, legacy = false) => { | ||
exports.default = (colour, isLegacy = false, cache = false) => { | ||
const key = colour + "|" + isLegacy; | ||
if (cache && cache[key]) { | ||
return cache[key]; | ||
} | ||
try { | ||
const parsed = (0, _color2.default)(colour.toLowerCase()); | ||
const alpha = parsed.alpha(); | ||
if (alpha === 1) { | ||
const toHex = (0, _toShorthand2.default)(parsed.hex().toLowerCase()); | ||
return shorter(_keywords2.default[toHex], toHex); | ||
const result = shorter(_keywords2.default[toHex], toHex); | ||
if (cache) { | ||
cache[key] = result; | ||
} | ||
return result; | ||
} else { | ||
const rgb = parsed.rgb(); | ||
if (!legacy && !rgb.color[0] && !rgb.color[1] && !rgb.color[2] && !alpha) { | ||
return 'transparent'; | ||
if (!isLegacy && !rgb.color[0] && !rgb.color[1] && !rgb.color[2] && !alpha) { | ||
const result = 'transparent'; | ||
if (cache) { | ||
cache[key] = result; | ||
} | ||
return result; | ||
} | ||
let hsla = parsed.hsl().string(); | ||
let rgba = rgb.string(); | ||
return hsla.length < rgba.length ? hsla : rgba; | ||
let result = hsla.length < rgba.length ? hsla : rgba; | ||
if (cache) { | ||
cache[key] = result; | ||
} | ||
return result; | ||
} | ||
} catch (e) { | ||
// Possibly malformed, so pass through | ||
return colour; | ||
const result = colour; | ||
if (cache) { | ||
cache[key] = result; | ||
} | ||
return result; | ||
} | ||
}; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -7,15 +7,15 @@ Object.defineProperty(exports, "__esModule", { | ||
var _browserslist = require('browserslist'); | ||
var _browserslist = require("browserslist"); | ||
var _browserslist2 = _interopRequireDefault(_browserslist); | ||
var _postcss = require('postcss'); | ||
var _postcss = require("postcss"); | ||
var _postcss2 = _interopRequireDefault(_postcss); | ||
var _postcssValueParser = require('postcss-value-parser'); | ||
var _postcssValueParser = require("postcss-value-parser"); | ||
var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser); | ||
var _colours = require('./colours'); | ||
var _colours = require("./colours"); | ||
@@ -29,2 +29,3 @@ var _colours2 = _interopRequireDefault(_colours); | ||
const bubble = callback(node, index, parent); | ||
if (node.nodes && bubble !== false) { | ||
@@ -36,27 +37,2 @@ walk(node, callback); | ||
function transform(legacy, decl) { | ||
if (/^(composes|font|filter|-webkit-tap-highlight-color)/i.test(decl.prop)) { | ||
return; | ||
} | ||
const ast = (0, _postcssValueParser2.default)(decl.value); | ||
walk(ast, (node, index, parent) => { | ||
if (node.type === 'function') { | ||
if (/^(rgb|hsl)a?$/i.test(node.value)) { | ||
const { value } = node; | ||
node.value = (0, _colours2.default)((0, _postcssValueParser.stringify)(node), legacy); | ||
node.type = 'word'; | ||
const next = parent.nodes[index + 1]; | ||
if (node.value !== value && next && (next.type === 'word' || next.type === 'function')) { | ||
parent.nodes.splice(index + 1, 0, { type: 'space', value: ' ' }); | ||
} | ||
} else if (node.value.toLowerCase() === 'calc') { | ||
return false; | ||
} | ||
} else if (node.type === 'word') { | ||
node.value = (0, _colours2.default)(node.value, legacy); | ||
} | ||
}); | ||
decl.value = ast.toString(); | ||
} | ||
/* | ||
@@ -70,6 +46,6 @@ * IE 8 & 9 do not properly handle clicks on elements | ||
function hasTransparentBug(browser) { | ||
return ~['ie 8', 'ie 9'].indexOf(browser); | ||
return ~["ie 8", "ie 9"].indexOf(browser); | ||
} | ||
exports.default = _postcss2.default.plugin('postcss-colormin', () => { | ||
exports.default = _postcss2.default.plugin("postcss-colormin", () => { | ||
return (css, result) => { | ||
@@ -82,4 +58,49 @@ const resultOpts = result.opts || {}; | ||
}); | ||
css.walkDecls(transform.bind(null, browsers.some(hasTransparentBug))); | ||
const isLegacy = browsers.some(hasTransparentBug); | ||
const colorminCache = {}; | ||
const cache = {}; | ||
css.walkDecls(decl => { | ||
if (/^(composes|font|filter|-webkit-tap-highlight-color)/i.test(decl.prop)) { | ||
return; | ||
} | ||
if (cache[decl.value]) { | ||
decl.value = cache[decl.value]; | ||
return; | ||
} | ||
const parsed = (0, _postcssValueParser2.default)(decl.value); | ||
walk(parsed, (node, index, parent) => { | ||
if (node.type === "function") { | ||
if (/^(rgb|hsl)a?$/i.test(node.value)) { | ||
const { value } = node; | ||
node.value = (0, _colours2.default)((0, _postcssValueParser.stringify)(node), isLegacy, colorminCache); | ||
node.type = "word"; | ||
const next = parent.nodes[index + 1]; | ||
if (node.value !== value && next && (next.type === "word" || next.type === "function")) { | ||
parent.nodes.splice(index + 1, 0, { | ||
type: "space", | ||
value: " " | ||
}); | ||
} | ||
} else if (node.value.toLowerCase() === "calc") { | ||
return false; | ||
} | ||
} else if (node.type === "word") { | ||
node.value = (0, _colours2.default)(node.value, isLegacy, colorminCache); | ||
} | ||
}); | ||
const optimizedValue = parsed.toString(); | ||
decl.value = optimizedValue; | ||
cache[decl.value] = optimizedValue; | ||
}); | ||
}; | ||
}); |
@@ -11,3 +11,4 @@ 'use strict'; | ||
} | ||
return hex; | ||
}; |
{ | ||
"name": "postcss-colormin", | ||
"version": "4.0.2", | ||
"version": "4.0.3", | ||
"description": "Minify colors in your CSS files with PostCSS.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
169
0
9007
7