gulp-reduce-file
Reduce files to one single output using custom callbacks.
This module provides (me) a little extra plumbing where gulp-concat
or gulp-compile-hogan
don't
provide the flexibility I need, while while abstracting the streams
related boilerplate away.
Usage
Trivial example of providing a directory listing as json object:
var gulp = ; var reduce = ; { memofilerelative = true; } { return memo; } gulp; // produces a dist/filelist.json containing a list of files in src
Examples
1. use case rendering templates
Suppose you have a view structure where partials per template are contained in a sub directory, and you want to maintain that structure pre-rendered:
views/index/template.html
views/index/partials/head.html
views/index/partials/foooter.html
var gulp = ; var reduce = ; { var stack = templates parts = filerelative compiled = hogan; while partslength var part = parts; if partslength == 0 stackpart = compiled; stack = stackpart = stackpart || {}; return templates; } { return 'module.exports = ' + JSON + ';'; } gulp;
2. concatinating translations
Suppose you'd want to bundle a bunch of .json
translations into a single module:
var gulp = require('gulp');
var reduce = require('gulp-reduce-files');
function collect( file, memo ){
var locale = file.relative.split('/')[0];
memo[locale] = JSON.parse( file.contents );
return memo;
}
function end( memo ){
return 'module.exports = ' + JSON.stringify(memo, null, 2);
}
gulp.task('translations', function () {
return gulp.src('i18n/*.json')
.pipe(reduce('translations.json', collect, end, {} ) )
.pipe(gulp.dest('client'));
});