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

1

5

I've found several different threads on including assets in the vendor/assets directory but did not find anything that worked for me. Previously, I had all of my vendor assets in the app/ directory but moved the JavaScript to the vendor/assets/javascripts/ directory and the CSS to the vendor/assets/stylesheets/ directory.

I'm now trying to load my vendor assets both in development and production, and they're not loading at all. Here's my assets.rb file:

# Be sure to restart your server when you modify this file.

# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'

# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path

# 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
]

I've also tried adding the vendor/assets/ directories to Rails.application.config.assets.paths, and that didn't help.

How can I include all of the vendor assets in the asset pipeline?

UPDATE

I got my vendor JavaScript to load by adding the following to app/assets/javascripts/application.js:

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

However, I'm using Sass and am still not able to get the SCSS files to load.

Koetke answered 4/7, 2019 at 19:43 Comment(2)
Did you restart your server when you added these lines? You should add the folder like this: config.assets.paths << Rails.root.join('vendor', 'assets')Woolfell
@Woolfell Thank you for your comment. I did restart my server, and adding the folders in that way didn't work either unfortunately.Koetke
K
7

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!

Koetke answered 4/7, 2019 at 20:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.