Javascript 使用gulp watch运行现有任务

Javascript 使用gulp watch运行现有任务,javascript,gulp,gulp-watch,Javascript,Gulp,Gulp Watch,我已经在gulpfile.js中定义了一些任务,我想使用gulpwatch插件(在新文件上运行任务)。我的问题是,因为我找不到任何东西,我可以在运行watch(从插件)功能的同时运行我现有的任务吗 var gulp = require('gulp'), watch = require('gulp-watch'), ...; gulp.task('lint', function () { return gulp.src(path.scripts) .pipe(js

我已经在
gulpfile.js
中定义了一些任务,我想使用
gulpwatch
插件(在新文件上运行任务)。我的问题是,因为我找不到任何东西,我可以在运行watch(从插件)功能的同时运行我现有的任务吗

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

gulp.task('lint', function () {
  return gulp.src(path.scripts)
      .pipe(jshint())
      .pipe(jshint.reporter(stylish));
});

gulp.task('watch', function () {
  watch({ glob: 'app/**/*.js' }); // Run 'lint' task for those files
});
因为我不想在我所有的任务中都包含
watch()
任务。我想只有一个任务-手表,它将结合所有的“手表”

-----编辑---- (我可能没有完全明白我的意思):

我需要从
gulp('watch')
task的内部运行任务。例如:

就像我大口喝了一大口。看一看:

gulp.task('watch', function () {
  gulp.watch('files', ['task1', 'task2']);
});
我也需要这样做,但是使用
gulpwatch
plugin,类似于(我知道它不起作用):


您可以只调用一个任务,然后它包括两个任务

gulp.task('default', ['lint','watch'])
gulp.task('default', ['lint','watch'])

因此,在这里您只需调用“gulp”

您很可能希望运行与您正在查看的文件相关的特定任务-

gulp.task('watch',['lint'], function () {
    gulp.watch('app/**/*.js' , ['lint']);
});
您还可以使用
['lint']
部分在第一次调用watch时运行任何必需的任务,或者利用任务与异步运行


工作正常,除了警告之外

我还遇到了想要使用gulp watch(而不是gulp.watch)的问题,需要使用回调表单,并且很难找到合适的方式在回调中运行任务

我的用例是,我想查看所有手写笔文件,但只处理包含所有其他文件的主手写笔文件。新版本的GulpWatch可能会解决这个问题,但我在4.3.x上遇到了问题,所以我被困在了4.2.5上

  • gulp.run已被弃用,因此我不想使用它
  • gulp.start运行良好,但gulp的作者conta也建议不要使用它
  • run sequence插件运行良好,允许您定义运行 命令,但这是一个自称的黑客:
  • 相反,建议编写简单的旧函数并调用 那些这对我来说是一个新的想法,但我认为下面的例子抓住了这个想法
你挑吧

var gulp = require('gulp'),
    watch = require('gulp-watch'), // not gulp.watch
    runSequence = require('run-sequence');

// plain old js function
var runStylus = function() {
    return gulp.src('index.styl')
        .pipe(...) // process single file
}

gulp.task('stylus', runStylus);

gulp.task('watch', function() {
    // watch many files
    watch('*.styl', function() {
        runSequence('stylus');
        OR 
        gulp.start('stylus');
        OR
        runStylus();
    });
});

所有这些都在毫无警告的情况下对我有效,但我仍然不确定是否能从4.2.x版的gulp watch中获得“完成”回调。

哇,我最近一直在运行gulp,以了解一些同事对它的使用情况;您的回答虽然没有被接受,但已经回答了我关于
watch
run
run sequence
,以及在某种程度上使用普通函数的其他4个问题。因此,感谢您为我节省了大量时间:您可以根据事件类型将此扩展到从
gulp.watch
中调用特定任务吗。
gulp.task('watch', function() {
  watch(files, function() {
    gulp.run(['task1', 'task2']);
  });
});
var gulp = require('gulp'),
    watch = require('gulp-watch'), // not gulp.watch
    runSequence = require('run-sequence');

// plain old js function
var runStylus = function() {
    return gulp.src('index.styl')
        .pipe(...) // process single file
}

gulp.task('stylus', runStylus);

gulp.task('watch', function() {
    // watch many files
    watch('*.styl', function() {
        runSequence('stylus');
        OR 
        gulp.start('stylus');
        OR
        runStylus();
    });
});