如何在乙烯基中设置内容属性?

时间:2019-01-24 11:18:08

标签: node.js plugins gulp

我正在学习如何创建gulp插件,并且只是在玩一些。我正在尝试创建一个从文件内容中删除语音字母的插件。看起来很简单,但是在设置输出乙烯基的内容属性时遇到错误。错误提示:

Error: File.contents can only be a Buffer, a Stream, or null

我潜入vinyl code on github,发现在设置 contents 属性时会检查该值是否不是 Stream Buffer null 引发该错误。

我遵循了该规则,并通过Buffer.from方法创建了内容值。我以为这足够了,但是仍然出现相同的错误。因此,我深入vinyl code搜索此验证,发现它正在使用Buffer.isBuffer方法。接下来的事情是,如果我的内容值确实是一个缓冲区实例,并且发现了以下内容,则将其写入控制台:

var Buffer = require('Buffer').Buffer;
var Vinyl = require('vinyl');

# stuff for creating gulp plugin
var vinylInstance = // stuff for creating vinyl file.
var content = Buffer.from(new String('this is my content'));

# check if content is an array
console.log(Buffer.isBuffer(content)); // console shows true
console.log(vinylInstance.isBuffer(content)); // console shows false

现在,我被困在这一点上,不知道如何实现它。我用Google搜索很多次,所有文章,帖子,答案等,都发现show以与我相同的方式构建了content属性。这是我的代码:

var removeLetters = new stream.Transform({
        readableObjectMode: true,
        writableObjectMode: true,

        transform: function(chunk, encoding, callback) {
            var contents = Buffer.from(chunk.contents.toString());
            var file = new vinyl({
                base: chunk.base,
                path: chunk.base + '/transform.js',
                contents: Buffer.from(chunk.contents.toString().replace(/aeiou/gi).trim()),
            });
            this.push(file);
            callback();
        }
    });

有人可以帮我知道我在做什么吗?

谢谢。

0 个答案:

没有答案