Stripping Debug Functions in Javascript

UglifyJS2is a great tool for processing your Javascript source files for a production system. Compressing files means your page loads noticeably faster, even to Google, which means your page could rank higher than other similar websites.

I was tasked with doing some advanced compression of our Javascript source files at work, integrating the step into our PHP framework, Symfony2. We have a custom console.log wrapper called debug, and we wanted to remove all occurrences of this call in our combined, compressed final file.

Uglify has a compressor flag that you must pass into the binary which enables the compressor and its default settings. When the compressor flag is passed, it also opens up a number of compression options that you can use to customize your processing operation.

pure_funcs is one such option. pure_funcs will remove any occurrences of the specified function name if the variable that it was bound to is never used, or if the function call is not bound to a variable. This makes it perfect to strip out the debug calls. The documentation is slightly hazy on how to pass these options and their values, so hopefully this post can clear up any issues for future developers.

$ uglifyjs -mc 'pure_funcs="debug"' -o out.js -- in.js

When you pass the compressor flag -c or --compress, you then immediately pass in a STRING of options. In the above example, pure_funcs is passed an "array of names" (as the documentation says, though it's probably referring to grunt/js invocation of the binary). To pass more than a single function name, write it like so: 'pure_funcs="debug,Math.floor"'.

Note that in the above command line call, I combined -mc, which is shortform for --mangle --compress.

Here is an example, using the exact uglifyjs call from above. This is precisely what we were looking for.


(function() {
    function debug() {


     * WAT

    debug('lol', 'this', 'does', 'not', 'work');

    var a = 'lol';

    debug(a, 'nowai');

    function laugh(msg) {
        return msg + '... HAHAHAHAHA!';

    var b = laugh('SYMFONY FORMS?');

    function dead() {}


!function(){function n(n){return n+"... HAHAHAHAHA!"}function o(){}Math.floor(25.11),n("SYMFONY FORMS?");o()}();