2

For example, here is a simple gulp plugin

'use strict';

var match = require('gulp-match');
var ternaryStream = require('ternary-stream');
var through2 = require('through2');

module.exports = function (condition, trueChild, falseChild, minimatchOptions) {
    if (!trueChild) {
        throw new Error('gulp-if: child action is required');
    }

    if (typeof condition === 'boolean') {
        // no need to evaluate the condition for each file
        // other benefit is it never loads the other stream
        return condition ? trueChild : (falseChild || through2.obj());
    }

    function classifier (file) {
        return !!match(file, condition, minimatchOptions);
    }

    return ternaryStream(classifier, trueChild, falseChild);
};

And here is a typical use case

gulp.task('task', function() {
  gulp.src('./src/*.js')
    .pipe(gulpif(condition, uglify()))
    .pipe(gulp.dest('./dist/'));
});

I am trying to figure out what is going on

According to the documentation, every gulp plugin is suppose to pass in a vinyl file and return a vinyl file.

But where is the passed in vinyl file ?

The only passed in parameter is condition, trueChild, falseChild, minimatchOptions

Furthermore how does pipe receive the return vinyl file from the plugin?

user3591466
  • 797
  • 2
  • 8
  • 18
  • 1
    If you want to learn how to write gulp plugins then [read the gulp docs](https://github.com/gulpjs/gulp/blob/master/docs/writing-a-plugin/README.md) and familiarize yourself with [node.js streams](https://nodejs.org/api/stream.html). Looking at the source for `gulp-if` is way too advanced for you if you don't even understand the basics. – Sven Schoenung Aug 30 '16 at 10:53
  • Possible duplicate of [node heap exhausted when piping JSONStream.parsed() data through es.map() and JSONStream.stringify() to file stream](https://stackoverflow.com/questions/38986744/node-heap-exhausted-when-piping-jsonstream-parsed-data-through-es-map-and-js) – Paul Sweatte Jun 15 '17 at 05:32

0 Answers0