Tuesday, March 13, 2012

Faster Rails Asset Precompilation

If you have multiple Javascript assets getting compressed by Uglify, your assets:precompile step might take a long time. Mine was adding several minutes to every deployment.

The assets:precompile task actually compiles everything twice. Once with digest-based filenames, and once with plain filenames. But as long as you're careful to always use the Rails-provided asset path helpers, you don't need the plain filenames, and you can use this task instead to cut your precompilation time in half:


namespace :assets do
  namespace :precompile do
    task :digest_only do
      invoke_or_reboot_rake_task "assets:precompile:primary"
    end
  end
end
Just drop that into your lib/tasks directory and invoke it with rake assets:precompile:digest_only.