Simplifying vendored JavaScript library installation with Bower and Bower-Rails
I’ve been working with Ember.js a fair bit lately, and have many maintenance-mode Ruby on Rails projects. Learning and working within the javascript ecosystem has provided a number of interesting ideas and approaches to bring back to new or existing Rails projects. Avoid bloated DOM with Handlebars templates, for one, or the possibility of sprinkling some Ember back into Rails. I’ve found Ember-CLI-Rails great to work with, and [Ember Islands] was mention this year at Ember Conf – I haven’t used this yet myself, but it sounds like a similar idea to the approach that Brandon Hays spoke of in his Refactoring Toward Ember talk at Rails Conf 2014.
Working with ember-cli has opened my eyes to Bower and, err, npm for package management.
It’s been subtly troubling me for a long time working with third-party javascript libraries in Rails. Originally – and in some legacy projects that I’ve inherited – the third party libraries would sit in the public folder next to everything else. I eventually realized that it made sense to extract them into a public/javascripts/vendor folder, for example. When the asset pipeline came around it was easy to separate out application assets under app/assets/ from vendored libraries into vendor/assets/. At this point thought if I incorporated a third library, I was still going through a painful, and painfully stupid process of splitting a library into various images/ javascripts/ stylesheets/ etc folders.And when a release came out? Pure hell.
Along came the bower-rails gem, which was a boon in two regards: one it allowed vendored packages to be sensibly controller by Bower – obviously – but also clued me in to the idea that assets don’t need to be split into their respective folders – with Bower-rails, assets are all stored in their respective library folders, under vendor/assets/bower_components. This alone, was a huge boon to be made aware of.
Bower Rails allows you to control and version your vendored javascript libraries in the same way that Ruby gems are now controlled and versioned in a Gemfile thanks to Bundler. This makes it a heck of a lot easier to manage third-party libraries, a lot easier to update them, easier to remove them, and, well, hopefully you get the idea.
Bower Rails give you the option of working with a native bower.json file, or a more Ruby-like Bowerfile, which is a welcome boon to Rails and Ruby developers more familiar with the Gemfile.
If you haven’t tried Bower or bower-rails, I encourage you to check it out, and hopefully you find it a boon to your Rails development workflow.
I am available for Ruby on Rails consulting work – get in touch to learn more.