Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove console.log on assets precompile

During assets:precompile the javascript is minified, but console.logs are left in.

is there a way to remove all console.logs on precompile when the code is pushed to production?

like image 327
w2bro Avatar asked Aug 01 '12 17:08

w2bro


People also ask

What does rake assets Precompile do?

rake assets:precompile. We use rake assets:precompile to precompile our assets before pushing code to production. This command precompiles assets and places them under the public/assets directory in our Rails application.

What is Require_tree?

The require_tree directive tells Sprockets to recursively include all JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file.

What does console log () do?

The console. log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console.

What is assets in Ruby on Rails?

Assets are basically: javascript, stylesheets, fonts, and images which are part of the site design itself, not part of the user-uploaded content.


2 Answers

As of Uglifier 2.4.0 the :compress option includes support for :drop_console which means you can easily remove all console.* functions using something like this in your config/environments/production.rb file:

# Compress JavaScripts
config.assets.compress = true
config.assets.js_compressor = Uglifier.new(
  # Remove all console.* functions
  :compress => { :drop_console => true }
) if defined? Uglifier
like image 170
cousinmayan Avatar answered Oct 12 '22 15:10

cousinmayan


You could add this to application.js.erb. This solution would prevent any logging to console.log() in production environment, period. But it will still allow logging to console.error().

<% if Rails.env.production? %>
  window.console = {};
  window.console.log = function(){};
<% else %>
  // the || in the code ensures IE compatability
  window.console = window.console || {};
  window.console.log = window.console.log || function(){};
<% end %>
like image 22
Hengjie Avatar answered Oct 12 '22 15:10

Hengjie