Rails 5 “gemify” assets manifest file

2019-07-14 10:58发布

Update

Got this in a working state. Gem can be found here: https://github.com/jakehockey10/popcircle

Original post:

I am trying to "gemify" a jquery plugin as a learning experience. The plugin I'm using also depends on jquery.easing, so in the gem's vendor/assets/javascripts folder, I have both jquery.easing.js as well as the other jquery plugin.

Here is what my gem's <name_of_gem>.rb file looks like:

require '<name_of_gem>/version'

module <NameOfGem>
  class Engine < ::Rails::Engine
    class Engine < ::Rails::Engine
      initializer '<name_of_gem>.assets.precompile' do |app|
        app.config.assets.precompile += %w(
          big_round.png
          one.png
          two.png
          three.png
          four.png
          five.png
        )
      end
  end
end

Here is the current structure of the gem:

.
├── bin
│   ├── console
│   └── setup
├── CODE_OF_CONDUCT.md
├── Gemfile
├── Gemfile.lock
├── lib
│   ├── <name_of_gem>
│   │   └── version.rb
│   └── <name_of_gem>.rb
├── LICENSE.txt
├── name_of_gem.gemspec
├── Rakefile
├── README.md
├── test
│   ├── <name_of_gem>_test.rb
│   └── test_helper.rb
└── vendor
└── assets
└── javascripts
├── jquery.easing.js
└── jquery.<name-of-gem>.js

When using the gem in a test app, I'd like to be able to just require the gem like //= require <name-of-gem> and have that be sufficient to have both of the javascripts files in the gem's vendor folder to be loaded into the test app's asset pipeline. However, instead, I have to //= require jquery.easing and the other file individually in my test app.

Is there a way to have my gem bundle those assets up together and not have the developer of the app have to require both files individually?

Instead of this in my test app's application.js:

//= require jquery-easing.js
//= require jquery.<name-of-plugin>.js

I'd like to give the developers the opportunity to just say this in application.js:

//= require <name_of_gem>

0条回答
登录 后发表回答