mirror of
https://github.com/systemjs/systemjs.git
synced 2026-02-01 15:59:54 +00:00
49 lines
1.6 KiB
JavaScript
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);
|
|
}
|
|
} |