Last active
March 3, 2023 18:17
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some discussion about "vinyl adapters" here.