简体   繁体   English

完成可写流

[英]Finish writable stream

I am using gulp to process, minify and bundle my css files (and js too). 我正在使用gulp来处理,缩小和捆绑我的css文件(以及js)。 But I don't want to write file immediately. 但是我不想立即写文件。 I want to check if md5 sum of the processed content is not equal to md5 sum of the existing version of bundled file and write only in this case. 我想检查已处理内容的md5总和是否不等于捆绑文件现有版本的md5总和,并且仅在这种情况下才写。

Rather then write file immediately I try to get is as string from the process. 而是立即写入文件,我尝试从过程中获取字符串。

But the construction doesn't work. 但是,该构造无法正常工作。

const Stream = require('stream');
const writableStream = new Stream.Writable({ objectMode: true });

var file = "";

writableStream._write = (chunk, encoding, next) => {
    file += chunk.contents.toString();
    next();
}

let bundlePipe = gulp.src([...vendorLinks.map(e => path.resolve(htmlPath + e)), ...resourceLinks.map(e => path.resolve(htmlPath + e))], { sourcemaps: true })
    .pipe(postcss([cssnext, cssnano]))
    .pipe(concat("style.bundle.min.css"))
    .pipe(writableStream);
    //.pipe(gulp.dest(path.resolve(paths["html/static/css"]))); 


bundlePipe.on("end", () => {
    // It never goes there!
    console.log("get file contents", file); 
}

Seems writableStream which I create doesn't know that all data was already passed to it and never issues end signal. 我创建的writableStream似乎不知道所有数据都已经传递给它,并且从不发出结束信号。

What can I do so that writableStream close after it get all data from pipe? 我该怎么办,以便writableStream在从管道中获取所有数据后将其关闭?

Writable streams don't emit end , only readable streams emit this kind of event. 可写流不会发出end ,只有可读流会发出这种事件。

The equivalent on a writable stream is finish . 可写流上的等效项已finish You can find the details on this event in the documentation of Node.js. 您可以在Node.js 文档中找到有关此事件的详细信息。

The reason why readable and writable streams have different events to signal that they have ended / finished is that there are also hybrid streams (such as the DuplexStream ) which consist of a readable stream and a writable stream at the same time, and hence you need to be able to differ between one and the other type of event. 可读流和可写流具有不同的事件来表示它们已结束/结束的信号的原因是,还有混合流(例如DuplexStream )同时包含一个可读流和一个可写流,因此您需要以便能够在一种事件与另一种事件之间有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM