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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7602393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:40:15  来源:igfitidea点击:

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

javascriptruby-on-railssprockets

提问by tamersalama

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

如果我//=require_tree .在 application.css 中使用,除了诉诸//=require_directory树状组织之外,有没有办法排除特定文件?

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

也许像 //= require_tree ., {except: 'something'}

回答by Joseph Ravenwolfe

This is possible with Sprocket's new stubdirective 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 stubdirective and it will officially be in Rails 4.0 and above.

这可以通过 Sprocket 的新stub指令实现,该指令在 Sprocket v2.2.0 及更高版本中可用。但是,Rails 3.2 将只使用没有此功能的 Sprockets v2.1.3。到目前为止,当前的 Edge Rails 有该stub指令,它将正式出现在 Rails 4.0 及更高版本中。

Usage:

用法:

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

stubdirectives can not be overridden by subsequent requireor includedirectives.

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

If you want to use the stubdirective 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.

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

回答by Autodidact

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 //= stubdirective that latest sprockets have.

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

//= stub unwanted_js

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

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

所以,要使用它,只需升级到 Rails 3.2.9

回答by Richard Hulse

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

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

回答by Roman

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

回答by brutal de luxe

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

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

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

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

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.

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