How can I run a grunt task from within a grunt task?

后端 未结 4 1697
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 08:28

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 an

相关标签:
4条回答
  • 2020-12-13 09:02

    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

    0 讨论(0)
  • 2020-12-13 09:05

    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
    
    0 讨论(0)
  • 2020-12-13 09:15

    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');
        });
    
    0 讨论(0)
  • 2020-12-13 09:22

    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']);
    });
    
    0 讨论(0)
提交回复
热议问题