我已经在gulpfile.js中定义了一些任务,我想使用gulp-watch插件(在新文件上运行任务)。我的问题是,因为我找不到任何东西,我可以在运行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')任务内部运行任务。例如:
就像我对gulp.watch做的那样
gulp.task('watch', function () {
gulp.watch('files', ['task1', 'task2']);
});我需要做同样的事情,但是使用gulp-watch插件,就像这样(我知道它不会工作):
var watch = require('gulp-watch');
gulp.task('watch', function () {
watch({ glob: 'files' }, ['task1', 'task2']);
});发布于 2015-07-29 00:48:21
我还遇到了这样的问题:想要使用gulp watch(而不是gulp.watch),需要使用回调表单,并且在回调中找到合适的方法来运行任务时遇到了麻烦。
我的用例是,我想查看所有的手写笔文件,但只处理包含所有其他文件的主手写笔文件。新版本的gulp-watch可能会解决这个问题,但我在4.3.x上遇到了问题,所以我被困在了4.2.5上。
选你所选。
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中获得“完成”回调。
发布于 2015-01-15 05:07:51
您很可能希望运行与您正在查看的文件相关的特定任务-
gulp.task('watch',['lint'], function () {
gulp.watch('app/**/*.js' , ['lint']);
});您还可以在调用watch first时使用['lint']部分来运行任何必需的任务,或者利用这些任务来运行异步
gulp.task('default', ['lint','watch'])发布于 2015-01-15 05:02:24
您可以只调用一个任务,然后包含两个任务
gulp.task('default', ['lint','watch'])所以在这里你只需要叫“吞咽”
https://stackoverflow.com/questions/27952209
复制相似问题