我怎样才能编写一个简单的一揽子管道功能? [英] How can I write a simple gulp pipe function?
问题描述
我一直在努力编写两个管道函数,一个编译较少的文件,另一个编译这些文件。我想学习如何为更复杂的插件编写转换流/管道。
因此,我想知道如何从另一个管道读取数据,以及如何更改数据并将其发送到下一个管道。这是我到目前为止:
gulp.src(sources)
.pipe(through.obj(function (chunk,enc,cb){
var t = this;
// console.log(chunk,chunk.path);
fs.readFile(chunk。如果(err){cb(err);}
less.render(data,{
filename:chunk.path ,
sourceMap:{
sourceMapRootpath:true
}
})
.then(function(outputCss){
// console.log(less ();
$);
t.push(chunk) (函数(chunk,enc,cb){
console.log(chunk,chunk.path));
b) ); // not event getting called。
cb();
)))
我无法获得第二个管道中每个文件的outputCSS。我怎么发送它?
好吧,您不需要使用 fs
在这里,你已经得到了文件流(这里是你的 chunk
)。
var通过= require('through2')
gulp.src(sources)
.pipe(through.obj(function(chunk,enc,cb)){
console.log 'chunk',chunk.path)//现在应该记录
cb(null,chunk)
}))
在ES2015中:
从'through2'输入直通
(gulp.src(sources))
.pipe(through.obj((chunk,enc,cb)=> cb(null,chunk)))
并且针对您的具体示例:
.pipe(through。 obj(function(file,enc,cb){
less.render(file.contents,{filename:file.path,...})// add other (函数(res){
file.contents = new Buffer(res.css)
cb(null,file)
})
} ))
这仍然非常基本,我不检查错误,如果它不是流等等,但是这应该给你一些暗示你错过了什么。
I've been trying for a day to write two pipe functions, one that compiles less files and another one that concats these files. I want to learn how to write transform streams/pipes for more complex plugins.
So I want to know how TO read data from another pipe, and how to alter that data and send it to the next pipe. This is what I have so far:
gulp.src(sources)
.pipe(through.obj(function (chunk, enc, cb) {
var t = this;
// console.log("chunk", chunk.path);
fs.readFile(chunk.path, enc, function (err,data) {
if (err) { cb(err); }
less.render(data, {
filename : chunk.path,
sourceMap : {
sourceMapRootpath : true
}
})
.then(function (outputCss) {
// console.log("less result",outputCss);
t.push(chunk);// or this.push(outputCss) same result
cb();
});
});
}))
.pipe(through.obj(function (chunk, enc, cb) {
console.log("chunk", chunk.path); // not event getting called.
cb();
}))
I can't get the outputCSS for each file in the second pipe. How can I send it?
Well, you don't need to use fs
here, you already got the stream of file (here your chunk
).
Another point, you're not sending back to the pipe the files, so I guess that's why nothing is called on your second one.
var through = require('through2')
gulp.src(sources)
.pipe(through.obj(function (chunk, enc, cb) {
console.log('chunk', chunk.path) // this should log now
cb(null, chunk)
}))
In ES2015:
import through from 'through2'
gulp.src(sources)
.pipe(through.obj((chunk, enc, cb) => cb(null, chunk)))
And for your specific example:
.pipe(through.obj(function (file, enc, cb) {
less.render(file.contents, { filename: file.path, ... }) // add other options
.then(function (res) {
file.contents = new Buffer(res.css)
cb(null, file)
})
}))
This is still pretty basic, I don't check for errors, if it's not a stream and so on, but this should give you some hint on what you've missed.
这篇关于我怎样才能编写一个简单的一揽子管道功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!