50

I've created a new grunt task and within it I want to use grunt-contrib-concat to concatenate a few files together.

I looked through the docs but I don't find anything that hinted at being able to do this. It seems like a trivial use case, so I'm probably just over looking something.

Update 1:

I also want to be able to configure this task from within my custom task.

For example, I create a list of files in my custom task. After I have that list, I want to pass them to the concat task. How can I do that?

I would like to be able to do something like this.

grunt.task.run('concat', { src: ['file1','file2'], dest: 'out.js'})

Update 2:

To achieve what I want, I have to manually configure the grunt task. Here's an example that showed me what I wanted.

https://github.com/gruntjs/grunt-contrib/issues/118#issuecomment-8482130

ekkis
  • 9,804
  • 13
  • 55
  • 105
Arron S
  • 5,511
  • 7
  • 50
  • 57

5 Answers5

34

Here's an example of manually configuring a task within a task and then running it.

https://github.com/gruntjs/grunt-contrib/issues/118#issuecomment-8482130

 grunt.registerMultiTask('multicss', 'Minify CSS files in a folder', function() {
        var count = 0;
        grunt.file.expandFiles(this.data).forEach(function(file) {
            var property = 'mincss.css'+count+'.files';
            var value = {};
            value[file] = file;
            grunt.config(property, value);
            grunt.log.writeln("Minifying CSS "+file);
            count++;
        });
        grunt.task.run('mincss');
    });
Arron S
  • 5,511
  • 7
  • 50
  • 57
27

From https://github.com/gruntjs/grunt/wiki/Creating-tasks

grunt.registerTask('foo', 'My "foo" task.', function() {
  // Enqueue "bar" and "baz" tasks, to run after "foo" finishes, in-order.
  grunt.task.run('bar', 'baz');
  // Or:
  grunt.task.run(['bar', 'baz']);
});
Pascal Belloncle
  • 11,184
  • 3
  • 56
  • 56
  • Please see my edit. I added the specifics of how I want to use concat from my task. – Arron S Mar 08 '13 at 17:14
  • 1
    you probably need to configure the tasks like in @ArronS answer. – Pascal Belloncle Jun 04 '14 at 22:21
  • 5
    There is one thing I stumbled upon today. If you want to run the same task multiple times but with different config, then it's bad idea to pass options to each task through `grunt.config` because the tasks are enqueued not run, thus when they start executing, only the last assigned config values will be effective. For example, if you want to schedule concatenating JS files to one destination and CSS files to another destination, only your last config will be effective. To avoid this, you need to also set up separate targets for each task dynamically. – JustAMartin Jun 20 '14 at 15:20
11

Thx to Arron that pointed us out in the right direction to his own question. The grunt.config is the key from the example above. This task will override the src property of the browserify task

Task definition:

  grunt.registerTask('tests', function (spec) {

    if (spec) {
      grunt.config('browserify.tests.src', spec);
    }

    grunt.task.run(['jshint', 'browserify:tests', 'jasmine']);

  }); 

Task call:

grunt tests

or

grunt tests:somewhere/specPath.js
elmuchacho
  • 424
  • 3
  • 8
0

If you are feeling lazy I ended up publishing a npm module that forwards the configs from your task into the subtask that you want to run:

https://www.npmjs.org/package/extend-grunt-plugin

ruyadorno
  • 866
  • 5
  • 16
0

How can we run same task multiple times from a task for eg.

grunt.registerTask('asyncfoo', 'My "asyncfoo" task.', function() {
  
  var done = this.async();
  // Run some sync stuff.
  grunt.log.writeln('Processing task...');
  var versions = Object.keys(versionConf);
 
  setTimeout(function() {
    versions.forEach(function(version) {
      console.info(version);
      //process.env.version = version;
      grunt.config('port', versionConf[version].port);
      grunt.task.run('test-perf');
    })
    done();
  }, 1000);
 
});

I want to run test-perf with diff port every time according to version.

Shilpi
  • 21
  • 3