Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm using uglify-js to minify the source code. I want to remove the console.log statements of the original source code. Is it possible? Or is there any other compressor tool supports this?

I use the code as below in Node.js.

var uglify = require('uglify-js');
var originalSourceCode = 'var name = function(){var str = "test"; return str}; console.log("log data");';
var minifiedCode = uglify.minify(originalSourceCode, {
                fromString : true,
                mangle: {},
                warnings: true
            });
console.log(minifiedCode);

The output is:

$node m.js
{ code: 'var name=function(){var a="test";return a};console.log("log data");',
  map: 'null' }

In the minified code the console.log isn't removed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
460 views
Welcome To Ask or Share your Answers For Others

1 Answer

There's also another option called drop_console which has been added recently (late 2013)

drop_console -- default false. Pass true to discard calls to console.* functions

This is added to the grunt init config like this:

grunt.initConfig({
  uglify: {
    options: {
      compress: {
        drop_console: true // <-
      }
    },
    my_target: {
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

As taken from the grunt-contrib-uglify github documents


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...