运行顺序不会按顺序运行吞吐任务 [英] run-sequence doesn't run gulp tasks in order
问题描述
我在我的Gulp文件中有3个任务必须按此顺序运行:
-
clean
(删除/ dist
文件夹中的所有内容) -
copy
(将多个文件复制到/ dist
文件夹中) -
替换
(替换/ dist
文件夹中某些文件中的一些字符串)
I'已阅读所有其他帖子,我试过运行序列,但它不工作,因为替换任务没有最后运行。我对使用回调感到困惑。var gulp = require('gulp');
var runSequence = require('run-sequence');
gulp.task('runEverything',function(callback){
runSequence('clean',
'copy',
'replace',
回调);
});
$ b gulp.task('clean',function(){
return del(
'dist / ** / *'
);
} );
$ b gulp.task('copy',function(){
gulp.src('node_modules / bootstrap / dist / ** / *')
.pipe(gulp。 dest('dist / vendor'))
// ...
return gulp.src(['index.html','404.html','.htaccess'])
.pipe(gulp.dest('dist /'));
});
$ b $ gulp.task('replace',function(){
gulp.src(['dist / index.php','dist / info.php'])
(替换('fakedomain.com','realdomain.com'))
.pipe(gulp.dest('dist'));
return gulp.src([ dist / config.php'])
.pipe(replace('foo','bar'))
.pipe(gulp.dest('dist'));
});
使用这3个任务的完整示例值得赞赏。谢谢。
解决方案run-sequence
文档有以下说明有关异步操作的任务:
确保它们返回一个流或承诺,或处理回调
您的
副本
和替换
任务有多个流。您必须返回所有流,而不仅仅是最后一个。如果你不返回它们,Gulp不会知道任何关于其他流的信息,因此不会等到它们完成。
既然你只能返回一个单一的流你必须合并流[在这里插入Ghostbusters参考]。这会给你一个合并的流,你可以从你的任务中返回。
以下是如何使用
merge-stream
package :
var merge = require('merge-stream');
gulp.task('copy',function(){
var stream1 = gulp.src('node_modules / bootstrap / dist / ** / *')
.pipe (gulp.dest('dist / vendor'))
// ...
var stream2 = gulp.src(['index.html','404.html','.htaccess'] )
.pipe(gulp.dest('dist /'));
return merge(stream1,stream2);
});
gulp.task('replace',function(){
var stream1 = gulp.src(['dist / index.php','dist / info.php'])
.pipe(替换('fakedomain.com','realdomain.com'))
.pipe(gulp.dest('dist'));
var stream2 = gulp .src(['dist / config.php'])
.pipe(replace('foo','bar'))
.pipe(gulp.dest('dist'));
返回合并(stream1,stream2);
});
I have 3 tasks in my Gulp file that must run in this order:
clean
(deletes everything in the/dist
folder)copy
(copies multiple files into the/dist
folder)replace
(replaces some strings in some files in the/dist
folder)
I've read all the other posts, I've tried "run-sequence" but it's not working as the "replace" task isn't running last. I'm confused by the use of "callback". Running the tasks individually works fine.
var gulp = require('gulp'); var runSequence = require('run-sequence'); gulp.task('runEverything', function(callback) { runSequence('clean', 'copy', 'replace', callback); }); gulp.task('clean', function () { return del( 'dist/**/*' ); }); gulp.task('copy', function() { gulp.src('node_modules/bootstrap/dist/**/*') .pipe(gulp.dest('dist/vendor')) //... return gulp.src(['index.html', '404.html', '.htaccess']) .pipe(gulp.dest('dist/')); }); gulp.task('replace', function(){ gulp.src(['dist/index.php', 'dist/info.php']) .pipe(replace('fakedomain.com', 'realdomain.com')) .pipe(gulp.dest('dist')); return gulp.src(['dist/config.php']) .pipe(replace('foo', 'bar')) .pipe(gulp.dest('dist')); });
A full example using these 3 tasks would be appreciated. Thank you.
解决方案The
run-sequence
documentation has the following to say about tasks with asynchronous operations:make sure they either return a stream or promise, or handle the callback
Both your
copy
andreplace
tasks have more than one stream. You have to return all the streams, not just the last one. Gulp won't know anything about the other streams if you don't return them and therefore won't wait for them to finish.Since you can only ever return a single stream you have to merge the streams [insert Ghostbusters reference here]. That will give you one merged stream that you can return from your task.
Here's how to do it using the
merge-stream
package :var merge = require('merge-stream'); gulp.task('copy', function() { var stream1 = gulp.src('node_modules/bootstrap/dist/**/*') .pipe(gulp.dest('dist/vendor')) //... var stream2 = gulp.src(['index.html', '404.html', '.htaccess']) .pipe(gulp.dest('dist/')); return merge(stream1, stream2); }); gulp.task('replace', function(){ var stream1 = gulp.src(['dist/index.php', 'dist/info.php']) .pipe(replace('fakedomain.com', 'realdomain.com')) .pipe(gulp.dest('dist')); var stream2 = gulp.src(['dist/config.php']) .pipe(replace('foo', 'bar')) .pipe(gulp.dest('dist')); return merge(stream1, stream2); });
这篇关于运行顺序不会按顺序运行吞吐任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!