Skip to content

Instantly share code, notes, and snippets.

@MaZderMind
Forked from louisstow/require.js
Last active August 29, 2015 14:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save MaZderMind/4abfea2bf782a0e9b5af to your computer and use it in GitHub Desktop.
/**
* Shim the require function used in node.js
*/
(function() {
if (window.require !== undefined)
throw 'RequireException: \'require\' already defined in global scope';
window.require = function(module) {
var url = window.require.resolve(module);
if(window.require.cache.hasOwnProperty(url))
return window.require.cache[url];
var request = new XMLHttpRequest();
var exports;
request.open('GET', url, false);
request.send();
try {
eval(
"(function() {" +
request.response +
"})();"
);
} catch(e) {
console.error("Error loading "+module);
console.error(e);
}
window.require.cache[url] = exports;
return exports;
}
window.require.resolve = function(module) {
var r = module.match(/^(\.{0,2}\/)?([^\.]*)(\..*)?$/);
return (r[1]?r[1]:'./')+r[2]+(r[3]?r[3]:(r[2].match(/\/$/)?'index.js':'.js'));
}
// INFO initializing module cache
window.require.cache = {};
})();
@MaZderMind
Copy link
Author

fixed cache, fixed loading modules without exports, fixed indenting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment