GithubHelp home page GithubHelp logo

casetext / amd-loader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from guybedford/amd-loader

0.0 5.0 0.0 183 KB

RequireJS loader plugin helper for transpiler and template plugins

License: MIT License

JavaScript 99.87% Shell 0.10% CoffeeScript 0.03%

amd-loader's Introduction

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 requirejs/r.js#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

amd-loader's People

Contributors

guybedford avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.