joemaller
joemaller

Reputation: 20616

Gulp pipe minimal function?

What is smallest function that can be placed in a Gulp pipe and still work?

This doesn't work:

gulp.src('**')
  .pipe(function() {
    // what's the minimum that can go here and still be a functional pipe?
    // what needs to be returned? 
  });

Ideally this would be vanilla JS using only the foundational modules Gulp was built on; vinyl, through, etc.

Upvotes: 9

Views: 4914

Answers (2)

cr0ybot
cr0ybot

Reputation: 4090

Yes, it's 4 years later, but I thought I'd throw in my solution to this since I was looking for the same answer.

I wanted some simple, modular pipe functions that I could reuse in a few different tasks, without excess dependencies. This might not be an elegant solution, but it works:

const gulp = require('gulp');

var someFlag = true;

gulp.task('default', doTask);

function doTask(done) {
  let stream = gulp.src(['file.txt'])
  .pipe(/* some gulp plugin here */);

  if (someFlag) {
    stream = alterStream(stream);
  }

  return stream;
}

function alterStream(stream) {
  stream.pipe(/* another gulp plugin here */);
  return stream;
}

Instead of returning the stream first directly (return gulp.src(...)) I'm storing it in a variable, then altering it as needed with other pipes, and returning it last.

Upvotes: 2

Preview
Preview

Reputation: 35846

The problem with your pipe is that you're dropping the files of your glob since you don't return them.

Using through2, you can do something like this:

const through = require('through2')

gulp.src('**')
  .pipe(through.obj((file, enc, cb) => cb(null, file)))

Upvotes: 13

Related Questions