rails 3.1资产管道:忽略gem中的资产

时间:2011-08-23 15:08:41

标签: ruby-on-rails sprockets asset-pipeline

我不太确定实际行为是什么,所以我的第一个问题是:
来自宝石(在我的案例中是Spree)的资产(例如javascripts)是否总是被编译?我不使用Spree的javascripts,因此不希望它们被编译。我不需要在application.js或任何其他javascript文件中使用它们,但

rake assets:precompile

尽管如此。我只是不希望他们躺在public/assets文件夹中。

所以我想我的问题是,有没有办法禁止从gem中编译javascripts?

2 个答案:

答案 0 :(得分:3)

它无法在Rails 4.X上运行,可能的(脏)解决方法是:

require 'sprockets/railtie'

Bundler.require(:default, Rails.env)

module Sprockets
  module Paths
    SKIP_GEMS = ["rails-assets-jquery", "rails-assets-bootstrap"]

    def append_path_with_rails_assets(path)
      append_path_without_rails_assets(path) unless SKIP_GEMS.any? { |gem| path.to_s.start_with?(Gem.loaded_specs[gem].full_gem_path) }
    end

    alias_method_chain :append_path, :rails_assets
  end
end

答案 1 :(得分:2)

我想有一种聪明的方法可以使用sprockets来实现您的目标。也许有些require_directory代替require_tree

但最直接的方法是从资产路径中删除这些资产。要实现此目的,请在application.rb文件的最后 添加 (在初始值设定项中不起作用):

class Engine < Rails::Engine
   initializer "remove assets directories from pipeline" do |app|
     app.config.assets.paths = app.config.assets.paths - app.config.assets.paths.grep(/nice_regexp_here_to_match_the_dir_where_the_unwanted_files_live/)
   end
end

刚试了一次黑客攻击:将代码放在initializer中,但在application.rb的末尾需要它:

require "config/initializers/your_file'

我更喜欢非常具体的代码以这种方式显示。