gulp watch 立即终止 [英] gulp watch terminates immediately

查看:31
本文介绍了gulp watch 立即终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常小的 gulpfile,如下所示,并注册了一个监视任务:

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", function() {
  gulp.src("app/assets/**/*.js")
    .pipe(jshint())
    .pipe(jshint.reporter("default"));
});

gulp.task('watch', function() {
  gulp.watch("app/assets/**/*.js", ["lint"]);
});

我无法让监视任务连续运行.一旦我运行 gulp watch,它就会立即终止.

I cannot get the watch task to run continuously. As soon as I run gulp watch, it terminates immediately.

我已经清除了我的 npm 缓存,重新安装了依赖项等,但没有任何问题.

I've cleared my npm cache, reinstalled dependencies etc, but no dice.

$ gulp watch
[gulp] Using gulpfile gulpfile.js
[gulp] Starting 'watch'...
[gulp] Finished 'watch' after 23 ms

推荐答案

它本身并没有退出,它是 同步运行任务.

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

您需要从 lint 任务中返回流,否则 gulp 不知道该任务何时完成.

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"));
});

<小时>

另外,您可能不想使用 gulp.watch 和任务来完成这种监视.使用 gulp-watch 插件 可能更有意义,因此您只能处理更改的文件,有点像这样:


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"));
});

此任务不仅会在文件更改时进行 lint,而且还会对添加的任何新文件进行 lint.

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

这篇关于gulp watch 立即终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆