GitXplorerGitXplorer
g

amd-loader

public
33 stars
8 forks
1 issues

Commits

List of commits on branch master.
Unverified
b0044385de64e235c6fd34b94946433cd3a1e3b8

Update amd-loader.js

gguybedford committed 12 years ago
Unverified
797108f46da39ccf89b2b97eb42cf63dce5a93e3

Update README.md

gguybedford committed 12 years ago
Unverified
5d2e52c2bb4b991416da6dcf7c3884965664a29f

Update README.md

gguybedford committed 12 years ago
Unverified
7fb36562c0fa1a5b0936945e19e63ca27b5395bd

Update amd-loader.js

gguybedford committed 12 years ago
Unverified
6d8a9a6e1e63e3ff70e024de5fa503ef6a4d44a1

Update README.md

gguybedford committed 12 years ago
Unverified
5d17cc61a80e353c840b4f12273d98979af27417

Update README.md

gguybedford committed 12 years ago

README

The README file for this repository.

AMD Loader

A RequireJS plugin helper module.

Useful for creating loader plugins for:

  • Templates (eg Handlebars, Mustache)
  • Compilers (eg CoffeeScript, ES6 conversion)

Supports:

  • Cross-browser XHR for dynamic loading
  • Resource builds in different server environments
  • Cross-origin dynamic loading with CORS
  • Precompiled resources with the optimizeAllPluginResources: true r.js build option.

This module is in planning to be used as a base for the Require-CS module.

Plugins Built with AMD-Loader

Example Usage

Suppose I want to make a template plugin called "awesometpl". I want to allow users to do:

  define(['awesometpl!some-template-file'], function(compiledTemplate) {
    document.body.innerHTML = compiledTemplate({ tpl: 'var' });
  });

And have it automatically load some-template-file.awesomeext and compile it for us, including build support.

Manually creating this plugin can be a lot of work.

AMD Loader can make it for us in just a few lines:

awesometpl.js:

  define(['amd-loader', 'awesome-compiler'], function(amdLoader, awesomeCompiler) {
    return amdLoader('awesometpl', 'awesomeext', function(name, source, req, callback, errback, config) {
      callback(awesomeCompiler.compile(source));
    });
  });

Fine-grained build support (optional)

When used in production, one still has to manually stub the plugin to exclude from the build, as well as the compiler. This also stops dynamic loads working in production.

These configurations can be avoided entirely, and dynamic loads can still work in production, by using a pluginBuilder form of the loader helper.

For the template example above, we can then do the following:

awesometpl.js:

  define(['amd-loader'], function(amdLoader) {
    var pluginBuilder = './awesometpl-build';
    return amdLoader('awesometpl', 'awesomeext', function(name, source, req, callback, errback, config) {
      require(['awesome-compiler'], function(awesomeCompiler) {
        callback(awesomeCompiler.compile(source));
      });
    });
  });

awesometpl-build.js:

  define(['amd-loader', 'awesome-compiler'], function(amdLoader, awesomeCompiler) {
    return amdLoader('awesometpl', 'awesomeext', function(name, source, req, callback, errback, config) {
      callback(awesomeCompiler.compile(source));
    });
  });

Now builds with the plugin will work without needing any configuration. awesome-compiler is excluded from the build by default (pending issue https://github.com/jrburke/r.js/issues/289), and we don't need to provide any stub configuration to exclude the plugin from the build.

Because the awesome-compiler is only loaded when the first dynamic call is made, it isn't included in production by default, but can still be loaded in if necessary.

License

MIT