Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did Rails4 drop support for "assets" group in the Gemfile

In Rails 3, gems used exclusively to generate assets in the asset pipeline were properly placed in the assets group of the Gemfile:

...

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails'
  gem 'coffee-rails'
  gem 'uglifier'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby
end

Now, according to the (still in progress) upgrade documentation:

Rails 4.0 removed the assets group from Gemfile. You'd need to remove that line from your Gemfile when upgrading.

Sure enough, making a new project with RC1 yields a Gemfile with asset-related gems included by default outside of any group:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0.rc1'

# Use sqlite3 as the database for Active Record
gem 'sqlite3'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0.rc1'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

...

Does this mean these gems will now be bundled in production builds by default? If so, why the change of heart? Is Rails 4 moving towards the dynamic generation of assets in production?

like image 906
jemmons Avatar asked May 06 '13 19:05

jemmons


3 Answers

Previously the assets group existed to avoid unintended compilation-on-demand in production. As Rails 4 doesn't behave like that anymore, it made sense to remove the asset group.

This is explained in more detail in the commit that changed that. I extracted some quotes with the actual answer.

Some gems can be needed (in production) like coffee-rails if you are using coffee templates and the fact that now assets are not precompiled on demand in production anymore.

(not precompiled on demand in production) Means that if you have that gems in production environment in 3.2.x and forget to precompile, Rails will do exactly what it does in development, precompile the assets that was requested. This is not true anymore in Rails 4, so if you don't precompile the assets using the tasks you will get a 404 when the assets are requests.

like image 166
Filipe Giusti Avatar answered Nov 24 '22 10:11

Filipe Giusti


Rails 4 try to force you to precompile your assets before deployment. You have to precompile your assets with

$ RAILS_ENV=production bundle exec rake assets:precompile

And why? I found this in Guide:

By default Rails assumes that assets have been precompiled and will be served as static assets by your web server.

(Source: http://edgeguides.rubyonrails.org/asset_pipeline.html#in-production)

But many time you have to use these 'assets' gems in production... for example, if you use a js.coffee file in your views directory, then Rails needs coffee compiler in production mode as well.

So I guess, the reason of this change is performance improvement... and looks more simple as well. :)

like image 36
Zoltan Avatar answered Nov 24 '22 09:11

Zoltan


We want coffeescript with AJAX (history), so coffee-rails moves out of the assets group.
sass-rails misbehaves (history), so it moves out of the assets group.

Axe the assets group.

like image 33
mockturtl Avatar answered Nov 24 '22 08:11

mockturtl