How to include assets from one gem inside another gem?

安稳与你 提交于 2019-12-24 00:47:36

问题


I'm building a gem - a Rails engine - let's call it my_new_gem - and inside of my_new_gem I'm trying to include JavaScript assets from another gem - let's call it the_other_gem.

In the gemspec for my_new_gem I've included 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

And then in the app/assets/javascripts/my_gem.js in my_new_gem I have required the JS from the_other_gem the standard way:

// Trying to require a JS file from another gem in my gem here
//= require the_other_gem

When I boot up a test Rails app that includes my gem in the gemfile with gem 'my_new_gem' I get a Sprockets::FileNotFound error when attempting to load any page in a browser:

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

When I scan through all the above paths, the app is NOT including any vendor or asset paths from the_other_gem in that list, like I'm expecting it to.

Although it does make sense that if the path to the JS in the_other_gem is not showing up in my Rails app asset paths above that the JS file wouldn't be found, I don't understand WHY the path isn't being included.

If I include the_other_gem directly in my Rails app with gem 'the_other_gem' and also include //= require the_other_gem in the Rails app application.js directly then everything loads and Sprockets successfully finds the paths for the_other_gem assets.

Although this works, I'd rather not require folks to include the gem directly in their own Rails app, when I thought I could just tell my_new_gem to include it automagically for them under the hood.

So the mystery is, how can I get my_new_gem to include the right paths in the final Rails app so the javascript files from the_other_gem will load correctly? I feel like I'm missing something super obvious ... thanks so much!

来源:https://stackoverflow.com/questions/58829381/how-to-include-assets-from-one-gem-inside-another-gem

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