Gulp 如何添加一个示例乙烯基 Gulp 流?

mzaanser  于 2022-12-08  发布在  Gulp
关注(0)|答案(1)|浏览(121)

我想将一个new Vinyl文件添加到gulp任务的流中。

function task_add_vinyl_file(cb){
    const file = new Vinyl({
        cwd: '/',
        base: '/test/',
        path: '/test/file.js',
        contents: Buffer.from('var x = 123')
      });
    return gulp.src(
        [
            'path/to/files/**'
        ])
        .pipe(file)
        .pipe(gulp.dest('.'))
}

问题是file.pipe()不兼容
如何将乙烯基示例添加到吞咽管?

58wvjzkj

58wvjzkj1#

在开始管道传输/使用gulp src函数生成的流之前,您需要将vinyl文件对象写入流(在下面的示例中,它将预先添加vinyl对象,并且一旦流开始使用,通过src调用创建的流将随后写入数据)。

// create vinyl objects of interest
const file = new Vinyl({
    cwd: '/',
    base: '/test/',
    path: '/test/file.js',
    contents: Buffer.from('var x = 123')
  });

// the following initializes a stream, that we'll use to preppend the
// vinyl object.
let myStream = src(['path/to/files/**']);

// the following essentially pre-writes the vinyl object onto the stream
// note, this write can be done for multiple vinyl objects sequentially
myStream.write(file);

// and once we start piping, the stream consumes data until there
// is nothing else to consume, starting with the vinyl object
// and then moving onto the results of the glob in the src command.
myStream.pipe(gulp.dest('.'))

为了了解更多,我鼓励阅读本文的人阅读nodejs streams

相关问题