javascriptnode.jsgruntjsgrunt-contrib-uglify

How to create many individual uglify tasks in grunt?


when I try to create many uglify tasks I get errors. I have to comment and uncomment one task in order to get one task working correctly. Hope someone can help me. I need two sperate minified .js files created using this plugin but I can't figure it out. if it's possible to create both tasks in one task command that would create two sperate minified .js files like below that would be also great. Looking forward to ideas, please.

module.exports = function(grunt){

grunt.loadNpmTasks('grunt-contrib-uglify');

    grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify:{
        t1:{
            files:{
                'build/scripts1.js' : ['abc.js', 'xc.js']

            }
        }
    },

    uglify:{
        t2:{
            files:{
                'build/scripts2.js' : ['123.js', 63a5.js]

            }
        }
    }
    });
};

Solution

  • You need to add both Targets t1 and t2 to one single uglify Task as follows:

    Gruntfile.js

    module.exports = function (grunt) {
    
      grunt.loadNpmTasks('grunt-contrib-uglify');
    
      grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
    
        uglify:{
          t1:{
            files:{
              'build/scripts1.js' : [ 'abc.js', 'xc.js' ]
            }
          },
          t2:{
            files:{
                'build/scripts2.js' : [ '123.js', '63a5.js' ]
            }
          }
        }
      });
    
      grunt.registerTask('default', ["uglify"]);
      grunt.registerTask('foo', ["uglify:t1"]);
      grunt.registerTask('quux', ["uglify:t2"]);
    };
    

    Running grunt

    The Gruntfile.js above has three registerTask's, namely; default, foo, and quux.

    Both foo and quux use the colon notation (I.e. "uglify:t1" and "uglify:t2") to reference only one of the Targets.

    The default task references only uglify which will result in all targets (however many) in the uglify target being run.