49

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 vendor/assets/javascripts. I can just add the assets to the config.assets.precompile list, but this seems annoying. I don't want to refer to them in the application.js manifest, because I don't want them included in all pages.

In summary, any way to request that all .js files found in vendor/assets/javascripts get precompiled by rake assets:precompile, but without having them included in all pages?

Thilo
  • 17,565
  • 5
  • 68
  • 84
Peter
  • 127,331
  • 53
  • 180
  • 211

3 Answers3

70

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'
pat
  • 16,116
  • 5
  • 40
  • 46
  • I apologize for being a little daft here, but could you show an example of a adding a regexp to add all plain js and css but NOT all coffee or scss files? I've been trying to write one and it isn't working... – Andrew Sep 16 '11 at 03:22
  • 20
    You probably want to overwrite what's already in precompile then: `config.assets.precompile = ['*.js', '*.css']`. – pat Sep 16 '11 at 04:29
  • 1
    Although you'll probably want to add something for your images too. – pat Sep 16 '11 at 04:30
  • 1
    @pat Actually, all images in `asset/images` directories are included. This is likely because they don't require any processing. – coreyward Dec 10 '11 at 22:43
  • 7
    Though not mentioned by the documentation, if you look at the code of sprockets, you will find that `config.assets.precompile` also accepts `Proc`, which means that you can do some tricks like this: https://gist.github.com/1529093 – Limbo Peng Dec 28 '11 at 18:38
  • 5
    Not sure why rake assets:precompile doesn't do this by default logically it should have done this. – gouravtiwari21 Feb 10 '12 at 20:55
  • @gouravtiwari21 They only precompile application.js and application.css as it's best practice to include other files using the `= require` syntax within application.(js|css). – theodorton May 09 '12 at 01:02
  • @theodorton yes I agree and with that syntax it should automatically precompile other files, but the question clearly mentions that sometimes I do not want to include all js on all the pages, and as of now I don't see any provision for that other than answered by pat – gouravtiwari21 May 09 '12 at 20:04
  • @gouravtiwari21 I just skimmed through my layouts to check which javascripts and stylesheets were included, then added them as an array ['*application.js', '*application.css', '*welcome.js', '*welcome.css']. You'll normally have one master js/css file for each layout, so it's really not much work I'd say. Vendor assets should be required from your "master files". As for having a lot of JS on all pages, with proper caching and naming conventions for IDs and CSS classes it shouldn't be a problem. – theodorton May 09 '12 at 23:02
  • Note that this causes a problem with bootstrap-sass: http://stackoverflow.com/questions/9915950/bootstrap-sass-undefined-variable-baselineheight – Jo P Nov 09 '12 at 19:57
2

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
}
Community
  • 1
  • 1
Piotr Kuczynski
  • 699
  • 7
  • 9
0
# 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)

Ode
  • 549
  • 6
  • 19