npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grunt-lessvars

v0.3.1

Published

Parse a set of LESS files, extract variables, and write to a JavaScript file.

Downloads

42

Readme

grunt-lessvars Build Status Coverage Status

Parse a set of LESS files, extract variables, and write to a JavaScript file.

Getting Started

This plugin requires Grunt ~0.4.5

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-lessvars --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-lessvars');

The "lessvars" task

Overview

In your project's Gruntfile, add a section named lessvars to the data object passed into grunt.initConfig().

grunt.initConfig({
    lessvars: {
        options: {
            // Task-specific options go here.
        },
        target: {
            // Target-specific file lists and/or options go here
        }
    }
});

Options

options.format

Type: String|Function(vars:Object, options:Object):String Default value: "json"

How to format the output. Can be one of "json", "angular", or a custom formatter function. The function is given an object containing the variables, and the processed task options.

options.indent

Type: Number Default value: 0

The identation level of the output for the "json" and "angular" formatters. Pass 2 or 4 for a more readable output file.

options.module

Type: String Default value: "less"

The module name used by the "angular" formatter. This is the module to which the exported variables are attached.

options.constant

Type: String Default value: "vars"

The name of the exported constant used by the "angular" formatter.

options.units

Type: Boolean|Array<String> Default value: true

A boolean value indicating whether to drop or keep units in dimensions, or an array containing units to preserve.

options.rename

Type: String|Function(name:String):String|Array<String|Function> Default value: name => name

How to rename LESS variables into their JS counterparts. The function is given the original LESS variable name, with the ampersand stripped off. If a string is passed, it is interpreted as a method name from the change-case library. If an array is passed, each item is interpreted as a separate alias, and the output may contain multiple versions of each variable.

LESS compiler options

Additional options will be forwarded to the LESS compiler.

Usage Examples

Default Options

grunt.initConfig({
    lessvars: {
        myTarget: {
            files {
                'vars.json': 'input.less'
            }
        }
    }
});

Then, given the following input.less,

@x: 2;
@y: 3;

the output vars.json will be

{"x":2,"y":3}

Custom Options

grunt.initConfig({
    lessvars: {
        myTarget: {
            options: {
                format: 'angular',
                module: 'myModule',
                constant: 'myLessVars',
                indent: 2
            },
            files: {
                'vars.js': 'input.less'
            }
        }
    }
})

Then, given the following input.less,

@x: 2;
@y: 3;

the output vars.js will be

angular.module("myModule").constant("myLessVars", {
  x: 2,
  y: 3
});

Units

grunt.initConfig({
    lessvars: {
        myTarget: {
            options: {
                format: 'json',
                indent: 2,
                units: [ 'em', '%' ]
            },
            files: {
                'vars.json': 'input.less'
            }
        }
    }
});

Then, given the following input.less,

@x: 2em;
@y: 3px;
@z: 4%;

the output vars.json will be

{
  "x": "2em",
  "y": 3,
  "z": "4%"
}

Renaming

grunt.initConfig({
    lessvars: {
        myTarget: {
            options: {
                format: 'json',
                indent: 2,
                rename: [ 'camel', 'snake', function (name) { return 'LESS' + name; } ]
            },
            files: {
                'vars.json': 'input.less'
            }
        }
    }
});

Then, given the following input.less,

@my-var: 2;

the output vars.json will be

{
  "myVar": 2,
  "my_var": 2,
  "LESSmy-var": 2
}

Contributing

All source code, tests, and meta-code (build tools and configuration) are written in EcmaScript 6 and transpiled using Babel. The command grunt babel will transpile the src directory into the tasks directory which allows this module to be dropped in as a Grunt plugin. The Babel runtime/polyfill is not used outside of developer tools, and I would like to keep it this way.

Code style grunt style

Code style is enforced using JSCS. The style guide is located in .jscsrc.

Lint grunt lint

Linting is performed using JSHint, configured in .jshintrc.

Testing grunt test

Integration tests are configured in grunt/lessvars.js and test/lessvars.js. Please look at existing examples of how to add integration tests.

All of the above above can be run with the grunt command (no arguments).

Release History

0.3.0

  • Breaking change Added options to control variable naming/aliases. Previously, variables were output under the camel-cased name as well as the original variable name. Now, by default, only the original name is used. See the rename option.
  • Added options to control how units are processed in dimension values.
  • Fixed processing of quoted LESS values (quotes will be dropped in the output).
  • Fixed processing of expression LESS values (will be output as arrays).