Bundler预编译与延迟编译

Bundler预编译与延迟编译

本文介绍了Rails / Bundler预编译与延迟编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails应用程序中的 config / application.rb 文件中,包含以下代码部分:

In the config/application.rb file in a Rails app, there's the following section of code:

if defined?(Bundler)
    # If you precompile assets before deploying to production, use this line
    Bundler.require *Rails.groups(:assets => %w(development test))
    # If you want your assets lazily compiled in production, use this line
    # Bundler.require(:default, :assets, Rails.env)
end

我可能不清楚在做。我的印象是它需要Gemfile中的指定部分,但是我不清楚为什么 Bundler.require * Rails.groups(...)会导致它进行预编译,然后 Bundler.require(...)导致资产被延迟加载。

I'm perhaps not clear what Bundler.require is doing. I was under the impression that it required the specified sections in the Gemfile, but I'm not clear as to why Bundler.require *Rails.groups(...) causes it to precompile and Bundler.require(...) causes assets to be lazily loaded.

推荐答案

这些行实际上并不会改变资产的使用方式。

These lines don't actually change how your assets are used.

第一行,

Bundler.require *Rails.groups(:assets => %w(development test))

仅在开发和测试环境中从资产组中加载宝石。这意味着 sass-rails uglifier 之类的产品将无法在生产中使用,这意味着您赢得了如果您正在使用这些宝石,就无法正确地在生产中即时编译/最小化/无论您使用什么资产。

only loads gems from the assets group in your development and test environment. This means that things like sass-rails and uglifier won't be available in production, which then means that you won't be able to properly compile/minify/whatever your assets on the fly in production if you're making use of those gems.

另一方面,

Bundler.require(:default, :assets, Rails.env)

将在任何环境中加载资产组,使这些宝石可用于生产中以进行资产编译/缩小/

will load the assets group in any environment, making those gems available in production to do asset compilation/minification/whatever on the fly.

因此,如上所述,这些行实际上并没有改变资产管道的行为-它只是意味着您应该使用第一个if您将要对资产进行预编译以进行生产,或者如果您要在生产中懒散地进行编译,则使用第二个资产。

So, as stated above, these lines don't actually change the behaviour of your asset pipeline - it simply means that you should use the first if you're going to precompile your assets for production, or use the second if you're going to lazily compile in production.

这篇关于Rails / Bundler预编译与延迟编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 20:23