Skip to content

Instantly share code, notes, and snippets.

@ehynds
Created August 6, 2012 15:16
Show Gist options
  • Save ehynds/3275345 to your computer and use it in GitHub Desktop.
Save ehynds/3275345 to your computer and use it in GitHub Desktop.
Grunt task to remove console logging
grunt.registerMultiTask("removelogging", "Remove console logging", function() {
var done = this.async();
var exec = require("child_process").exec;
var options = this.data.options || {};
grunt.file.expandFiles(this.data.files).forEach(function(file) {
var cmd = "sed -E 's/console\.(log|warn|error|assert|count|clear|group|groupEnd|trace|debug|dir|dirxml|profile|profileEnd|time|timeEnd)\((.*)\);?//g' " + file;
exec(cmd, options, function(error, stdout) {
if(error !== null) {
grunt.log.error('exec error: ' + error);
return;
}
grunt.file.write(file, stdout);
done();
});
});
});
grunt.initConfig({
removelogging: {
dist: {
files: [ "dist/file.js" ],
// options to pass to exec: http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
options: {
maxBuffer: 1024 * 1024
}
}
}
});
@patrickod
Copy link

For those who arrived at this through a Google search there's a more recently updated npm module that does this https://github.com/ehynds/grunt-remove-logging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment