transfob replaces through2 for Gulp plugins
15 Aug 2017
As Metafizzy uses Gulp for running tasks, I write a lot of Gulp plugins. For example, here's a simple one to add a file's basename to the Vinyl file
object.
var path = require('path');
var transfob = require('transfob');
gulp.src('content/*.hbs')
// add basename data
.pipe( transfob( function( file, enc, next ) {
file.basename = path.basename( file.path, '.hbs' );
next( null, file );
})
// ..
This plugin uses transfob, a tiny, no-dependency replacement for through2.obj
. The name transfob comes from "Transform object." As I dug into through2
's source and node's Stream Transform class, I discovered there wasn't much code needed to work with Gulp streams. It's in fact so small, I can include its entire source code here:
var Transform = require('stream').Transform;
module.exports = function transfob( _transform ) {
var transform = new Transform({
objectMode: true
});
transform._transform = _transform;
return transform;
};
I use transfob for all the Metafizzy docs sites.
through2 is a proper utility for streams, so you may need it for more heavy lifting. But for my purposes, working with Gulp, transfob is all I need. So, If you're writing Gulp plugins, consider switching out through2.obj
for transfob
.