Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp, using current filename in `pipe()` function

I'm trying to render each file in my gulp source files with it's own json file, but I can't figure out how to access the current filename in the pipe function.

var gulp = require('gulp');
var handlebars = require('handlebars');
var gulpHandlebars = require('gulp-compile-handlebars');

gulp.task('compile-with-sample-data', function () {
  var options = {}
  return gulp.src('./src/**/*.html')
    .pipe(gulpHandlebars({ data: require('./data/' + filename +'.json') }, options))
    .pipe(gulp.dest('./build/'));

});

Where I can get it to work with the same file each time by just using require('./data/orders-complete.json'):

var gulp = require('gulp');
var handlebars = require('handlebars');
var gulpHandlebars = require('gulp-compile-handlebars');

gulp.task('compile-with-sample-data', function () {
  var options = {}
  return gulp.src('./src/**/*.html')
    .pipe(gulpHandlebars({ data: require('./data/orders-complete.json') }, options))
    .pipe(gulp.dest('./build/'));

});

It's not clear how I would do this.

like image 240
JP Silvashy Avatar asked Oct 19 '15 01:10

JP Silvashy


1 Answers

Use gulp-tap, it enables you to get the file name and even change it for downstream pipes.

like image 138
Meir Avatar answered Sep 28 '22 06:09

Meir