如何使用Gulp在流中进行替换? [英] How to replace in stream using Gulp?

查看:435
本文介绍了如何使用Gulp在流中进行替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从Grunt切换到Gulp,我遇到了一个问题:

我从两个文件中读取两个流

  var fileStream = gulp.src(file); 
var injectionStream = gulp.src(injection)
.pipe(replace('#class-name#',argv.cname));

如果我的控制台参数--remove不存在,我没有问题连接这些流 p>

  .pipe(concat('animation.styl'))
.pipe(gulp.dest('./ dist' ))

然而当'--remove'为true时,我想删除注入,换句话说,从$ fileStream中减去injectStream。

我试过了:

$ p $ var es =要求(事件流);
es.replace()

var replace = require('gulp-replace');

它适用于字符串,但是我无法用从文件读取的流成功。有人可以给我一个小提示?



也许这是一个不正确的生成任务的工具,我应该留在Grunt和其他工具,如哟等?

感谢您的时间!

好的工作:)看起来像 gulp-replace 可能是一种更简单的方式,让民众通过google来到这里。

  var replace = require('gulp-replace'); 
$ b gulp.task('templates',function(){
gulp.src(['file.txt'])
.pipe(replace(/ foo(。{ 3})/ g,'$ 1foo'))
.pipe(gulp.dest('build / file.txt'));
});


I try to switch from Grunt to Gulp and I have an issue:

I read two streams from two files

var fileStream = gulp.src(file);
var injectionStream = gulp.src(injection)
.pipe(replace('#class-name#', argv.cname));

If my console argument "--remove" is absent I have no problem to concatenate these streams

.pipe(concat('animation.styl'))
.pipe(gulp.dest('./dist'))

However when '--remove' is true I want to delete injection, in other words, subtract injectionStream from fileStream.

I tried:

var es = require('event-stream');
es.replace()

var replace = require('gulp-replace');

It works with strings, but I cannot succeed with streams read from files. Can anybody give me a small hint?

And maybe it is an incorrect tool for generation task and I should stay with Grunt and\or other tools like yo,etc?
Thank you for your time!

解决方案

Nice work :) Looks like gulp-replace might be an easier way for folk coming here from google..

var replace = require('gulp-replace');

gulp.task('templates', function(){
  gulp.src(['file.txt'])
    .pipe(replace(/foo(.{3})/g, '$1foo'))
    .pipe(gulp.dest('build/file.txt'));
});

这篇关于如何使用Gulp在流中进行替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆