2011-08-23 139 views
6

我不太確定實際行爲是什麼,所以我的第一個問題是:
來自gem(在我的情況下是Spree)的資產(例如javascripts)總是被編譯?我不使用Spree的JavaScript,因此不希望它們被編譯。我不要求他們在我application.js或任何其他JavaScript文件,但rails 3.1資產管道:忽略來自寶石的資產

rake assets:precompile 

編譯他們仍然。我只是不想讓他們躺在我的public/assets文件夾中。

所以我想我的問題是,有沒有辦法禁用從寶石編譯JavaScript的?

回答

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' 

我喜歡非常具體的代碼是可見的這種方式。

+0

日Thnx了很多,我會盡力的! –

+0

在Rails 4.0.0中,它看起來像初始化器被忽略。嘗試: class Engine mmell

3

它沒有on 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 
+0

把這個放到config/initializers/duplicate_assets.rb中對我也很好。可以用rails runner測試「puts Rails.application.assets.find_asset('jquery.js').to_a.map(&:pathname)」 – grosser