如何通过gulp插件运行缓冲区?(How to run a buffer through a gulp plugin?)

编程入门 行业动态 更新时间:2024-10-19 21:37:12
如何通过gulp插件运行缓冲区?(How to run a buffer through a gulp plugin?)

我正在尝试编写一个函数,让你可以使用任何gulp插件作为一个简单的内存缓冲区变换器(隐藏所有的流/乙烯基东西)。 例如,它可以像这样调用:

const gulpMinifyHtml = require('gulp-minify-html'); transformBufferWithGulpPlugin(inputBuffer, gulpMinifyHtml()).then(outputBuffer => { // outputBuffer should now be a minified version of inputBuffer });

(注意:我正在寻找一种通用的方法来处理任何 gulp插件; gulp-minify-html只是一个例子。)


看起来这很容易,但是我试图让它发挥作用。 我想我需要做的是:

构造一个新的Vinyl实例,其内容设置为inputBuffer 将此乙烯基实例放入对象流(?) 通过gulp插件管理对象流 从流中收集输出 当流完成输出时,从乙烯基实例中获取内容缓冲区,然后解决它。

这些步骤基本正确吗? 任何人都可以向我展示一个有效的例子,或者指向一个模块吗?

I'm trying to write a function that lets you use any gulp plugin as a simple in-memory buffer-transformer (hiding away all the stream/vinyl stuff). For example, it could be called like this:

const gulpMinifyHtml = require('gulp-minify-html'); transformBufferWithGulpPlugin(inputBuffer, gulpMinifyHtml()).then(outputBuffer => { // outputBuffer should now be a minified version of inputBuffer });

(NB. I'm looking for a generic way to do this with any gulp plugin; gulp-minify-html is just an example.)


It seems like it would be easy but I'm tearing my hair out trying to make it work. I think what I need to do is:

construct a new Vinyl instance with contents set to inputBuffer put this vinyl instance into an object stream (?) pipe the object stream through the gulp plugin collect output from the stream when the stream is finished outputting, grab the contents buffer from the vinyl instance, and resolve with it.

Are those steps basically correct? Can anyone show me a working example, or point me to a module that does it?

最满意答案

以下应该有效。 这使用stream-array来创建流。

var File = require('vinyl'); var streamify = require('stream-array'); function transformBufferWithGulpPlugin(inputBuffer, gulpPlugin) { return new Promise(function(resolve, reject) { streamify([new File({path:'dummy', contents:inputBuffer})]) .pipe(gulpPlugin) .on('error', reject) .on('data', function(file) { resolve(file.contents); }); }); } var minify = require('gulp-html-minify'); transformBufferWithGulpPlugin(new Buffer('<p>\n Test \n</p>'), minify()) .then(function(result) { console.log(result.toString()); }, function(error) { console.error(error); });

如上所述,并不是必须指定虚拟path ( vinyl不需要它),但某些插件可能仅在存在path属性时才有效。

The following should work. This uses stream-array to create the stream.

var File = require('vinyl'); var streamify = require('stream-array'); function transformBufferWithGulpPlugin(inputBuffer, gulpPlugin) { return new Promise(function(resolve, reject) { streamify([new File({path:'dummy', contents:inputBuffer})]) .pipe(gulpPlugin) .on('error', reject) .on('data', function(file) { resolve(file.contents); }); }); } var minify = require('gulp-html-minify'); transformBufferWithGulpPlugin(new Buffer('<p>\n Test \n</p>'), minify()) .then(function(result) { console.log(result.toString()); }, function(error) { console.error(error); });

It's not strictly necessary to specify a dummy path as I do above (vinyl doesn't require it), but some plugins might only work if there is a path property.

更多推荐

本文发布于:2023-04-28 01:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329703.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓冲区   插件   gulp   plugin   buffer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!