设置一个Gulp任务是默认的

我在我的gulpFile有以下任务,由我的团队中的其他人创build:

 gulp.task('serve', [], function(){ gulp.run( 'fonts', 'browsersync', 'watch' ); }); 

我想离开它,但我也想把默认任务映射到这个任务。 所以我试了一下:

 gulp.task('default',['serve']); 

它似乎工作,在服务器运行,但由于某种原因,“观看”任务没有发生,我没有得到更改浏览器刷新。

这一切都按计划运行,如果我运行“吞噬服务”,而不是“吞咽”。 我做错了什么?

编辑:这是手表任务:

 gulp.task('watch', ['styles', 'browsersync'], function() { //'default' gulp.watch( [ './app/assets/sass/**/*.scss', './app/modules/**/*.scss' ], ['styles']); gulp.watch([ './app/**/*.js', './app/**/*.html' ], function() { reload(); }); }); 

尝试更新默认任务以将监视任务包含在数组参数中,而不是在服务器中运行。 像这样:

 gulp.task('default', ['serve', 'watch']); 

如果您检查了有关asynchronous任务支持的Gulp文档,特别是最后一个示例,您将看到在指定的任务应该启动之前,您可以要求从属任务完成。

 var gulp = require('gulp'); // takes in a callback so the engine knows when it'll be done gulp.task('one', function(cb) { // do stuff -- async or otherwise cb(err); // if err is not null and not undefined, the run will stop, and note that it failed }); // identifies a dependent task must be complete before this one begins gulp.task('two', ['one'], function() { // task 'one' is done now }); gulp.task('default', ['one', 'two']); 

gulp.rungulp.start被认为是不好的做法:

https://github.com/gulpjs/gulp/issues/426
https://github.com/gulpjs/gulp/issues/505

不幸的是,这里的答案似乎是你的同事可能不会真正理解Gulp。 您可能无法解决此问题而不更改其代码。

没有更多的上下文,像整个gulp文件,我不能重现你的确切问题。 不过,我的直觉是,这与Gulpasynchronous/连续执行任务的方式有关。 可能是您的“默认”任务过早退出,因为gulp.run不会同步执行。 无论如何,Gulp对什么时候需要等待什么任务感到困惑。 您正在使用两种完全不同的工具来pipe理您的运行顺序。

而不是gulp.run ,你的“服务”任务应该真的使用依赖来运行其他任务:

 gulp.task('serve', ['fonts', 'browsersync', 'watch']); gulp.task('default', ['serve']); 

此外,值得指出的是,您的监视任务已经将“browsersync”列为依赖项。 虽然在技术上不正确(Gulp会第二次忽略它),但这会导致过度复杂化和混乱,因此可能不是一个好主意。 如果'watch'取决于'browsersync',那么你可以从'serve'中删除'browsersync'依赖项:

 gulp.task('watch', ['styles', 'browsersync'], function () { gulp.watch([ './app/assets/sass/**/*.scss', './app/modules/**/*.scss' ], ['styles']); gulp.watch([ './app/**/*.js', './app/**/*.html' ], function() { reload(); }); }); gulp.task('serve', ['fonts', 'watch']); gulp.task('default', ['serve']); 

这应该让你find你想要的结果。


所有这一切,如果你真的坚持遵循不好的做法,你可能会尝试在你的“默认”任务中使用gulp.run

 gulp.task('default', function() { gulp.run('serve'); }); 

我怀疑你的主要问题是你正在混合使用数组任务依赖和gulp.run ,但无论如何, gulp.run是“做错了”。