vinyl-bufferstream

2018-10-13 admin

vinyl-bufferstream是什么

什么是vinyl-bufferstream,Deal with vinyl file contents, regardless of whether it is Buffer/Stream

vinyl-bufferstream使用教程帮助文档

vinyl-bufferstream

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

Deal with vinyl file contents, regardless of whether they are a Buffer or 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) {
    var self = this;
    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 it’s first and second arguments, respectively. The callback will be invoked with Node-style callback arguments (error, result), where result is the buffered file.contents.

vinylBufferStream(file, callback)

file: Object (vinyl file object) callback: Function

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

When file.contents is a Stream, it will call transformFunction, passing the buffered file.contents as the first argument.

When file.contents isnull, it won’t call the transformFunction.

callback(err, contents)

error: Error or null contents: Buffer or Stream

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

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

When 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 © 2014 - 2016 Shinnosuke Watanabe

Licensed under the MIT License.

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-42598.html

文章标题:vinyl-bufferstream

回到顶部