Coder Social home page Coder Social logo

amd's Introduction

#A lightweight module loader for node and the browser.

The goal of AMD is to create the simplest possible module loader that is compatible with both node and the browser. it is a partial implementation of [CommonJS/Modules/SimpleAsynchronous] (http://wiki.commonjs.org/wiki/Modules/SimpleAsynchronous)

AMD forces you to declare all your dependencies up front, and not require anything new after that point.

This makes module loading easy.

##AMD modules:

  • work in browser and node
  • can be loaded with require() from normal node modules.
  • does not add any overhead to the modules - unlike other node-browser-module systems!

It is necessary to define your modules a new way:

###the old way:

//ab.js
var a = require('./a')
  , b = require('./b')
  
exports.ab = function (x){return a(b(x))}

###the NEW way:

//ab.js - amd style
require('amd') // (loads monkeypatch which makes amd work in node)

module.define(['./a', './b'], function AB (a,b){

  return {ab: function (x){return a(b(x))} } //exports is returned.
})

module.define takes two arguments an array of dependencies and a initializer function. the dependencies are loaded and passed to the initializer in the same order that they are in the array. The return value of the initializer is the module's exports.

####then do $ amd ab.js > ab-browser.js:

AMD will load the modules, get their dependencies, topologically sort them, stringify them, and plug dependencies into the correct places, with very little overhead!

then it will return a script that you can simply include on your page:

(function (M){
M[1] =
  (function A(){
    return function (x){'!' + x + '!'}
  })();

M[2] =
  (function B(){
    return function (x){'?' + x + '?'}
  })();

return  (function AB(a,b){
    return function (x){return a(b(x))} //exports is returned.
  })(M[1],M[2]);

})({});

##limitations

AMD does not support:

  • circular dependencies.
  • dynamic dependencies.
  • __filename, __dirname, or module variables (in the browser)
  • no way to catch errors which may occur in the initializer functions.

If you think these features should be supported please [email me] (mailto:[email protected]) with your use-case.

Currently these features are ignored to keep this module simple, or because they aren't appropriate on browser side modules.

##FORTHCOMING FEATURES: (things I am thinking about, at least)

  • get semi-dynamic dependencies by specifying passing dependencies to amd/bundle.
  • way to catch errors or defer loading, so that it is possible to write a test framework.

amd's People

Contributors

dominictarr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

nrkn duzun

amd's Issues

But no browser support

The current script does not support loading modules AMD in the browser.
Are there any development plans?

pass function that loads module, not the module

instead of injecting the module, inject the module loader function,
so that the module loader function isn't executed each time that a dep is used,
wrap each module function in a function that calls it once, and then caches the result.

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.