简体   繁体   English

GruntJS,多个任务和grunt.option

[英]GruntJS, Multiple Tasks and grunt.option

I am starting to use GruntJS for one of my projects. 我开始将GruntJS用于我的一个项目。 I have easily accomplished to write a simple build script using simple aliases. 我已经轻松完成了使用简单别名编写简单构建脚本的过程。

However my script contains many tasks that are basically the same, the only difference are some parameters like the source folder and destination folder. 但是,我的脚本包含许多基本相同的任务,唯一的区别是一些参数,例如源文件夹和目标文件夹。

For example: 例如:

    sass: {
        options:{
            trace: true,
            debugInfo: true,
            style: 'compressed'
        },
        html: {
            files: {
                'build/html/css/main.css': 'sass/html.sass'
            }
        },
        html2: {
            files: {
                'build/html2/css/main.css': 'sass/html2.sass'
            }
        },
        html3: {
            files: {
                'build/html3/css/main.css': 'sass/html3.sass'
            }
        }
    }

What I would like to achieve is to have only one task and then pass parameters (dest,src) to that task. 我要实现的是仅拥有一个任务,然后将参数(dest,src)传递给该任务。

I tried to implement this using MultiTasks: 我尝试使用MultiTasks来实现这一点:

grunt.registerTask('sass2', 'Run all Sass compilation tasks.', function() {
    var projects = ['html','html2','html3'];

    projects.forEach(function(proj){
        grunt.config.set('sass.files.dest', 'build/' + proj + '/css/main.css');
        grunt.config.set('sass.files.src', 'sass/' + proj + '.sass');
        grunt.log.writeln(grunt.config.get('sass.files.dest'));
        grunt.log.writeln(grunt.config.get('sass.files.src'));
        grunt.task.run('sass');

    });
});

Grunt log outputs the right values for the params, however only the html3 sass gets compiled. Grunt日志为参数输出正确的值,但是仅html3 sass被编译。

I do not understand why only one of the projects gets compiled and how could I fix this. 我不明白为什么只有一个项目会被编译,我该如何解决。

Perhaps there is another way to solve this problem. 也许还有另一种方法可以解决此问题。 A better way. 更好的方法。 Maybe using templates? 也许使用模板?

Any kind of help or tips would be appreciated. 任何帮助或技巧将不胜感激。

Thank you! 谢谢!

Only the last configuration is used because grunt.task.run queues them to run after the current task is complete. 仅使用最后一个配置,因为grunt.task.run将它们排队以当前任务完成运行。 From the API : API中

Enqueue one or more tasks. 排队一个或多个任务。 Every specified task in taskList will be run immediately after the current task completes, in the order specified 当前任务完成后,taskList中的每个指定任务将按照指定的顺序立即运行

What you have is: 您拥有的是:

  • Set config 1 设置配置1
  • Set config 2 设置配置2
  • Set config 3 设置配置3
  • Run three tasks using the currently active config, which is #3. 使用当前活动的配置#3运行三个任务。

Instead, you can do something like this to dynamically create lots of config sections and then run those tasks: 相反,您可以执行以下操作来动态创建许多配置节,然后运行这些任务:

grunt.config.set('sass.' + proj + '.files', [{
    src: 'sass/' + proj + '.sass',
    dest: 'build/' + proj + '/css/main.css'
}]);

and then call runtask with your newly-created section: 然后使用新创建的部分调用runtask:

grunt.task.run('sass:' + proj);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM