Async Await + Gulp - GulpUglifyError:无法缩小JavaScript?

时间:2017-10-12 08:27:03

标签: javascript ecmascript-6 gulp async-await

这是我的gulpfile中的一部分:

gulp.task('compile-js', function () {
    // app.js is your main JS file with all your module inclusions
  return browserify({
    extensions: ['.js', '.jsx'],
    entries: 'javascripts/app.js',
    debug: true
  })
  .transform('babelify', { presets: ['es2015', 'react'] })
  .bundle()
  .pipe(source('bundle.min.js'))
  .pipe(buffer())
  .pipe(sourcemaps.init())
  .pipe(uglify())
  .pipe(sourcemaps.write('./maps'))
  .pipe(gulp.dest('dist'))
  .pipe(livereload())
})

javascripts/app.js

我有一个例子:

async function write () {
    var txt = await read();
    console.log(txt);
  }

我收到此错误:

GulpUglifyError: unable to minify JavaScript

这是我的package.json:

的一部分
  "dependencies": {
    "animate.css": "^3.5.2",
    "aos": "^2.2.0",
    "autosize": "^4.0.0",
    "axios": "^0.16.2",
    "es6-docready": "^1.0.0",
    "foundation-icon-fonts": "^0.1.1",
    "foundation-sites": "^6.4.3",
    "gulp-clean-css": "^3.9.0",
    "gulp-cssimport": "^5.1.1",
    "jquery": "^3.1.1",
    "jquery-ui-bundle": "^1.12.1",
    "material-design-icons": "^3.0.1",
    "swiper": "^3.4.2",
    "vue": "^2.4.4"
  },
  "repository": {
    "type": "git",
    "url": "[git-url-of-your-project]"
  },
  "devDependencies": {
    "babel-preset-es2015": "^6.16.0",
    "babel-preset-react": "^6.16.0",
    "babelify": "^7.3.0",
    "browserify": "^14.3.0",
    "browserify-shim": "^3.8.12",
    "gulp": "^3.9.1",
    "gulp-clean-css": "^3.0.4",
    "gulp-clone": "^1.0.0",
    "gulp-concat": "^2.6.0",
    "gulp-concat-css": "^2.3.0",
    "gulp-cssimport": "^5.0.0",
    "gulp-foreach": "^0.1.0",
    "gulp-htmlmin": "^3.0.0",
    "gulp-less": "^3.3.2",
    "gulp-livereload": "^3.8.1",
    "gulp-sourcemaps": "^1.6.0",
    "gulp-uglify": "^2.1.2",
    "streamqueue": "^1.1.1",
    "vinyl-buffer": "^1.0.0",
    "vinyl-source-stream": "^1.1.0"
  },

我还应该为ES6的gulp流程添加什么?

修改

所以关注es2017的babel bug

我已安装:

npm install --save-dev babel-preset-es2017

改变了我的gulpfile:

...
.transform('babelify', {
    presets: ['es2015', 'es2017', 'react'],

我在编译过程中没有收到任何错误,但是当我在浏览器上运行代码时,我在浏览器控制台上收到此错误:

Uncaught ReferenceError: regeneratorRuntime is not defined

2 个答案:

答案 0 :(得分:3)

Had similar issue with gulp-uglify, and as I've found out it seems that minifying async/await is not supported with base gulp-uglify, so I switched to babel-minify, and it worked for me.

EDIT: For regeneratorRuntime is not defined error, I think that regenerator-runtime could be helpful.

答案 1 :(得分:2)

我已经解决了替换问题:

.pipe(uglify()) .pipe(minify())

https://www.npmjs.com/package/gulp-minify