gulp-uglify events.js unhandled 'error' ev

2019-03-18 06:01发布

问题:

I'm getting this error. Running gulp yesterday worked perfectly fine, but this morning (changed NO code) and I'm getting this error.

$ gulp
[08:54:10] Using gulpfile C:\Source\Source2\bunny-meadows\gulpfile.js
[08:54:10] Starting 'scripts'...
[08:54:10] 'scripts' errored after 11 ms
[08:54:10] TypeError: listener must be a function
    at TypeError (<anonymous>)
    at DestroyableTransform.addListener (events.js:130:11)
    at DestroyableTransform.Readable.on (C:\Source\Source2\bunny-meadows\node_mo
dules\gulp-uglify\node_modules\through2\node_modules\readable-stream\lib\_stream
_readable.js:729:33)
    at Gulp.<anonymous> (C:\Source\Source2\bunny-meadows\gulpfile.js:37:10)
    at module.exports (C:\Source\Source2\bunny-meadows\node_modules\gulp\node_mo
dules\orchestrator\lib\runTask.js:34:7)
    at Gulp.Orchestrator._runTask (C:\Source\Source2\bunny-meadows\node_modules\
gulp\node_modules\orchestrator\index.js:273:3)
    at Gulp.Orchestrator._runStep (C:\Source\Source2\bunny-meadows\node_modules\
gulp\node_modules\orchestrator\index.js:214:10)
    at Gulp.Orchestrator.start (C:\Source\Source2\bunny-meadows\node_modules\gul
p\node_modules\orchestrator\index.js:134:8)
    at c:\Users\hschillig.SCDL\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js
:129:20
    at process._tickCallback (node.js:419:13)

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error
    at new JS_Parse_Error (C:\Source\Source2\bunny-meadows\node_modules\gulp-ugl
ify\node_modules\uglify-js\lib\parse.js:196:18)
    at js_error (C:\Source\Source2\bunny-meadows\node_modules\gulp-uglify\node_m
odules\uglify-js\lib\parse.js:204:11)
    at croak (C:\Source\Source2\bunny-meadows\node_modules\gulp-uglify\node_modu
les\uglify-js\lib\parse.js:675:9)
    at token_error (C:\Source\Source2\bunny-meadows\node_modules\gulp-uglify\nod
e_modules\uglify-js\lib\parse.js:683:9)
    at expect_token (C:\Source\Source2\bunny-meadows\node_modules\gulp-uglify\no
de_modules\uglify-js\lib\parse.js:696:9)
    at expect (C:\Source\Source2\bunny-meadows\node_modules\gulp-uglify\node_mod
ules\uglify-js\lib\parse.js:699:36)
    at function_ (C:\Source\Source2\bunny-meadows\node_modules\gulp-uglify\node_
modules\uglify-js\lib\parse.js:959:9)
    at expr_atom (C:\Source\Source2\bunny-meadows\node_modules\gulp-uglify\node_
modules\uglify-js\lib\parse.js:1188:24)
    at maybe_unary (C:\Source\Source2\bunny-meadows\node_modules\gulp-uglify\nod
e_modules\uglify-js\lib\parse.js:1358:19)
    at expr_ops (C:\Source\Source2\bunny-meadows\node_modules\gulp-uglify\node_m
odules\uglify-js\lib\parse.js:1393:24)
    at maybe_conditional (C:\Source\Source2\bunny-meadows\node_modules\gulp-ugli
fy\node_modules\uglify-js\lib\parse.js:1398:20)
    at maybe_assign (C:\Source\Source2\bunny-meadows\node_modules\gulp-uglify\no
de_modules\uglify-js\lib\parse.js:1422:20)

My gulpfile.js looks like this:

var gulp = require('gulp');

// Include Our Plugins
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var livereload = require('gulp-livereload');

var paths = {
    // using framework version of jquery
    scripts: [
        'public/assets/js/jquery.min.js',
        'public/assets/plugins/mustachejs/mustache.js',
        'public/assets/js/modernizr.js',
        'public/assets/js/bootstrap.min.js',
        'public/assets/js/placeholders.min.js',
        'public/assets/plugins/bootstrap-wysiwyg/bootstrap3-wysihtml5.all.min.js',
        'public/assets/plugins/bootstrap-wysiwyg/bootstrap3-wysihtml5.min.js',
        'public/assets/plugins/responsiveslides/responsiveslides.min.js',
        'public/assets/plugins/bxslider/jquery.bxslider.min.js',
        'public/assets/plugins/magnific-popup/magnific-popup.js',
        'public/assets/js/jquery.autocomplete.min.js',
        'public/assets/js/plugins/additems.js',
        'public/assets/js/plugins/farm/farm.js',
        'public/assets/js/plugins/plusminus.js',
        'public/assets/js/core.js'
    ]
};

// Concatenate & Minify JS
gulp.task('scripts', function() {
    return gulp.src(paths.scripts)
        .pipe(concat('all.js'))
        .pipe(gulp.dest('public/assets/js'))
        .pipe(rename('all.min.js'))
        .pipe(uglify())
        .on('error')
        .pipe(gulp.dest('public/assets/js'))
        .pipe(livereload());
});

// Watch Files For Changes
gulp.task('watch', function() {
    livereload.listen();
    gulp.watch(paths.scripts, ['scripts']);
});

// Default Task
gulp.task('default', ['scripts', 'watch']);

Again I didn't change anything so I don't get why all of a sudden this morning it doesn't want to work.

回答1:

Following this helped:

  1. Install Gulp-util
  2. Import the Gulp-util by using the following statement: var gutil = require('gulp-util');
  3. Finally, when you are uglifying the code, attach the error handler like this: .pipe(uglify().on('error', gutil.log))

I was able to debug it. It was a syntax error in one of the minified files I was including.



回答2:

The best way to understand errors in a Gulp pipeline is to use a helper module, such as pump. As explained in the Gulp documentation Why Use Pump? you can get the correct errors by wrapping your pipeline.

gulp.task('scripts', function(cb) {
    pump([
        gulp.src(paths.scripts),
        concat('all.js'),
        gulp.dest('public/assets/js'),
        rename('all.min.js'),
        uglify(),
        gulp.dest('public/assets/js'),
        livereload()
    ], cb);
});

Attaching an error handler to the UglifyJS part of the pipeline won't tell you about errors in the others, and it won't properly signal an error to Gulp's task running system.