-
-
Save dshaw/1039535 to your computer and use it in GitHub Desktop.
A super simple module system for browsers. Assumes all source files are concatenated and in browser.
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 define(name, fn) { | |
if (!defs) { defs = {}; } | |
defs[name] = fn; | |
} | |
function require(name) { | |
console.log("Loading " + name); | |
if (modules && modules.hasOwnProperty(name)) return modules[name]; | |
if (defs && defs.hasOwnProperty(name)) { | |
if (!modules) { modules = {}; } | |
var fn = defs[name]; | |
defs[name] = function () { throw new Error("Circular Dependency"); } | |
return modules[name] = fn(); | |
} | |
throw new Error("Module not found: " + name); | |
} |
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
define('foo', function () { | |
return 42; | |
}); | |
define('mymod', function () { | |
return { | |
stuff: "goes here", | |
"and here": require('foo') | |
}; | |
}); | |
var MyMod = require('mymod'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment