-
-
Save MaZderMind/4abfea2bf782a0e9b5af 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
/** | |
* 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 = {}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fixed cache, fixed loading modules without exports, fixed indenting