vinyl-bufferstream
Deal with vinyl file contents, regardless of whether it is Buffer/Stream
Last updated 11 years ago by shinnn .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install vinyl-bufferstream 
SYNC missed versions from official npm registry.

vinyl-bufferstream

NPM version Build Status Build status Coverage Status Dependency Status devDependency Status

Deal with vinyl file contents, regardless of whether it is Buffer/Stream

var through = require('through2');
var VinylBufferStream = require('vinyl-bufferstream');

function yourGulpPlugin() {
  var vinylBufferStream = new VinylBufferStream(function(buf, done) {
    syncOrAsyncFn(buf, done); 
  });

  return through.obj(function(file, enc, cb) {
    vinylBufferStream(file, function(err, contents) {
      if (err) {
        self.emit('error', err);
      } else {
        file.contents = contents;
        self.push(file);
      }
      cb();
    });
  });
}

Installation

Use npm.

npm install vinyl-bufferstream

API

var VinylBufferStream = require('vinyl-bufferstream');

vinylBufferStream = new VinylBufferStream(transformFunction)

(new operator is optional.)

transformFunction: Function
Return: Function

The argument must be a function taking a Buffer and a callback function as its first and second argument, which calls the callback function with passing Node-style callback arguments (error, result).

vinylBufferStream(file, callback)

file: Object (vinyl file object)
callback: Function

When the file.contents is a Buffer, it will call the transformFunction with passing file.contents to the first argument.

When the file.contents is a Stream, it will call the transformFunction with passing the buffered stream of file.contents to the first argument.

When the file.contents is a Stream, it won't call the transformFunction.

callback(err, contents)

error: Error or null
contents: Buffer or Stream

When the file.contents is a Buffer, contents will be a result that transformFunction produces.

When the file.contents is a Stream, contents will be a stream that emits a data transformFunction produces.

When the file.contents is null, contents will be null.

var gulp = require('gulp');
var SVGO = require('svgo');
var through = require('through2');
var VinylBufferStream = require('vinyl-bufferstream');

function svgminPlugin(options) {
  var svgo = new SVGO(options);
  var vinylBufferStream = new VinylBufferStream(function(buf, done) {
    svgo.optimize(String(buf), function(result) {
      if (result.error) {
        done(result.error);
        return;
      }
      done(null, result.data);
    });
  });

  return through.obj(function(file, enc, cb) {
    vinylBufferStream(file, function(err, contents) {
      if (err) {
        self.emit('error', err);
      } else {
        file.contents = contents;
        self.push(file);
      }
      cb();
    });
  });
}

gulp.task('buffer', function() {
  return gulp.src('*.svg')
    .pipe(svgminPlugin())
    .pipe(gulp.dest('dest'));
});

gulp.task('stream', function() {
  return gulp.src('*.svg', {buffer: false})
    .pipe(svgminPlugin())
    .pipe(gulp.dest('dest'));
});

License

Copyright (c) 2014 - 2015 Shinnosuke Watanabe

Licensed under the MIT License.

Current Tags

  • 1.0.1                                ...           latest (11 years ago)

3 Versions

  • 1.0.1                                ...           11 years ago
  • 1.0.0                                ...           11 years ago
  • 0.0.0                                ...           11 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (1)
Dev Dependencies (8)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |