如果助手不在,如何从Grunt中的代码中调用任务

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

本文关键字:Grunt 代码 任务 调用 如果      更新时间:2023-09-26

Grunt正在删除助手,这已经在Grunt contrib中发生了。

然而,我有一个Grunt文件,它依赖于一些调用这些助手的自定义任务。没有帮手,它就坏了。我只是想知道应该用什么正确的方式来替换这些。

我会以某种方式直接调用任务,但我不确定如何调用。一个例子会有很大帮助,因为Grunt文档还没有更新。

谢谢。

好的,经过一些研究和grunt contrib维护人员的帮助,我重写了我的任务:

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

进入:

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

配置文件中不需要其他更改。新的代码段使用了任务本身,而不是已经消失的助手。

这可能不是最好的方法,Grunt 0.4.0可能会再次改变游戏,但现在Grunt 0.3.15和Grunt contrib 0.2确实有效。