gulpjs/vinyl

async processing via new gutil.File() ?

VaJoy opened this issue · 3 comments

VaJoy commented

I'm experiencing some Error: stream.push() after EOF errors when I run my gulp plugin:

        var embedder = new ResourceEmbedder(filepath);

        embedder.get(function (markup) {
            var f = new gutil.File({
                path: path.basename(filepath),
                contents: new Buffer(markup)
            });
            this.push(f);
            cb();
        }.bind(this));

but the codes below run successfully with no error:

        var embedder = new ResourceEmbedder(filepath);

        embedder.get(function (markup) {
            var f = new gutil.File({
                path: path.basename(filepath),
                contents: new Buffer(markup)
            });

            setTimeout(function(){
                this.push(f);
                cb();
            }.bind(this),3000)

        }.bind(this));

so I wonder if new gutil.File() was an async function and how to solve it?
p.s, embedder refer to resource-embedder

This has nothing to do with vinyl, this is a stream bug. Your problem is that your stream is ending before you have finished pushing all the files, use something like through2

VaJoy commented

hi @contra ,I DO use through2 already, and the gutil.File was just vinyl(here) itself.
btw here`s my whole codes:

'use strict';

var path = require('path');
var gutil = require('gulp-util');
var through2 = require('through2');
var PluginError = gutil.PluginError;
var ResourceEmbedder = require('./lib/resource-embedder');

module.exports = function (options) {
    return through2.obj(function (file, enc, cb) {

        var filepath = file.path;

        //file not exsists
        if (file.isNull()) {
            this.emit('error', new PluginError('gulp-embed', 'File not found: "' + filepath + '"'));
            return cb();
        }

        var embedder = new ResourceEmbedder(filepath);

        embedder.get(function (markup) {
            var f = new gutil.File({
                cwd: '',
                path: path.basename(filepath),
                contents: new Buffer(markup)
            });

            this.push(f);
            cb();
        }.bind(this));

    })
};

its weird that sometime it runs successfully but sometime runs with errors as Error: stream.push() after EOF`

VaJoy commented

oh sorry its bug from lib/resource-embedder2 and Ive fixed it