-
Notifications
You must be signed in to change notification settings - Fork 2
/
checkCssVars.cjs
37 lines (31 loc) · 1.02 KB
/
checkCssVars.cjs
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
const postcss = require('postcss');
const cssVariables = require('./cssVariables.json');
console.log({ cssVariables })
const plugin = () => {
return {
postcssPlugin: 'postcss-check-variables',
Once(root, { result }) {
// Check for undefined variables
const localCssVariables = [];
root.walkDecls(decl => {
if (decl.prop.startsWith('--')) {
localCssVariables.push(decl.prop);
}
});
root.walkDecls(decl => {
if (decl.value.includes('var(')) {
const matches = decl.value.match(/var\(([^)]+)\)/);
if (matches) {
const variable = matches[1];
if (!cssVariables.includes(variable) && !localCssVariables.includes(variable)) {
const file = decl.source.input.file || 'unknown';
const line = decl.source.start.line;
result.warn(`${file} - ${line} : Undefined variable ${variable} used in ${decl.toString()}`);
}
}
}
});
}
}
};
module.exports = plugin;