systemjs/lib/extension-bundles.js
2014-05-05 15:56:44 -07:00

49 lines
1.6 KiB
JavaScript

/*
System bundles
Allows a bundle module to be specified which will be dynamically
loaded before trying to load a given module.
For example:
System.bundles['mybundle'] = ['jquery', 'bootstrap/js/bootstrap']
Will result in a load to "mybundle" whenever a load to "jquery"
or "bootstrap/js/bootstrap" is made.
In this way, the bundle becomes the request that provides the module
*/
function bundles(loader) {
if (typeof indexOf == 'undefined')
indexOf = Array.prototype.indexOf;
// bundles support (just like RequireJS)
// bundle name is module name of bundle itself
// bundle is array of modules defined by the bundle
// when a module in the bundle is requested, the bundle is loaded instead
// of the form System.bundles['mybundle'] = ['jquery', 'bootstrap/js/bootstrap']
loader.bundles = loader.bundles || {};
var loaderFetch = loader.fetch;
loader.fetch = function(load) {
if (!loader.bundles)
loader.bundles = {};
// if this module is in a bundle, load the bundle first then
for (var b in loader.bundles) {
if (indexOf.call(loader.bundles[b], load.name) == -1)
continue;
// we do manual normalization in case the bundle is mapped
// this is so we can still know the normalized name is a bundle
return Promise.resolve(loader.normalize(b))
.then(function(normalized) {
loader.bundles[normalized] = loader.bundles[normalized] || loader.bundles[b];
return loader.load(normalized);
})
.then(function() {
return '';
});
}
return loaderFetch.apply(this, arguments);
}
}