Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering Grunt tasks whose code is located in external JavaScript files

I've written a function which I'd like to use as a Grunt task. I can do this by adding this to the Gruntfile:

grunt.registerTask('foo', function () {
    // code here
});

However, it makes more sense to keep the function code in a separate file. I plan to define a bunch of these custom tasks and I don't want to bloat the Gruntfile.

I'm not sure what the preferred way of registering such tasks is. I have found this to work:

grunt.registerTask('foo', function () {
    require('./path/to/foo.js')(grunt);
});

So, I'm having the inline function like in the fist example, but this time, I'm loading an external file and invoking it immediately. In that external file, I of course have to write:

module.exports = function (grunt) {
    // code here
}

This works, but it feels hackish. Is there a more proper way of doing this?

like image 911
Šime Vidas Avatar asked Sep 05 '13 13:09

Šime Vidas


People also ask

Which of the following will register task using Grunt?

Alias Tasks For instance, when you define a taskList with jshint, concat, and uglify tasks and specify the taskName as default, all the listed tasks will be run automatically if Grunt is executed without specifying any tasks. grunt. registerTask('dist', ['concat:dist', 'uglify:dist']);

How do we check if a task exists in Grunt?

grunt.task.existsCheck with the name, if a task exists in the registered tasks.

What is Grunt js file?

json file, which is then parsed to a JavaScript object. Grunt has a simple template engine to output the values of properties in the configuration object. Here we tell the concat task to concatenate all files that exist within src/ and end in .

When a task is run Grunt looks for its configuration under a?

When a task is run, Grunt looks for its configuration under a property of the same name. Multi-tasks can have multiple configurations, defined using arbitrarily named "targets." In the example below, the concat task has foo and bar targets, while the uglify task only has a bar target.


1 Answers

Short answer: the alternative to this

grunt.registerTask('foo', function () {
    require('./path/to/foo.js')(grunt);
});

is http://gruntjs.com/api/grunt#grunt.loadtasks

Long answer:

Normally when you have tasks in external files there are served as other nodejs modules. So, if that is something that you will use in several projects you may want to register it in the registry. Later inside your Gruntfile.js you will have:

grunt.loadNpmTasks('yout-module-here');

The grunt's documentation says:

Load tasks from the specified Grunt plugin. This plugin must be installed locally via npm, and must be relative to the Gruntfile

However, if you don't want to upload anything to the registry you should use loadTasks

grunt.loadTasks('path/to/your/task/directory');

So, once the task is loaded you may use it in your configuration.

Here is a simple grunt task placed in external file:

'use strict';

module.exports = function(grunt) {

    grunt.registerMultiTask('nameoftask', 'description', function() {

        var self = this;

        // this.data here contains your configuration

    });
};

And later in Gruntfile.js

grunt.initConfig({
    nameoftask: {
        task: {
            // parameters here
        }
    }
});
like image 104
Krasimir Avatar answered Oct 20 '22 23:10

Krasimir