-
-
Save caridy/1a67aaf433ae03a42a8b to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
Copyright 2014 Yahoo! Inc. All rights reserved. | |
Licensed under the BSD License. | |
https://github.com/yahoo/es-loader-extensions/blob/master/LICENSE.md | |
*/ | |
/*jslint esnext: true*/ | |
/*global Promise*/ | |
export default function (loader, options) { | |
var loaderNormalize = loader.normalize.bind(loader), | |
triggers = options.triggers || {}; | |
function testCondition(name) { | |
var trigger = triggers[name]; | |
if (!trigger) { | |
return name; | |
} | |
if (typeof trigger.test !== 'function') { | |
throw new TypeError('Triggers must include a test function'); | |
} | |
// Trigger tests may return true/false or a promise for a boolean | |
// value. Promise failures are interpreted as test failures, | |
// same as returning false. | |
// Also, testCondition being run inside a Promise init function | |
// means thrown errors are turned into test failures | |
return new Promise(function (resolve, reject) { | |
// tests being run inside a Promise initialization function | |
// means thrown errors are turned into test failures | |
try { | |
resolve(trigger.test()); | |
} catch (err) { | |
reject(err); | |
} | |
}).then(function (result) { | |
return result ? trigger.replaceWith : name; | |
}, function () { | |
return name; | |
}); | |
} | |
loader.normalize = function (name) { | |
return Promise.resolve(loaderNormalize(name)).then(testCondition); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment