7

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.

JP Silvashy
  • 46,977
  • 48
  • 149
  • 227
  • 2
    Sounds like you need something like [gulp-tap](https://www.npmjs.com/package/gulp-tap/) to expose your file paths, see this [answer](http://stackoverflow.com/questions/21806925/get-the-current-file-name-in-gulp-src) – 1cgonza Oct 19 '15 at 03:56
  • Did you manage to get it working? Can't achieve the desired effect with gulp-tap – eugenesqr Nov 30 '16 at 17:48

1 Answers1

4

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

Meir
  • 14,081
  • 4
  • 39
  • 47