rails 3.1 asset pipeline: ignore assets from a gem
Rails 4.X
It didn't work on Rails 4.X, a possible (dirty) workaround is:
require 'sprockets/railtie'
Bundler.require(:default, Rails.env)
module Sprockets
module Paths
SKIP_GEMS = ["rails-assets-jquery", "rails-assets-bootstrap"]
def append_path_with_rails_assets(path)
append_path_without_rails_assets(path) unless SKIP_GEMS.any? { |gem| path.to_s.start_with?(Gem.loaded_specs[gem].full_gem_path) }
end
alias_method_chain :append_path, :rails_assets
end
end
Update for Rails 5.X
alias_method_chain
is deprecated since Rails 5.X. Here is an updated version using prepend
, and overriding the Sprockets::Environment
module instead of Sprockets::Paths
.
module SprocketsPathsOverride
SKIP_GEMS = ["rails-assets-jquery", "rails-assets-bootstrap"]
def append_path(path)
should_skip = SKIP_GEMS.any? do |gem|
path.to_s.start_with?(Gem.loaded_specs[gem].full_gem_path)
end
super(path) unless should_skip
end
end
Sprockets::Environment.prepend(SprocketsPathsOverride)
I guess there is a smart way to achieve your goal using sprockets
. Maybe some require_directory
instead of require_tree
.
But the most direct thing would be to remove theses assets from your assets paths. To achieve this, add this at the very end of your application.rb
file (doesn't work in an initializer):
class Engine < Rails::Engine
initializer "remove assets directories from pipeline" do |app|
app.config.assets.paths = app.config.assets.paths - app.config.assets.paths.grep(/nice_regexp_here_to_match_the_dir_where_the_unwanted_files_live/)
end
end
Just tried a hack: put the code in an initializer
but require it at the end of your application.rb
:
require "config/initializers/your_file'
I prefer very specific code to be visible this way.