使用同步语法在运行时导入commonJS,AMD和ES6模块

时间:2014-09-16 21:04:14

标签: module ecmascript-6 systemjs

我最近使用import {variable} from 'ES6module'语法编写了很多ES6,并使用traceur-compiler在浏览器中编译代码。我开始关注systemjs,因为它似乎可以让我使用相同的语法来导入AMD,commonJS, ES6模块。

阅读文档已开始让我感到困惑。我看到很多异步调用,如System.import('path/to/module').then(function(variable) { ... }),我不习惯用于依赖项导入。

我已阅读Practical workflows for ES6 modules,其中涵盖了许多不同的工作流程,其中没有一项涉及在运行时导入ES6模块 AMD / commonjs模块。我在想这样的事情:

import {myObject} from 'my/es6/module';
import {_} from 'lib/underscore';

或者如果不是,至少:

import {myObject} from 'my/es6/module';
var _ = require('lib/underscore');

使用systemjs是否可以使用其中任何一种?

1 个答案:

答案 0 :(得分:9)

此用例正是SystemJS的设计目的。

你可以写:

import {myObject} from 'my/es6/module';
import _ from 'lib/underscore';

原因是因为SystemJS中的CommonJS,AMD和Global模块被视为只导出default属性,这与上面的默认导入语法相对应。