簡體   English   中英

grunt.registerTask無法修改全局grunt任務設置

[英]grunt.registerTask can not modify global grunt task settings

以下代碼讀取app/modules/中每個子目錄js的內容(例如app / modules / module1 / js /,app / modules / module2 / js /,aso。)

這個腳本在沒有使用最后一個命令grunt.task.run('concat:' + dir);之前工作grunt.task.run('concat:' + dir); 一段時間它現在停止工作,所以我不得不在forEach循環內添加對任務concat的調用。

通常我會在concat配置中保存新配置,稍后調用生成的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任務的設置中,這樣如果我對該配置有其他手動添加,那么對於掃描的每個目錄都不會運行這些設置?

感謝幫助。

grunt.task.run(); 盡管它的名字,不運行任務。 Grunt始終是同步的,因此grunt.task.run()會將任務排隊,以便在當前任務完成后運行。

所以我會避免在數組中使用grunt.task.run() ,而是構建一個grunt.task.run()運行的任務/目標列表:

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);
});

我們還可以直接在這里提供配置,以便為具有多個模塊的更大項目隨時運行的不同任務。 即使我們需要處理根目錄之外的文件:

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);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM