Last active
October 13, 2018 20:05
-
-
Save wallabra/ba3e3a0a9fd493ebe382d0d887ed50a1 to your computer and use it in GitHub Desktop.
JavaScript Bilateral Module Loader (JS-BML) – the core of CrossCore (pun not intended)!
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
var AjaxRequest = null; | |
if ((typeof require) === 'undefined') { | |
if ((typeof XMLHttpRequest) !== 'undefined') | |
AjaxRequest = function () { | |
return new XMLHttpRequest(); | |
}; | |
else if ((typeof ActiveXObject) !== 'undefined') | |
AjaxRequest = function () { | |
return new ActiveXObject("Microsoft.XMLHTTP"); | |
}; | |
else | |
throw new Error('The CrossCore bilateral module loader needs an AJAX-capable JavaScript API on your browser, but neither XMLHttpRequest, nor ActiveXObject, were found. Try using a newer browser.'); | |
} | |
var loader = { | |
cache: {}, | |
root: ".", | |
setRoot: function setRoot(root) { | |
if ((typeof require) !== 'undefined') | |
loader.root = root; | |
}, | |
importModule: function importModule(modulePath, moduleName, callback, force) { | |
if (Object.keys(loader.cache).indexOf(moduleName) > -1 && !force) | |
callback(loader.cache[moduleName]); | |
if ((typeof require) === 'undefined') { | |
let req = new AjaxRequest(); | |
req.onreadystatechange = function () { | |
if (this.readyState == 4 && this.status == 200) { | |
var mod = new Function("loader", this.responseText)(loader); | |
loader.cache[moduleName] = mod; | |
callback(mod); | |
} | |
}; | |
req.open("GET", modulePath, true); | |
req.send(); | |
} else { | |
var mod = new Function("loader", require('fs').readFileSync(require('path').join(loader.root, modulePath)))(loader); | |
loader.cache[moduleName] = mod; | |
callback(mod); | |
} | |
}, | |
importModuleSync: function importModule(modulePath, moduleName, force) { | |
if (Object.keys(loader.cache).indexOf(moduleName) > -1 && !force) | |
return loader.cache[moduleName]; | |
var mod; | |
if ((typeof require) === 'undefined') { | |
let req = new XMLHttpRequest(); | |
req.open("GET", modulePath, false); | |
req.send(); | |
if (req.status == 200) { | |
mod = new Function("loader", req.responseText)(loader); | |
loader.cache[moduleName] = mod; | |
return mod; | |
} | |
else | |
return null; | |
} else { | |
mod = new Function("loader", require('fs').readFileSync(require('path').join(loader.root, modulePath)))(loader); | |
loader.cache[moduleName] = mod; | |
return mod; | |
} | |
}, | |
globalImport: function globalImport(modulePath, moduleName, force) { | |
function callback(mod) { | |
if ((typeof require) === 'undefined') | |
window[moduleName] = mod; | |
else | |
global[moduleName] = mod; | |
} | |
importModule(modulePath, moduleName, callback, force); | |
}, | |
globalImportSync: function globalImportSync(modulePath, moduleName, force) { | |
function callback(mod) { | |
if ((typeof require) === 'undefined') | |
window[moduleName] = mod; | |
else | |
global[moduleName] = mod; | |
} | |
callback(importModuleSync(modulePath, moduleName, force)); | |
}, | |
}; | |
if ((typeof require) !== 'undefined') { | |
require.modules = {}; | |
module.exports = loader; | |
} else | |
window.loader = loader; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment