Monday 13 March 2017

How to use others' plugin in through2

Background

I am going to create my first gulp plugin. I use through2 to handle the stream, which is recommended in the official document. My plugin 's target is to input the svg files and output a css file. svg -> png -> dataurl -> render in css file.

Stuck In

I want to use the svg2png plugin in through2 but I have no idea how to.

//index.js
var through       = require('through2');
var svg2png       = require('gulp-svg2png');

function dealWithSVG() {
        if (file.isNull()) {
            return cb(null, file);
        }

        if (file.isStream()) {
            this.emit('error', new GulpError(PLUGIN_NAME, 'Streaming not supported'));
            return cb();
        }

        /* I want to use svg2png() *here* */

        // Other Function
        // ...

        this.push(file);
        cb(null)
    });
}

module.exports = dealWithSVG;



// gulpfile.js
var gulp       = require('gulp');
var dealWithSVG = require('./index');
var svg2png = require('gulp-svg2png')

gulp.task('default', function () {
    return gulp.src('static/*.svg')
        .pipe(dealWithSVG())
        .pipe(gulp.dest('assets'));
});

My Try

I try file.pipe(svg2png()), and it return a stream. So the problem comes to how to deal with a new stream in through2? Or I just wrong?



via Rocker Lau

No comments:

Post a Comment