Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp consecutive tasks as stream

Tags:

gulp

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?

like image 931
Tim S. Avatar asked Feb 06 '26 19:02

Tim S.


1 Answers

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)

like image 108
ddprrt Avatar answered Feb 12 '26 13:02

ddprrt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!