babel-polyfill
is required.
(babel-polyfill
。)
You must also install it in order to get async/await working.(您还必须安装它才能使异步/等待工作。)
npm i -D babel-core babel-polyfill babel-preset-es2015 babel-preset-stage-0 babel-loader
package.json
(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
(.babelrc)
{
"presets": [ "es2015", "stage-0" ]
}
.js with async/await (sample code)
(带有async / await的.js(示例代码))
"use strict";
export default async function foo() {
var s = await bar();
console.log(s);
}
function bar() {
return "bar";
}
In the startup file
(在启动文件中)
require("babel-core/register");
require("babel-polyfill");
If you are using webpack you need to put it as the first value of your entry
array in your webpack configuration file (usually webpack.config.js
), as per @Cemen comment:
(如果您使用的是webpack ,则需要按照@Cemen注释 ,将其作为entry
数组的第一个值放入webpack配置文件(通常是webpack.config.js
)中:)
module.exports = {
entry: ['babel-polyfill', './test.js'],
output: {
filename: 'bundle.js'
},
module: {
loaders: [
{ test: /.jsx?$/, loader: 'babel', }
]
}
};
If you want to run tests with babel then use:
(如果要使用babel运行测试,请使用:)
mocha --compilers js:babel-core/register --require babel-polyfill
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…