让我说,我有两个任务:
gulp.task('pug', function () {
return gulp.src('src/**/*.pug')
.pipe(cache(pug()))
// ---- begin ---
.pipe(some_action1())
.pipe(some_action2())
.pipe(some_action3());
// ---- end ---
});
gulp.task('html', function () {
return gulp.src('src/**/*.html')
// ---- begin ---
.pipe(some_action1())
.pipe(some_action2())
.pipe(some_action3());
// ---- end ---
});
some_action1 / 2/3的代码克隆让我很难过。所以我希望将它们改为:
all_actions = /* do some_action1, some_action2, and, some_action3 */({/* ??? */});
gulp.task('pug', function () {
return gulp.src('src/**/*.pug')
.pipe(cache(pug()))
.pipe(all_actions());
});
gulp.task('html', function () {
return gulp.src('src/**/*.html')
.pipe(all_actions());
});
gulp 的方式是什么,以及如何?
答案 0 :(得分:0)
您可以使用管道和所有内容提取任务函数并使用gulp-merge调用它,我们可以通过::
合并两组管道merge(pipes_1, pipes_2)
代码
var gulpMerge = require('gulp-merge');
gulp.task('pug', function () {
return gulpMerge(gulp.src('src/**/*.pug')
.pipe(cache(pug())), allActions('src/**/*.pug'));
});
gulp.task('html', function () {
return allActions('src/**/*.pug');
});
function allActions(source){
return gulp.src(source)
// ---- begin ---
.pipe(some_action1())
.pipe(some_action2())
.pipe(some_action3());
// ---- end ---
}
此外,您可以维护任务的粒度,而不是组合管道,通过将所有操作任务作为依赖任务调用它们来保持它们的小巧和专业化。
假设您有task_1,task_2和task_3。说任务: someActions 需要task_1和task_2,而任务 allActions 需要所有这三项任务。然后你可以:
gulp.task('someActions', ['task_1', 'task_2']);
gulp.task('allActions', ['task_1', 'task_2', 'task_3']);