How to use grunt-contrib-uglify to gzip the js files also?

keeg picture keeg · Oct 25, 2013 · Viewed 9k times · Source

I'm running a grunt task:

uglify: {
    options: {
        report: 'gzip'
    },
    all: {
        expand: true,
        flatten: true,
        cwd: 'js/',
        src: ['*.js', '!*.min.js'],
        dest: 'js/min',
        ext: '.min.js'
    }

}

The files are compressed onto one file, while running the report option

options: {
    report: 'gzip'
}

I see that the files would be significantly smaller when gzipped, but the output files are not gzipped, they are the size as per the "minified" report.

So the question is, how do I configure uglify to gzip the files also. Or is this a task for a different task?

Answer

Ben picture Ben · Oct 25, 2013

gzipping is a technique used by a webserver to pack static assets, helping to reduce the size of transmitted data by half or more. The gzip report just lets you know how much the technique will save, but it is obviously unable to compress the file beyond normal minification. This post has some further information if you're interested:

http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/

Personally I'd turn gzip reporting off since it doesn't perform well; perhaps only save it for when you're ready to deploy.