简体   繁体   English

即使gulpfile.js中包含jshint,gulp也会使服务器停止出错

[英]gulp stops server on error even with jshint included in gulpfile.js

I don't know why the server still stops whenever there's an error in my js files even though I have jshint in my gulpfile. 我不知道为什么即使我的gulpfile中包含jshint,但我的js文件中出现错误时服务器仍会停止。 I installed jshint and included it in my project because it reports errors in js files, but it's still failing. 我安装了jshint并将其包含在我的项目中,因为它报告js文件中的错误,但是仍然失败。 How can I fix this? 我怎样才能解决这个问题?

gulp.task('scripts', () => {
    return gulp.src('assets/js/src/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('jshint-stylish', {beep: true}))
        .pipe(concat('main.js'))
        .pipe(gulp.dest('assets/js/build/'))
        .pipe(uglify())
        .pipe(gulp.dest('assets/js/'))
        .pipe(browserSync.stream({stream: true}));
});

gulp-jshint does what you says it does: it reports errors in JavaScript files. gulp-jshint做到了您所说的:它报告JavaScript文件中的错误。 Nothing more, nothing less. 仅此而已。 It doesn't prevent defective JavaScript files from reaching later pipe stages like uglify() (which throws up and thus stops your server if there's any error in a JavaScript file). 它不会阻止有缺陷的JavaScript文件到达uglify()类的后续管道阶段uglify()如果JavaScript文件中有任何错误,它将抛出该异常,从而停止服务器)。

If you want to prevent defective JavaScript files from wrecking your server, you need to put all the jshint stuff into it's own task and make sure that task fails when any JavaScript file has an error : 如果要防止有缺陷的JavaScript文件破坏服务器,则需要将所有jshint内容放入其自己的任务中,并确保当任何JavaScript文件有错误时该任务都会失败

gulp.task('jshint', () => {
    return gulp.src('assets/js/src/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('jshint-stylish', {beep: true}))
        .pipe(jshint.reporter('fail'))
});

Then you need to make your scripts task depend on that jshint task: 然后,需要使scripts任务依赖于该jshint任务:

gulp.task('scripts', ['jshint'], () => {
    return gulp.src('assets/js/src/*.js')
        .pipe(concat('main.js'))
        .pipe(gulp.dest('assets/js/build/'))
        .pipe(uglify())
        .pipe(gulp.dest('assets/js/'))
        .pipe(browserSync.stream({stream: true}));
});

Now your scripts task will only run when the jshint task was successful. 现在,您的scripts任务将仅在jshint任务成功时运行。 If any JavaScript file was defective jshint will output the error to the console while your server continues to run using the last good version of your JavaScript. 如果任何JavaScript文件存在缺陷,则jshint会将错误输出到控制台,同时您的服务器将使用最新的JavaScript版本继续运行。

The simplest fix would be to use gulp-plumber to handle the error a little more gracefully: 最简单的解决方法是使用gulp-plumber更加优雅地处理错误:

var plumber = require("gulp-plumber");

gulp.task('scripts', () => {
    return gulp.src('assets/js/src/*.js')
        .pipe(plumber())
        .pipe(jshint())
        .pipe(jshint.reporter('jshint-stylish', {beep: true}))
        .pipe(concat('main.js'))
        .pipe(gulp.dest('assets/js/build/'))
        .pipe(uglify())
        .pipe(gulp.dest('assets/js/'))
        .pipe(browserSync.stream({stream: true}));
});

Personally, I don't like that solution because it will prevent your minified file from being updated. 我个人不喜欢这种解决方案,因为它会阻止您的压缩文件被更新。 Here's what I would recommend: 这是我的建议:

var jshintSuccess = function (file) {
    return file.jshint.success;
}

gulp.task('scripts', () => {
    return gulp.src('assets/js/src/*.js')
        .pipe(sourcemaps.init())
        .pipe(jshint())
        .pipe(jshint.reporter('jshint-stylish', {
            beep: true
        }))
        .pipe(gulpif(jshintSuccess, uglify()))
        .pipe(concat('main.js'))
        .pipe(sourcemaps.write('maps'))
        .pipe(gulp.dest('assets/js/'))
        .pipe(browserSync.stream({
            stream: true
        }));
});

First, notice that I'm not writing to multiple destinations. 首先,请注意我没有写到多个目标。 Instead, I'm using sourcemaps so that you don't need unminified code. 相反,我使用的是源地图,这样您就不需要未缩小的代码。 Second, I'm using gulp-if to conditionally pipe your code through uglify based on the results of jshint. 其次,我使用gulp-if根据jshint的结果有条件地通过uglify传递代码。 Code with errors will bypass uglify so that it still makes it into to your destination file. 有错误的代码将绕过uglify,以便仍将其放入目标文件中。

Now, you can inspect and debug it with the developer tools. 现在,您可以使用开发人员工具对其进行检查和调试。

Note: I recommend this for local development only. 注意:我建议仅用于本地开发。 I wouldn't connect this to a continuous integration pipeline because you'll only want good code to make it into production. 我不会将其连接到持续集成管道,因为您只想要好的代码就可以将其投入生产。 Either set up a different task for that or add another gulp-if condition to prevent broken code from building based on environment variables. 为此要么设置其他任务,要么添加另一个gulp-if条件,以防止基于环境变量构建损坏的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM