我对 gulp 比较陌生,我想知道 .pipe()
在 gulp 任务中到底做了什么?我收集到它通常在 return
和 .src
之后运行,但肯定不止这些。我在网上或 gulp 的文档中找不到任何东西,我真的很想了解我在使用什么。
编辑 我找到了 this ,但解释起来很糟糕
最佳答案
来自 Node 文档:
https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
The readable.pipe() method attaches a Writable stream to the readable, causing it to switch automatically into flowing mode and push all of its data to the attached Writable. The flow of data will be automatically managed so that the destination Writable stream is not overwhelmed by a faster Readable stream.
因此,在 Gulp 中,您可以使用 pipe()
方法将多个任务链接在一起。 Gulp 使用流。有可读和可写的流。以以下代码段为例:
gulp.src(config.jsSrc)
.pipe(uglify())
.pipe(gulp.dest(config.dest + '/js'))
.pipe(size());
gulp.src(...)
将 config.jsSrc
处的路径转换为可读的数据流,然后我们将其传送到 gulp-丑化
模块。 uglify 任务返回一个流,然后我们将其通过管道传输到我们的目的地,依此类推......
由于流通常是单个文件,有些人可能会混淆 Gulp 如何将多个文件放入流中。 Gulp 实际上在 object mode 中使用流:
gulp.src('*.js')
reads all files ending in.js
and emits each as an object on the stream.
来源:https://medium.com/gulpjs/gulp-sips-how-we-use-streams-d7790b22bf1a
关于node.js - .pipe() 在 gulp 中到底是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38404862/