Last active
September 19, 2020 13:50
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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