Gulp Babel Async / Await

问题描述

尝试缩小文件并直接在浏览器中运行。

我正在用口水和通心粉来做。问题出在我尝试使用异步/等待功能时。

package.json

{
    "@babel/core": "^7.11.6","@babel/plugin-transform-async-to-generator": "^7.12.1","@babel/plugin-transform-runtime": "^7.12.1","@babel/preset-env": "^7.11.5","gulp": "^4.0.2","gulp-babel": "^8.0.0","gulp-concat": "^2.6.1",...
}

文件

const i = async () => {
    return await fetchAll();
};

Gulp / Babel配置

const BabelConfig = {
    presets: ['@babel/env'],plugins: ['@babel/plugin-transform-async-to-generator']
};
const imports = ['./dev/**.*.js'];
return gulp.src(imports)
    .pipe(babel(BabelConfig))
    .pipe(concat('min.js'))
    .pipe(gulp.dest(paths.dist.js));

这只是抛出“未定义regeneratorRuntime”。

所以我尝试添加“ @ babel / plugin-transform-runtime”。

Gulp / Babel配置

const BabelConfig = {
    presets: ['@babel/env'],plugins: ['@babel/plugin-transform-async-to-generator','@babel/plugin-transform-runtime']
};
const imports = ['./dev/**.*.js'];
return gulp.src(imports)
    .pipe(babel(BabelConfig))
    .pipe(concat('min.js'))
    .pipe(gulp.dest(paths.dist.js));

但是现在我得到“需求未定义”。

有人对如何实现这一目标有任何线索吗?

解决方法

你快到了!我有一个类似的问题,问题是编译的 Gulp 代码包含大多数浏览器不支持的“require”语句。对我来说解决问题的是将 Webpack 添加到 Gulp 工作流以捆绑所有内容:

npm install --save-dev webpack webpack-cli webpack-stream

在你的 gulpfile.js 中:

const {src,dest,watch} = require('gulp');
const gulpBabel = require('gulp-babel');
const webpack = require('webpack-stream');
const compiler = require('webpack');

function myES6Transpiler() {
    return src('./es6/utilities.js')
        .pipe(gulpBabel({
            presets: ['@babel/preset-env','babel-preset-minify'],plugins: ['@babel/transform-runtime','@babel/plugin-transform-async-to-generator'],}))
        .pipe(webpack(require('./webpack.config-util.js'),compiler,function (err,stats) {
            /* Use stats to do more things if needed */
        }))
        .pipe(dest('./js/'))
}

exports.myES6Transpiler = myES6Transpiler;

您还需要添加一个 Webpack 配置文件: webpack.config.js

module.exports = {
    mode: "production",output: {
        filename: "utilities.js"
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...