Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove calls to console.* #293

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

sjhewitt
Copy link

@sjhewitt sjhewitt commented Jan 6, 2012

This change removes calls to console.* statements as requested in #124 (I couldn't get this code to attach to that issue...)

It does so by replacing statements that call console methods with an empty block to be removed later by the squeeze function.

The caveat is that it only fully removes console function calls if they are executed as statements. If the calls are made elsewhere in the tree then they will just be replaced with 0. For example, the following boolean expression:

console.log("a") && console.log("b")

will be compressed to:

0&&0

Also, the function arguments will be removed so users should be aware that if an argument is a function call, it won't be executed. For example, the foo function call will be totally removed in the following example:

console.log("this is bad", foo())

This can be executed from the commandline by adding the --no-console flag

@davidlevy
Copy link

Do you know when it will be committed in the master ?

@iliakan
Copy link

iliakan commented May 29, 2012

Looks good so far.. How about to make it external to uglify? So I can remove anything or can modify it.. Maybe add hooks to default uglifier? Right now I can replace default squeezer by hijacking require(..)

e.g uglify-console.js:

var uglify = require("uglify-js"), 
    pro = uglify.uglify;

pro.ast_squeeze_console = function(ast) {
        var w = pro.ast_walker(), walk = w.walk, scope;
         return w.with_walkers({
                "stat": function(stmt) {
                        if(stmt[0] === "call" && stmt[1][0] == "dot" && stmt[1][1] instanceof Array && stmt[1][1][0] == 'name' && stmt[1][1][1] == "console") {
                                return ["block"];
                        }
                        return ["stat", walk(stmt)];
                },
                "call": function(expr, args) {
                        if (expr[0] == "dot" && expr[1] instanceof Array && expr[1][0] == 'name' && expr[1][1] == "console") {
                                return ["atom", "0"];
                        }
                }
        }, function() {
                return walk(ast);
        });
};

var ast_squeeze = pro.ast_squeeze;

pro.ast_squeeze = function() {
  var ast = ast_squeeze.apply(this, arguments);
  ast = pro.ast_squeeze_console(ast);
  return ast;
}

require('./uglify.js');

@sjhewitt
Copy link
Author

@iliakan I'm not convinced by that method of monkey-patching the ast_squeeze function. If you're going to be using it from within some node script you have control over, you might as well include the ast_squeeze_console function and make your own custom version of the uglify function that is in uglify.js:

var uglify = require('uglify-js');
function ast_squeeze_console(){ ... };

function myUglify(orig_code, options){
    options || (options = {});
    var jsp = uglify.parser;
    var pro = uglify.uglify;

    var ast = jsp.parse(orig_code, options.strict_semicolons); // parse code and get the initial AST
    ast = ast_squeeze_console(ast); // get rid of console statements
    ast = pro.ast_mangle(ast, options.mangle_options); // get a new AST with mangled names
    ast = pro.ast_squeeze(ast, options.squeeze_options); // get an AST with compression optimizations
    var final_code = pro.gen_code(ast, options.gen_options); // compressed code here
    return final_code;
};

For command-line usage via bin/uglify some kind of plugin architecture could be useful, but I don't hold out much hope of it being included.

@simenbrekken
Copy link

I believe you can simply use sed to comment out any console statements before passing the script to UglifyJS.

sed -E 's/(console\.)/\/\/\1/g' script.js | uglifyjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants