gulp执行任务出现错误的时候显示详细的信息

今天执行下面一个gulp任务:

const gulp = require('gulp');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');

gulp.task('uglify', function() {
  return gulp.src(["www/dist_js/*.js"])
    .pipe(concat('all.min.js'))
    .pipe(uglify({outSourceMap: false, cache: true}))
    .pipe(gulp.dest("asserts/js"));
});

执行之后出现了下面的错误:

GulpUglifyError: unable to minify JavaScript
    at createError (/Users/jetma/app-spaces/javatang-app/node_modules/gulp-uglify/lib/create-error.js:6:14)
    at wrapper (/Users/jetma/app-spaces/javatang-app/node_modules/gulp-uglify/node_modules/lodash/_createHybrid.js:87:15)
    at trycatch (/Users/jetma/app-spaces/javatang-app/node_modules/gulp-uglify/minifier.js:26:12)
    at DestroyableTransform.minify [as _transform] (/Users/jetma/app-spaces/javatang-app/node_modules/gulp-uglify/minifier.js:79:19)
    at DestroyableTransform.Transform._read (/Users/jetma/app-spaces/javatang-app/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:182:10)
    at DestroyableTransform.Transform._write (/Users/jetma/app-spaces/javatang-app/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:170:83)
    at doWrite (/Users/jetma/app-spaces/javatang-app/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:406:64)
    at writeOrBuffer (/Users/jetma/app-spaces/javatang-app/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:395:5)
    at DestroyableTransform.Writable.write (/Users/jetma/app-spaces/javatang-app/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:322:11)
    at DestroyableTransform.ondata (/Users/jetma/app-spaces/javatang-app/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:612:20)
    at emitOne (events.js:115:13)
    at DestroyableTransform.emit (events.js:210:7)
    at addChunk (/Users/jetma/app-spaces/javatang-app/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:284:12)
    at readableAddChunk (/Users/jetma/app-spaces/javatang-app/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:271:11)
    at DestroyableTransform.Readable.push (/Users/jetma/app-spaces/javatang-app/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:238:10)
    at DestroyableTransform.Transform.push (/Users/jetma/app-spaces/javatang-app/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:146:32)
    at /Users/jetma/app-spaces/javatang-app/node_modules/gulp-fs-cache/index.js:47:15
    at FSReqWrap.oncomplete (fs.js:152:21)

这错误提示真是无语了 ... :astonished: 除了显示一个 unable to minify JavaScript 之外没有任何有用的信息,不知道问题出在什么地方。

这个时候可以添加 on('error', function(err) {},代码如下:

const gulp = require('gulp');
const gutil = require('gulp-util');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');

gulp.task('uglify', function() {
  return gulp.src(["www/dist_js/*.js"])
    .pipe(concat('all.min.js'))
    .pipe(uglify({outSourceMap: false, cache: true}))
    .on('error', function (err) {
      gutil.log(gutil.colors.red('[Error]'), err.toString());
    })
    .pipe(gulp.dest("asserts/js"));
});

再次执行之后显示如下的错误信息:

[Error] GulpUglifyError: unable to minify JavaScript
Caused by: SyntaxError: Unexpected character '`'
File: /Users/jetma/app-spaces/javatang-app/platforms/ios/www/dist_js/app.min.js
Line: 5548

这样很容易就可以找到造成错误的原因,如果想要出现错误的时候任务停止,可以在function中将错误抛出,最终代码如下:

const gulp = require('gulp');
const gutil = require('gulp-util');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');

gulp.task('uglify', function() {
  return gulp.src(["www/dist_js/*.js"])
    .pipe(concat('all.min.js'))
    .pipe(uglify({outSourceMap: false, cache: true}))
    .on('error', function (err) {
      gutil.log(gutil.colors.red('[Error]'), err.toString());
      throw err;
    })
    .pipe(gulp.dest("asserts/js"));
});

执行的效果如下:

参考资料:
GulpUglifyError: unable to minify JavaScript

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注