a blog for those who code

Tuesday 1 September 2015

Little about Tasks in Grunt

In this post we are going to discuss about tasks in Grunt. In our previous posts Getting familiar with Grunt JS - Part 1 & Getting familiar with Grunt JS - Part 2 we have installed and configured grunt, in this post we are going to create our own tasks and execute them using Grunt. Tasks are specified in gruntfile.js via grunt.iniconfig method. Task are the backbone of Grunt, every-time Grunt is run it actually runs one or more tasks.

Creating Tasks in Grunt

Creating tasks in grunt is extremely simple. We need to give a name and a function to grunt.registerTask and then we are ready to execute those tasks. Tasks are useful when you need to perform it once in a given build. For example we write a hello tasks which will give us a log message about the time

grunt.registerTask('hello', function() {
  console.log('\nGrunt executed at: ' + new Date() + '.\n');

To run the above tasks you need execute the command grunt hello and it will give the output as shown below.

The task object has the following properties :

name - task name
async - a function that notifies Grunt that this task is asynchronous and returns a callback.
requires - a function which takes task names and then ensures that they have previously run.
requiresConfig - it is an alias to the grunt.config.requires function.
nameArgs - task name including arguments used when running the task
args - an array of all arguments
flags - it is an object having key as args and value as true.
errorCount - Number of errors
options - a function used to retrieve the task's configuration options.

Example for using task object is

grunt.registerTask('hello', function() {
    console.log('\nGrunt executed at: ' + new Date() + '.\n');
    console.log('\nTask Name: ' + this.name);
    console.log('\nTask Arguments: ' + this.args);
    console.log('\nTask Total Error Count: ' + this.errorCount);

Task Aliasing

If you would have gone through the gruntfile.js from the previous post you would have seen the code like grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']); which is nothing nut task aliasing. Here istead of providing a function to the grunt.registerTask we are providing an array of strings which will create a new task. The new task will sequentially run each of the tasks listed in the array. For example grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']); will run jshint, qunit, concat and uglify in sequence.


Multitasks are like tasks however they accept multiple configurations. Grunt will use each property of a multitasks configuration as an individual configuration. This helps us in creating a single tasks and run it multiple times.

At first you need to add the specified configuration inside grunt.initconfig() as shown below.

multi: {
     a : 1,
     b : 'Hello',

Then you need to write a grunt.registerMultiTask on that config like below

grunt.registerMultiTask('multi', function() {
    grunt.log.writeln(this.target + ' ' + this.data);

When you run grunt multi:a it will give the output as a 1 and when you run grunt multi:b it will give the output as b Hello.

Please Like and Share CodingDefined.com Blog, if you find it interesting and helpful.

No comments:

Post a Comment