babel ES7 Async - regeneratorRuntime 未定义

babel ES7 Async - regeneratorRuntime is not defined

我在我的项目中使用 browserify + gulp + babel,ES7 功能有问题。这些是我安装的:

这是我的 gulp 代码:

gulp.task('build', () => {
    let buildPath;
    let destPath;

    buildPath = `./src`;
    destPath = `./dist`;

    return browserify(`${buildPath}/app.js`)
    .transform(babelify, { 
        presets: ["es2015", "es2016", "stage-0"],
        plugins: ["transform-decorators-legacy", "transform-async-to-generator"]
    })
    .bundle()
    .pipe(source('app.js'))
    .pipe(buffer())
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest(`${destPath}`));
});

这是我的 js 代码:

import 'babel-polyfill';

// Async Functions
function wait(t) {
    return new Promise((r) => setTimeout(r, t));
}

async function asyncMania() {
    console.log('1');
    await wait(1000);
    console.log('2');
}

asyncMania().then(() => console.log('3'));

当我尝试这个时,出现错误:

Uncaught ReferenceError: regeneratorRuntime is not defined

使用 require 而不是 import 也不起作用。大多数问题都使用 Webpack,而不是 browserify 并且其他方法对我不起作用,所以非常感谢告诉我我应该怎么做。

还有一个问题,大家可以看到,babel-preset-es2015和babel-preset-es2016我都安装了,都在用。如果我删除 es2015 插件,我还能使用 ES6 功能吗?我还包括了 babel-preset-stage-0,据我所知,这是针对实验性 ES7 功能的。 babel-preset-es2016 得到了什么?

我有同样的错误并使用 "babel-plugin-transform-runtime" 修复它。 希望这对你也有用。