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?
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.
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.
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.
Assets are basically: javascript, stylesheets, fonts, and images which are part of the site design itself, not part of the user-uploaded content.
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
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 %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With