Ste*_*eve 14 node.js gulp gulp-watch
gulpFile
我的团队中有其他人创建了以下任务:
gulp.task('serve', [], function(){
gulp.run(
'fonts',
'browsersync',
'watch'
);
});
Run Code Online (Sandbox Code Playgroud)
我想不管它,但我也想将默认任务映射到此任务.所以我尝试过:
gulp.task('default',['serve']);
Run Code Online (Sandbox Code Playgroud)
它似乎工作,因为服务器运行,但由于某种原因,"监视"任务没有发生,我没有得到浏览器刷新更改.
如果我运行"gulp serve"而不是"gulp",这一切都按计划运行.我做错了什么?
编辑: 这是监视任务:
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();
});
});
Run Code Online (Sandbox Code Playgroud)
Set*_*eth 15
尝试更新默认任务以将监视任务包含在数组参数中,而不是在其中运行serve
.像这样:
gulp.task('default', ['serve', 'watch']);
Run Code Online (Sandbox Code Playgroud)
如果您查看有关异步任务支持的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']);
Run Code Online (Sandbox Code Playgroud)
gulp.run
而gulp.start
被认为是不好的做法:
https://github.com/gulpjs/gulp/issues/426
https://github.com/gulpjs/gulp/issues/505
不幸的是,这里的答案似乎是你的同事可能不太了解Gulp.如果不更改代码,您可能无法解决此问题.
没有更多的上下文,比如整个gulpfile,我无法重现您的确切问题.但是,我的预感是它与Gulp以异步/连续方式运行任务的方式有关.可能是您的"默认"任务过早退出,因为gulp.run
不会同步执行.无论如何,Gulp对哪些任务需要等待什么,什么时候感到困惑.您使用两种完全不同的工具来管理运行顺序.
而不是gulp.run
,您的"服务"任务应该真正使用依赖项来运行其他任务:
gulp.task('serve', ['fonts', 'browsersync', 'watch']);
gulp.task('default', ['serve']);
Run Code Online (Sandbox Code Playgroud)
此外,值得指出的是,您的监视任务已经将"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']);
Run Code Online (Sandbox Code Playgroud)
这应该可以得到您正在寻找的结果.
所有这一切,如果你真的坚持遵循不良做法,你可以尝试使用gulp.run
你的'默认'任务:
gulp.task('default', function() {
gulp.run('serve');
});
Run Code Online (Sandbox Code Playgroud)
我怀疑你的主要问题是你正在混合数组任务依赖的使用gulp.run
,但无论哪种方式,gulp.run
都是"做错了".
归档时间: |
|
查看次数: |
28155 次 |
最近记录: |