/
index.js
32 lines (30 loc) · 946 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import postcss from 'postcss';
import parser from 'postcss-value-parser';
import functionalNotation from './lib/rgb-functional-notation';
function transformRgb(value) {
return parser(value).walk(node => {
/* istanbul ignore if */
if (node.type !== 'function' ||
node.value !== 'rgb' &&
node.value !== 'rgba'
) {
return;
}
node.value = functionalNotation.legacy(parser.stringify(node));
node.type = 'word';
}).toString();
}
module.exports = postcss.plugin('postcss-color-rgb', function () {
return function (root) {
root.walkDecls(decl => {
/* istanbul ignore if */
if (!decl.value ||
decl.value.indexOf('rgb(') === -1 &&
decl.value.indexOf('rgba(') === -1
) {
return;
}
decl.value = transformRgb(decl.value);
});
};
});