gpt4 book ai didi

node.js - 如何将可读流通过管道传递给 gulp.dest()?

转载 作者:搜寻专家 更新时间:2023-10-31 23:35:19 24 4
gpt4 key购买 nike

我有以下示例代码,它尝试将流通过管道传输到 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'));

此代码产生以下错误:

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)

但是,我看不出代码到底有什么问题。如果我用 process.stdout 替换 gulp.dest() 那么它就可以工作并且 gulp.dest() 在其他调用的上下文中工作。这样做的可行方法是什么?

最佳答案

Gulp 使用 Vinyl 流:

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('.'));

Gulp 流正常 begins with some file or files as source ,因此您需要将该可读流包装到 Vinyl stream 中允许 Gulp 和任何 gulp-plugin 从中获取信息,比如文件名(显然是伪造的),避免你的错误。

所以,Gulp 流是文件流,只是 check the source ...

关于node.js - 如何将可读流通过管道传递给 gulp.dest()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30810499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com