A Yeoman generator for Backbone projects

backbone, javascript, browserify

May 2014

By using a code generator, you can speed up the productivity for writing software. Asking the question whether you should use code generator, is a bit similar to choosing between manual or automatic transmission when selecting a car. Automatic transmission (and code generators) can give less interruptions to the flow of driving. However, shifting gears manually (or typing code manually) gets easier with practice, and can ensure that you consume the minimum amount of energy.

Yeoman

Assuming you have a need for project automation, let’s have a look Yeoman. Yeoman was inspired by the scaffolding idea in Ruby-on-Rails. In contrast to Ruby-on-Rails, Yeoman can support all kinds of automation for projects. The important abstraction that enables this flexibility are Yeoman-generators.

I met Yeoman first via the fantastic Yeoman generator for Thorax, but other very interesting Yeoman generators are the Ember and Backbone generator.

To understand what a generator does, it makes sense to inspect the “/app/templates” directory. For example, in the templates directory of the Ember generator you can see templates for a Gruntfile, Bower and Karma.

Actually, most generators that I saw support an automated setup with Grunt or Gulp, and often use RequireJS. For sandboxing a Node application, I currently prefer CommonJS and Browserify however. This gives nice combination with a REPL on the server and browser.

A generator for Browserify and Handlebars

Since I found no Yeoman generator supporting this etup, it became time to write a Yeoman generator. As a start you can use the Yeoman generator-generator, and scaffold some basic application template. Then, you can easily setup a package.json where you include the required Node modules. Right now, I skip Gulp or Grunt, and just include a basic Makefile with a command to call browserify.

To optimally use Browserify, it can be handy to create symlinks into a directory node_modules, e.g.:

$ mkdir -p app/views
$ mkdir app/node_modules
$ cd app/node_modules
$ ln -sf ../views

With this symlink, you can require any module easily as require(‘views/foo’).

Yet, this is open, as I haven’t found out how to create symlinks with Yeoman.

The current result is the Yeoman generator-backbone-browserify and its source is at Github

Leave me feedback

Follow me on Twitter here.

comments powered by Disqus