如何在一个 gulp 任务中运行多个操作?以下内容不起作用,因为事情似乎没有按顺序运行并导致各种奇怪的错误。我试过event-stream
的merge
就像 How to perform multiple gulp commands in one task但这似乎不适用于 del。
我知道我可以将其分为多个任务并使用 run-sequence
插件,但就像在链接的问题中一样,我不想用永远不会单独运行并且在给定上下文之外没有意义的任务来膨胀我的 Gulpfile。
gulp.task('task', function() {
del('....');
gulp.src('....')
.pipe(gulp.dest('....'));
gulp.src('....')
.pipe(gulp.dest('....'));
});
最佳答案
您需要将流分配给变量,然后与 es.merge 一起运行它们(或者如果您不需要所有事件流,则可以使用 merge-stream)。至于使用 del
运行,请查看设置一个依赖任务来执行清理操作:
https://github.com/gulpjs/gulp/blob/master/docs/recipes/delete-files-folder.md
gulp.task('clean', function (cb) {
del([
'dist/report.csv',
// here we use a globbing pattern to match everything inside the `mobile` folder
'dist/mobile/**',
// we don't want to clean this file though so we negate the pattern
'!dist/mobile/deploy.json'
], cb);
});
然后您可以像这样定义其他任务:
var merge = require('merge-stream');
gulp.task('task', ['clean'], function () {
var someOperation = gulp.src('./').pipe(gulp.dest('out'));
var someOtherOperation = gulp.src('./assets').pipe(gulp.dest('out/assets'));
return merge(someOperation, someOtherOperation);
});
这将首先进行清理,等待完成,然后运行其他操作。
关于javascript - 如何在一个 gulp 任务中运行多个操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27704727/