<i id='RHfF8'><tr id='RHfF8'><dt id='RHfF8'><q id='RHfF8'><span id='RHfF8'><b id='RHfF8'><form id='RHfF8'><ins id='RHfF8'></ins><ul id='RHfF8'></ul><sub id='RHfF8'></sub></form><legend id='RHfF8'></legend><bdo id='RHfF8'><pre id='RHfF8'><center id='RHfF8'></center></pre></bdo></b><th id='RHfF8'></th></span></q></dt></tr></i><div id='RHfF8'><tfoot id='RHfF8'></tfoot><dl id='RHfF8'><fieldset id='RHfF8'></fieldset></dl></div>
  1. <tfoot id='RHfF8'></tfoot>

    <small id='RHfF8'></small><noframes id='RHfF8'>

    • <bdo id='RHfF8'></bdo><ul id='RHfF8'></ul>
      <legend id='RHfF8'><style id='RHfF8'><dir id='RHfF8'><q id='RHfF8'></q></dir></style></legend>

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

      时间:2023-09-04

        <small id='2mJVd'></small><noframes id='2mJVd'>

        • <bdo id='2mJVd'></bdo><ul id='2mJVd'></ul>

            <tbody id='2mJVd'></tbody>
                <i id='2mJVd'><tr id='2mJVd'><dt id='2mJVd'><q id='2mJVd'><span id='2mJVd'><b id='2mJVd'><form id='2mJVd'><ins id='2mJVd'></ins><ul id='2mJVd'></ul><sub id='2mJVd'></sub></form><legend id='2mJVd'></legend><bdo id='2mJVd'><pre id='2mJVd'><center id='2mJVd'></center></pre></bdo></b><th id='2mJVd'></th></span></q></dt></tr></i><div id='2mJVd'><tfoot id='2mJVd'></tfoot><dl id='2mJVd'><fieldset id='2mJVd'></fieldset></dl></div>
              • <legend id='2mJVd'><style id='2mJVd'><dir id='2mJVd'><q id='2mJVd'></q></dir></style></legend>

              • <tfoot id='2mJVd'></tfoot>
              • 本文介绍了如何遍历数组并运行 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 选项传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:当插入符号进入特定的 div/span/a 标签以及插入符号离开标签时触发事件 下一篇:在 Emberjs 中设置从路由到控制器错误的数据

                相关文章

                1. <small id='xvAc9'></small><noframes id='xvAc9'>

                  <legend id='xvAc9'><style id='xvAc9'><dir id='xvAc9'><q id='xvAc9'></q></dir></style></legend>
                  <tfoot id='xvAc9'></tfoot>

                    <bdo id='xvAc9'></bdo><ul id='xvAc9'></ul>

                    <i id='xvAc9'><tr id='xvAc9'><dt id='xvAc9'><q id='xvAc9'><span id='xvAc9'><b id='xvAc9'><form id='xvAc9'><ins id='xvAc9'></ins><ul id='xvAc9'></ul><sub id='xvAc9'></sub></form><legend id='xvAc9'></legend><bdo id='xvAc9'><pre id='xvAc9'><center id='xvAc9'></center></pre></bdo></b><th id='xvAc9'></th></span></q></dt></tr></i><div id='xvAc9'><tfoot id='xvAc9'></tfoot><dl id='xvAc9'><fieldset id='xvAc9'></fieldset></dl></div>