Gulp 大口喝表立即终止

Gulp 大口喝表立即终止,gulp,Gulp,我有一个非常小的gulpfile,如下所示,注册了一个watch任务: 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', f

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

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,它就会立即终止

我已经清除了我的npm缓存,重新安装了依赖项等,但没有骰子

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

从本质上说,它不是退出,而是退出

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

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

此外,您可能不想使用
gulp.watch
和此类手表的任务。使用它可能更有意义,因此您只能处理更改的文件,有点像这样:

var watch = require('gulp-watch');

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

此任务不仅会在文件更改时挂起,而且添加的任何新文件也会挂起。

添加到OverZeous的答案是正确的

gulp.watch
现在允许您传递字符串数组作为回调,这样您就可以有两个单独的任务。例如,
hint:watch
和'hint'。 然后,您可以执行以下操作

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']);
})

不过这只是一个示例,理想情况下,您可以将其定义为在一个压缩的dist文件上运行。

是否有任何文件与您的模式匹配
app/assets/***.js
?如果没有,手表将退出。是的,一大堆文件和文件夹。手表退出还有其他原因吗?它是通过关闭节点并让您返回命令提示符来“终止”还是通过说“结束”来“终止”?@robrich,如上所述,手表在23ms后结束。现在工作正常,谢谢!混淆在于“开始观看”和“完成观看”的术语。对于我来说,
{glob:“app/assets/***/.js”}
给出了一个错误,只需使用
“app/assets/***/.js”
。(NodeJS 6.3.1,Gulp 3.91,watch 4.3.9)您的示例中的
源代码是什么?一组全局路径?这不是每次一个文件更改时都会删除所有文件吗?这实际上也已经过时了,因为
gulp watch
实际上无法获得正确的结果,而且更容易使用gulps watch。上面的代码确实会在一次文件更改时删除glob中的所有文件。在这种情况下,最好的做法是对文件进行加密,然后只需要一个提示。或者,您可以让hint引用dist文件夹,并使用类似于
gulpchange
的方法过滤掉未更改的文件。