Rails 3.1 Sprockets require directives - is there a way to exclude certain files?

If I use //=require_tree . in application.css, is there a way to exclude certain files besides accessing //=require_directory and organizing the tree?

Maybe something like //= require_tree ., {except: 'something'}

+42
javascript ruby-on-rails sprockets
Sep 29 '11 at 19:25
source share
5 answers

This is possible with the new stub Sprocket directive, which is available in Sprockets v2.2.0 and higher. However, Rails 3.2 will only use Sprockets v2.1.3, which does not have this feature. Currently, the current Edge Rails have a stub directive, and it will officially be in Rails 4.0 and higher.

Using:

 //= require jquery //= require_tree . //= stub unwanted_js 
Directives

stub cannot be overridden by the following require or include directives.

If you want to use the stub directive in your Rails 3.2 project, you will have to switch to Edge Rails or branch your Rails stone with your Sprockets dependency, modified to version 2.2.0.

+62
Mar 08 2018-12-12T00:
source share

Starting with the release of rails 3.2.9, it supports sprocket locking up to version 2.2.x so that we can use the //= stub directive that the latest sprockets have.

 //= stub unwanted_js 

http://weblog.rubyonrails.org/2012/11/12/ann-rails-3-2-9-has-been-released/

So, to use it, just upgrade to Rails 3.2.9

+17
Nov 24
source share

Note. This answer is now deprecated, with an update for Sprockets that has this feature. See answer below.

===

This is not possible with the current Sprockets directives, but it seems like a convenient feature.

Another way to manually list each desired file.

Perhaps you could write this as a function request on the Sprockets repo? :-)

+4
Oct 02 2018-11-11T00:
source share

The following monkey patch solves this for me:

 module Sprockets class DirectiveProcessor # support for: require_tree . exclude: "", "some_other" def process_require_tree_directive(path = ".", *args) if relative?(path) root = pathname.dirname.join(path).expand_path unless (stats = stat(root)) && stats.directory? raise ArgumentError, "require_tree argument must be a directory" end exclude = args.shift == 'exclude:' ? args.map {|arg| arg.sub(/,$/, '')} : [] context.depend_on(root) each_entry(root) do |pathname| if pathname.to_s == self.file or exclude.include?(pathname.basename(pathname.extname).to_s) next elsif stat(pathname).directory? context.depend_on(pathname) elsif context.asset_requirable?(pathname) context.require_asset(pathname) end end else # The path must be relative and start with a `./`. raise ArgumentError, "require_tree argument must be a relative path" end end end end 
0
Jan 6 2018-12-12T00:
source share

Try better https://github.com/QubitProducts/miniMerge

It supports not only JS, but is also compatible with basic modes.

You can exclude not only file levels, but also block or even lines.

Complete management of depenedncies with multiple source databases.

I have used asterisks in the past and it is better, I use it also for CSS.

0
Aug 05 '14 at 2:39
source share



All Articles