Jonathan Pettersson

Full stack developer & Team lead

@Spotify, Stockholm

Using a microgem to manage JavaScript dependencies in Rails


I’ve changed my mind and it feels good! Bower is a great tool for managing web packages regardless of what application framework you’re running. Read my bower article if you want to know more.

Original text:

I use Rails & Middleman a lot. Rubygems and Bundler makes it a breeze to manage all Ruby dependencies, but things get fuzzy when it comes to JS and CSS. In the past I’ve simply committed the dependencies together with the source code, or used git submodules, both of which have been bulky and inflexible.

Rails and Middleman use Sprockets to compile assets and it’s become popular to distribute JS libraries as small Sprockets-compatible gems which I think is great!

The problem however, is that not all JS libraries & CSS frameworks are distributed as gems, nor would it make sense if they were.

Package the libs in a microgem

A microgem is just a regular gem, but with a small and specific function. Microgems should not be hosted on, instead they can be installed directly from a git repository.

I’ve packaged my standard JavaScript libraries and CSS frameworks in a microgem called web-lib. Libraries can either be vendored into the gem or can have their official gem linked as a runtime dependency.

The only function of the gem is to make these JS & CSS files available to Sprockets compatible applications. This allows me include them in my projects without having to vendor them and I can use the version of the microgem to represent the collective version of my current lib snapshot.

I can easily switch out a JS lib and keep on going without having to worry about breaking old projects. Also, I can use branches to try out new stuff and experiment.

Example usage


gem "web-lib", :git => ''


/*Require all libs*/
          //= require_tree 'web-lib'
          /*Or be selective*/
          //= require 'web-lib/SomeJsFile'


The workflow and the microgem itself are easily forked. The idea behing the generic namespace ‘web-lib’ is that anyone can customize the contents to suit their needs. It also implies that there should be only one jslib per project to keep things manageable.

Manage web dependencies with Bower