Get Gem Vendor Files in Asset Pipeline Path

Rails 5 - How to include all vendor assets in the asset pipeline?

I finally figured it out after remembering that the Rails asset load paths and Sprockets are different systems. Here's what I did:

In config/initializes/assets.rb, I added the following lines:

# Add additional assets to the asset load path
Rails.application.config.assets.paths += [
Rails.root.join('vendor', 'assets').to_s
]

# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
Rails.application.config.assets.precompile += [
Rails.root.join('vendor/assets/javascripts/*').to_s,
Rails.root.join('vendor/assets/stylesheets/*').to_s
]

The first block adds vendor assets to the Rails asset path. This allows me to use vendor assets in view helpers (ex: image_tag). The second block allows my vendor JavaScript and CSS to be precompiled with the rest of my assets.

Then, I added the following line to app/assets/javascripts/application.js above //= require_tree .:

//= require_tree ../../../vendor/assets/javascripts/.

This will automatically add to my application all of the JavaScript files I put into vendor/assets/javascripts.

For the Sass, I added the following to app/assets/stylesheets/application.sass:

@import "../../../vendor/assets/stylesheets/*";

I added this line before @import "*"; so that my own application's styles will take preference. This will include all of the vendor Sass files in with my application. Then I renamed all of my vendor .css files to use the .scss extension.

Now that development is working fine, I was worried about precompilation. I ran rails assets:precompile locally, and sure enough, all of my vendor assets were included!

Rails - how do you edit files included by a gem in the asset pipeline?

So the asset pipeline consists of potentially many directories (assuming you are using gems that inject their own assets into the pipeline). When an asset is being grabbed in Rails, Rails goes through these directories (in the same order, every time) to find the asset. When the name of the file is first found, that's it, Rails grabs it and uses that file.

Vendor asset directories are specified after app assets, I believe. So, if you place the image that you want to change in the app/assets/images folder, you'll essentially be overriding that vendor image in your application with your own image since Rails will search it's own app/assets first. Obviously, the files need to be named the same.

Why use gems for serving assets instead of the vendor file?

The advantage is you don't have to add the file(s) to your repo and manage updates, you update the gem and you've updated the dependency. They can also add helpers to use the assets more easily.

Not all JS/CSS projects are out-of-the-box compatible with the asset pipeline too, so sometimes the gems will do that work for you as well.

Just because the files get served to clients doesn't make it much different than any other dependency in your application.

Getting sprockets-rails to compile assets in vendor and lib automatically like it used to in rails 3

In your environments file put the following to restore the old default behavior.

config.assets.precompile += [Proc.new { |path| !%w(.js .css).include?(File.extname(path)) }]

This is lifted directly from the old code.

As of the date of posting this in the current version of sprockets-rails setting config.assets anything is broken, the gem is trying to get the environment before its loaded. But this will work if you patch it yourself or use one of the patched version out there of both sprockets and sprockets-rails.

sprockets-rails
https://github.com/rails/sprockets-rails/pull/36

sprockets
https://github.com/sstephenson/sprockets/pull/404

Rails 3.1 serving images from vendor/assets/images

I had to restart my rails server after creating the vendor/assets/images directory. Before this, I was seeing the same error as you ("No route matches [GET]").

My guess is that the rails server does not check these directories if they did not exist when it was first started. When you open a rails console to diagnose the issue, you get a new instance of rails which knows about the directory, which only adds to the confusion.

Developing rails engines and using the asset pipeline

Turns out I still need to require the the css from the app that is loading the gem..

Is this a normal situation? Because I haven't read about that anywhere..

Anyways, got it to work, thanks for looking into it, hope this topic can helps some others out..



Related Topics



Leave a reply



Submit