postcss-advanced-variables-mod
Version:
Use Sass-like variables, conditionals, and iterators in CSS
25 lines (19 loc) • 801 B
JavaScript
// tooling
import getReplacedString from './get-replaced-string';
import setVariable from './set-variable';
// transform declarations
export default function transformDecl(decl, opts) {
// update the declaration value with its variables replaced by their corresponding values
decl.value = getReplacedString(decl.value, decl, opts);
// if the declaration is a variable declaration
if (isVariableDeclaration(decl)) {
// set the variable on the parent of the declaration
setVariable(decl.parent, decl.prop.slice(1), decl.value, opts);
// remove the declaration
decl.remove();
}
}
// return whether the declaration property is a variable declaration
const isVariableDeclaration = decl => matchVariable.test(decl.prop);
// match a variable ($any-name)
const matchVariable = /^\$[\w-]+$/;