简体   繁体   English

如何将可读流传输给gulp.dest()?

[英]How can you pipe a readable stream to gulp.dest()?

I've got the following sample code which attempts to pipe a stream to gulp.dest(): 我有以下示例代码尝试将流传输到gulp.dest():

  var gulp = require('gulp');
  var stream = require('stream');

  var readable = new stream.Readable;
  readable.push('Hello, world!');
  readable.push(null);
  readable
  .pipe(gulp.dest('./test.txt'));

This code produces the following error: 此代码生成以下错误:

path.js:146
      throw new TypeError('Arguments to path.resolve must be strings');
      ^
TypeError: Arguments to path.resolve must be strings
    at Object.win32.resolve (path.js:146:13)
    at DestroyableTransform.saveFile [as _transform] (C:\paylocity\expense\node_modules\gulp\node_modules\vinyl-fs\lib\dest\index.js:36:26)
    at DestroyableTransform.Transform._read (C:\paylocity\expense\node_modules\gulp\node_modules\vinyl-fs\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:184:10)
    at DestroyableTransform.Transform._write (C:\paylocity\expense\node_modules\gulp\node_modules\vinyl-fs\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:172:12)
    at doWrite (C:\paylocity\expense\node_modules\gulp\node_modules\vinyl-fs\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:237:10)
    at writeOrBuffer (C:\paylocity\expense\node_modules\gulp\node_modules\vinyl-fs\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:227:5)
    at DestroyableTransform.Writable.write (C:\paylocity\expense\node_modules\gulp\node_modules\vinyl-fs\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:194:11)
    at Readable.ondata (_stream_readable.js:540:20)
    at Readable.emit (events.js:107:17)
    at Readable.read (_stream_readable.js:373:10)

I can't, however, see what exactly is wrong with the code. 但是,我不能看到代码到底出了什么问题。 If I replace gulp.dest() with process.stdout then it works and the gulp.dest() works within the context of other calls. 如果我用process.stdout替换gulp.dest()然后它工作,gulp.dest()在其他调用的上下文中工作。 What is a viable way of doing this? 这样做的可行方法是什么?

Gulp works with Vinyl streams: Gulp与乙烯基流合作:

var gulp   = require('gulp'),
    stream = require('stream'),
    source = require('vinyl-source-stream');

var readable = new stream.Readable;
readable.push('Hello, world!');
readable.push(null);

readable
    .pipe(source('test.txt'))
    .pipe(gulp.dest('.'));

A Gulp stream normally begins with some file or files as source , so you need to wrap that readable stream into a Vinyl stream allowing Gulp and any gulp-plugin getting info from it like filename (obviously faked), avoiding that error of yours. Gulp流通常以一些文件或文件作为源开始 ,因此您需要将可读流包装到Vinyl流中,允许Gulp和任何gulp-plugin从文件名中获取信息(显然是假的),避免您的错误。

So, Gulp streams are streams of files, just check the source ... 所以,Gulp流是文件流,只需检查源 ...

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

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