ES6 モジュール システムは、CommonJs / AMD 構文を統合するのに適しているようです。requireJs/AMD ユーザーとして、ES6 モジュールに変換したいと考えています (今のところは babel.js を使用しています)。
ただし、1 つの問題があるようです。ドキュメントとチュートリアルを読んでみると、複数の baseurl に依存するモジュール パッケージをロードすることはできないようです。requireJs を使用すると、これは次のcontext
フィールドを使用して解決できます。
// 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();
});
});
main.js で
define(['main-deps'], function(mainDeps){
// loaded using http://path/to/domain/js/main-deps.js
})
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();
})
}
})
System
ES6 非同期モジュールのインポートでは、シングルトンであるため、これは不可能です。
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 **/ });
});
私の質問は次のとおりです:ドキュメントに見逃したものがありますか(複数のbaseUrlを構成できるようにSystemをサブクラス化できます)、またはこれは将来のモジュール拡張のために機能するものですか?