Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do nested object literals mean in grunt.js files?

I'm having trouble understanding nesting that sometimes shows up in grunt.js files. In the following example, what do nested objects like concat.dist and min.dist mean? Is the dist key referencing another named task or is it simply a configuration object? What exactly gets called when executing the concat and min tasks?

module.exports = function (grunt) {
  grunt.initConfig({
    // …
    concat: {
      dist: {
        src: ["<banner:meta.banner>", "<file_strip_banner:lib/main.js>"],
        dest: "dist/main.js",
      }
    },
    min: {
      dist: {
        src: ["<banner:meta.banner>", "<config:concat.dist.dest>"],
        dest: "dist/main.min.js",
      }
    },
    // …
  });

  // …
  grunt.registerTask("default", "lint qunit concat min");
};
like image 783
Chris Calo Avatar asked Nov 25 '12 00:11

Chris Calo


1 Answers

In grunt, tasks that support this kind of nesting are called multi tasks, and the nested objects are called targets. Suppose you have the following multi-task:

 concat: {
    dist: {
       src: ["<banner:meta.banner>", "<file_strip_banner:lib/main.js>"],
       dest: "dist/main.js",
    },
    dev: {
       (...)
    }
 }

This means you have the multi task concat with the targets dist and dev inside it. You can run all targets of any multi task by typing the name of the task on console. For example:

grunt concat

will run both concat and dev. On the other hand, you can also specify which target to run explicitly:

grunt concat:dist  

will only execute the dist target.

As far as I know, if you have two multitasks with targets of the same name (like your example min.dist and concat.dist), this doesn't mean that both dist are somehow referencing the same thing, they just happen to share the same name.

like image 78
alemangui Avatar answered Nov 15 '22 09:11

alemangui