Search code examples
javascriptangularjsgruntjsgrunt-contrib-uglify

Grunt uglify task goes blank/idle


I am very new to task-runners in JS and this is my first attempt with GruntJS. In my Gruntfile.js I have kept several tasks like jshint, cssmin etc. I am able to run them all from command line, but when I run `grunt uglify' there is no response, the cursor goes to another line and remains so for hours. No error message is shown as well.

Below is my relevant code, I have tried various uglify configuration as I saw different users providing a different set of properties

//uglify:{
        //    options: {
        //        mangle : {
        //            except : ['jQuery', 'angular']
        //        },
        //        compress: {
        //            drop_console : true
        //        },
        //        banner: '*****Minified file for Pricing Plan*****',
        //        footer: '*********File ends**********',
        //        preserveComments: false
        //    },
        //    my_target:{
        //        options: {
        //            beautify: true,
        //            files: {
        //                'scripts/pp.min.js': ['scripts/pp.js']
        //            }
        //        }
        //    }
        //},
        uglify: {
            options: {
                banner: '*****Minified file for Pricing Plan*****'
            },
            dist: {
                src: 'scripts/pp.js',
                dest: 'scripts/pp.min.js'
            }
        },


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

grunt.registerTask('uglify', ['uglify']);

On command line I am running grunt uglify

Can someone suggest whats wrong here and the way to rectify it.

Just if required: Its an Angular1.x.x project

EDIT: I tried installing uglifyJs and ran the uglifyjs command. It successfully uglifies and minifies my code. So is there any thing that needs to be done apart from what I have above.


Solution

  • but when I run `grunt uglify' there is no response, the cursor goes to another line and remains so for hours.

    The Issue:

    I think the issue is related to how you are registering your uglify task. This following line of code:

    grunt.registerTask('uglify', ['uglify']); //<-- Issue is here.
    

    How to fix it:

    When creating an Alias Task using grunt.registerTask() avoid naming the taskName the same as any of the items/tasks defined in the taskList array.

    grunt.registerTask('minify', ['uglify']); //<-- Correct.
    

    NOTE: Although in the example code above I have changed the taskName to minify it can be any valid name you prefer. Just ensure whatever taskName you choose does not also exist as one of the items in the taskList array.

    You now run the revised registered uglify task via CLI as follows:

    $ grunt minify

    Gruntfile.js

    Here is the revised Gruntfile.js:

    module.exports = function(grunt) {
    
        grunt.initConfig({
            uglify: {
                options: {
                    banner: '/*****Minified file for Pricing Plan*****/'
    
                    // ... <-- Your other options
                },
                dist: {
                    src: 'scripts/pp.js',
                    dest: 'scripts/pp.min.js'
                }
            }
        });
    
        grunt.loadNpmTasks('grunt-contrib-uglify');
        grunt.registerTask('minify', ['uglify']);
    };
    

    (Note: Also added forward slashes to your banner string to produce a valid JavaScript comment in the resultant file)