Created
July 27, 2018 10:49
-
-
Save peterekepeter/9d5b83861bf3a76f3e325f66cbe58f75 to your computer and use it in GitHub Desktop.
This file contains 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
// AMD module loader for common js (node) | |
// relative paths not supported | |
const amdmodules={}; | |
let amdcontext=null; | |
// global amd's define | |
define=function(dependencies, factory) { | |
if (typeof dependencies==='function'){ | |
factory = dependencies; | |
dependencies = []; | |
} | |
amdcontext.dependencies = dependencies; | |
amdcontext.factory = factory; | |
} | |
// amd module require | |
function amdrequire(name) { | |
var module=amdmodules[name]; | |
if(module==null) amdmodules[name]=module={name:name}; | |
if(module.built!=null) return module.built; | |
amdcontext=module; | |
var stuff = require(name); // commonjsload | |
var loaded = amdcontext.dependencies.map(amdrequire); | |
module.built = module.factory(...loaded); | |
return module.built; | |
} | |
// require the user code | |
var tests = amdrequire('some/user_module.js'); | |
// we can just log the whole thing for debugging | |
console.log(amdmodules); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment