Skip to content

Instantly share code, notes, and snippets.

@caridy
Created June 18, 2014 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caridy/1a67aaf433ae03a42a8b to your computer and use it in GitHub Desktop.
Save caridy/1a67aaf433ae03a42a8b to your computer and use it in GitHub Desktop.
/*
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