Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3dbaa04

Browse files
author
Vlad Shilov
authored
Simplify postcss-colormin (#1207)
* Simplify `postcss-colormin` * Update postcss-colormin tests * Upgrade colord and fix postcss-colormin specs * Lowercase original input
1 parent 2b5841e commit 3dbaa04

File tree

8 files changed

+33
-116
lines changed

8 files changed

+33
-116
lines changed

packages/postcss-colormin/package.json

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"dependencies": {
3333
"browserslist": "^4.16.6",
3434
"caniuse-api": "^3.0.0",
35-
"colord": "^2.0.1",
35+
"colord": "^2.9.1",
3636
"postcss-value-parser": "^4.1.0"
3737
},
3838
"bugs": {

packages/postcss-colormin/src/__tests__/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ test(
113113
'should minify color values in background gradients (2)',
114114
processCSS(
115115
'h1{background:linear-gradient(yellow, orange), linear-gradient(black, rgba(255, 255, 255, 0))}',
116-
'h1{background:linear-gradient(#ff0, orange), linear-gradient(#000, hsla(0, 0%, 100%, 0))}'
116+
'h1{background:linear-gradient(#ff0, orange), linear-gradient(#000, hsla(0,0%,100%,0))}'
117117
)
118118
);
119119

@@ -205,7 +205,7 @@ test(
205205
'should not mangle percentage based rgba values',
206206
processCSS(
207207
'h1{color:rgba(50%, 50%, 50%, 0.5)}',
208-
'h1{color:hsla(0, 0%, 50%, 0.5)}'
208+
'h1{color:hsla(0,0%,50%,.5)}'
209209
)
210210
);
211211

@@ -297,7 +297,7 @@ test('should passthrough broken syntax', passthroughCSS('h1{color:}'));
297297

298298
test(
299299
'should not convert this specific rgba value to "transparent" (old IE)',
300-
passthroughCSS('h1{color:rgba(0, 0, 0, 0)}', { env: 'ie8' })
300+
passthroughCSS('h1{color:rgba(0,0,0,0)}', { env: 'ie8' })
301301
);
302302

303303
test('should use the postcss plugin api', usePostCSSPlugin(plugin()));

packages/postcss-colormin/src/__tests__/minifyColor.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ test(
2626

2727
test(
2828
'should convert translucent hsla to rgba',
29-
isEqual('hsla(0, 100%, 50%, .5)', 'rgba(255, 0, 0, 0.5)')
29+
isEqual('hsla(0, 100%, 50%, .5)', 'rgba(255,0,0,.5)')
3030
);
3131

3232
test(
@@ -52,7 +52,7 @@ test(
5252

5353
test(
5454
'should convert rgba to hsla when shorter',
55-
isEqual('rgba(221, 221, 221, 0.5)', 'hsla(0, 0%, 87%, 0.5)')
55+
isEqual('rgba(221, 221, 221, 0.5)', 'hsla(0,0%,87%,.5)')
5656
);
5757

5858
test(
@@ -89,12 +89,12 @@ test(
8989

9090
test(
9191
'should convert signed numbers',
92-
isEqual('rgba(-100,0,-100,.5)', 'rgba(0, 0, 0, 0.5)')
92+
isEqual('rgba(-100,0,-100,.5)', 'rgba(0,0,0,.5)')
9393
);
9494

9595
test(
9696
'should convert signed numbers (2)',
97-
isEqual('hsla(-400, 50%, 10%, 0.5)', 'rgba(38, 13, 30, 0.5)')
97+
isEqual('hsla(-400, 50%, 10%, 0.5)', 'rgba(38,13,30,.5)')
9898
);
9999

100100
test(
@@ -104,7 +104,7 @@ test(
104104

105105
test(
106106
'should convert percentage based rgba values (2)',
107-
isEqual('rgba(50%, 50%, 50%, 0.5)', 'hsla(0, 0%, 50%, 0.5)')
107+
isEqual('rgba(50%, 50%, 50%, 0.5)', 'hsla(0,0%,50%,.5)')
108108
);
109109

110110
test(
@@ -114,12 +114,12 @@ test(
114114

115115
test(
116116
'should convert percentage based rgba values (4)',
117-
isEqual('rgba(100%,100%,100%,0.5)', 'hsla(0, 0%, 100%, 0.5)')
117+
isEqual('rgba(100%,100%,100%,0.5)', 'hsla(0,0%,100%,.5)')
118118
);
119119

120120
test(
121121
'should convert percentage based rgba values (5)',
122-
isEqual('rgba(100%, 64.7%, 0%, .5)', 'rgba(255, 165, 0, 0.5)')
122+
isEqual('rgba(100%, 64.7%, 0%, .5)', 'rgba(255,165,0,.5)')
123123
);
124124

125125
test(
@@ -163,9 +163,9 @@ test('should convert to hex8', () => {
163163

164164
test('should not convert to alpha hex since the conversion is not lossless', () => {
165165
expect(min('rgba(0, 0, 0, 0.075)', { supportsAlphaHex: true })).toBe(
166-
'rgba(0, 0, 0, 0.075)'
166+
'rgba(0,0,0,.075)'
167167
);
168168
expect(min('hsla(0, 0%, 50%, 0.515)', { supportsAlphaHex: true })).toBe(
169-
'hsla(0, 0%, 50%, 0.515)'
169+
'hsla(0,0%,50%,.515)'
170170
);
171171
});

packages/postcss-colormin/src/lib/color.js

Lines changed: 0 additions & 73 deletions
This file was deleted.

packages/postcss-colormin/src/lib/getShortestString.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

packages/postcss-colormin/src/minifyColor.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import { process } from './lib/color';
2-
import getShortestString from './lib/getShortestString';
1+
import { colord, extend } from 'colord';
2+
import namesPlugin from 'colord/plugins/names';
3+
import minifierPlugin from 'colord/plugins/minify';
4+
5+
extend([namesPlugin, minifierPlugin]);
36

47
/**
58
* Performs color value minification
@@ -15,15 +18,18 @@ export default function minifyColor(input, options = {}) {
1518
...options,
1619
};
1720

18-
const instance = process(input);
21+
const instance = colord(input);
1922

2023
if (instance.isValid()) {
21-
// Try to shorten the string if it is a valid CSS color value.
22-
// Fall back to the original input if it's smaller or has equal length/
23-
return getShortestString([
24-
input.toLowerCase(),
25-
instance.toShortString(settings),
26-
]);
24+
// Try to shorten the string if it is a valid CSS color value
25+
const minified = instance.minify({
26+
alphaHex: settings.supportsAlphaHex,
27+
transparent: settings.supportsTransparent,
28+
name: true,
29+
});
30+
31+
// Fall back to the original input if it's smaller or has equal length
32+
return minified.length < input.length ? minified : input.toLowerCase();
2733
} else {
2834
// Possibly malformed, so pass through
2935
return input;

packages/postcss-minify-gradients/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
},
2727
"repository": "cssnano/cssnano",
2828
"dependencies": {
29+
"colord": "^2.9.1",
2930
"cssnano-utils": "^2.0.1",
30-
"colord": "^2.6",
3131
"postcss-value-parser": "^4.1.0"
3232
},
3333
"bugs": {

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3026,10 +3026,10 @@ color-name@~1.1.4:
30263026
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
30273027
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==
30283028

3029-
colord@^2.0.1, colord@^2.6:
3030-
version "2.7.0"
3031-
resolved "https://registry.yarnpkg.com/colord/-/colord-2.7.0.tgz#706ea36fe0cd651b585eb142fe64b6480185270e"
3032-
integrity sha512-pZJBqsHz+pYyw3zpX6ZRXWoCHM1/cvFikY9TV8G3zcejCaKE0lhankoj8iScyrrePA8C7yJ5FStfA9zbcOnw7Q==
3029+
colord@^2.9.1:
3030+
version "2.9.1"
3031+
resolved "https://registry.yarnpkg.com/colord/-/colord-2.9.1.tgz#c961ea0efeb57c9f0f4834458f26cb9cc4a3f90e"
3032+
integrity sha512-4LBMSt09vR0uLnPVkOUBnmxgoaeN4ewRbx801wY/bXcltXfpR/G46OdWn96XpYmCWuYvO46aBZP4NgX8HpNAcw==
30333033

30343034
colorette@^1.2.1, colorette@^1.2.2, colorette@^1.3.0:
30353035
version "1.4.0"

0 commit comments

Comments
 (0)