node.js 将 Gulp 任务设置为默认任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28456118/
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
Set a Gulp task to be the default
提问by Steve
I have the following task in my gulpFile, created by someone else on my team:
我有以下任务gulpFile,由我团队中的其他人创建:
gulp.task('serve', [], function(){
gulp.run(
'fonts',
'browsersync',
'watch'
);
});
I would like to leave it alone, but I also wanted to map the default task to this task. So I tried:
我想不管它,但我也想将默认任务映射到这个任务。所以我试过:
gulp.task('default',['serve']);
It appeared to work, in that the server runs, but for some reason the "watch" task is not happening, I am not getting browser refresh on changes.
它似乎工作,因为服务器运行,但由于某种原因“观察”任务没有发生,我没有在更改时刷新浏览器。
It all works as planned if I run "gulp serve" but not "gulp". What did I do wrong?
如果我运行“gulp serve”而不是“gulp”,一切都会按计划进行。我做错了什么?
EDIT:Here's the watch task:
编辑:这是监视任务:
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();
});
});
回答by Seth
Try updating your default task to include the watch task in the array argument instead of running it inside serve. Like so:
尝试更新您的默认任务以将 watch 任务包含在数组参数中,而不是在serve. 像这样:
gulp.task('default', ['serve', 'watch']);
If you checkout the Gulp documentation on asynchronous task support, particularly the last example, you'll see that you can require a dependent task to finish before the designated task is supposed to start.
如果您查看有关异步任务支持的 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']);
回答by Jon Hieb
gulp.runand gulp.startare considered bad practice:
gulp.run并且gulp.start被认为是不好的做法:
https://github.com/gulpjs/gulp/issues/426
https://github.com/gulpjs/gulp/issues/505
https://github.com/gulpjs/gulp/issues/426
https://github.com/gulpjs/gulp/issues/505
Unfortunately, the answer here appears to be that your coworker may not really understand Gulp. You may not be able to fix this problem without changing their code.
不幸的是,这里的答案似乎是您的同事可能并不真正了解 Gulp。如果不更改他们的代码,您可能无法解决此问题。
Without more context, like the entire gulpfile, I can't reproduce your exact problem. However, my hunch is that it has something to do with the way that Gulp runs tasks asynchronously/continuously. It may be the case that your 'default' task is exiting prematurely, because gulp.rundoes not execute synchronously. One way or another, Gulp is confused about which tasks need to wait on what, when. You're using two completely different tools to manage your run-sequence.
没有更多上下文,就像整个 gulpfile 一样,我无法重现您的确切问题。然而,我的预感是它与 Gulp 异步/连续运行任务的方式有关。可能是您的“默认”任务过早退出,因为gulp.run不会同步执行。无论如何,Gulp 对哪些任务需要等待什么、何时等待感到困惑。您正在使用两种完全不同的工具来管理您的运行序列。
Instead of gulp.run, your 'serve' task should really use dependencies to run other tasks:
而不是gulp.run,您的“服务”任务应该真正使用依赖项来运行其他任务:
gulp.task('serve', ['fonts', 'browsersync', 'watch']);
gulp.task('default', ['serve']);
Also, it is worth pointing out that your watch task is already listing 'browsersync' as a dependency. While not technically incorrect (Gulp will ignore it the second time), it can lead to overcomplication and confusion and so probably isn't a good idea. If 'watch' depends on 'browsersync', you can just remove the 'browsersync' dependency from 'serve':
此外,值得指出的是,您的监视任务已经将“浏览器同步”列为依赖项。虽然在技术上不是错误的(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']);
This should get you the result you're looking for.
这应该会让你得到你正在寻找的结果。
All that being said, if you really insist on following bad practice, you might try using gulp.runin your 'default' task:
话虽如此,如果你真的坚持遵循不好的做法,你可以尝试gulp.run在你的“默认”任务中使用:
gulp.task('default', function() {
gulp.run('serve');
});
I suspect your main problem is that you are mixing the usage of array task dependencies and gulp.run, but either way, gulp.runis "doing it wrong".
我怀疑您的主要问题是您正在混合使用数组任务依赖项和gulp.run,但无论哪种方式,gulp.run都是“做错了”。

