Skip to content

Instantly share code, notes, and snippets.

@hail2u
Created August 29, 2019 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hail2u/77e3a20056f445f3b01bca76d030e1a6 to your computer and use it in GitHub Desktop.
Save hail2u/77e3a20056f445f3b01bca76d030e1a6 to your computer and use it in GitHub Desktop.
List unused custom properties
// list unused custom properties
// $ node list-unused-custom-properties.js <FILENAME>
const fs = require("fs");
const postcss = require("postcss");
const css = postcss.parse(fs.readFileSync(process.argv[2], "utf8"));
const customProperties = {};
css.walkDecls(decl => {
if (decl.prop.startsWith("--")) {
customProperties[decl.prop] = 0;
}
const variables = decl.value.match(/\bvar\(.*?\)/g);
if (!variables) {
return;
}
variables.forEach(variable => {
const customProperty = variable.replace(/var\((.*?)\)/, "$1");
delete customProperties[customProperty];
});
});
console.log(customProperties);
@hail2u
Copy link
Author

hail2u commented Aug 29, 2019

This works against a concatenated CSS file only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment