In this post I will go through using grunt for our css minification task and also showcase another preprocessor for css called less.

First off lets start with a express 4 application, we will create it with Visual Studio NTVS, the latest RC contains a template for express 4.

Node Express App Visual Studio

Node Express App Visual Studio

We can install grunt by command prompt npm install

or by using the npm installer in Visual Studio, remember to choose the development dependency type

Npm Installer

Npm Installer

We should now see grunt in our dev when we expand the npm icon in Visual Studio

Grunt Visual Studio

Grunt Visual Studio

In our express 4 application we will now add a mystyle.css file to public/stylesheets folder like below.

Css file added

Css file added

The css contains just 3 styles

What we want our grunt task to do, is to minify this css for us in our deployment folder, so that it is deploying mystyle.min.css, in order to do so we can use the grunt-contrib-cssmin

Lets first install with npm, again you can use the visual installer if you prefer in visual studio.

Now in our grunt task we can load the npm and also call the cssmin in the initConfig object.

One thing to note is that in cssmin, in the files enclosure one has to put the destination, following the source file.

Let’s try to improve this css a bit by introducing less, basically less is a preprocessor that allows us to write cleaner css. We can introduce variables inside of less so that we don’t have to repeat ourselves. There is more to less and I will not get into the details of it. If you wish to learn more about less go to http://lesscss.org/

We would now need grunt-contrib-less installed with npm and load it into our grunt file.

Our grunt file would look like

But what if we wanted to combine these 2 files, and also to minify them? Then you have to wait till the next blog post, stay tuned.