为什么 Rails4 放弃了对“资产"的支持?在 Gemfile 中分组 [英] Why did Rails4 drop support for "assets" group in the Gemfile

查看:23
本文介绍了为什么 Rails4 放弃了对“资产"的支持?在 Gemfile 中分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 从 Gemfile 中删除了资产组.升级时,您需要从 Gemfile 中删除该行.

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

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

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

...

这是否意味着这些 gem 现在将默认捆绑在生产版本中?如果是这样,为什么要改变主意?Rails 4 是否正在朝着生产中动态生成资产的方向发展?

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?

推荐答案

以前存在资产组是为了避免生产中意外的按需编译.由于 Rails 4 不再有那样的行为,因此删除资产组是有意义的.

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.

(不在生产​​中按需预编译)意味着如果你在 3.2.x 的生产环境中有那些 gems 并且忘记预编译,Rails 会做它在开发中所做的事情,预编译请求的资产.在 Rails 4 中不再如此,因此如果您不使用任务预编译资产,当资产是请求时,您将得到 404.

(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.

这篇关于为什么 Rails4 放弃了对“资产"的支持?在 Gemfile 中分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆