/less-vars-to-json

Read LESS variables from the contents of a file and returning them as a javascript object.

Primary LanguageJavaScriptMIT LicenseMIT

Build Status npm

less-vars-to-json

Read LESS variables from the contents of a less file and return them as a javascript object.

$ yarn add less-vars-to-json

Why?

One can point to a .less file from javascript and read the variables. It is useful to declare the variable only once to reduce code duplication. One usecase is to re-use the less variables in runtime theming.

This package has been inspired from less-vars-to-js, and a few modifications were made.

  • The variables will be resolved from other values in the same file, or from a supplied object.
  • The commented values be ignored.
  • The resulting object will not contain the @ character.

Example

Example :

// Colour palette
@blue: #0d3880;
@pink: #e60278;

// Elements
@background: @gray;
@favourite: @blue;

// Grid
@row-height: 9;

.element {
    @foreground: black;
    color: @foreground;
}
// Sass
$sass-works-too: #0d3880

Example output:

{
    "background": "#eee",
    "blue": "#0d3880",
    "favourite": "#0d3880",
    "foreground": "black",
    "pink": "#e60278",
    "row-height": "9"
}
{
    "sass-works-too": "#0d3880"
}

Usage

import fs from 'fs';
import lessToJson from 'less-vars-to-json';

// Read the less file in as string
const paletteLess = fs.readFileSync('palette.less', 'utf8');

// Pass in file contents
const palette = lessToJson(paletteLess, { gray: "#eee" });
console.log(palette);

License

MIT