Gulp: how to get gulp result as variable

匆匆过客 提交于 2020-01-02 09:39:07

问题


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:


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/'));


来源:https://stackoverflow.com/questions/36422699/gulp-how-to-get-gulp-result-as-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!