Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 asset precompilation - include all javascript files

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?

like image 832
Peter Avatar asked Sep 02 '11 00:09

Peter


People also ask

Where should you put images JavaScript and CSS so that they get processed by the asset pipeline?

The default locations are: the images , javascripts and stylesheets directories under the app/assets folder, but these subdirectories are not special - any path under assets/* will be searched. Assets inside subdirectories can also be accessed. You can view the search path by inspecting Rails. application.

How do you Precompile an asset?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What does rake assets Clean do?

The clean it removes the old versions of the precompiled assets while leaving the new assets in place. Show activity on this post. rake assets:clean removes compiled assets. It is run by cap deploy:assets:clean to remove compiled assets, generally from a remote server.

What are assets in Rails?

Generally asset is anything that browser loads after it gets the HTML page. Meaning javascript, css and any images. But as you pointed out there are two different image types in a rails project.


2 Answers

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' 
like image 172
pat Avatar answered Oct 01 '22 14:10

pat


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 } 
like image 34
Piotr Kuczynski Avatar answered Oct 01 '22 14:10

Piotr Kuczynski