Less.js - get variable values ​​inside parser callback

I use less.js (1.3.0) for more parsing css on the client side. Inside the parser callback, I want to get a value for each variable. I tried the following without success.

var data = "@colour: red; #example { background-color: @colour; }",

parser = new less.Parser({});
parser.parse(data, function (error, root) {
  console.log( root.toCSS() );

  var varsDef = root.variables();
  for (k in varsDef) {
    console.log(varsDef[k]);

    // how to get the value for the var?
      //not working
    console.log(varsDef[k].eval());
      //not working
    console.log(varsDef[k].toCSS());
      //is an object but looking for a string value
    console.log(varsDef[k].value); 
      //returns an empty string
    console.log(varsDef[k].value.toCSS());                
  }
});

Neither eval () nor toCSS () gave me any results. I do not understand the less parsers work. Each variable object has the variable property varsDef [k] .value, which is the object itself. But I just need the string value of the variable.

Does anyone know how to get the values ​​of variables as a string?

+5
source share
3 answers
varsDef[k].value.toCSS()

must be meaning

varsDef[k].name

must be a variable name

varsDef[k].toCSS()

, - CSS .

0

, , , , - , ,

@redColor: #900;  // responds to .toCSS()
@fooColor: desaturate(@redColor, 20%);  // both of these error out 
@barColor: lighten(@fooColor, 10%);  // when calling .toCSS()

tree.Value @barColor, , , , barcolor: {[value: {value: [{lighten: {...}}]}]} somesuch. parsing-fu , - , , tree.toCSS, .

, , ,

@import "varfile.less";

.foo {
  redColor: @redColor;
  fooColor: @fooColor;
  barColor: @barColor;
}

less , , redColor css , , . css , , . :

.foo{
  redColor: #990000;
  fooColor: #8a0f0f;
  barColor: #b81414;
}

, , . json . , . , , , .

, , , . , .

i node, , , , json dict . , github nsfmc/less-extractor, , stdout json dict. , , .

, github, : xhr, , , , , .

, !

0

; .

CSS ( ), - , , .

This function returns a key / value pair for each of the variables in the specified smaller file. This will not work if the LESS file has several values ​​per line, you can improve it with a regular expression. I used it to parse the boot variable file, which works well.

getLessVars = (lessStr) ->
  lines = lessStr.split('\n')
  lessVars = {}
  for line in lines
    if line.indexOf('@') is 0
      keyVar = line.split(';')[0].split(':')
      lessVars[keyVar[0]] = keyVar[1].trim()
  return lessVars
-2
source

All Articles