gulp watch terminates immediately

后端 未结 2 1159
灰色年华
灰色年华 2021-02-19 10:59

I have a very minimal gulpfile as follows, with a watch task registered:

var gulp = require(\"gulp\");
var jshint = require(\"gulp-jshint\");

gulp.task(\"lint\"         


        
2条回答
  •  野性不改
    2021-02-19 11:06

    It's not exiting, per se, it's running the task synchronously.

    You need to return the stream from the lint task, otherwise gulp doesn't know when that task has completed.

    gulp.task("lint", function() {
      return gulp.src("./src/*.js")
      ^^^^^^
        .pipe(jshint())
        .pipe(jshint.reporter("default"));
    });
    

    Also, you might not want to use gulp.watch and a task for this sort of watch. It probably makes more sense to use the gulp-watch plugin so you can only process changed files, sort of like this:

    var watch = require('gulp-watch');
    
    gulp.task('watch', function() {
      watch({glob: "app/assets/**/*.js"})
        .pipe(jshint())
        .pipe(jshint.reporter("default"));
    });
    

    This task will not only lint when a file changes, but also any new files that are added will be linted as well.

提交回复
热议问题