我正在使用 gulp-sass 插件将 SCSS 转换为 CSS 代码。这一切都工作正常,但我还想使用 gulp 从 Unix 管道接收输入(SCSS 代码)(即读取 process.stdin
)并使用它并将输出流式传输到 process.stdout
。
通过阅读 process.stdin
是一个 ReadableStream
和 vinyl
似乎可以包装 stdin
然后在 gulp 任务中使用,例如
gulp.task('stdin-sass', function () {
process.stdin.setEncoding('utf8');
var file = new File({contents: process.stdin, path: './test.scss'});
file.pipe(convert_sass_to_css())
.pipe(gulp.dest('.'));
});
但是,当我这样做时,我收到错误:
TypeError: file.isNull is not a function
这让我觉得 stdin
在某种程度上很特别,但 Node.js 的官方文档表明它是一个真正的 ReadableStream
。
最佳答案
因此,我通过处理 process.stdin
并写入 process.stdout
来实现此目的:
var buffer = require('vinyl-buffer');
var source = require('vinyl-source-stream');
var through = require('through2');
gulp.task('stdio-sass', function () {
process.stdin.setEncoding('utf8');
process.stdin.pipe(source('input.scss'))
.pipe(buffer())
.pipe(convert_sass_to_css())
.pipe(stdout_stream());
});
var stdout_stream = function () {
process.stdout.setEncoding('utf8');
return through.obj(function (file, enc, complete) {
process.stdout.write(file.contents.toString());
this.push(file);
complete();
});
};
关于node.js - 如何使用 process.stdin 作为 gulp 任务的起点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34814017/