Grunt在For循环中运行 [英] Grunt Run in a For Loop
问题描述
我是新来的咕噜。
我有一个适用于单一版本的gruntfile。有几个目标链接在一起完成该过程。问题是我需要使用可变数据创建几个不同的版本。我想弄清楚如何在我的gruntfile中做到这一点。
今天做一个构建,我只需要运行
grunt --foo = bar build
或
grunt --foo = baz build
想要做,并试图做的是创建一个使用数组来定义foo数据的构建全部目标,如下所示:
grunt.registerTask('build-all',function(){
var foos = [bar,baz];
for(var i in foos){
grunt.config.set(foo,foos [i]);
grunt.task.run(build);
}
});
从外观上看,运行任务似乎是非阻塞的。这意味着foo在第一次运行之前被设置为baz,并为相同的值运行两次。
是否有更好的方法来设置参数/在这种情况下的选项?或者以阻塞的方式运行任务?
我最近遇到了这个问题,并写了 grunt-galvanize 帮助解决这个问题。以下是它的工作原理,适用于您的示例:
grunt.registerTask('build-all',function(){
$ var vos = [bar,baz];
var galvanizeConfig = [];
$ b $ for(var i in foos){
galavanizeConfig.push({ configs:{foo:foos [i]}});
}
grunt.option('galvanizeConfig',galvanizeConfig);
grunt.task.run(['galvanize:build'] );
});
这将运行带有指定的每个选项/配置的 build 任务in galvanizeConfig
。
PS。我也在需要并发的情况下使用grunt-multi,但对于不需要并发的情况,grunt-galvanize是一个简单得多的工具。
I'm new to grunt.
I have a gruntfile that works for a single build. There are several target that are chained together to complete the process. The problem is that I need to create several different builds using variable data. I'm trying to figure out how to do this in my gruntfile.
Today, to do a build, I just need to run
grunt --foo=bar build
or
grunt --foo=baz build
What I'd like to, and have tried to do, is create a build-all target that uses an array to define the foo data, like so:
grunt.registerTask('build-all', function() {
var foos = ["bar", "baz"];
for (var i in foos) {
grunt.config.set("foo", foos[i]);
grunt.task.run("build");
}
});
From the looks of things, it seems that the run task is non-blocking. And that means that "foo" is being set to "baz" before the first run, running it twice for the same value.
Is there a better way to set arguments/options in this situation? Or to run the task in a blocking way?
I recently ran up against this same issue, and wrote grunt-galvanize to help with this. Here's how it works, applied to your example:
grunt.registerTask('build-all', function() {
var foos = ["bar", "baz"];
var galvanizeConfig = [];
for (var i in foos) {
galavanizeConfig.push({configs: {foo: foos[i]}});
}
grunt.option('galvanizeConfig', galvanizeConfig);
grunt.task.run(['galvanize:build']);
});
This will run the build task with each of the options/configs specified in galvanizeConfig
.
PS. I also use grunt-multi for cases where I desire concurrency, but grunt-galvanize is a much simpler tool for cases where concurrency is not required.
这篇关于Grunt在For循环中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!