Rails config.assets.precompile setting to process all CSS and JS files in app/assets

后端 未结 8 1578
粉色の甜心
粉色の甜心 2020-11-28 06:42

I wish to precompile all the CSS and JS files in my project\'s app/assets folder. I do NOT want to precompile everything in vendor/assets or lib/assets, only th

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 07:04

    I wanted all assets from both /app and /vendor to be compiled, except partials (which name starts from underscore _). So here is my version of an entry in application.rb:

    config.assets.precompile << Proc.new { |path|
      if path =~ /\.(css|js)\z/
        full_path = Rails.application.assets.resolve(path).to_path
        app_assets_path = Rails.root.join('app', 'assets').to_path
        vendor_assets_path = Rails.root.join('vendor', 'assets').to_path
    
        if ((full_path.starts_with? app_assets_path) || (full_path.starts_with? vendor_assets_path)) && (!path.starts_with? '_')
          puts "\t" + full_path.slice(Rails.root.to_path.size..-1)
          true
        else
          false
        end
      else
        false
      end
    }
    

    Additionally it outputs list of files being compiled for debugging purposes...

提交回复
热议问题