我正在尝试创建两个 gulp 任务,我希望第二个任务获取第一个任务的输出流并继续对其应用插件。
我可以将第一个任务的返回值传递给第二个任务吗?
以下不起作用:
// first task to be run
gulp.task('concat', function() {
// returning a value to signal this is sync
return
gulp.src(['./src/js/*.js'])
.pipe(concat('app.js'))
.pipe(gulp.dest('./src'));
};
// second task to be run
// adding dependency
gulp.task('minify', ['concat'], function(stream) {
// trying to get first task's return stream
// and continue applying more plugins on it
stream
.pipe(uglify())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest('./dest'));
};
gulp.task('default', ['minify']);
Run Code Online (Sandbox Code Playgroud)
有什么办法可以做到这一点吗?
您无法将流传递给其他任务。但您可以gulp-if根据条件使用模块跳过某些管道方法。
var shouldMinify = (0 <= process.argv.indexOf('--uglify'));
gulp.task('script', function() {
return gulp.src(['./src/js/*.js'])
.pipe(concat('app.js'))
.pipe(gulpif(shouldMinify, uglify())
.pipe(gulpif(shouldMinify, rename({suffix: '.min'}))
.pipe(gulp.dest('./dest'));
});
Run Code Online (Sandbox Code Playgroud)
执行这样的任务来缩小
gulp script --minify
Run Code Online (Sandbox Code Playgroud)