2

Is there a way to define a less variable at compile time?

What I would like to do is define @base-path depending on the environment that is compiling the less files so that the server references the CDN whereas the path would point to a local path on developer machines.

Bagee
  • 140
  • 8
  • 1
    What tool are you compiling it with? (PHP::lessc, plessc, less.js on node.js, etc)? – Paul Jun 12 '12 at 18:03
  • I suppose it's the less.js running with node. – Bagee Jun 12 '12 at 18:40
  • possible duplicate of [Dynamically define a variable in LESS CSS](http://stackoverflow.com/questions/18039082/dynamically-define-a-variable-in-less-css) – Bass Jobsen Oct 27 '14 at 19:20
  • I can't tell if this is a duplicate or not – Software Engineer Oct 27 '14 at 21:09
  • @BassJobsen This doesn't seem to be a duplicate of that to me - this question seems much simpler: include environment variables in less at compile time. The suggestion seems nothing to do with environment variables, it seems to be something complex with normal variables. I'm slightly surprised this is the only ticket talking about this. – rjmunro Feb 28 '22 at 12:55

1 Answers1

0

The lessc compiler on NodeJS, mentions the following options (from https://lesscss.org/usage/#less-options):

Global Variables

lessc --global-var="color1=red" { globalVars: { color1: 'red' } }

This option defines a variable that can be referenced by the file. Effectively the declaration is put at the top of your base Less file, meaning it can be used but it also can be overridden if this variable is defined in the file.

Modify Variables

lessc --modify-var="color1=red" { modifyVars: { color1: 'red' } }

As opposed to the global variable option, this puts the declaration at the end of your base file, meaning it will override anything defined in your Less file.

Depending on how you call the compiler, you can insert environment variables here.

rjmunro
  • 27,203
  • 20
  • 110
  • 132