Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instruct Ajax Minifier to remove console.log from javascript

I have lines in my js files like this

console.log('FunctionName()');

The default Ajax Minifier settings do not remove these lines from the .min.js output.

I noticed in this discussion a conversation about Kill switches.

Looking at the Kill Switch page here. I noticed there is this switch:

/// <summary>
/// remove "debug" statements
/// </summary>
StripDebugStatements = 0x0000000000800000,

I am not using the command line, I am referencing the DLL. This is how I have implemented it.

CodeSettings jsSettings = new CodeSettings()
            {
                KillSwitch = 800000,
            };

and then later the actual minifier method.

string fileMinified = minifier.MinifyJavaScript(fileSource, jsSettings);

How can i remove console.log()?

like image 567
Valamas Avatar asked Mar 22 '12 05:03

Valamas


People also ask

Why do we need to minify JavaScript?

Minification is the process of minimizing code and markup in your web pages and script files. It's one of the main methods used to reduce load times and bandwidth usage on websites. Minification dramatically improves site speed and accessibility, directly translating into a better user experience.

How do I get rid of console errors?

Alternatively you can call console. clear() to remove all existing items within the console. However I wouldn't advise doing either of these. A much better approach would be to fix your codebase so that you avoid errors and implement graceful error handling where unexpected errors may occur.


1 Answers

Make you calls to console.Log from methods in "Debug" namespace ( http://ajaxmin.codeplex.com/wikipage?title=Preprocessor )

Sample:

var Debug = {};
Debug.myTrace = function(message){
 console.log(message);
};

///#DEBUG 
someDebugOnlyCode();
///#ENDDEBUG 

All calls to Debug.myTrace will be removed during minification ("debug" namespace), as well as call to someDebugOnlyCode (inside DEBUG/ENDDEBUG comments).

like image 171
Alexei Levenkov Avatar answered Oct 17 '22 02:10

Alexei Levenkov