我有一个非常小的gulpfile,如下所示,注册了一个监视任务:
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,它就立即终止。
我已经清除了我的npm缓存,重新安装了依赖等,但没有骰子。
$ gulp watch
[gulp] Using gulpfile gulpfile.js
[gulp] Starting 'watch'...
[gulp] Finished 'watch' after 23 ms发布于 2014-03-17 21:22:37
它本身并不是退出,而是running the task synchronously。
您需要从lint任务返回流,否则gulp不知道该任务何时完成。
gulp.task("lint", function() {
return gulp.src("./src/*.js")
^^^^^^
.pipe(jshint())
.pipe(jshint.reporter("default"));
});此外,您可能不希望对此类监视使用gulp.watch和任务。使用the gulp-watch plugin可能更有意义,这样您就只能处理更改的文件,有点像这样:
var watch = require('gulp-watch');
gulp.task('watch', function() {
watch({glob: "app/assets/**/*.js"})
.pipe(jshint())
.pipe(jshint.reporter("default"));
});此任务不仅将在文件更改时进行lint,而且还将对添加的任何新文件进行linted。
发布于 2014-05-02 22:28:16
添加到OverZealous的正确答案中。
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文件上运行。
https://stackoverflow.com/questions/22453381
复制相似问题