grunt.registerTask不能修改全局的grunt任务设置

下面的代码读取app/modules/ (例如app / modules / module1 / js /,app / modules / module2 / js /,aso)中每个子目录js的内容。

这个脚本之前没有使用最后的命令grunt.task.run('concat:' + dir); 。 有一段时间它停止工作,所以我不得不添加一个调用forEach循环内的任务concat。

通常我会将新configuration保存在concatconfiguration中,稍后再调用最终的concat任务。

 grunt.registerTask('preparemodulejs', 'iterates over all module directories and compiles modules js files', function() { // read all subdirectories from your modules folder grunt.file.expand('./app/modules/*').forEach(function(dir){ // get the current concat config var concat = grunt.config.get('concat') || {}; // set the config for this modulename-directory concat[dir] = { src: [dir + '/js/*.js', '!' + dir + '/js/compiled.js'], dest: dir + '/js/compiled.js' }; // save the new concat config grunt.config.set('concat', concat); grunt.task.run('concat:' + dir); // this line is new }); }); 

在最近的版本中究竟发生了什么变化,我必须添加一个明确的task.run行?

有没有办法将这个任务的设置写入现有的concat任务的设置中,这样如果我有其他的手动添加到那个configuration,那么这些不会被扫描的每个目录都运行?

感谢帮助。

grunt.task.run(); 尽pipe它的名字,不运行任务。 Grunt始终是同步的,所以grunt.task.run()排队任务在当前任务完成后运行。

所以我会避免在一个数组中使用grunt.task.run() ,而是build立一个随后要运行的任务/目标列表:

 grunt.registerTask('preparemodulejs', 'iterates over all module directories and compiles modules js files', function() { var tasks = []; // read all subdirectories from your modules folder grunt.file.expand('./app/modules/*').forEach(function(dir){ // get the current concat config var concat = grunt.config.get('concat') || {}; // set the config for this modulename-directory concat[dir] = { src: [dir + '/js/*.js', '!' + dir + '/js/compiled.js'], dest: dir + '/js/compiled.js' }; // save the new concat config grunt.config.set('concat', concat); tasks.push('concat:' + dir); }); // queues the tasks and run when this current task is done grunt.task.run(tasks); }); 

我们也可以直接在这里提供一个configuration,用于执行不同的任务,以便在具有多个模块的较大项目上运行。 即使我们需要处理根目录之外的文件:

 grunt.registerTask('publishapp', 'uglify ivapp.js and upload to server', function (){ var tasks = []; grunt.file.expand('../outerdirectory/').forEach(function(dir) { // config for uglify that needs to execute before uploading on server var uglify = { options: { compress: { drop_console: true, }, banner: '/* Banner you want to put above js minified code. */\n' }, all: { files: [{ expand: true, cwd: '../', src: ['somedir/some.js'], dest: 'build', ext: '.js', extDot: 'last' }] } }; // set grunt config : uglify grunt.config.set('uglify', uglify); }); // prepare a tasks list tasks.push('uglify:all'); tasks.push('exec:publish'); // execute a tasks to perform grunt.task.run(tasks); });