How do I minify CSS in Rails 4? How do I minify CSS in Rails 4? ruby ruby

How do I minify CSS in Rails 4?


I was having the same problem in my production environment, where I couldn't get the CSS to minify upon deploying to Heroku. After turning on compression with the following:

production.rb

config.assets.css_compressor = :sass

Gemfile

gem 'sass-rails', '~> 4.0.0'    

I managed to get it to minify by updating the assets version:

production.rb

config.assets.version = '1.1' # was '1.0'

Doing a few tests afterwards, I found that updating the source CSS/SASS had the same effect. So try updating your stylesheets (as opposed to only the config), which should "kickstart" the minification process when Heroku precompiles your assets after you push, without needing to update the assets version.


Precompile

You'll need to precompile the assets

Rails minifies your assets if you precompile them. This is only for production, but means you're able to use files such as application.js and application.css with minified code

Try this:

$ rake assets:precompile RAILS_ENV=production$ git add .$ git commit -a -m "Precompiled Assets"$ git push heroku master

This will precompile (& minify) your assets, allowing you to use the compiled files in production