Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 the dependencies of my files as needed.

I tried the following wildcard setting, but it incorrectly precompiles everything. This results in lots of extra work and even causes a compilation failure when using bootstrap-sass.

config.assets.precompile += ['*.js', '*.css'] 

What is my best bet to only process my files in app/assets? Thanks!

like image 617
keithgaputis Avatar asked Apr 10 '12 23:04

keithgaputis


People also ask

How do you Precompile in Rails?

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.

How asset pipeline works in Rails?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB. Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets.


2 Answers

config.assets.precompile = ['*.js', '*.css']

That will compile any JavaScript or CSS in your asset path, regardless of directory depth. Found via this answer.

like image 135
techpeace Avatar answered Sep 24 '22 13:09

techpeace


This task is made more difficult by the fact that sprockets works with logical paths that do not include where the underlying, uncompiled resourced is located.

Suppose my project has the JS file "/app/assets/javascripts/foo/bar.js.coffee".

The sprockets compiler will first determine the output file extension, in this case ".js", and then the evaluate whether or not to compile the logical path "foo/bar.js". The uncompiled resource could be in "app/assets/javascripts", "vendor/assets/javascripts", "lib/assets/javascripts" or a gem, so there is no way to include/exclude a particular file based on the logical path alone.

To determine where the underlying resource is located, I believe it is necessary to ask the sprockets environment (available via the object Rails.application.assets) to resolve the real path of the resource given the logical path.

Here is the solution that I am using. I am fairly new to Ruby so this is not the most elegant code:

# In production.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     if full_path.starts_with? app_assets_path       puts "including asset: " + full_path       true     else       puts "excluding asset: " + full_path       false     end   else     false   end } 

With sprockets > 3.0, this will not work in production because Rails.application.assets will be nil (assuming default: config.assets.compile = false).

To workaround you replace the full_path assignment with:

@assets ||= Rails.application.assets || Sprockets::Railtie.build_environment(Rails.application) full_path = @assets.resolve(path) 

See also: https://github.com/rails/sprockets-rails/issues/237

like image 45
keithgaputis Avatar answered Sep 24 '22 13:09

keithgaputis