-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
58 lines (45 loc) · 1.74 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import getCustomProperties from './lib/get-custom-properties';
import importCustomPropertiesFromSources from './lib/import-from';
import parser from 'postcss-value-parser';
import transformAST from './lib/transform';
module.exports = (opts = {}) => {
// how unresolved functions and arguments should be handled (default: "throw")
const unresolvedOpt = String(Object(opts).unresolved || 'throw').toLowerCase();
// how transformed colors will be produced in CSS
const stringifierOpt = Object(opts).stringifier || (color => color.toLegacy());
// sources to import custom selectors from
const importFrom = [].concat(Object(opts).importFrom || []);
// whether var() within color-mod() should use Custom Properties or var() fallback
const transformVarsOpt = 'transformVars' in Object(opts) ? opts.transformVars : true;
// promise any custom selectors are imported
const customPropertiesPromise = importCustomPropertiesFromSources(importFrom);
return {
postcssPlugin: 'postcss-color-mod-function',
async Once (root, { result }) {
const customProperties = Object.assign(
await customPropertiesPromise,
getCustomProperties(root, { preserve: true })
);
root.walkDecls(decl => {
const originalValue = decl.value;
if (colorModFunctionMatch.test(originalValue)) {
const ast = parser(originalValue);
transformAST(ast, {
unresolved: unresolvedOpt,
stringifier: stringifierOpt,
transformVars: transformVarsOpt,
decl,
result,
customProperties
});
const modifiedValue = parser.stringify(ast);
if (originalValue !== modifiedValue) {
decl.value = modifiedValue;
}
}
});
}
};
};
module.exports.postcss = true;
const colorModFunctionMatch = /(^|[^\w-])color-mod\(/i;