43

If I'm using //=require_tree . in application.css, is there a way to exclude particular files other than resorting to //=require_directory and tree organization?

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

tamersalama
  • 4,093
  • 1
  • 32
  • 35

5 Answers5

66

This is possible with Sprocket's new stub directive which is available in Sprockets v2.2.0 and up. However, Rails 3.2 will only ever use Sprockets v2.1.3 which does not have this feature. As of now, the current Edge Rails has the stub directive and it will officially be in Rails 4.0 and above.

Usage:

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

stub directives can not be overridden by subsequent 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 gem with its Sprockets dependency modified to version 2.2.0.

Joseph Ravenwolfe
  • 6,480
  • 6
  • 31
  • 31
  • The current release of Rails (3.2.2) relies on Sprockets 2.1.2 which does NOT have this feature! – Richard Hulse Mar 09 '12 at 07:52
  • "gem list sprockets" to get your installed version – Graham Bass Sep 21 '15 at 15:45
  • This also works for stylesheets beginning with Rails 3.2.9. Also, watch your file naming convention. If your file is called jsfile.js, do not use //= stub jsfile_js, it won't work. Instead use //= stub jsfile. – Matteo Jun 17 '16 at 16:52
  • Can I use stub for a specific vendor asset folder also? I have been trying the same but not working. – Deepanshu Goyal Mar 30 '17 at 07:09
  • Stub can cause unexpected behaviour. It's not just "don't include this file" but "don't include this file AND don't include it's dependencies". If you have a dependency included in both files then it will be removed in the one using "stubs". – keoghpe Apr 27 '18 at 15:31
18

Since the release of rails 3.2.9, it has support to lock the sprockets to version 2.2.x so that we can use the //= stub directive that 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

Koen.
  • 25,449
  • 7
  • 83
  • 78
millisami
  • 9,931
  • 15
  • 70
  • 112
4

NB: This answer is now out of date, with an update to Sprockets having this feature. See the answer below.

===

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

The other way to to manually list each file you want.

Perhaps you could file this as a feature request over on the Sprockets repo? :-)

Richard Hulse
  • 10,383
  • 2
  • 33
  • 37
0

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

It supports not only JS and is in basic mode sprockets compatible.

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

Full depenedncies managment with multiple source bases.

I used sprockets in past and this one is better, I use it also for CSS.

0

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
Roman
  • 13,100
  • 2
  • 47
  • 63
  • Hi @Roman, I also want this to work. So, I added your above patch inside the file `config/initializers/sprockets.rb` file and used that directive, it still gets served. I'm on rails 3.2.1 and sprockets 2.1.3 . – millisami Nov 20 '12 at 13:47
  • Thanks, but where do I place this file? Tried in `lib`, `config/initializers`, both didnt work. And whats the version of the rails and sprockets in the Gemfile.lock ? – millisami Nov 22 '12 at 17:06
  • 1
    I'm on Rails 3.2.9 and this file is in initializers. – Roman Nov 22 '12 at 20:42
  • Does this patch works to exclude directories? It doesn't looks like. – Vadorequest Apr 01 '14 at 08:27
  • 1
    Dunno.. In the end, it doesn't worth the time. Juts put the things in some subdirectories and only use require_dir and not require_tree. – Roman Apr 01 '14 at 10:19