The ES6 module system seems to be a proper fit for unifying the CommonJs / AMD syntaxes. As a requireJs/AMD-user I'd like to convert to ES6 modules (using babel.js for now).
There seems to be one problem though; reading through the docs and tutorials, there doesn't seem to be possible to load module packages that are dependendent on more than one baseurl. Using requireJs this is solvable using the context
field:
// async dependencies are loaded from http://path/to/domain
var contextedRequire1 = require.config({
baseUrl: 'http://path/to/domain/js',
context: 'mainContext'
});
// async dependencies are located on http://path/to/otherdomain
var contextRequire2 = require.config({
baseUrl: 'http://path/to/otherdomain/js',
context: 'pluginContext'
});
contextedRequire1(['main.js'], function(main){
// loaded using http://path/to/domain/js/main.js
contextedRequire2(['plugin-lazyloading-deps.js'], function(plugin){
plugin.init();
});
});
In main.js
define(['main-deps'], function(mainDeps){
// loaded using http://path/to/domain/js/main-deps.js
})
In plugin-lazyloading-deps.js
define(['require'], function(require){
// loaded using http://path/to/otherdomain/js/plugin-lazyloading-deps.js
if(Modernizr.touch) {
require(['hammer'], function(){
// loaded using http://path/to/otherdomain/js/hammer.js
hammer.init();
})
}
})
In ES6 async module imports this isn't possible, since System
is a singleton
System.baseURL = "http://path/to/domain/js";
System.import("main").then(function(main){
// loaded using http://path/to/domain/js/main.js
// This will potentially break when main.js tries to load hammer.js from http://path/to/domain/js
System.baseURL = "http://path/to/otherdomain/js";
System.import("plugin-lazyloading-deps").then(function(){ /** code **/ });
});
My question is: Is there something in the docs that I've missed (possible to subclass System to be able to config several baseUrls), or is this something in the works for future module extensions?