gulp watch terminates immediately

假装没事ソ 提交于 2019-12-04 01:12:51

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.

To add to OverZealous' answer which is correct.

gulp.watch now allows you to pass a string array as the callback so you can have two separate tasks. For example, hint:watch and 'hint'. You can then do something like the following.

gulp.task('hint', function(event){
    return gulp.src(sources.hint)
        .pipe(plumber())
        .pipe(hint())
        .pipe(jshint.reporter("default"));
})
gulp.task('hint:watch', function(event) {
   gulp.watch(sources.hint, ['hint']);
})

This is only an example though and ideally you'd define this to run on say a concatted dist file.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!