如何在forEach中调用的Grunt任务中使用动态值? [英] How to use dynamic values in Grunt tasks called from inside a forEach?

查看:189
本文介绍了如何在forEach中调用的Grunt任务中使用动态值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使用grunt.config.set为动态concatinated字符串运行grunt任务。这些grunt.configs被设置在一个forEach循环中,并且在任务运行之前每次都进行更改。

不幸的是这种方法不行,因为grunt只使用最后的grunt.config .set和运行它多次使用相同的值。

看到这个例子与一个简单的复制任务(复制只是一个例子,我们想在其他任务中也使用这种动态选项):

pre $ copy $ {
projectFiles {
文件:[{
src:'src /<%= directoryName%> / *',
dest:'build /<%= directoryName%> / *'
}]



grunt.registerTask('copyFiles',function(){
var projects = ['directory1','directory2','directory3 ','directory4'];

projects.forEach(function(project){
grunt.config.set(directoryName,project);
grunt.task.run ('copy:projectFiles');
});
});

这个任务复制了四次src / directory4。

是否有可能建立这种使用动态值的任务?这将是很好,因为唯一的解决办法是用静态字符串多次复制每个任务。



谢谢!
Daniel

解决方案

问题在于 grunt.task.run 不会立即运行任务,只是将其推送到稍后运行的任务列表中,而立即执行 grunt.config.set 。所以你最终得到一个执行相同任务的4倍的列表。



你可以通过动态定义不同的目标来得到你想要的,然后运行它们, :
$ b $ pre $ g $ g $ c $ g $ g $ g $ g $ g $ g $ g $ g $% (project){
grunt.config.set(copy。+ project,{
文件:[{
src:'src /'+ project +'/ *',
dest:'build /'+ project +'/'
}]
} );
grunt.task.run('copy:'+ project);
});
});


We are trying to run grunt tasks using grunt.config.set for dynamically concatinated strings. These grunt.configs are set in a forEach loop and change each time before the task is run.

This does unfortunately not work, as grunt only uses the last grunt.config.set and runs it multiple times with that very same value.

See this example with a simple "copy" task ("copy" is just an example, we want to use this kind of dynamic options in other tasks, too):

copy: {
    projectFiles : {
        files : [{
            src: 'src/<%= directoryName %>/*',
            dest: 'build/<%= directoryName %>/*'
        }]
    }
}   

grunt.registerTask('copyFiles', function() {
    var projects = ['directory1','directory2','directory3','directory4'];

    projects.forEach(function(project){
        grunt.config.set("directoryName", project);
        grunt.task.run('copy:projectFiles');
    });
});

This tasks copies four times src/directory4.

Is it somehow possible to build that kind of tasks which use dynamic values? It would be nice, as the only other solution would be to copy each task multiple times with static strings.

Thank you! Daniel

解决方案

The issue is that grunt.task.run does not run the task immediately, it just pushes it to the list of tasks to run later, while grunt.config.set executes immediately. So you end up with a list of 4 times the same task to execute.

You can get what you want by defining different targets dynamically, then running them, something like below:

grunt.registerTask('copyFiles', function() {
    var projects = ['directory1','directory2','directory3','directory4'];

    projects.forEach(function(project){
        grunt.config.set("copy." + project, {
            files : [{
                src: 'src/' + project + '/*',
                dest: 'build/' + project + '/'
            }]
        });
        grunt.task.run('copy:' + project);
    });
});

这篇关于如何在forEach中调用的Grunt任务中使用动态值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆