Javascript 使用 gulp-watch 运行现有任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27952209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running existing task with gulp-watch
提问by Tomasz Kasperek
I've got some tasks already defined in gulpfile.jsand I want to use gulp-watchplugin (to run tasks on new files). My question is, because I couldn't find anything, can I run my existing tasks while running watch (from plugin) function?
我已经定义了一些任务gulpfile.js,我想使用gulp-watch插件(在新文件上运行任务)。我的问题是,因为我找不到任何东西,我可以在运行 watch (from plugin) 功能的同时运行我现有的任务吗?
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
});
Because I don't want to include watch()task in every task I have. I would like to have only 1 task - watch, which will combine all "watches".
因为我不想watch()在我拥有的每项任务中都包含任务。我只想有 1 个任务 - 手表,它将结合所有“手表”。
----- EDIT ---- (as I probably didn't quite get my point):
----- 编辑----(因为我可能不太明白我的意思):
I need to run task from inside of gulp('watch')task. for example:
我需要从任务内部运行gulp('watch')任务。例如:
like I did it with gulp.watch:
就像我做的那样gulp.watch:
gulp.task('watch', function () {
gulp.watch('files', ['task1', 'task2']);
});
I need to do the same but with gulp-watchplugin, something like (I know it wouldn't work):
我需要做同样的事情,但使用gulp-watch插件,比如(我知道它行不通):
var watch = require('gulp-watch');
gulp.task('watch', function () {
watch({ glob: 'files' }, ['task1', 'task2']);
});
回答by AgentOrange
I have also run into the problem of wanting to use gulp-watch (not gulp.watch), needing to use the callback form, and having trouble finding a suitable way to run a task in the callback.
我也遇到过想使用gulp-watch(不是gulp.watch)的问题,需要使用回调形式,在回调中找不到合适的方式运行任务的问题。
My use case was that I wanted to watch all stylus files, but only process the main stylus file that includes all the others. Newer versions of gulp-watch may address this but I'm having problems with 4.3.x so I'm stuck on 4.2.5.
我的用例是我想查看所有手写笔文件,但只处理包含所有其他手写笔文件的主手写笔文件。较新版本的 gulp-watch 可能会解决这个问题,但我在 4.3.x 上遇到了问题,所以我被困在 4.2.5 上。
- gulp.run is deprecated so I don't want to use that.
- gulp.start works well, but is also advised against by the gulp author, contra.
- The run-sequence plugin works well and lets you define a run order, but it is a self-proclaimed hack: https://www.npmjs.com/package/run-sequence
- Contra suggest writing plain old functions and calling those. This is a new idea to me, but I think the example below captures the idea. https://github.com/gulpjs/gulp/issues/505
- gulp.run 已弃用,所以我不想使用它。
- gulp.start 运行良好,但也被 gulp 作者反对。
- run-sequence 插件运行良好,可让您定义运行顺序,但它是自称的 hack:https: //www.npmjs.com/package/run-sequence
- Contra 建议编写简单的旧函数并调用它们。这对我来说是一个新想法,但我认为下面的例子抓住了这个想法。https://github.com/gulpjs/gulp/issues/505
Take your pick.
随你挑。
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();
});
});
All of these are working for me without warnings, but I'm still unsure about getting the "done" callback from the 4.2.x version of gulp-watch.
所有这些都对我有用,没有警告,但我仍然不确定从 4.2.x 版本的 gulp-watch 获得“完成”回调。
回答by Kelly J Andrews
You will most likely want to run specific tasks related to the files you are watching -
您很可能希望运行与您正在观看的文件相关的特定任务 -
gulp.task('watch',['lint'], function () {
gulp.watch('app/**/*.js' , ['lint']);
});
You can also use the ['lint']portion to run any required tasks when watch first gets called, or utilize the tasks to run async with
您还可以使用该['lint']部分在首次调用 watch 时运行任何必需的任务,或利用这些任务与
gulp.task('default', ['lint','watch'])
回答by Andrew C
You can just call one task, that then includes both task
你可以只调用一个任务,然后包括两个任务
gulp.task('default', ['lint','watch'])
so here you would just call 'gulp'
所以在这里你可以称之为“gulp”
回答by gongqj
gulp.task('watch', function() {
watch(files, function() {
gulp.run(['task1', 'task2']);
});
});
work fine, except a warning
工作正常,除了警告

