【发布时间】:2019-07-04 22:48:24
【问题描述】:
我正在使用 gulp 来处理、缩小和捆绑我的 css 文件(以及 js 文件)。但我不想立即写文件。我想检查已处理内容的 md5 总和是否不等于捆绑文件现有版本的 md5 总和,仅在这种情况下写入。
而不是立即写入文件,我尝试从进程中获取字符串。
但是构造不起作用。
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);
}
似乎我创建的 writableStream 并不知道所有数据都已传递给它并且从不发出结束信号。
在 writableStream 从管道中获取所有数据后,我该怎么做才能关闭它?
【问题讨论】:
标签: javascript node.js ecmascript-6 stream pipe