我有这个gulp任务,并且工作正常,但是在查看日志时,它始终显示“正在启动任务”,然后立即显示“已完成任务”,然后开始记录所有内容。如何让它等到一切都完成后才说完成?我认为这是由于该函数处于异步状态,所以done()立即被调用,但是我不确定该怎么做。
gulp功能
gulp.task("img-projects", function(done) {
gulp.src('projects/*/images/**/*.*')
.pipe(imagemin())
.pipe(rename(function (path) {
path.dirname = path.dirname.replace('/images','');
console.log('path');
return path;
}))
.pipe(gulp.dest('public'));
done();
});
输出:
[19:11:55] Starting 'js-projects'...
[19:11:55] Finished 'js-projects' after 34 ms
[19:11:55] path
[19:11:55] path
[19:11:55] path
答案 0 :(得分:1)
您需要添加的是on('end', ...)
侦听器,以等到gulp流完成后再调用done()
:
gulp.task("img-projects", function(done) {
gulp.src('projects/*/images/**/*.*')
.pipe(imagemin())
.pipe(rename(function (path) {
path.dirname = path.dirname.replace('/images','');
console.log('path');
return path;
}))
.pipe(gulp.dest('public'))
.on('end', done);
});
来源: How do you run a gulp "on end" task but only at the end of the current task?
gulp API(src()
/ dest()
/ etc的文档):https://github.com/gulpjs/gulp/tree/master/docs/api
node.js流API,它提供on(...)
:https://nodejs.org/api/stream.html
类似的问题:https://github.com/gulpjs/gulp/issues/1181#issuecomment-126694791
答案 1 :(得分:0)
尝试
gulp函数是异步的
gulp.task("img-projects", function() {
return gulp.src('projects/*/images/**/*.*')
.pipe(imagemin())
.pipe(rename(function (path) {
path.dirname = path.dirname.replace('/images','');
console.log('path');
return path;
}))
.pipe(gulp.dest('public'));
});