babel-polyfill
(自Babel 7.4起已弃用)。您还必须安装它才能使异步/等待工作。
npm i -D babel-core babel-polyfill babel-preset-es2015 babel-preset-stage-0 babel-loader
package.json
"devDependencies": {
"babel-core": "^6.0.20",
"babel-polyfill": "^6.0.16",
"babel-preset-es2015": "^6.0.15",
"babel-preset-stage-0": "^6.0.15"
}
.babelrc
{
"presets": [ "es2015", "stage-0" ]
}
带有async / await的.js(示例代码)
"use strict";
export default async function foo() {
var s = await bar();
console.log(s);
}
function bar() {
return "bar";
}
在启动文件中
require("babel-core/register");
require("babel-polyfill");
如果您使用的是webpack,则需要将其作为entry
数组的第一个值放在webpack配置文件中(通常为webpack.config.js
),按照@Cemen注释:
module.exports = {
entry: ['babel-polyfill', './test.js'],
output: {
filename: 'bundle.js'
},
module: {
loaders: [
{ test: /\.jsx?$/, loader: 'babel', }
]
}
};
如果要使用babel运行测试,请使用:
mocha --compilers js:babel-core/register --require babel-polyfill