Rails 3.1 Sprockets 需要指令 - 有没有办法排除特定文件?

发布于 2024-12-07 13:18:14 字数 196 浏览 0 评论 0原文

如果我在 application.css 中使用 //=require_tree . ,除了诉诸 //=require_directory 和树组织之外,是否有其他方法可以排除特定文件?

也许类似于 //= require_tree ., { except: 'something'}

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'}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

橘和柠 2024-12-14 13:18:14

这可以通过 Sprockets v2.2.0 及更高版本中提供的新 stub 指令实现。然而,Rails 3.2 只会使用 Sprockets v2.1.3,而它没有此功能。截至目前,当前的 Edge Rails 具有 stub 指令,它将正式出现在 Rails 4.0 及更高版本中。

用法:

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

stub 指令不能被后续的 requireinclude 指令覆盖。

如果您想在 Rails 3.2 项目中使用 stub 指令,您必须切换到 Edge Rails,或者将您的 Rails gem 分支,并将其 Sprockets 依赖项修改为版本 2.2.0。

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.

眉黛浅 2024-12-14 13:18:14

自rails 3.2.9发布以来,它支持将sprocket锁定到2.2.x版本,以便我们可以使用最新sprocket具有的//=stub指令。

//= stub unwanted_js

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

因此,要使用它,只需升级到 Rails 3.2.9

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

九公里浅绿 2024-12-14 13:18:14

注意:这个答案现在已经过时了,Sprockets 的更新具有此功能。请参阅下面的答案。

===

这对于当前的 Sprockets 指令来说是不可能的,但它似乎是一个方便的功能。

另一种方法是手动列出您想要的每个文件。

也许您可以将其作为功能请求提交到 Sprockets 存储库上? :-)

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? :-)

心作怪 2024-12-14 13:18:14

以下猴子补丁为我解决了这个问题:


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

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
静水深流 2024-12-14 13:18:14

尝试更好的 https://github.com/QubitProducts/miniMerge

它不仅支持 JS,而且是基本的模式链轮兼容。

您不仅可以在文件级别排除,还可以排除块甚至行。

具有多个源库的完整依赖性管理。

我过去使用过链轮,这个更好,我也将它用于 CSS。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文