Gulp: how to get gulp result as variable

2019-05-28 13:51发布

Needed something like this:

gulp.src(path.join(conf.paths.src, '/**/*.less'))
    .pipe($.less()) // already have some result, but HUGE changes are needed
    .pipe(HERE_I_GET_IT_AS_A_TEXT(function(str){
        // a lot of different changes
        return changed_str;
    }))
    .pipe(rename('styles.css')) // go on doing smth with changed_str
    .pipe(gulp.dest('./app/theme/'));

Is there something in gulp to get some transitional result as text, process text with js and pass it to the next pipes? Everywhere I find only solution with reading files directly, this way is pointless for me.

1条回答
做自己的国王
2楼-- · 2019-05-28 14:28

Use map-stream:

var gulp = require('gulp');
var map = require('map-stream');

gulp.src(path.join(conf.paths.src, '/**/*.less'))
  .pipe($.less())
  .pipe(map(function(file, done) {
    var str = file.contents.toString();

    str = str.replace(/foo/, "bar");

    file.contents = new Buffer(str);

    done(null, file);
  }))
  .pipe(rename('styles.css')) // go on doing smth with changed_str
  .pipe(gulp.dest('./app/theme/'));
查看更多
登录 后发表回答