-
-
Save mindplay-dk/5909303 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// # tinyAMD: a Minimal AMD shim. | |
// I define Minimal AMD as the following: | |
// * Every define() call provides a module id field (no filename magic) | |
// * Every require() call consistently provides a list of dependencies, and | |
// optionally a callback to receive the resolved dependencies | |
// * No additional network traffic to fetch modules | |
// * All dependencies must be defined before a module may be required | |
// ## Uses | |
// * small-footprint production shim for use with an r.js optimized project | |
// * If you write modules in Minimal AMD coding style, you can use tinyAMD | |
// for both development and production. | |
// ## Why Minimal AMD? | |
// * Clean consistent conventions | |
// * Module dependencies are always at the header of the file | |
// * No js optmization required beyond concatenation and minification | |
(function() { | |
var defined = {}; | |
var resolved = {}; | |
function define(id, deps, module) { | |
defined[id] = [deps, module]; | |
} | |
function resolve(id) { | |
if (!resolved[id]) { | |
var definition = defined[id]; | |
if (!definition) { | |
throw 'Attempted to resolve undefined module ' + id; | |
} | |
var deps = definition[0]; | |
var module = definition[1]; | |
var rDeps = []; | |
for (var i=0; i<deps.length; i++) { | |
rDeps.push(require(deps[i])); | |
} | |
resolved[id] = module.apply(window, rDeps) || window; | |
} | |
return resolved[id]; | |
} | |
function require(id, callback) { | |
if (id.constructor === Array) { | |
var rDeps = []; | |
for (var i=0; i<id.length; i++) { | |
rDeps.push(resolve(id[i])); | |
} | |
return callback | |
? callback.apply(window, rDeps) || rDeps | |
: rDeps; | |
} else { | |
return resolve(id); | |
} | |
} | |
window.require = require; | |
window.define = define; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment