0

I am writing my own gulp plugin which looks like this...

var through2 = require('through2');
var order = require('gulp-order');

module.exports = function() {
    return through2.obj(function(file, encoding, callback) {
        callback(null, transform(file));
    });
};

function transform(file) {
    // I will modify file.contents here - its ok
    return file;
}

and I would like to apply some other gulp plugin on my buffer which came from gulp.src. Is it possible using through2? For example before calling through2.obj() I would like to apply gulp-order plugin - how can I do this?

mikep
  • 5,880
  • 2
  • 30
  • 37

1 Answers1

0

If you want to chain different gulp plugins together lazypipe is generally is good option:

var through2 = require('through2');
var order = require('gulp-order');

function yourPlugin()
    return through2.obj(function(file, encoding, callback) {
        callback(null, transform(file));
    });
}

function transform(file) {
    // I will modify file.contents here - its ok
    return file;
}

function orderPlugin()
    return order(['someFolder/*.js', 'someOtherFolder/*.js']);
}

module.exports = function() {
   return lazypipe().pipe(orderPlugin).pipe(yourPlugin)();
};
Sven Schoenung
  • 30,224
  • 8
  • 65
  • 70
  • Thank you! It works like a charm! Is there some solution to process buffer two times. E.g. I have 3 files in buffer (from gulp.src) A, B and C and I have 2 plugins functions yourPlugin1 and yourPlugin2. Using lazy pipe files will be processed in this order yourPlugin1:A, yourPlugin2:A, yourPlugin1:B, yourPlugin2:B, yourPlugin1:C, yourPlugin2:C. I would like to get yourPlugin1:A, yourPlugin1:B, yourPlugin1:C and then yourPlugin2:A, yourPlugin2:B, yourPlugin2:C. – mikep Oct 13 '16 at 14:37
  • That's a new question. Make a new post. – Sven Schoenung Oct 13 '16 at 14:39