Last Updated: February 25, 2016
·
985
· antage

Eliminate javascript debug code after assets compiling (Rails 4 only)

Uglifier is able to eliminate unreachable code, so we can get rid of any 'console.log' lines after assets compiling.

app/assets/javascripts/application.js:

if (typeof DEBUG === "undefined")
    DEBUG = true;

if (DEBUG)
    console.log("This is debug message.");

config/environments/production.rb:

config.assets.js_compressor = Uglifier.new(define: { DEBUG: false })
$ RAILS_ENV=production rake assets:clobber assets:precompile
$ find public/assets/ -name "application-*.js" -exec grep console.log {} \; wc -l
0

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside