Rails4为什么放弃了对 Gemfile“资产”集团的支持

在 Rails 3中,专门用于在资产管道中生成资产的 gem 被恰当地放置在 Gemfile 的 assets组中:

...


# 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

现在,根据(仍在进行中的) 升级文件:

Rails 4.0从 Gemfile 删除了资产组,升级时需要从 Gemfile 中删除这一行。

毫无疑问,使用 RC1创建一个新项目会产生一个 Gemfile,其中默认包含任何组之外的资产相关的 gem:

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


...

这是否意味着默认情况下这些精华将被捆绑到生产构建中?如果是这样,为什么改变主意了?Rails 4是否正朝着生产中动态生成资产的方向发展?

23310 次浏览

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. :)

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.

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.