We recently suffered from asset pipeline performance issues. In order to boost deploying, we decide to switch to capistrano-local-precompile strategy.
However, local asset compiling in Rails 4 is blazing fast, but in Rails 3 is not.
And much nightmare is if we can skip compiling prcoess if assets weren't changed when we compiling at remote, but we need to compile asset everytime if we choose to compile locally.
So we comes out this crazy idea: How about running Rails 4 asset pipeline in Rails 3 project.
Here is how:
1. modify Gemfile
group :assets do
gem 'sprockets', '2.2.2.backport2'
gem 'sprockets-rails', :require => 'sprockets/railtie', :git => "git@github.com:logdown/sprockets-rails.git"
gem 'sass-rails', '~> 4.0.1', :git => "git@github.com:logdown/sass-rails.git"
gem "compass-rails", "~> 1.1.2", :git => "git@github.com:logdown/compass-rails.git"
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
2. edit config/enviorments/production.rb
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
# Compress JavaScripts and CSS
config.assets.compress = true
# Generate digests for assets URLs
config.assets.digest = true
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :sass
3. change config/deploy.rb
You should install capistrano-local-precompile
first, and override precompile command. It's d because the precompile machism in different versions are different.
set :precompile_cmd, "bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile"
Behinde the scene
Basically we are doing serveral things:
- make
sprockets-rails
edge running withsprockets
2.2 - make
sass-rails
edge running under rails 3 - make
compass-rails
edge running with rails 4 strategy
to resolve asset_paths, SASS resolver issue...etc.
Warning
Before doing this, make sure you really understand what your are doing. And it exists many different between sprockets 1 & 2.