webjars/webjars-locator — pull request #40 by jamesward (2014-02-08 22:04:31+00:00)
Title: add requirejs support

This adds direct support for RequireJS into the `webjars-locator` so that it can be reusable for any web framework. It also changes the way WebJars are located by RequireJS. The `webjars-play` RequireJS support has used a custom plugin loader which has run into a few issues:
* Plugin loaders can't be chained so the `webjars!` loader didn't work well with the `text!` loader
* Locating a RequireJS module was using a client-side `webjars-locator`-like approach which was unneeded and error-prone
* Libraries that internally use RequireJS did not work with the `webjars!` loader

The new approach doesn't use a plugin loader and instead uses RequireJS configuration to set the `paths` for a module. For instance, doing `require('angularjs'` uses a `paths` setting from the `angularjs` WebJar to locate the asset. These `paths` settings come from the optional `webjars-requirejs.js` file in each WebJar. In order to avoid hard coding the versions into these files there is a helper JavaScript function in `webjars-locator` that sets up the correct URL. For instance:

requirejs.config({
paths: { "angular": webjars.path("angularjs", "angular") };
});

To preserve backwards compatibility in the WebJars with the old RequireJS support the `shim` can include both the standard style dependency definition and the old `webjars!` style dependency:

requirejs.config({
shim: { "angular-route": ["angular", "webjars!angular.js"] };
});

To handle this there is a no-op `webjars!` plugin loader in the `webjars-locator` RequireJS JavaScript.

The new RequireJS JavaScript also now includes the `webjars-requirejs.js` contents for all of the WebJars the user is using (has on their CLASSPATH). This avoids unnecessary HTTP requests.

This change will require updating `webjars-play` and the WebJars. Here is a preview of those changes:

* For `webjars-play`: https://github.com/webjars/webjars-play/compare/use-locator-requirejs-support
* For the `angularjs` WebJar: https://github.com/webjars/angularjs/compare/new-requirejs-support
* For the `requirejs-text` WebJar: https://github.com/webjars/requirejs-text/compare/new-requirejs-support

Here is a sample Play app that brings it all together:
https://github.com/jamesward/angular-seed-play-new-requirejs

Here is the only thing a Play developer will have to use to setup RequireJS:
https://github.com/jamesward/angular-seed-play-new-requirejs/blob/master/app/views/index.scala.html#L7