1

我正在使用 gulp 通过 gulp-sass 插件将 SCSS 转换为 CSS 代码。这一切都很好,但我也想使用 gulp 从 Unix 管道(即 read process.stdin)接收输入(SCSS 代码)并使用它并将输出流式传输到process.stdout.

从阅读开始,process.stdin它似乎可以包装,然后在 gulp 任务中继续使用,例如ReadableStreamvinylstdin

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.

4

1 回答 1

0

所以我通过处理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();
    });
};
于 2016-01-15T15:35:20.820 回答