define.js: Support libs that require `exports`

I'm not sure how this actually is supposed to work,
but I believe this is how we can support it so that
it's still useful.
This commit is contained in:
Johannes Meyer 2021-04-15 16:06:15 +02:00
parent 0da4a11d91
commit c202eccf65
1 changed files with 21 additions and 5 deletions

View File

@ -60,17 +60,33 @@
*/
define.resolve = function (name) {
var requirements = define.defines[name]['requirements'];
if (requirements.filter(define.has).length < requirements.length) {
return false;
}
var exports, ref;
var requiredRefs = [];
for (var i = 0; i < requirements.length; i++) {
requiredRefs.push(define.get(requirements[i]));
if (define.has(requirements[i])) {
ref = define.get(requirements[i]);
} else if (requirements[i] === 'exports') {
exports = ref = {};
} else {
return false;
}
requiredRefs.push(ref);
}
var factory = define.defines[name]['factory'];
define.set(name, factory.apply(null, requiredRefs));
var resolved = factory.apply(null, requiredRefs);
if (typeof exports === 'object') {
if (typeof resolved !== 'undefined') {
throw new Error('Factory for ' + name + ' returned, although exports were populated');
}
resolved = exports;
}
define.set(name, resolved);
for (var definedName in define.defines) {
if (define.defines[definedName]['requirements'].indexOf(name) >= 0) {