Instantly share code, notes, and snippets.

@letorbi /require.js
Last active Nov 20, 2018

Embed
What would you like to do?
The first and now obsolete version of my Node.js' require function for browsers. Check https://github.com/letorbi/smoothie for a latest version.
// NEW VERSION AVAILABLE: Check out my GitHub repository at
// https://github.com/letorbi/smoothie for a new and improved version.
// Require() 0.3.4 unstable
//
// Copyright 2012,2013 Torben Haase <http://pixelsvsbytes.com/>
//
// Require() is free software: you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// Require() is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details. You should have received a copy of the GNU Lesser General Public
// License along with Require(). If not, see <http://www.gnu.org/licenses/>.
(function() {
// NOTE If we would use strict mode for this closure we won't allow the modules
// to be executed in normal mode.
if (window.require !== undefined)
throw 'RequireException: \'require\' already defined in global scope';
window.require = function(module, callback) {
var url = window.require.resolve(module);
if (require.cache[url]) {
// NOTE The callback should always be called asynchronously
callback && setTimeout(function(){callback(require.cache[url])}, 0);
return require.cache[url];
}
var exports = new Object();
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState != 4)
return;
if (this.status != 200)
throw 'Require() exception: GET '+url+' '+this.status+' ('+this.statusText+')';
if (window.require.cache[url]) {
exports = window.require.cache[url];
}
else if (this.getResponseHeader('content-type').indexOf('application/json') != -1) {
exports = JSON.parse(this.responseText);
window.require.cache[url] = exports;
}
else {
window.require.cache[url] = exports;
var source = this.responseText.match(/^\s*(?:(['"]use strict['"])(?:;\r?\n?|\r?\n))?\s*((?:.*\r?\n?)*)/);
eval('(function(){'+source[1]+';var exports=window.require.cache[\''+url+'\'];\n\n'+source[2]+'\n})();\n//@ sourceURL='+url+'\n');
}
callback && callback(window.require.cache[url]);
};
request.open('GET', url, !!callback);
request.send();
return exports;
}
window.require.resolve = function(module) {
var r = module.match(/^(\.{0,2}\/)?([^\.]*)(\..*)?$/);
return (r[1]?r[1]:'/js_modules/')+r[2]+(r[3]?r[3]:(r[2].match(/\/$/)?'index.js':'.js'));
}
// INFO initializing module cache
window.require.cache = new Object();
})();
(function(){if(void 0!==window.require)throw"RequireException: 'require' already defined in global scope";window.require=function(a,c){var b=window.require.resolve(a);if(require.cache[b])return c&&setTimeout(function(){c(require.cache[b])},0),require.cache[b];var d={},e=new XMLHttpRequest;e.onreadystatechange=function(){if(4==this.readyState){if(200!=this.status)throw"Require() exception: GET "+b+" "+this.status+" ("+this.statusText+")";if(window.require.cache[b])d=window.require.cache[b];else if(-1!=
this.getResponseHeader("content-type").indexOf("application/json"))d=JSON.parse(this.responseText),window.require.cache[b]=d;else{window.require.cache[b]=d;var a=this.responseText.match(/^\s*(?:(['"]use strict['"])(?:;\r?\n?|\r?\n))?\s*((?:.*\r?\n?)*)/);eval("(function(){"+a[1]+";var exports=window.require.cache['"+b+"'];\n\n"+a[2]+"\n})();\n//@ sourceURL="+b+"\n")}c&&c(window.require.cache[b])}};e.open("GET",b,!!c);e.send();return d};window.require.resolve=function(a){a=a.match(/^(\.{0,2}\/)?([^\.]*)(\..*)?$/);
return(a[1]?a[1]:"/js_modules/")+a[2]+(a[3]?a[3]:a[2].match(/\/$/)?"index.js":".js")};window.require.cache={}})();
@letorbi

This comment has been minimized.

Copy link
Owner

letorbi commented Feb 19, 2013

This GIST here is obsolete. Check out my GitHub repository at https://github.com/letorbi/tarp.require for a new and improved version of require().

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