Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(postcss-minify-gradients): replace is-color-stop with own code (#…
…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
1 parent
04bd16e
commit 50eb53e
Showing
5 changed files
with
64 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/postcss-minify-gradients/src/__tests__/isColorStop.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters