我正在构建一个 gem - Rails 引擎 - 让我们称之为它my_new_gem
- 以及里面my_new_gem
我正在尝试包含来自另一个 gem 的 JavaScript 资产 - 让我们称之为它the_other_gem
.
In the gemspec
for my_new_gem
我已经包括了the_other_gem
using add_dependency
:
Gem::Specification.new do |s|
s.name = 'my_new_gem'
s.version = '0.0.1'
s.date = '2019-11-12'
...
s.add_dependency 'the_other_gem'
end
然后在app/assets/javascripts/my_gem.js
in my_new_gem
我需要 JSthe_other_gem
标准方式:
// Trying to require a JS file from another gem in my gem here
//= require the_other_gem
当我启动一个测试 Rails 应用程序时,其中包含我的 gemgemfile
with gem 'my_new_gem'
我得到一个Sprockets::FileNotFound
尝试在浏览器中加载任何页面时出错:
couldn't find file 'the_other_gem' with type 'application/javascript'
Checked in these paths:
/Users/me/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/babel-source-5.8.35/lib
/Users/me/Projects/my_project/app/assets/config
/Users/me/Projects/my_project/app/assets/images
...
当我扫描上述所有路径时,该应用程序是NOT包括来自的任何供应商或资产路径the_other_gem
在该列表中,就像我所期望的那样。
尽管如果 JS 的路径在the_other_gem
没有显示在我的 Rails 应用程序资源路径中,找不到 JS 文件,我不明白WHY该路径未包含在内。
如果我包括the_other_gem
直接在我的 Rails 应用程序中使用gem 'the_other_gem'
并且还包括//= require the_other_gem
在 Rails 应用程序中application.js
然后一切都会加载,Sprockets 会成功找到路径the_other_gem
assets.
虽然这可行,但我不想要求人们将 gem 直接包含在他们自己的 Rails 应用程序中,因为我认为我可以告诉my_new_gem
自动将其包含在引擎盖下。
所以谜题是,我怎样才能得到my_new_gem
在最终的 Rails 应用程序中包含正确的路径,以便来自的 javascript 文件the_other_gem
会正确加载吗?我觉得我错过了一些非常明显的东西......非常感谢!