lessvars

1.0.1 • Public • Published

lessvars

Read LESS variables from a less-file and return all variables as a javascript object.

$ npm install --save lessvars

How does it work?

This library uses the less-library to parse the less-file into an AST. The AST is then walked in order to find all variables, this includes variables defined in imported files and where color-functions has been used.

Example

// toBeImported.less 
@fromAnImport: blue;
 
// index.less 
@import "tobeImported";
 
@minVar1: blue;
@minVar2: #f00;
@minVar3: #f0ff00;
@minVar4: rgb(124124124);
@minVar5: rgba(1241241240.5);
@minVar6: hsl(120100%50%);
@minVar7: hsla(120100%50%0.75);
@minVar8: #ffffff;
 
@compondVar1: lighten(@minVar520%);
@compondVar2: darken(@minVar620%);
@compondVar3: mix(@minVar1@minVar250%);
 
 
 
@notACOlor: 3%;
@not2ACOlor: 3;
@hsv: hsv(120100%100%);
 
.test {
  @includeThis: blue;
}

Example output:

{
  "fromAnImport": "blue",
  "minVar1": "blue",
  "minVar2": "#f00",
  "minVar3": "#f0ff00",
  "minVar4": "#7c7c7c",
  "minVar5": "rgba(124, 124, 124, 0.5)",
  "minVar6": "#00ff00",
  "minVar7": "rgba(0, 255, 0, 0.75)",
  "minVar8": "#ffffff",
  "compondVar1": "rgba(175, 175, 175, 0.5)",
  "compondVar2": "#009900",
  "compondVar3": "#800080",
  "notACOlor": "3%",
  "not2ACOlor": "3",
  "hsv": "#00ff00",
  "includeThis": "blue"
}

NOTE: As seen by the example, it returns variables defined in imported files, within rules and resolves darken, lighten etc.

Usage

import fs from 'fs';
import lessvars from 'lessvars';
 
const content = fs.readFileSync('./index.less', 'utf-8');
 
function printObject(obj) {
    Object.entries(obj)
        .forEach(([key, value]) => console.log(key, value));
}
 
// Use with callback
lessvars(content, (err, variables) => {
    printObject(variables);
});
 
// Use with promise
lessvars(content)
    .then(printObject);

Lisence

X11 (This license is identical to the MIT License, but with an extra sentence that prohibits using the copyright holders' names for advertising or promotional purposes without written permission. )

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.1
    0
    • latest

Version History

Package Sidebar

Install

npm i lessvars

Weekly Downloads

0

Version

1.0.1

License

X11

Unpacked Size

71.5 kB

Total Files

10

Last publish

Collaborators

  • nutgaard