3

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();
    })
  }
})

SystemES6 非同期モジュールのインポートでは、シングルトンであるため、これは不可能です。

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をサブクラス化できます)、またはこれは将来のモジュール拡張のために機能するものですか?

4

2 に答える 2

1

少なくとも SystemJS の現在のバージョンでは、ワイルドカード パスを指定できます。https://github.com/systemjs/systemjs/wiki/Configuration-Options#paths-unstable

私はそれを自分で使用したことはありませんが、あなたの場合は使用するようです

System.baseURL = 'http://path/to/domain/js';
System.paths['plugin-*'] = 'http://path/to/otherdomain/js/plugin-*';
于 2015-05-31T17:18:54.250 に答える