Rails 3.1 asset precompilation - include all javascript files

后端 未结 3 1166
-上瘾入骨i
-上瘾入骨i 2020-12-01 03:32

I want Rails 3.1 to pick up more of my assets for precompilation. In particular, the default matcher for compiling files doesn\'t add .js files from vendo

相关标签:
3条回答
  • 2020-12-01 03:52

    I modified example given in Rails config.assets.precompile setting to process all CSS and JS files in app/assets and here is my version, which takes all assets from /app and /vendor except partials (starting from _)

    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
    }
    
    0 讨论(0)
  • 2020-12-01 04:04

    config.assets.precompile accepts regular expressions and wildcard matching - so to ensure all js files get compiled, without specifying each by name, something like this should do the trick:

    config.assets.precompile << '*.js'
    
    0 讨论(0)
  • 2020-12-01 04:11
    # Precompile *all* assets, except those that start with underscore
    config.assets.precompile << /(^[^_\/]|\/[^_])[^\/]*$/
    

    Reference the 55minutes Blog for the full explanation.

    This will precompile any assets, not just JavaScript (.js, .coffee, .swf, .css, .scss)

    0 讨论(0)
提交回复
热议问题