Rails 5: rails s 生产 config.eager_load=true 失败而 gem 'gem_name', 要求: false

Rails 5: rails s production with config.eager_load=true fails while gem 'gem_name', require: false

我一直在努力使用 config.eager_load = true 在生产中部署应用程序。

我知道此配置会在内存中加载大部分 Rails 和应用程序代码,在生产(如)环境中这是一个很好的功能,问题是我有一个自定义 rails带有一些代码的引擎,这些代码仅在我的主要应用程序代码的某些模块上使用,并且仅在某些文件上需要。

我的自定义引擎是这样安装在Gemfile中的:

gem 'gem_name', require: false

并且在这样的文件中需要:

require 'gem_name'

因此,当我 运行 rails s -e production(已激活 config.eager_load=true)时,它会自动失败并出现以下错误

bootsnap/load_path_cache/core_ext/kernel_require.rb:58:in `load': No such file to load (LoadError)

Gemfile 不需要 gem 时,关于如何使 eager_load 工作的任何想法?

gem 'gem_name', require: false

如果我在生产(如)环境中将 eager_load 设置为 false,我会遇到什么问题?

我研究了一下,尝试更新 bootsnap gem,尝试从 tmp/cache 文件夹中删除 'bootsnap-load-path-cache' 和 'bootsnap-compile-cache'。 这篇文章可能有助于理解 eager load https://blog.arkency.com/2014/11/dont-forget-about-eager-load-when-extending-autoload/

这个主题可能有助于理解

它指向其他解决方案,不是要求而是自动加载(http://www.rubyinside.com/ruby-techniques-revealed-autoload-1652.html)

来自指南(请阅读)https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoload-paths-and-eager-load-paths

对于那些需要自动加载目录中的所有文件而不是要求它们的用户,您可以这样做:

Dir.glob(File.join(some_path, 'lib', 'extensions', '*.rb')).map do |file| autoload File.basename(file).gsub('.rb', '').classify.to_sym, file end

而不是经典

Dir[File.join(some_path, 'lib', 'extensions', '*.rb')].each do |f| require f end