我正在尝试在Babel 6上从头开始使用async/await,但我得到的是regeneratorRuntime没有定义。
.babelrc文件
{
"presets": [ "es2015", "stage-0" ]
}
package.json文件
"devDependencies": {
"babel-core": "^6.0.20",
"babel-preset-es2015": "^6.0.15",
"babel-preset-stage-0": "^6.0.15"
}
.js文件
"use strict";
async function foo() {
await bar();
}
function bar() { }
exports.default = foo;
在没有async/await的情况下正常使用它,效果很好。知道我做错了什么吗?
如果你正在构建一个应用程序,你只需要@babel/preset-env和@babel/polyfill:
npm i -D @babel/preset-env
npm i @babel/polyfill
(注意:您不需要安装core js和再生器运行时包,因为它们都将由@babel/polyfill安装)
然后在.babelrc中:
{
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "entry" // this is the key. use 'usage' for further codesize reduction, but it's still 'experimental'
}
]
]
}
现在设置目标环境。在这里,我们在.browserlistrc文件中执行此操作:
# Browsers that we support
>0.2%
not dead
not ie <= 11
not op_mini all
最后,如果使用useBuiltIns:“entry”,请将import@babel/polyfill放在条目文件的顶部。否则,你就完蛋了。
使用此方法将有选择地导入那些polyfills和“再生器运行时”文件(此处未定义修复再生器的运行时问题),前提是任何目标环境/浏览器都需要它们。
需要babel polyfill(自babel 7.4起已弃用)。您还必须安装它才能使异步/等待工作。
npm i -D babel-core babel-polyfill babel-preset-es2015 babel-preset-stage-0 babel-loader
包.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"
}
巴氏合金
{
"presets": [ "es2015", "stage-0" ]
}
js与async/await(示例代码)
"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,则需要将其作为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
当我尝试使用ES6生成器时,使用gulf with rollup时会出现以下错误:
gulp.task('scripts', () => {
return rollup({
entry: './app/scripts/main.js',
format: "iife",
sourceMap: true,
plugins: [babel({
exclude: 'node_modules/**',
"presets": [
[
"es2015-rollup"
]
],
"plugins": [
"external-helpers"
]
}),
includePaths({
include: {},
paths: ['./app/scripts'],
external: [],
extensions: ['.js']
})]
})
.pipe(source('app.js'))
.pipe(buffer())
.pipe(sourcemaps.init({
loadMaps: true
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('.tmp/scripts'))
.pipe(reload({ stream: true }));
});
我可能认为解决方案是将babel polyfill作为bower组件:
bower install babel-polyfill --save
并将其作为依赖项添加到index.html中:
<script src="/bower_components/babel-polyfill/browser-polyfill.js"></script>
安装@babel polyfill并将其保存在开发依赖项中
npm安装--保存dev@babel/polyfill
复制和粘贴需要(“@babel/polyfill”);在输入文件的顶部
入口.js
require("@babel/polyfill"); // this should be include at the top
在条目数组中添加@babel/polyfill您需要在预设数组中预设env
webpack.config.js
常量路径=require('path');要求(“@babel/polyfill”);//必要的,必要的模块导出={条目:['@babel/polyfill','./src/js/index.js'],输出:{路径:路径.解析(__dirname,'to_be_deployed/js'),文件名:'main.js'},模块:{规则:[{测试:/\.m?js$/,exclude:/node_modules/,使用:{loader:“babel loader”,选项:{预设:['@babel/preset-env']}}}]},mode:'开发'}