Mathias Panzenböck avatar Mathias Panzenböck committed 82db019

require.js: require() implementation for browsers using XHR

because XHR only works for the same domain this only works for the same domain

Comments (0)

Files changed (1)

+const require = (function () {
+	// be careful so that nothing is bound to the closure made by eval:
+	return (function (_eval) {
+		var loading = {};
+		var loaded  = {};
+	
+		function _require (module, opts) {
+			if (opts === undefined) {
+				opts = {};
+			}
+	
+			if (opts.async === undefined) {
+				opts.async = false;
+			}
+	
+			var moduleObj = loaded[module];
+	
+			if (moduleObj === undefined) {
+				moduleObj = loading[module];
+			}
+	
+			if (moduleObj === undefined) {
+				loading[module] = moduleObj = {};
+	
+				var argnames = ['exports', 'require'];
+				var args = [moduleObj, _require];
+	
+				if (opts.globals) {
+					for (argname in opts.globals) {
+						if (!/^[_a-zA-Z][_a-zA-Z0-9]*$/.test(argname)
+								|| argname == 'exports'
+								|| argname == 'require') {
+							throw new Error('Illegal global name: '+argname);
+						}
+						argnames.push(argname);
+						args.push(opts.globals[argname]);
+					}
+				}
+	
+				var xhr = new XMLHttpRequest();
+				var err = null;
+	
+				xhr.addEventListener('abort', function () {
+					delete loading[module];
+					err = new Error(
+						'Error loading module: ' + module +
+						'\nTransfer aborted by user.');
+					if (opts.onError) {
+						opts.onError(err);
+					}
+				}, false);
+	
+				function transferError () {
+					delete loading[module];
+					err = new Error(
+						'Error loading module: ' + module +
+						'\nHTTP Status: ' + xhr.status + ' ' + xhr.statusText);
+					if (opts.onError) {
+						opts.onError(err);
+					}
+				}
+	
+				xhr.addEventListener('error', transferError, false);
+				
+				xhr.addEventListener('load', function () {
+					if (xhr.status != 200 && xhr.status != 304) {
+						transferError();
+						return;
+					}
+	
+					try {
+						_eval(
+							'(function ('+argnames.join(',')+') { ' +
+							xhr.responseText+' })').apply(null, args);
+							
+						delete loading[module];
+						loaded[module] = moduleObj;
+					}
+					catch (e) {
+						delete loading[module];
+						err = e;
+						if (opts.onError) {
+							opts.onError(err);
+						}
+	
+						return;
+					}
+	
+					if (opts.onLoad) {
+						opts.onLoad(moduleObj);
+					}
+				}, false);
+	
+				xhr.open('GET', module, opts.async);
+				xhr.send(null);
+	
+				if (err != null) {
+					throw err;
+				}
+			}
+			else if (opts.onLoad) {
+				opts.onLoad(moduleObj);
+			}
+	
+			return moduleObj;
+		}
+	
+		return _require;
+	})(function (code) { return eval(code); });
+})();
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.