Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rake assets:precompile for specific JS file

Could I run rake assets:precompile for specific JavaScript file?

Otherwise the full precompile lasts for 5 minutes and makes quick changes in JavaScript files very annoying.

like image 230
Paul Avatar asked Jul 10 '12 13:07

Paul


People also ask

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 Precompile do?

rake assets:precompile. We use rake assets:precompile to precompile our assets before pushing code to production. This command precompiles assets and places them under the public/assets directory in our Rails application.

What is Require_tree?

The require_tree directive tells Sprockets to recursively include all JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file.


1 Answers

If you wanted to precompile just one file, you could make a custom rake task to do so fairly easy.

namespace :assets do

  desc "compile one js file"

  task :compile_one_file => :environment do
    dest = "#{Rails.root}/vendor/assets/javascripts/compiled/"
    js_asset = "your_jsfile.js"
    File.write(dest + js_asset, Uglifier.compile(Rails.application.assets.find_asset(js_asset).to_s))
  end

end

then from the command line

rake assets:compile_one_file

Hope this helps, I find this useful for vendor js files that I don't change often such as jquery and jquery plugins. That way when im in development it speeds up my page loads keeping the asset pipeline from having to route all the separate requests for my vendor files. It just serves up one minified js file of all my vendor js.

like image 76
David Morrow Avatar answered Sep 27 '22 20:09

David Morrow