Book Image

Less Web Development Cookbook

Book Image

Less Web Development Cookbook

Overview of this book

Table of Contents (19 chapters)
Less Web Development Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using less.js with Rhino


Less also runs inside Rhino, which is an open source implementation of JavaScript written entirely in Java. It is typically embedded into Java applications to provide scripting to end users. Rhino enables you to use the original less.js distribution in a pure JVM environment.

Getting ready

To use less.js inside Rhino, you will have to download and install Rhino from the following links:

How to do it…

  1. Open your text editor and create a file named example.less. The example.less file can contain, for instance, the following code:

    @base-color: red;
    h1 {
    color: @base-color;
    }
  2. Now you can run the following command in your command prompt:

    java -jar js.jar -f less-rhino-1.7.0.js lessc-rhino-1.7.0.js example.less
    
  3. The preceding command should output the following lines of CSS code:

    h1 { 
      color: #ff0000; 
    }

How it works…

Rhino enables Java to run the JavaScript code, while js.jar runs the Less compiler and generates the CSS output.

To write the output of a file, you will have to append the filename of the CSS files to the list of commands, as follows:

java -jar js.jar -f less-rhino-1.7.0.js lessc-rhino-1.7.0.js example.less example.css

You can also add options for the compiler. You can add the -x option to compress the output as follows:

java -jar js.jar -f less-rhino-1.7.0.js lessc-rhino-1.7.0.js -x example.less 

The preceding command will then output the following line of CSS code:

h1{color:#f00}

There's more…

A Less compiler for Java has been built with Rhino. You can find out more information about this Less compiler for Java along with how to download it at https://github.com/marceloverdijk/lesscss-java.