jeremyckahn/rekapi — pull request #4 by sork (2012-04-19 16:45:55+00:00)
Title: Allow Rekapi to be loaded as an AMD module

TL;DR: Yay! AMD support!

Here is my contribution to add support for AMD loading to Rekapi, which I've only tested with RequireJS so far.

It will look like a huge changeset because I had to add very large files such as require.js and different versions of Underscore, both for testing purposes.

Here is an overview of what this pull request encompasses:

1. The gist of my changes are located in `src/rekapi.init.js`, a new file I introduced, which includes:
- A new `rekapi()` function invoking all the rekapi components and extensions, which have been changed from IIFEs (immediatly invoked function expressions) to local function variables. This allows to invoke them at a later time, depending on whether Rekapi is loaded via AMD or not.
- We detect if an AMD loader is being used and define Rekapi's dependencies. The code should be self-explanatory.
- Dependencies are passed to Rekapi via a `deps` object which replaces the need for globals such as `_` or `Tweenable`.
2. A `build.sh` update:
- All rekapi code is now wrapped into an IIFE as part of the build process, so that the global `this` reference is passed only once, at the bottom of the script.
- The new `rekapi.init.js` is also included in the build.
3. AMD-specific unit tests, located in `tests/test.amd.html`. They should cover most cases. I had to include libraries such as RequireJS in the new `lib/test-amd` directory.

Also, all other tests have been modified to include `` so that Rekapi is initialized.
4. I increased the version to `0.5.2` because I figured that this could be considered a bug fix to an existing AMD loading issue.
5. Some docs and code sample about using AMD and RequireJS in the `README.md` file.

I hope that this is a feature you will find worthy of merging. Please let me know if there is anything I should change to accomodate better to the coding style or architectural considerations.

My main concerns:

- This adds 606 bytes to the minified `rekapi.min.js` file. Perhaps you would prefer a separate `rekapi.amd.js` file. I chose to include it by default considering you had done the same for Shifty.

- Your code architecture is modified quite a bit by changing IIFEs to local function variables. Also, the `deps` object passed to each of them introduces a bit of repetitive boilerplate code:

```javascript
var _ = (deps && deps.underscore) ? deps.underscore : global._
,Tweenable = (deps && deps.Tweenable) ? deps.Tweenable : global.Tweenable;
````

Cheers!