4

When using lesscon the commandline, I can pass the option --modify-var="my-var=my-val".

How can I pass the same option when I use less programmatically via API with less.render(lessInput, options)?

I would somehow hope that I can set a property in options like {modifyVar:'my-var=my-val'}. But this seems not to work and I didn't find any documentation regarding this use case.

Thanks for any help.

jbandi
  • 17,499
  • 9
  • 69
  • 81

1 Answers1

5

Unfortunately the options are not described at the API documentation. The easiest way to understanding them, will be by studying the the source of https://github.com/less/less.js/blob/master/bin/lessc.

Both the options and modifyVars option should be an object. For the modifyVars option each variable should be a key of the object. Keys may but don't have to start with a @.

Example:

var less = require('less/lib/less-node');

var options = {};
options['modifyVars'] = {'color1' : 'blue', '@color2': 'darkblue'};


less.render('@color1: red; @color2:yellow; t {color1: @color1; color2: @color2;}', options)
.then(function(output) {
// output.css = string of css
// output.map = undefined
console.log(output.css);
});

The above should output as follows:

t {
  color1: blue;
  color2: darkblue;
}
Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224
  • It looks like the link you included is no longer valid. I believe this is the equivalent but anyone here can correct me if that's wrong: https://github.com/less/less.js/blob/master/packages/less/bin/lessc – Matt Haidet Sep 20 '21 at 22:26