Let Grunt do the heavy lifting for your web app

Django provides a number of great tools that make it easy to optimize your web application.  It can preprocess files, bundle them together, compress them, and much, much more.  There are instances though where you can’t bring Django along for the ride when launching your web app.  For instance, if you’re building a single-page app for mobile devices using Cordova, then you can’t take advantage of Django’s tools to optimize your app’s assets.

However, there’s another tool called Grunt that can keep your web app running as efficiently as possible when Django can’t.  It runs on the Node.js platform and provides many of the same features that web frameworks offer.  The advantage that Grunt provides is that these optimization tasks are performed ahead of time rather than on the web server as is done with Django.  Your entire web app can be compiled into a package which can then be dropped on to any web server, or into any Cordova app, and be ready to deploy without any further software support.

Let’s consider an example of how we might use Grunt to manage Less files.  Less allows you to define your app’s look and feel, but it requires a compiler to convert Less files into CSS.  The Recess plugin makes this compilation process available to you as a Grunt task.  Below is an example of how a Recess task could be defined in a Grunt job file.

recess: {
   options: {
       compile: true
   files: [{
       expand: true,
       cwd: '/styles',
       src: '{,*/}*.less',
       dest: '/styles/',
       ext: '.css'

Here, we configure the options for a basic Recess task.  We can tell it what options to use, where to find our less files, and where to compile them to.  All of this can be triggered in two ways.  The first is to invoke the task directly by running `grunt recess` from your command line interface.  The second is to define it as part of a larger group of tasks that should be run in a sequence.  At the end of the process, you have all the CSS files you need to make your web app look and feel great.  

Here are some examples of other plugins you can use with Grunt:

  • Usemin: This plugin can inspect all of your files and re-write them to use optimized copies of your images, style sheets, or javascript files.

  • Concat: The concat plugin allows you to bundle many different CSS or Javascript files into one, minimizing the number of files you have to download when starting a web app.

  • Uglify: Compress the size of your Javascript to reduce file sizes and to obfuscate your code.

  • Preprocess: Preprocessing allows you to insert high level logic that helps you to build variants of your web app.  One could be tailored for use on iOS while the other for the web, for instance.

  • Rev: The Rev plugin will prepend your asset’s file names with a random string.  This will ensure that the next time you deploy your web app, users will always get the latest and greatest version, and won’t get stuck with a stale file with the same name from their cache.

  • Imagemin: This plugin will optimize all of your image files to minimize their size without sacrificing the quality of the images.

  • Serve: This allows you to run a small web server from your development environment where you can test your web app before deploying it.

​​Grunt can be used to manage automated testing of your web apps as well.  Plugins are available that run your web app through automated Selenium browser tests, and unit tests using Karma.  All in all, Grunt doesn’t do anything that’s radically different from existing web frameworks, but it can provide you with the flexibility to deploy your web app anymore and to minimize its footprint.

Let Grunt do the heavy lifting for your web app

Leave a Reply

Your email address will not be published. Required fields are marked *