Skip to content

Instantly share code, notes, and snippets.

@funmaker
Last active September 19, 2020 13:50
Show Gist options
  • Save funmaker/fff7913edd44aa5f66e66205076a3308 to your computer and use it in GitHub Desktop.
Save funmaker/fff7913edd44aa5f66e66205076a3308 to your computer and use it in GitHub Desktop.
[LESS Plugin] Exports all visible LESS variables to CSS custom properties in the scope of @plugin call
let vars;
registerPlugin({
install(less, pluginManager, functions) {
functions.add('exportVars', () => {
return vars;
});
},
eval(context) {
vars = "";
for(const frame of context.frames) {
for(let variable of Object.values(frame.variables())) {
variable = variable.eval(context);
const varName = variable.name.substr(1)
.split(/([A-Z][a-z]+)/)
.filter(x => x).map(el => el.toLowerCase())
.join("-");
const value = variable.value.toCSS(context);
if(value) vars += `--${varName}: ${value};\n`;
}
}
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment