Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell UglifyJS to only compress and mangle all files except some which I only want concatenated?

Is it possible to specify and array of files that I want compressed and mangled (default Uglify behavior), but also a list of files that should not be touched, just concatenated?

Thanks.

like image 586
Francisc Avatar asked Mar 20 '14 13:03

Francisc


1 Answers

You can solve this in different ways. I'm posting an extended example to illustrate what one can do:

uglify: {
    doAll: {
        options: {
            banner: '// <%= pkg.name %> - v<%= pkg.version %> - ' + '<%= grunt.template.today("yyyy-mm-dd HH:mm:ss") %>\n\n',
            mangle: {
                except: [ // mangle is true for all else besides the specified exceptions
                    'src/input-d.js',
                    'src/input-e.js',
                    'src/input-f.js'
                ]
            },
            preserveComments: 'some'
        },
        files: 'dest/output.min.js': [ // concatenation, uglification (mangle) with exceptions, block comments preserved, minification and a banner
            'src/input-a.js',
            'src/input-b.js',
            'src/input-c.js',
            'src/input-d.js',
            'src/input-e.js',
            'src/input-f.js'
        ]
    },
    concatenateOnly: {
        options: {
            compress: false,
            mangle: false,
            preserveComments: 'all'
        },
        files: 'dest/output.js': [ // only concatenation
            'src/input-a.js',
            'src/input-b.js',
            'src/input-c.js',
            'src/input-d.js',
            'src/input-e.js',
            'src/input-f.js'
        ]
    }
}

The concatenateOnly task would do exactly what you wanted, only concatenate. You could specify which files would be concatenated there. You could run both concatenateAll and doAll at the same time by using the watch task:

watch: {
    js: {
        files: ['config/*.js', 'app/js/**/*.js'],
        tasks: ['jshint', 'jasmine', 'uglify:concatenateOnly', 'uglify:doAll']
    }
}

...or you could do a single task by combining some of the settings I pasted above, like using the options.mangle.except to your benefit.

like image 147
Wallace Sidhrée Avatar answered Sep 24 '22 02:09

Wallace Sidhrée