Last active
August 29, 2015 13:59
-
-
Save simonsmith/10871480 to your computer and use it in GitHub Desktop.
AMD Shim - Taken from Flight https://github.com/flightjs/flight/blob/master/tools/standalone
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
| (function(context) { | |
| var factories = {}, loaded = {}; | |
| var isArray = Array.isArray || function(obj) { | |
| return obj.constructor == Array; | |
| }; | |
| var map = Array.map || function(arr, fn, scope) { | |
| for (var i = 0, len = arr.length, result = []; i < len; i++) { | |
| result.push(fn.call(scope, arr[i])); | |
| } | |
| return result; | |
| }; | |
| function define() { | |
| var args = Array.prototype.slice.call(arguments), dependencies = [], id, factory; | |
| if (typeof args[0] == 'string') { | |
| id = args.shift(); | |
| } | |
| if (isArray(args[0])) { | |
| dependencies = args.shift(); | |
| } | |
| factory = args.shift(); | |
| factories[id] = [dependencies, factory]; | |
| } | |
| // Some UMD wrapped code might need define.amd | |
| define.amd = true; | |
| function require(id) { | |
| function resolve(dep) { | |
| var relativeParts = id.split('/'), depParts = dep.split('/'), relative = false; | |
| relativeParts.pop(); | |
| while (depParts[0] == '..' && relativeParts.length) { | |
| relativeParts.pop(); | |
| depParts.shift(); | |
| relative = true; | |
| } | |
| if (depParts[0] == '.') { | |
| depParts.shift(); | |
| relative = true; | |
| } | |
| if (relative) { | |
| depParts = relativeParts.concat(depParts); | |
| } | |
| return depParts.join('/'); | |
| } | |
| var unresolved, factory, dependencies; | |
| if (typeof loaded[id] == 'undefined') { | |
| unresolved = factories[id]; | |
| if (unresolved) { | |
| dependencies = unresolved[0]; | |
| factory = unresolved[1]; | |
| loaded[id] = factory.apply(undefined, map(dependencies, function(id) { | |
| return require(resolve(id)); | |
| })); | |
| } | |
| } | |
| return loaded[id]; | |
| } | |
| // App code here | |
| // Expose app to a global | |
| context.myApp = require('lib/index'); | |
| }(this)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment