我正在开发一个新的 Angular2 项目,其中 Webpack 作为模型加载器,Typescript 作为开发语言。
当我将 Typescript 编译器定位到 ES5 时,一切正常。但是当我以 ES6 为目标并将其添加babel-loader
为 ES5 的 trasplier 时,我得到了错误:Unexpected token import
工作 ES5 配置:
// tsconfig.json
"compilerOptions":
{
"target": "es5",
// ..
}
// webpack.config.js
module:
{
loaders:
[
{ test: /\.tsx?$/, exclude: /node_modules/, loader: "ts-loader" },
{ test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" },
// ...
]
}
// app.ts
import 'core-js' // this line is transpiled by ts-comiler/loader to 'require('core-js')'
不工作的 ES6 配置:
// tsconfig.json
"compilerOptions":
{
"target": "es6",
// ..
}
// webpack.config.js
module:
{
loaders:
[
{ test: /\.tsx?$/, exclude: /node_modules/, loader: "babel-loader!ts-loader" },
{ test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" },
// ...
]
}
// app.ts
import 'core-js' // this line throw the error: Uncaught SyntaxError: Unexpected token import
我不明白为什么没有定义 ES6 的 import 关键字???
我还注意到,如果我将 import 语句更改为 require() 语句,则不会出现错误,但我想使用 ES6 导入语法。