Use conventional text streams at the start of your gulp or vinyl pipelines
npx @tessl/cli install tessl/npm-vinyl-source-stream@1.1.0Vinyl Source Stream is a utility that enables the use of conventional text streams at the start of gulp or vinyl pipelines. It creates a bridge between standard Node.js streams and vinyl file objects, making it easier to integrate existing stream-based tools with gulp workflows without needing specialized wrapper plugins.
npm install vinyl-source-streamconst source = require('vinyl-source-stream');ES Modules:
import source from 'vinyl-source-stream';const source = require('vinyl-source-stream');
const gulp = require('gulp');
const fs = require('fs');
// Basic usage with a readable stream
fs.createReadStream('input.txt')
.pipe(source('output.txt'))
.pipe(gulp.dest('./dist'));
// Common usage with browserify
const browserify = require('browserify');
const bundleStream = browserify('./index.js').bundle();
bundleStream
.pipe(source('bundle.js'))
.pipe(gulp.dest('./dist'));Converts conventional text streams into vinyl file objects for use in gulp pipelines.
/**
* Creates a through stream that converts text input to vinyl file instances
* @param filename - Optional filename for the output vinyl file
* @param baseDir - Optional base directory for path resolution
* @returns Transform stream that emits vinyl file objects
*/
function vinylSourceStream(filename, baseDir)Parameters:
filename (string, optional): A "pretend" filename to use for the vinyl file. This affects how downstream processes handle the file (e.g., for determining output filename, content type detection). If not provided, the vinyl file will have no path property.
baseDir (string, optional): Base directory for resolving the file path. When combined with filename, creates the full file path using path.resolve(baseDir || process.cwd(), filename). Also sets the vinyl file's base property when provided. If not provided but filename is given, path resolution defaults to process.cwd() but no base property is set.
Returns:
Transform stream (through2 in object mode) that:
contents property is a readable stream containing all input dataStream Behavior:
Usage Examples:
const source = require('vinyl-source-stream');
const fs = require('fs');
const gulp = require('gulp');
// Basic file conversion
fs.createReadStream('data.txt')
.pipe(source('processed-data.txt'))
.pipe(gulp.dest('./output'));
// With custom base directory
someReadableStream
.pipe(source('result.json', './build'))
.pipe(gulp.dest('./'));
// Filename only (base defaults to process.cwd())
dataStream
.pipe(source('output.csv'))
.pipe(someGulpPlugin())
.pipe(gulp.dest('./dist'));
// Without filename (downstream plugins determine naming)
apiResponseStream
.pipe(source())
.pipe(someTransformPlugin())
.pipe(gulp.dest('./cache'));Since this is a JavaScript package, the following TypeScript-style definitions describe the expected types:
interface VinylFile {
contents: NodeJS.ReadableStream;
path?: string;
base?: string;
}
interface TransformStream extends NodeJS.ReadWriteStream {
// Standard Node.js Transform stream interface
// Accepts text/buffer chunks via write()
// Emits vinyl file objects via 'data' events
}const browserify = require('browserify');
const source = require('vinyl-source-stream');
const gulp = require('gulp');
gulp.task('bundle', function() {
return browserify('./src/app.js')
.bundle()
.pipe(source('app.bundle.js'))
.pipe(gulp.dest('./dist/js'));
});const someStreamingTool = require('some-streaming-tool');
const source = require('vinyl-source-stream');
someStreamingTool.createStream()
.pipe(source('tool-output.txt'))
.pipe(gulp.dest('./output'));The stream follows standard Node.js stream error handling patterns. Errors from the input stream will be forwarded to the output stream:
inputStream
.pipe(source('output.txt'))
.on('error', function(err) {
console.error('Stream error:', err);
})
.pipe(gulp.dest('./dist'));