Gulp consecutive tasks as stream

让人想犯罪 __ 提交于 2019-12-23 20:30:53

问题


Basically, I have tasks split in two. The first is for development. There’s no magic, just basic stuff to make it work. It’s fast and great. The latter is for production. It’s slow, and I only want to run it periodically for testing, debugging, optimisation and production. Hence they are two separate tasks.

The latter, the task for production, does rely on the former task, however, I’d like to continue the stream, as if the latter task is an addition to the former.

gulp.task('styles', function() {
    return gulp.src('src/sass/**/*.scss')
        .pipe(sass())
        .pipe(autoprefixer())
        .pipe(gulp.dest('dist/css'));
});

gulp.task('styles-production', function() {
    return runTaskAndUseStream('styles')
         // Continue where the styles task ended
         .pipe(uncss({
             ...
         }))
         .pipe(gulp.dest('dist/css'));
});

I was hoping the undocumented gulp.start('task') method would return a stream, but unfortunately it doesn’t. Many modules does allow you to fiddle with streams, but none of them returns a gulp task as a stream.

What do?


回答1:


One thing that is (most likely) considered best practice would be incorporating the uncss task into your styles task, but running it only when certain flags are set:

var gutil = require('gulp-util');

function production(task) {
    return (gutil.env.production ? task : gutil.noop());
}

gulp.task('styles', function() {
    return gulp.src('src/sass/**/*.scss')
        .pipe(sass())
        .pipe(autoprefixer())
        .pipe(production(uncss({

        })))
        .pipe(gulp.dest('dist/css'));
});

Packages you need: gulp-util for the noop task, and argument parsing

run it with gulp styles --production

(untested, but you get the concept)



来源:https://stackoverflow.com/questions/29428169/gulp-consecutive-tasks-as-stream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!