如何遍历数组并运行 Grunt 任务,将数组中的每个值作为 Grunt 选项传递

2023-09-04前端开发问题
0

本文介绍了如何遍历数组并运行 Grunt 任务,将数组中的每个值作为 Grunt 选项传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个类似于以下的数组:

I have an array similar to the following:

var themes = grunt.option('themes') || [
    'theme1',
    'theme2',
    'theme3'
];

还有一个变量:

var theme = grunt.option('theme') || 'theme1';

这个值在我的 grunt 文件中的不同地方使用,例如确定某些资产的路径等.

This value is used in various places in my grunt file for things such as determining the path to some assets etc.

长话短说,我运行以下命令来编译单个主题的资源:

To cut a long story short, I run the following command to compile the assets for a single theme:

grunt compile --theme=theme2

我正在寻找一种方法来循环遍历主题数组并使用适当的 grunt.option 运行 compile grunt 任务.从本质上讲,我希望实现的目标相当于:

I'm looking for a way to loop through the array of themes and run the compile grunt task with the appropriate grunt.option. Essentially, what I'm looking to achieve would be the equivalent of this:

grunt compile --theme=theme1 && grunt compile --theme=theme2 && grunt compile --theme=theme3

我尝试了以下方法:

grunt.registerTask('compile:all', function() {
    themes.forEach(function(currentTheme) {
        grunt.option('theme', currentTheme);
        grunt.task.run('compile');
    });
});

这会以适当的次数运行 compile 任务,但似乎没有设置 theme 选项.所以我的 Scss 文件生成了,但它们是空的.

This runs the compile task the appropriate number of times, but the theme option doesn't seem to get set. So my Scss files get generated, but they are empty.

我也试过这个:

grunt.registerTask('compile:all', function() {
    themes.forEach(function(currentTheme) {
        grunt.util.spawn({
            grunt : true,
            args  : ['compile', '--theme=' + currentTheme]
        });
    });
});

任务几乎立即完成并显示成功"消息,但它似乎没有做任何事情.

The task finishes almost instantly with a "success" message, but it doesn't appear to do anything.

我尝试的最后一件事与上面类似,除了我尝试使用异步:

The last thing I've tried is similar to the above, except I attempt to use async:

grunt.registerTask('compile:all', function() {
    themes.forEach(function(currentTheme) {
        var done = grunt.task.current.async();
        grunt.util.spawn({
            grunt : true,
            args  : ['compile', '--theme=' + currentTheme]
        }, done);
    });
});

但是这个任务失败了.我真的不确定我哪里出错了,

But this task fails. I'm not really sure where I'm going wrong,

感谢您的帮助

推荐答案

我认为你的问题是你的个人编译任务被 grunt.task.run('compile');,但是,当它们执行时,您的 themes.forEach 循环已经完成,并且您的 theme 选项设置为 themes 中的最后一个值.

I think your problem is that your individual compile tasks are getting queued-up by grunt.task.run('compile');, but, by the time they execute, your themes.forEach loop has completed and your theme option is set to the last value in themes.

我认为您需要注册一个单独的任务,该任务负责设置 theme 选项运行编译任务.

I think you will need to register a separate task that is responsible for setting the theme option and running the compile task.

grunt.registerTask('compile_theme', function (theme) {
    grunt.option('theme', theme);
    grunt.task.run('compile');
});

对于每个主题,您可以在 compile:all 任务中将此任务排入队列:

You would enqueue this task within your compile:all task for each of your themes:

themes.forEach(function(currentTheme) {
    grunt.task.run('compile_theme:' + currentTheme);
});

如果您希望能够在命令行中指定要编译的主题,则需要更新您的 compile:all 任务以读取所有 --theme= 参数并强制该值是一个数组:

If you want to be able to specify at the command-line which themes to compile, you would need to update your compile:all task to read all --theme= parameters and enforce that the value is an array:

grunt.registerTask('compile:all', function () {
    var compileThemes = grunt.option('theme') || 'theme1';

    if (grunt.util.kindOf(compileThemes) === 'string') {
        compileThemes = [compileThemes];
    }

    compileThemes.forEach(function(currentTheme) {
        grunt.task.run('compile_theme:' + currentTheme);
    });
});

您可以按如下方式调用该命令:

You would call the command as follows:

grunt compile:all // compiles 'theme1'
grunt compile:all --theme=theme2 // compiles 'theme2'
grunt compile:all --theme=theme2 --theme=theme3 // compiles 'theme2' and 'theme3'

注意:此时您可能想要重命名您的 compile:all 任务,因为它不再需要编译 all 主题.

Note: You would probably want to rename your compile:all task at this point because it no longer necessarily compiles all themes.

编辑

它不起作用,因为我们对 theme 选项的期望过高.我们正在尝试使用它来获取在命令行中输入的主题,以便在我们的配置中动态组合值(例如,dest: theme + '/app.js'.按照我构建答案的方式,theme 无法在配置中使用.

It is not working because we are expecting too much of the theme option. We are trying to use this to get the themes entered at the command-line and to compose values dynamically in our configuration (like, dest: theme + '/app.js'. With the way I have structured my answer, theme cannot be used in the configuration.

我会改为为 theme 使用配置变量,该变量将在配置中使用.这意味着更新 compile_theme 任务:

I would instead use a configuration variable for theme that will be used in the config. This would mean updating the compile_theme task:

grunt.registerTask('compile_theme', function (theme) {
    grunt.config('theme', theme);
    grunt.task.run('compile');
});

我们需要通过替换 theme 的模板字符串来更新我们的配置.例如:

We would need to update our configuration by substituting template strings for theme. For example:

dest: '<%= theme %>/app.js'

这篇关于如何遍历数组并运行 Grunt 任务,将数组中的每个值作为 Grunt 选项传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用百度地图API获取地理位置信息
首先,我们需要在百度地图开放平台上申请一个开发者账号,并创建一个应用。在创建应用的过程中,我们会得到一个密钥(ak),这是调用API的凭证。 接下来,我们需要准备一个PHP文件,以便可以在网页中调用。首先,我们需要引入百度地图API的JS文件,代码如下...
2024-11-22 前端开发问题
244

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

正则表达式([A-Za-z])为啥可以匹配字母加数字或特殊符号?
问题描述: 我需要在我的应用程序中验证一个文本字段。它既不能包含数字,也不能包含特殊字符,所以我尝试了这个正则表达式:/[a-zA-Z]/匹配,问题是,当我在字符串的中间或结尾放入一个数字或特殊字符时,这个正则表达式依然可以匹配通过。 解决办法: 你应...
2024-06-06 前端开发问题
165