Skip to content

Commit

Permalink
chore(postcss-minify-gradients): replace is-color-stop with own code (#…
Browse files Browse the repository at this point in the history
…1181)

Get rid of 7 dependencies in a cssnano install.
is-color-stop duplicates functionality available from other dependencies. It copies
unit parsing code directly from postcss-value-parser.
cssnano has color detection logic is already available in colord.
  • Loading branch information
ludofischer committed Aug 17, 2021
1 parent 04bd16e commit 50eb53e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 45 deletions.
2 changes: 1 addition & 1 deletion packages/postcss-minify-gradients/package.json
Expand Up @@ -27,7 +27,7 @@
"repository": "cssnano/cssnano",
"dependencies": {
"cssnano-utils": "^2.0.1",
"is-color-stop": "^1.1.0",
"colord": "^2.6",
"postcss-value-parser": "^4.1.0"
},
"bugs": {
Expand Down
@@ -0,0 +1,9 @@
import isColorStop from '../isColorStop.js';

test('should recognise color stops', () => {
expect(isColorStop('yellow')).toBe(true);
expect(isColorStop('yellow', '12px')).toBe(true);
expect(isColorStop('yellow', 'px')).toBe(false);
expect(isColorStop('yellow', 'calc(100%)')).toBe(true);
expect(isColorStop(undefined)).toBe(false);
});
2 changes: 1 addition & 1 deletion packages/postcss-minify-gradients/src/index.js
@@ -1,6 +1,6 @@
import valueParser, { unit, stringify } from 'postcss-value-parser';
import { getArguments } from 'cssnano-utils';
import isColorStop from 'is-color-stop';
import isColorStop from './isColorStop.js';

const angles = {
top: '0deg',
Expand Down
52 changes: 52 additions & 0 deletions packages/postcss-minify-gradients/src/isColorStop.js
@@ -0,0 +1,52 @@
import { unit } from 'postcss-value-parser';
import { colord, extend } from 'colord';
import namesPlugin from 'colord/plugins/names';

extend([namesPlugin]);

/* Code derived from https://github.com/pigcan/is-color-stop */

const lengthArray = [
'PX',
'IN',
'CM',
'MM',
'EM',
'REM',
'POINTS',
'PC',
'EX',
'CH',
'VW',
'VH',
'VMIN',
'VMAX',
'%',
];

function isCSSLengthUnit(input) {
return lengthArray.includes(input.toUpperCase());
}

function isStop(str) {
let stop = !str;

if (!stop) {
const node = unit(str);
if (node) {
if (
node.number === 0 ||
(!isNaN(node.number) && isCSSLengthUnit(node.unit))
) {
stop = true;
}
} else {
stop = /^calc\(\S+\)$/g.test(str);
}
}
return stop;
}

export default function isColorStop(color, stop) {
return colord(color).isValid() && isStop(stop);
}
44 changes: 1 addition & 43 deletions yarn.lock
Expand Up @@ -3130,7 +3130,7 @@ color-name@~1.1.4:
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

colord@^2.0.1:
colord@^2.0.1, colord@^2.6:
version "2.6.0"
resolved "https://registry.yarnpkg.com/colord/-/colord-2.6.0.tgz#6cd716e1270cfff8d6f66e751768749650e209cd"
integrity sha512-8yMrtE20ZxH1YWvvSoeJFtvqY+GIAOfU+mZ3jx7ZSiEMasnAmNqD1BKUP3CuCWcy/XHgcXkLW6YU8C35nhOYVg==
Expand Down Expand Up @@ -3371,11 +3371,6 @@ cross-spawn@^7.0.1, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
shebang-command "^2.0.0"
which "^2.0.1"

css-color-names@^0.0.4:
version "0.0.4"
resolved "https://registry.yarnpkg.com/css-color-names/-/css-color-names-0.0.4.tgz#808adc2e79cf84738069b646cb20ec27beb629e0"
integrity sha1-gIrcLnnPhHOAabZGyyDsJ762KeA=

css-color-names@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/css-color-names/-/css-color-names-1.0.1.tgz#6ff7ee81a823ad46e020fa2fd6ab40a887e2ba67"
Expand Down Expand Up @@ -4711,11 +4706,6 @@ he@^1.2.0:
resolved "https://registry.yarnpkg.com/he/-/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f"
integrity sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==

hex-color-regex@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/hex-color-regex/-/hex-color-regex-1.1.0.tgz#4c06fccb4602fe2602b3c93df82d7e7dbf1a8a8e"
integrity sha512-l9sfDFsuqtOqKDsQdqrMRk0U85RZc0RtOR9yPI7mRVOa4FsR/BVnZ0shmQRM96Ji99kYZP/7hn1cedc1+ApsTQ==

homedir-polyfill@^1.0.1:
version "1.0.3"
resolved "https://registry.yarnpkg.com/homedir-polyfill/-/homedir-polyfill-1.0.3.tgz#743298cef4e5af3e194161fbadcc2151d3a058e8"
Expand All @@ -4735,16 +4725,6 @@ hosted-git-info@^4.0.0, hosted-git-info@^4.0.1:
dependencies:
lru-cache "^6.0.0"

hsl-regex@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/hsl-regex/-/hsl-regex-1.0.0.tgz#d49330c789ed819e276a4c0d272dffa30b18fe6e"
integrity sha1-1JMwx4ntgZ4nakwNJy3/owsY/m4=

hsla-regex@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/hsla-regex/-/hsla-regex-1.0.0.tgz#c1ce7a3168c8c6614033a4b5f7877f3b225f9c38"
integrity sha1-wc56MWjIxmFAM6S194d/OyJfnDg=

html-encoding-sniffer@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/html-encoding-sniffer/-/html-encoding-sniffer-2.0.1.tgz#42a6dc4fd33f00281176e8b23759ca4e4fa185f3"
Expand Down Expand Up @@ -5060,18 +5040,6 @@ is-ci@^3.0.0:
dependencies:
ci-info "^3.1.1"

is-color-stop@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/is-color-stop/-/is-color-stop-1.1.0.tgz#cfff471aee4dd5c9e158598fbe12967b5cdad345"
integrity sha1-z/9HGu5N1cnhWFmPvhKWe1za00U=
dependencies:
css-color-names "^0.0.4"
hex-color-regex "^1.1.0"
hsl-regex "^1.0.0"
hsla-regex "^1.0.0"
rgb-regex "^1.0.1"
rgba-regex "^1.0.0"

is-core-module@^2.2.0, is-core-module@^2.4.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.5.0.tgz#f754843617c70bfd29b7bd87327400cda5c18491"
Expand Down Expand Up @@ -8315,16 +8283,6 @@ reusify@^1.0.4:
resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76"
integrity sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==

rgb-regex@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/rgb-regex/-/rgb-regex-1.0.1.tgz#c0e0d6882df0e23be254a475e8edd41915feaeb1"
integrity sha1-wODWiC3w4jviVKR16O3UGRX+rrE=

rgba-regex@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/rgba-regex/-/rgba-regex-1.0.0.tgz#43374e2e2ca0968b0ef1523460b7d730ff22eeb3"
integrity sha1-QzdOLiyglosO8VI0YLfXMP8i7rM=

rimraf@^2.6.3:
version "2.7.1"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.7.1.tgz#35797f13a7fdadc566142c29d4f07ccad483e3ec"
Expand Down

0 comments on commit 50eb53e

Please sign in to comment.