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

后端 未结 5 954
独厮守ぢ
独厮守ぢ 2020-12-28 12:24

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

5条回答
  •  Happy的楠姐
    2020-12-28 12:59

    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
    

提交回复
热议问题