Sprockets processing image assets from a gem

291 Views Asked by At

I'm migrating from sprockets 3 to sprockets 4 and v4 seems to ignore assets from gems. Those were automatically handled by v3, but I can't seem to find anything about porting this behaviour.

For example there's gem mediaelement_rails which includes mediaelement_rails-0.8.2/app/assets/images/mediaelement_rails/skipback.png

In sprockets v3 it was automatically included during asset compilation, but now, even with manifest:

//= require mediaelement_rails
//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css

The skipback.png is not included.

I could do this very explicitly: query the gem path and add it to the assets paths. But is there a way to do this automatically similar to v3?

1

There are 1 best solutions below

0
amystherdam On

Sprockets v4 no longer automatically includes gem resources.

To include gem resources in your assets, you must explicitly register the gem paths that contain the resources. This can be done using the configuration: config.assets.paths in your config/application.rb file.

For example, to include the mediaelement_rails gem image resources in your assets, you could add the following to your config/application.rb file:

config.assets.paths << Rails.root.join('vendor', 'assets', 'mediaelement_rails', 'images')

This will add the vendor/assets/mediaelement_rails/images directory to the assets path.

If the gem you're using isn't following the standard naming conventions for its assets (i.e. putting your assets in subdirectories named after javascripts, stylesheets, or images), you'll need to explicitly register those directories. using a config.assets.precompile setting in your config/application.rb file.

Here they talk a little about inserting paths that don't follow a pattern

Here, the rails documentation also already shows something about this