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?

3 Answers 3

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'
Sign up to request clarification or add additional context in comments.

10 Comments

You probably want to overwrite what's already in precompile then: config.assets.precompile = ['*.js', '*.css'].
Although you'll probably want to add something for your images too.
@pat Actually, all images in asset/images directories are included. This is likely because they don't require any processing.
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: gist.github.com/1529093
Not sure why rake assets:precompile doesn't do this by default logically it should have done this.
|
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
}

Comments

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)

2 Comments

This is great. Is there any reason I wouldn't want to precompile all assets?
@AlexChaffee if you're using something like SASS or the require comments to prepare 'bundles' of assets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.