Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 266 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can uglify-js remove the console.log statements?

#1
I'm using [uglify-js][1] 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.

[1]:

[To see links please register here]

Reply

#2
You can use [Groundskeeper][1] to do this though it will be a separate step.


[1]:

[To see links please register here]

Reply

#3
In the lastest uglify-js ( v2.4.3), a new compress option ‘pure_funcs’ is added. If I add the console.log functions to this array, it will be removed in the minified js file. The test code below shows how this option works. This is exactly what I want.

// file: m.js
var uglify = require('uglify-js');
var originalSourceCode = 'var name = function(){var str = "test"; return str}; console.log("log data" + name());';
var minifiedCode = uglify.minify(originalSourceCode, {
fromString : true,
mangle: {},
warnings: true,
compress:{
pure_funcs: [ 'console.log' ]
}
});
console.log(minifiedCode);

$node m.js
WARN: Dropping side-effect-free statement [?:1,53]
{ code: 'var name=function(){var n="test";return n};',
map: 'null' }

Quotes from

[To see links please register here]


> pure_funcs -- default null. You can pass an array of names and UglifyJS will assume that those functions do not produce side effects.
> DANGER: will not check if the name is redefined in scope. An example
> case here, for instance var q = Math.floor(a/b). If variable q is not
> used elsewhere, UglifyJS will drop it, but will still keep the
> Math.floor(a/b), not knowing what it does. You can pass pure_funcs: [
> 'Math.floor' ] to let it know that this function won't produce any
> side effect, in which case the whole statement would get discarded.
> The current implementation adds some overhead (compression will be
> slower).
Reply

#4
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][1]


[1]:

[To see links please register here]

Reply

#5
for **-c** option, set **drop_console** to be **true**:
```
uglifyjs app.js -m -c drop_console=true -o app.min.js
```
Reply

#6
If you are using gulp, use 'gulp-strip-debug' to remove console, alert and debugger.

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through