【发布时间】:2015-05-31 09:05:08
【问题描述】:
ES6 模块系统似乎非常适合统一 CommonJs / AMD 语法。作为 requireJs/AMD 用户,我想转换为 ES6 模块(目前使用 babel.js)。
不过似乎有一个问题;通读文档和教程,似乎无法加载依赖于多个 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();
})
}
})
在 ES6 异步模块导入中这是不可能的,因为 System 是一个单例
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 **/ });
});
我的问题是:我错过了文档中的某些内容(可能将 System 子类化以能够配置多个 baseUrls),或者这是否正在为未来的模块扩展工作?
【问题讨论】:
-
从不同域加载异步模块包的用例是多个团队在一个大型站点(主应用程序)中创建子应用程序 - 需要在不同域上构建和部署正在重新部署的主应用程序。
标签: javascript asynchronous module ecmascript-6 systemjs