Gulp js错误通知未显示错误来源

时间:2016-01-17 20:40:36

标签: javascript gulp gulp-concat gulp-uglify

所有内容都已设置并正在进行连接和编译我的样式和脚本。当发生错误时gulp-notifygulp-plumber将错误作为mac通知抛出并在终端中,并且gulp不会停止运行 - 这很好:)

但是,我发现终端中抛出的脚本错误始终是编译文件script-dist.js的问题,而不是实际连接的源文件。

错误

gulp-notify: [JS Error] _PATH_/js/script-dist.js: Unexpected token keyword «var», expected punc «{»
Error in plugin 'gulp-uglify'
Message:
    _PATH_/js/script-dist.js: Unexpected token keyword «var», expected punc «{»
Details:
    fileName: _PATH_/js/script-dist.js
    lineNumber: 3

请注意这不会告诉我实际错误来自哪里(它应该说是_modal.js,这是我故意在其中故意创建错误的连接文件之一。

gulpfile.js

var gulp = require('gulp'); 

// --------------------------------------------------------------
// Plugins
// ---------------------------------------------------------------

var concat = require('gulp-concat');
var stripDebug = require('gulp-strip-debug');
var uglify = require('gulp-uglify');
var jshint = require('gulp-jshint');
var include = require('gulp-include');
var sass = require('gulp-sass');
var minifycss = require('gulp-minify-css');
var watch = require('gulp-watch');
var livereload = require('gulp-livereload');
var notify = require('gulp-notify');
var plumber = require('gulp-plumber');

// --------------------------------------------------------------
// JS
// ---------------------------------------------------------------

gulp.task('scripts', function() {
    return gulp.src(['./js/script.js'])
        .pipe(include())
        .pipe(plumber({errorHandler: errorScripts}))
        .pipe(concat('script-dist.js'))
        .pipe(uglify())
        .pipe(gulp.dest('./js/'))
        .pipe(livereload());
});

// --------------------------------------------------------------
// Styles
// ---------------------------------------------------------------

gulp.task("styles", function(){
    return gulp.src("./ui/scss/styles.scss")
        .pipe(include())
        .pipe(plumber({errorHandler: errorStyles}))
        .pipe(sass({style: "compressed", noCache: true}))
        .pipe(minifycss())
        .pipe(gulp.dest("./ui/css/"))
        .pipe(livereload());
});


// --------------------------------------------------------------
// Errors
// ---------------------------------------------------------------

// Styles
function errorStyles(error){
    notify.onError({title: "Sass Error", message: "", sound: "Sosumi"})(error); //Error Notification
    console.log(error.toString()); // Prints Error to Console
    this.emit("end");
};

// Scripts
function errorScripts(error){
    notify.onError({title: "JS Error", message: "", sound: "Sosumi"})(error); //Error Notification
    console.log(error.toString()); // Prints Error to Console
    this.emit("end");
};

// --------------------------------------------------------------
// Watch & Reload
// ---------------------------------------------------------------

gulp.task('watch', function() {   
    gulp.watch('./ui/scss/*.scss', ['styles']);
    gulp.watch(['./js/*.js', '!./js/script-dist.js'], ['scripts']);
});

gulp.task('default', ['styles', 'watch']);
gulp.task('default', ['scripts', 'watch']);

livereload.listen();
task("styles", function(){
        return gulp.src("./ui/scss/styles.scss")
            .pipe(include())
            .pipe(plumber({errorHandler: errorStyles}))
            .pipe(sass({style: "compressed", noCache: true}))
            .pipe(minifycss())
            .pipe(gulp.dest("./ui/css/"))
            .pipe(livereload());
    });


    // --------------------------------------------------------------
    // Errors
    // ---------------------------------------------------------------

    // Styles
    function errorStyles(error){
        notify.onError({title: "Sass Error", message: "", sound: "Sosumi"})(error); //Error Notification
        console.log(error.toString()); // Prints Error to Console
        this.emit("end");
    };

    // Scripts
    function errorScripts(error){
        notify.onError({title: "JS Error", message: "", sound: "Sosumi"})(error); //Error Notification
        console.log(error.toString()); // Prints Error to Console
        this.emit("end");
    };

    // --------------------------------------------------------------
    // Watch & Reload
    // ---------------------------------------------------------------

    gulp.task('watch', function() {   
        gulp.watch('./ui/scss/*.scss', ['styles']);
        gulp.watch(['./js/*.js', '!./js/script-dist.js'], ['scripts']);
    });

    gulp.task('default', ['styles', 'watch']);
    gulp.task('default', ['scripts', 'watch']);

    livereload.listen();

1 个答案:

答案 0 :(得分:0)

concat在传递给uglify之前,所以就Uglify而言,那里只有一个文件。您可能想要使用例如首先传递文件的linter,确保它们是正确的。

或者,使用sourcemaps,哪些工具可以利用它来显示发生错误的原始文件。