Chris Calo
Chris Calo

Reputation: 7838

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");
};

Upvotes: 8

Views: 2599

Answers (2)

Rob
Rob

Reputation: 4141

Like the answer states, those or "multi targets" .. I did a vid on multi tasks here

Upvotes: 0

alemangui
alemangui

Reputation: 3655

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.

Upvotes: 10

Related Questions