External tasks
In many instances, our Gruntfile.js
file can become quite large and complicated, especially when we start including our own custom tasks into the mix. Grunt provides the means to split our custom tasks into their own individual files, thereby reducing the complexity of our Gruntfile.js
and increasing reusability, since this new task file could also be used in other Grunt projects. In Chapter 3, Using Grunt, we learnt how to use grunt.registerTask
to create our own custom tasks; however, to move these calls to registerTask
into another file, we will need a reference to the grunt
object (since grunt
is passed into our Gruntfile.js
).
This is exactly what the grunt.loadTasks
function does: it is called with a directory path as the argument, then runs every JavaScript file inside the given directory as if it were a "mini" Gruntfile.js
file (also passing it the current grunt
object). The following example demonstrates this:
//Code example 02-external-tasks // Gruntfile.js module.exports...