2017-10-03 15:35:31 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-10-10 21:11:05 +02:00
|
|
|
const require = function(){
|
2017-10-03 15:35:31 +02:00
|
|
|
"use strict";
|
2018-10-02 13:20:40 +02:00
|
|
|
if (!window.scope){
|
|
|
|
window.scope = {};
|
|
|
|
}
|
2017-10-10 21:11:05 +02:00
|
|
|
const scope = window.scope;
|
2017-11-07 00:36:44 +01:00
|
|
|
|
|
|
|
function getScopeName(module){
|
2019-11-28 01:26:35 +01:00
|
|
|
const scopeName = module.replace(/^\..*\//, "").replace(/\..+/, "");
|
2019-04-08 00:02:29 +02:00
|
|
|
// console.log(scopeName);
|
2017-11-07 00:36:44 +01:00
|
|
|
return scopeName;
|
|
|
|
}
|
|
|
|
|
2017-10-10 21:11:05 +02:00
|
|
|
function require(module){
|
2019-04-08 00:02:29 +02:00
|
|
|
if (module.startsWith(".")){
|
2019-11-28 01:26:35 +01:00
|
|
|
const scopeName = getScopeName(module);
|
2017-10-10 21:11:05 +02:00
|
|
|
return scope[scopeName];
|
|
|
|
}
|
|
|
|
throw new ReferenceError("Unable to get non relative module " + module + "!");
|
2017-06-25 22:22:17 +02:00
|
|
|
}
|
2018-09-14 16:29:30 +02:00
|
|
|
|
2019-02-26 15:59:44 +01:00
|
|
|
require.register = function(moduleName, module = {}){
|
2019-03-12 22:24:23 +01:00
|
|
|
const scopeName = getScopeName(moduleName);
|
2019-04-08 23:56:25 +02:00
|
|
|
if (!scope.hasOwnProperty(scopeName)){
|
2019-02-26 15:59:44 +01:00
|
|
|
scope[scopeName] = module;
|
|
|
|
return module;
|
2018-09-14 16:29:30 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-02-26 15:59:44 +01:00
|
|
|
require("./logging").error("Module", moduleName, "already registered.");
|
2019-03-12 22:24:23 +01:00
|
|
|
return scope[scopeName];
|
2018-09-14 16:29:30 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
require.exists = function(module){
|
|
|
|
return scope.hasOwnProperty(getScopeName(module));
|
|
|
|
};
|
2017-09-23 23:47:44 +02:00
|
|
|
|
2017-10-10 21:11:05 +02:00
|
|
|
return require;
|
|
|
|
}();
|