Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call tasks from code in Grunt if helpers are gone

Grunt is removing the helpers and this already happened in grunt-contrib.

However I have a Grunt file relying on some custom tasks calling some of those helpers. Without helpers it breaks. I am just wondering what should be the correct way to replace those.

I get it will be by calling the tasks directly in some way but I am not sure how. An example would help a lot since Grunt documentation is not updated yet.

Thanks.

like image 684
GriffonRL Avatar asked Sep 11 '12 22:09

GriffonRL


1 Answers

Ok after some research and the help of the maintainers of grunt-contrib, I rewrote that task I have:

grunt.registerMultiTask('multicss', 'Minify CSS files in a folder', function() {
    grunt.file.expandFiles(this.data).forEach(function(file) {
        var minified = grunt.helper("mincss", grunt.file.read(file));
        grunt.file.write(file, minified);
        grunt.log.writeln("Minified CSS "+file);
    });
});

Into this:

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

No other change needed in the config file. The new piece of code makes use of the task itself instead of the helper that is gone.

This might not be the best approach and Grunt 0.4.0 might change the game again, but it does work right now with Grunt 0.3.15 and grunt-contrib 0.2.

like image 159
GriffonRL Avatar answered Nov 03 '22 01:11

GriffonRL