Recently, I've undertaken the task of converting a rather large Backbone.js app to using Require.js. Since the process can get tricky at times, here's a quick recap of what I had to do to get it done.
0. Why use Require.js in the first place?
Most projects start small. A couple of script tags that include jQuery (or Zepto, or whatever else you are using), a couple of plug-ins, and your own code. As the project matures, more code is added, and to keep things sane, we divide them into gradually more modular components. We usually end up with a lot of files, each one containing a specific portion of functionality. While this seems easy enough at first, it makes reusing them really hard since they usually depend of each other in all sorts of weird ways.
Require.js helps us define exactly what our component depends on, almost like "regular" imports in other development environments. It means we can lazily load dependencies as needed, as well as reuse components easily, as dependencies are pulled automatically. In addition, with r.js we can easily combine and minify our app to reduce the amount of script ...read more →