如何使用 Gulp 在流中替换? [英] How to replace in stream using Gulp?
问题描述
我尝试从 Grunt 切换到 Gulp,但遇到了问题:
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));
如果我的控制台参数--remove"不存在,我可以毫无问题地连接这些流
If my console argument "--remove" is absent I have no problem to concatenate these streams
.pipe(concat('animation.styl'))
.pipe(gulp.dest('./dist'))
但是,当 '--remove' 为真时,我想删除注入,换句话说,从 fileStream 中减去 injectionStream.
However when '--remove' is true I want to delete injection, in other words, subtract injectionStream from fileStream.
我试过了:
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?
也许它是一个不正确的生成任务工具,我应该继续使用 Grunt 和或其他工具,如 yo 等?
感谢您的宝贵时间!
And maybe it is an incorrect tool for generation task and I should stay with Grunt andor other tools like yo,etc?
Thank you for your time!
推荐答案
干得好 :) 看起来像 gulp-replace对于从谷歌来到这里的人来说,这可能是一种更简单的方式..
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屋!