Skip to content

Instantly share code, notes, and snippets.

@lukehorvat
Last active March 3, 2023 18:17
Show Gist options
  • Save lukehorvat/59362fbc5abdbb823267219ce8f6ab74 to your computer and use it in GitHub Desktop.
Save lukehorvat/59362fbc5abdbb823267219ce8f6ab74 to your computer and use it in GitHub Desktop.
A small example of a "Vinyl adapter" that replaces `gulp.src`. Demonstrates how to create a stream for a "fake" (in-memory only) file.
import gulp from 'gulp';
import Vinyl from 'vinyl';
import vinylAdapter from './vinyl-adapter';
gulp.task('build', () => {
const file = new Vinyl({
path: 'hello.js',
contents: Buffer.from(`console.log('👋');`)
});
// Output hello.js to the dist directory.
return vinylAdapter(file).pipe(gulp.dest('dist'));
});
import { PassThrough } from 'node:stream';
import Vinyl from 'vinyl';
/**
* Create a stream from a Vinyl file.
*
* Typically, `gulp.src` is used to create a stream of Vinyl files.
* Use this function when you need to work backwards i.e. you already
* have a reference to a Vinyl file and want to start streaming it.
*/
export default function(file) {
if (!Vinyl.isVinyl(file)) {
throw new Error('The specified file is not a Vinyl.');
}
const stream = new PassThrough({ objectMode: true });
stream.end(file);
return stream;
}
@lukehorvat
Copy link
Author

Some discussion about "vinyl adapters" here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment