javascript - Gulp 在 uglify() 未处理的错误事件上失败

标签 javascript jquery angularjs gulp build-process

我在执行 squish-jquery 任务时遇到了问题。当它运行时,它抛出这个错误:

Starting 'squish-jquery'...

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error
    at new JS_Parse_Error (/Users/shill7/Documents/socialprojects/social-ops-dashboard-angular/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:196:18)
    at js_error (/Users/shill7/Documents/socialprojects/social-ops-dashboard-angular/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:204:11)
    at croak (/Users/shill7/Documents/socialprojects/social-ops-dashboard-angular/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:679:41)
    at token_error (/Users/shill7/Documents/socialprojects/social-ops-dashboard-angular/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:683:9)
    at expect_token (/Users/shill7/Documents/socialprojects/social-ops-dashboard-angular/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:696:9)
    at expect (/Users/shill7/Documents/socialprojects/social-ops-dashboard-angular/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:699:36)
    at function_ (/Users/shill7/Documents/socialprojects/social-ops-dashboard-angular/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:959:9)
    at expr_atom (/Users/shill7/Documents/socialprojects/social-ops-dashboard-angular/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1188:24)
    at maybe_unary (/Users/shill7/Documents/socialprojects/social-ops-dashboard-angular/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1358:19)
    at expr_ops (/Users/shill7/Documents/socialprojects/social-ops-dashboard-angular/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1393:24)

我已经查过了,但不幸的是,我找不到与我的错误相关的任何内容。是我的设置问题还是 Uglify 问题?这是我的 gulpfile.js:

    // Load Gulp
var gulp    = require('gulp'),
    gutil   = require('gulp-util');
    plugins = require('gulp-load-plugins')();

var imagemin = require('gulp-imagemin');
var pngquant = require('imagemin-pngquant');
var ignore = require('gulp-ignore');

// Start Watching: Run "gulp"
gulp.task('default', ['watch']);

// Minify jQuery Plugins: Run manually with: "gulp squish-jquery"
gulp.task('squish-jquery', function() {
  return gulp.src(['src/bower_components/**/*.js', '!src/bower_components/**/*.min.js'])
    .pipe(ignore.exclude([ "**/*.map" ]))
    .pipe(plugins.uglify())
    .pipe(plugins.concat('jquery.plugins.min.js'))
    .pipe(gulp.dest('build')).on('error', gutil.log);
});

// Minify Custom JS: Run manually with: "gulp build-js"
gulp.task('build-js', function() {
  return gulp.src(['src/js/**/*.js', 'src/js/components-controllers/*.js'])
    .pipe(plugins.jshint())
    .pipe(plugins.jshint.reporter('jshint-stylish'))
    .pipe(plugins.uglify())
    .pipe(plugins.concat('scripts.min.js'))
    .pipe(gulp.dest('build'));
});

// Less to CSS: Run manually with: "gulp build-css"
gulp.task('build-css', function() {
    return gulp.src('src/less/all.less')
        .pipe(plugins.plumber())
        .pipe(plugins.less())
        .on('error', function (err) {
            gutil.log(err);
            this.emit('end');
        })
        .pipe(plugins.autoprefixer(
            {
                browsers: [
                    '> 1%',
                    'last 2 versions',
                    'firefox >= 4',
                    'safari 7',
                    'safari 8',
                    'IE 8',
                    'IE 9',
                    'IE 10',
                    'IE 11'
                ],
                cascade: false
            }
        ))
        .pipe(plugins.cssmin())
        .pipe(gulp.dest('build')).on('error', gutil.log);
});

//Image minification
gulp.task('imagemin', function() {
    return gulp.src('src/images/*')
        .pipe(imagemin({
            progressive: true,
            svgoPlugins: [{removeViewBox: false}],
            use: [pngquant()]
        }))
        .pipe(gulp.dest('build/images'));
});

// Default task
gulp.task('watch', function() {
    gulp.watch('src/bower_components/**/*.js', ['squish-jquery']);
    gulp.watch('src/js/**.js', ['build-js']);
    gulp.watch('src/images/*', ['imagemin']);
    gulp.watch('src/less/**/*.less', ['build-css']);
});

最佳答案

根据我上面的评论,您应该为 uglify 插件打开错误日志记录以确定错误实际发生的位置:

//....
.pipe(uglify())
.on('error', function(){
  //do whatever here
})
//....

关于javascript - Gulp 在 uglify() 未处理的错误事件上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29976843/

相关文章:

javascript - 通过 JavaScript 使页面无效

javascript - 并行异步 Mongoose 查询 || eval(返回) 语法错误 : Illegal return statement

javascript - 将可绑定(bind)复选框列添加到 Internet Explorer 中的网格

javascript - 有没有办法将按钮 ID onClick 发送到引导模式?

javascript - 使用 jQuery 将表单数据转换为 JavaScript 对象

php - laravel 在本地机器上返回 json 字符串,但在弹性 beantalk 实例上返回整数

javascript - 刷新 AngularJS 中的 map

javascript - 从无序列表构建 json 字符串

jquery - 设置模板中下拉列表的 selectedValue (加载时)

angularjs - Angular JS 过滤数字