Skip to content

Instantly share code, notes, and snippets.

@elijahmanor
Last active January 30, 2023 01:33
Show Gist options
  • Save elijahmanor/7884984 to your computer and use it in GitHub Desktop.
Save elijahmanor/7884984 to your computer and use it in GitHub Desktop.
// Avoid `console` errors in browsers that lack a console.
(function() {
var method;
var noop = function () {};
var methods = [
'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
'timeStamp', 'trace', 'warn'
];
var length = methods.length;
var console = (window.console = window.console || {});
while (length--) {
method = methods[length];
// Only stub undefined methods.
if (!console[method]) {
console[method] = noop;
}
}
}());
(function( etm, undefined ) {
var myVariable = 0;
etm.init = function() {
myVariable += 1;
};
function secret() {
myVariable += 2;
};
}( window.etm = window.etm || {} ));
(function( etm, undefined ) {
var myVariable = 0;
console.log( "test 1" );
etm.init = function() {
console.log( "test 2" );
myVariable += 1;
};
function secret() {
console.log( "test 3" );
myVariable += 2;
};
}( window.etm = window.etm || {} ));
module.exports = function( grunt ) {
grunt.initConfig({
pkg: grunt.file.readJSON( "package.json" ),
removelogging: {
dist: {
src: "src/<%= pkg.name %>.js",
dest: "build/<%= pkg.name %>.clean.js",
options: {}
}
}
});
grunt.loadNpmTasks( "grunt-remove-logging" );
grunt.registerTask( "default", ["removelogging"] );
};
debug.log( "test" ); // Level 1
debug.debug({ msg: "test" }); // Level 2
debug.info( false ); // Level 3
debug.warn( 42 ); // Level 4
debug.error( [1, 2, 3] ); // Level 5
# Uglify
uglifyjs --define DEBUG=false "hello.js" > "hello.min.js"
# Uglify2
uglifyjs --compress --define DEBUG=false hello.js -o hello.min.js
if ( typeof DEBUG === "undefined" ) DEBUG = true;
function sayHello() {
DEBUG && console.log( "Hello World!" );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment