XVoxel-ASync/lib/loader.js

33 lines
1005 B
JavaScript

if (!window.load) window.load = function (moduleName) {
return new Promise(function(resolve, reject) {
fetch(moduleName).then(function (response) {
if (response.status !== 200) return reject(new Error("Status code is not 200. "));
response.text().then(function (moduleContents) {
try {
var moduleObject = {
"exports": {}
};
var BoundFunction = Function.prototype.bind.apply(Function, [moduleObject]);
var moduleFunction = new BoundFunction("global", "window", "module", "exports",
"return eval(" + JSON.stringify(
moduleContents
+ "\n//# sourceURL=" + (new URL(moduleName, document.baseURI).href) + "\n"
) + ");"
).bind(moduleObject.exports);
var result = moduleFunction.apply(window, [window, window, moduleObject, moduleObject.exports]);
resolve(moduleObject.exports);
} catch (e) {
reject(e);
}
}).catch(function (err) {
reject(err);
});
}).catch(function (err) {
reject(err);
});
});
};