diff options
Diffstat (limited to 'lib/bundles.js')
-rw-r--r-- | lib/bundles.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/lib/bundles.js b/lib/bundles.js new file mode 100644 index 000000000..c22a44986 --- /dev/null +++ b/lib/bundles.js @@ -0,0 +1,73 @@ +/* + System bundles + + Allows a bundle module to be specified which will be dynamically + loaded before trying to load a given module. + + For example: + SystemJS.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 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 SystemJS.bundles['mybundle'] = ['jquery', 'bootstrap/js/bootstrap'] + hookConstructor(function(constructor) { + return function() { + constructor.call(this); + this.bundles = {}; + this._loader.loadedBundles = {}; + }; + }); + + // assign bundle metadata for bundle loads + hook('locate', function(locate) { + return function(load) { + var loader = this; + var matched = false; + + if (!(load.name in loader.defined)) + for (var b in loader.bundles) { + for (var i = 0; i < loader.bundles[b].length; i++) { + var curModule = loader.bundles[b][i]; + + if (curModule == load.name) { + matched = true; + break; + } + + // wildcard in bundles does not include / boundaries + if (curModule.indexOf('*') != -1) { + var parts = curModule.split('*'); + if (parts.length != 2) { + loader.bundles[b].splice(i--, 1); + continue; + } + + if (load.name.substring(0, parts[0].length) == parts[0] && + load.name.substr(load.name.length - parts[1].length, parts[1].length) == parts[1] && + load.name.substr(parts[0].length, load.name.length - parts[1].length - parts[0].length).indexOf('/') == -1) { + matched = true; + break; + } + } + } + + if (matched) + return loader['import'](b) + .then(function() { + return locate.call(loader, load); + }); + } + + return locate.call(loader, load); + }; + }); +})(); |