What is vinyl-buffer?
The vinyl-buffer npm package is used to convert streaming vinyl file objects into buffered vinyl file objects. This is particularly useful in build systems like Gulp where you may need to perform operations that require the entire file to be in memory.
What are vinyl-buffer's main functionalities?
Convert Stream to Buffer
This feature allows you to convert a stream into a buffer so that you can perform operations that require the entire file to be in memory. In this example, the vinyl-buffer package is used to convert the stream from browserify into a buffer so that it can be minified using gulp-uglify.
const gulp = require('gulp');
const vinylBuffer = require('vinyl-buffer');
const uglify = require('gulp-uglify');
const source = require('vinyl-source-stream');
const browserify = require('browserify');
gulp.task('scripts', function() {
return browserify('src/app.js')
.bundle()
.pipe(source('bundle.js'))
.pipe(vinylBuffer())
.pipe(uglify())
.pipe(gulp.dest('dist'));
});
Other packages similar to vinyl-buffer
gulp-buffer
The gulp-buffer package is similar to vinyl-buffer in that it also converts streaming vinyl file objects into buffered vinyl file objects. It is specifically designed to be used within Gulp pipelines, making it a more specialized alternative to vinyl-buffer.
through2
The through2 package is a general-purpose stream utility that allows you to transform and manipulate streams. While it is not specifically designed for converting vinyl file objects, it can be used to achieve similar functionality by writing custom transformation functions.
gulp-streamify
The gulp-streamify package is used to convert a stream into a buffered stream, allowing you to use plugins that only support buffered vinyl file objects. It is similar to vinyl-buffer but is designed to work specifically within Gulp pipelines.
vinyl-buffer
Convert streaming vinyl files to use
buffers.
An alternative to gulp-streamify
that you can pipe to, instead of being required to wrap your streams.
var browserify = require('browserify')
var source = require('vinyl-source-stream')
var buffer = require('vinyl-buffer')
var uglify = require('gulp-uglify')
var size = require('gulp-size')
var gulp = require('gulp')
gulp.task('build', function() {
var bundler = browserify('./index.js')
return bundler.bundle()
.pipe(source('index.js'))
.pipe(buffer())
.pipe(uglify())
.pipe(size())
.pipe(gulp.dest('dist/'))
})
Usage
vinylBuffer()
Creates a transform stream that takes vinyl files as input, and outputs
modified vinyl files as output. If file.isStream()
, file.contents
will
be converted to a Buffer
before being emitted again – otherwise, the file
will be emitted immediately.
License
MIT. See LICENSE.md for details.