Remove development gems from production with Bundler and Rails 4

做~自己de王妃 提交于 2019-12-05 21:45:20
Smudge

How do we remove development dependencies from production run-time?

This comment in the thread you referenced has instructions for enabling the old :assets group behavior:

Change Bundler.require(*Rails.groups) to Bundler.require(*Rails.groups(assets: %w[development test])) in config/application.rb, and add this to your rake tasks:

namespace :assets do
  # Override sprockets-rails task to put back assets group require, so as to
  # avoid memory bloat in web processes :-/
  task :environment do
    Bundler.require(:assets)
    Rake::Task['environment'].invoke
  end
end

Alternatively, what am I missing as to why this ability would be desirable/the default?

So, strictly speaking, these aren't development dependencies. You shouldn't really think of them that way, because asset precompilation should happen in a production environment, even if it's just during deploy. You definitely shouldn't be precompiling on your developer machine.

On top of that, the line between asset-specific gems and production gems has become more blurred since the earlier versions of the asset pipeline. For instance, many gems now expect a javascript interpreter to be available. Also, many Rails apps now use .coffee templates in views (instead of .js.erb), and because those can't be precompiled, coffeescript must be available in production.

Basically, as rails contributors started removing more and more gems from the :assets group, they realized that it didn't really need to exist anymore, and would simplify things if it just went away. It had only existed in the first place so to avoid unintended compilation-on-demand, but the asset pipeline was updated in Rails 4 to expect to serve only static assets by default.

In the end it may not have been the most memory-optimal decision (since you're requireing a bunch of gems you don't ever use) but it was the most universally compatible.

Edit: Also refer to this question for more discussion/answers to this question.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!