LessCss and Rhino

LessCss

My colleague front designer is using less.js. It’s a tool to improve CSS with variables, mixins, and other goodness. I really like it because unlike SASS, the syntax stays close to CSS (and my old habits and editor don’t go crazy) but also because you can use it just by include it a Javascript file in your page. On the other hand, SASS requires Ruby so unless you’re already working with Rails, each developer needs to configure its environment for that and it’s a pain.

Get ready for production!

This is all good for development: you include your less stylesheet, your Javascript file and you’re good to go. But for production it’s kind of stupid to render the page on every user’s browser: it takes precious loading time. You’re better off generating the CSS file once from your Less file, and serve it compiled to your users. Less actually provides a way to do that, using Node.js. Indeed, LessCss being written in Javascript, it’s easy to understand why they chose Node.js.

But Node.js poses the same problem as SASS with Ruby: more work to setup a dev environment. I want anyone to be able to compile the files with a minimum of dependencies. This is why I turned my eyes to Mozilla’s Javascript implementation for the JDK, Rhino. If you can run less.js on Rhino, all you have to do is to ship rhino.jar with you code and anyone with a JDK can run it. Since all my projects today are in Play Framework, everyone involved has necessary a JDK installed. It’s particularly convenient to use Rhino because I’m already including it in my repositories for JsHint.

I have found a few posts about LessJS and Rhino, but they all involved either integration with some framework I don’t care, servlets, maven… Not what I wanted. I already have less.js included in the page for development mode, all I want is to be able to generate the CSS in command line or in an ant task. I’ve also found an ant task but that relies on the Node version of lesscss, so it kind of defeats the purpose.

Here comes my code

So here it is: rhino compatibility for LessCss. I did a pull request to ask for inclusion. The file you need is less-rhino-x.y.z.js.

You can use it in command line:

java -jar rhino.jar style.less

Or in ant (make sure tool.rhino and tool.less point to rhino.jar and less-rhino.js):

<macrodef name="lessjs">
    <attribute name="input" />
    <attribute name="output" />
    <sequential>
        <java jar="${tool.rhino}" fork="true" output="@{output}">
            <arg path="${tool.less}" />
            <arg path="@{input}" />
        </java>
        <echo>Lessjs: generated @{output}</echo>
    </sequential>
</macrodef>

<target name="compilecss">
    <lessjs input="./public/stylesheets/main.less" output="./public/stylesheets/main.css" />
</target>

That’s all, folks! Now I have one more task to include to my pre-deployment task, along with JsHint checks and Closure Compiler minification…


Phones turning to netbooks are obsolete

I’ve read Ars review on the Motorola Axis. While it seems to be a really cool phone, the whole “your phone turns into a laptop” thing seams completely obsolete to me. It comes from a time when your data was on one device and couldn’t be transfered easily to an other. The future present is


Cross-Domain Requests, State of the Browsers

Client-side mashups are great: the response gets faster if it doesn’t have to go through one more machine, and user security and privacy can be improved by having the client directly fetch his own data, without having to grant access to a tihrd party web server. But the web browsers can’t just let web pages


New domain name

As you can see, I just changed the domain name of this blog from erwan.jp to caffeinelab.net. That means that if you’re using my feed to read this, you should update your reader to the new feed. Currently and for the 2 months left on erwan.jp there is a redirect on each page so no


Promote JS

In search engine, results for “Javascript” are crap. Let’s change that.


Asynchronous HTTP Client in Play Framework

I just finished migrating Play’s http client to Ning’s own asynchronous library. What does it mean? A lot. When you want to retrieve data from a different server, your web application behaves like a web client. Rather than serving a resource to a client, it requests one from a different server. For example, that’s what


Testing in Play’s Eclipse Plugin

In Play‘s Eclipse plugin, I just added a simple integration with the test framework. Currently, all it does is opening the test page directly within Eclipse. The obvious next step would be that, in case of a failure typically, links from the page brings you directly to your code at the correct line. That would


Deploy a Play Application on Gandi Cloud

Gandi Cloud is a cloud hosting service, similar to Amazon’s EC2. Since you get full access to a Linux installation, deployment is similar to using a dedicated server. They have prepackaged solutions for various frameworks but not for Play. Fortunately deploying Play in general is pretty straight-forward. Note that, just like EC2, it’s not free


Going to FOSDEM, talking about Play

I will be at FOSDEM in Bruxelles on Saturday 6 February. I will make a talk about the Play framework in the Free Java session, at 17:15.


What I’ve been up to: Zenexity, Play

Busy as I was, I realized I didn’t blog about my recent employment change. I left Yoono 2 months ago to join a company called Zenexity (site in French). It’s really cool because after Flock and Yoono that were very similar (consumer oriented/social mashup/Mozilla technologies), I get to work on really different stuff: more server-side,