Подтвердить что ты не робот

Gulp: Как читать содержимое файла в переменной?

У меня есть задача gulp, которая должна читать файл в переменной, а затем использовать его содержимое как вход для другой функции, которая выполняется на файлах в трубе. Как это сделать?

Пример psuedo-psuedo-code

gulp.task('doSometing', function() {
  var fileContent=getFileContent("path/to/file.something"); //How?

  return gulp.src(dirs.src + '/templates/*.html')
    .pipe(myFunction(fileContent))
    .pipe(gulp.dest('destination/path));
});
4b9b3361

Ответ 1

Таргор указал мне в правильном направлении:

gulp.task('doSomething', function() {
  var fileContent = fs.readFileSync("path/to/file.something", "utf8");

  return gulp.src(dirs.src + '/templates/*.html')
    .pipe(myFunction(fileContent))
    .pipe(gulp.dest('destination/path'));
});

Ответ 2

Это то, что вы ищете?

fs = require("fs"),

gulp.task('doSometing', function() {

  return gulp.src(dirs.src + '/templates/*.html')
    .pipe(fs.readFile("path/to/file.something", "utf-8", function(err, _data) {
      //do something with your data
    }))
   .pipe(gulp.dest('destination/path'));
  });