【问题标题】:How to call tasks from code in Grunt if helpers are gone如果助手不在,如何从 Grunt 中的代码调用任务
【发布时间】:2012-09-11 22:25:02
【问题描述】:

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

但是,我有一个 Grunt 文件,它依赖于一些自定义任务调用其中一些帮助程序。 没有帮手,它就会崩溃。我只是想知道替换它们的正确方法应该是什么。

我知道它会以某种方式直接调用任务,但我不确定如何。由于 Grunt 文档尚未更新,因此举个例子会有很大帮助。

谢谢。

【问题讨论】:

    标签: javascript node.js gruntjs


    【解决方案1】:

    好的,经过一些研究和 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。

    【讨论】:

    • 我也对这个问题很好奇,目前我正在做类似的事情,但这对我来说似乎效率很低!而不是像var result = grunt.task.run('mincss', ['style1.css', 'style2.css', ...]) 这样的事情,你去hack任务的配置,然后以通常的方式运行它。如果您正在采用为其他任务动态准备数据的方式,我相信一定有更好的方法来做到这一点。如果可能的话,您能否链接到您与 grunt-contrib 维护者的讨论?
    • 找到了,这里是讨论的链接:github.com/gruntjs/grunt-contrib/issues/118
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 2010-12-25
    • 2014-08-19
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多