Created
December 28, 2022 21:14
-
-
Save DarrenSem/f950b4104f444823e640a3385bc5a22b to your computer and use it in GitHub Desktop.
require.js in just 793 chars -- even works from local CSCRIPT.EXE JScript.js (because not everybody runs Node) -- var exports = require('moduleName')
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
// require.js in just 793 chars -- even works from local CSCRIPT.EXE JScript.js (because not everybody runs Node) -- var exports = require('moduleName') | |
(function(a){var b={},c=[],d=function(a,d){var f={exports:{}},g=a.length?e.resolve(a,c[0]):"",h=e.fetch(g),i=null!=h&&eval("[(function(exports,require,module,__filename,__dirname){"+(h+"})][0]"));if(i){b[d]=f.exports,c.unshift(g);try{i(b[d],e,f,g,e.dir(g))}catch(a){}return c.shift(),f.exports}},e=function(a){var c=(a+"").replace(/\\/g,"/").replace(/\.js$/i,""),e=c.toLowerCase();if(a)return e in b?b[e]:d(c,e)};e.resolve=function(a,b){return(/^\.{1,2}/.test(a)?e.dir(b||location.href.replace(/\\/g,"/"))+"/":"")+a+".js"},e.fetch=function(a){try{var b=a.length&&new XMLHttpRequest,c=b&&(b.send(b.open("GET",a,!1)),b.status),d=b&&b.responseText;if(200===c||d.length&&!c)return d}catch(a){}},e.dir=function(a){var b=a.lastIndexOf("/");return 0>b?"":a.slice(0,b)},a.require=a.require||e})(this); | |
(function(globalContext){ | |
var moduleName = "require"; | |
var cache = {}; | |
var loading = []; | |
var fetchModule = function(identifier, key) { | |
var mod = { exports: {} }; | |
var filename = !identifier.length ? "" | |
: out.resolve( identifier, loading[0] ); | |
var text = out.fetch(filename); | |
var func = text != null && (0, eval)( | |
"[(function(exports,require,module,__filename,__dirname){" | |
+ String(text) + "})][0]" | |
); | |
if(!func)return; | |
cache[key] = mod.exports; | |
loading.unshift(filename); | |
try { | |
func( cache[key], out, mod, filename, out.dir(filename) ); | |
} catch(e) {}; | |
loading.shift(); | |
return mod.exports; | |
}; | |
var out = function(moduleId) { | |
var identifier = String(moduleId) | |
.replace(/\\/g, "/") | |
.replace(/\.js$/i, ""); | |
var cacheKey = identifier.toLowerCase(); | |
if(moduleId)return cacheKey in cache ? cache[cacheKey] | |
: fetchModule(identifier, cacheKey); | |
}; | |
out.resolve = function(identifier, root) { | |
return ( | |
/^\.{1,2}/.test(identifier) ? out.dir( | |
root || location.href.replace(/\\/g, "/") | |
) + "/" : "" | |
) + identifier + ".js"; | |
}; | |
out.fetch = function(url) { | |
try { | |
var xhr = url.length && new XMLHttpRequest; | |
var code = xhr && ( | |
xhr.send( xhr.open("GET", url, false) ), | |
xhr.status | |
); | |
var text = xhr && xhr.responseText; | |
if( code === 200 || ( text.length && !code ) )return text; | |
} catch(e) {}; | |
}; | |
out.dir = function(filename) { | |
var finalSlash = filename.lastIndexOf("/"); | |
return finalSlash < 0 ? "" : filename.slice(0, finalSlash); | |
}; | |
globalContext[moduleName] = globalContext[moduleName] || out; | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment