Howto remove sprockets-rails from you Ruby on Rails project
Reading time: 3 minutes
Before removing sprockets-rails from dependencies of your Ruby on Rails project, check whether you are surfing images or fonts using sprockets through assets paths like:
… or any other assets except Style Sheets (CSS):
If you are using cssbundling-rails the sprockets default path for Style Sheets is used by default:
Remove it after the replacement
Removing the good old sprockets from your modern Ruby on Rails project may be a useless step, as it won’t stop you from using modern alternatives like:
- webpacker(er) is retired, see: Move from webpack(er) to jsbundling-rails
But I try to reduce the number of gems, dependencies, and setups I have in our projects. If it is not there, it won’t bother you at all.
Using cssbundling-rails and jsbundling-rails
If you want to move from webpacker to cssbundling-rails and jsbundling-rails these have a look and my howtos:
- Howto migrate from Webpacker to cssbundling-rails in Ruby on Rails for CSS
- Howto migrate from Webpacker to jsbundling-rails in Ruby on Rails
They include also information, how to move you images from webpack(er) setup to rails default assets pipeline. If you are on cssbundling-rails and jsbundling-rails, stop here as these are using sprockets by default.
Start by removing it from Gemfile
If you remove the gem from your Gemfile and run
bundle install bundle should
remove it from your Gemfile.lock which will look like this in git:
diff --git a/Gemfile.lock b/Gemfile.lock ... - sprockets (4.1.1) - concurrent-ruby (~> 1.0) - rack (> 1, < 3) - sprockets-rails (3.4.2) - actionpack (>= 5.2) - activesupport (>= 5.2) - sprockets (>= 3.0.0)
You can also run bundle to remove the dependency:
./bin/bundle remove sprockets-rails
If you are using sass-rails then, removing sprockets-rails from your setup won’t have any effect. Just stop here and undo your last changes.
This was the easy step as removing a dependency won’t remove setup and configuration for sprockets. You have to do it manually.
Find and remove the basic setup from sprockets
After removing sprockets-rails from your project, you want to try to start it and see what happens.
The most common errors will be:
- An existing setup file (even empty): config/initializers/assets.rb
- Set options in the development environment file config/environments/development.rb
diff --git a/config/environments/development.rb b/config/environments/development.rb ... # Suppress logger output for asset requests. - config.assets.quiet = true + #config.assets.quiet = true
Just check the error message until you can start your project and everything is working fine. Especially check for missing images, fonts, or other related stuff.
As you may have many different environments like test, staging, and development grep for the word assets:
grep "assets" config/environments/*
It may be useful to try all the environments in a local setup before pushing the branch to your git repository, which may trigger a CI/CD system to run tests or even deploy to staging or even production.
Previously sprockets were used for the whole asset pipeline and are still part of the default ruby on rails setup. But depending on your setup (especially with webpack(er)) you won’t need it anymore.
Removing not needed dependencies and their setup is an essential part of software development to reduce the complexity of a project.