I've written a gulp task to take data from json files and process it as html. When I first run the build this works like a charm, however I've set up a watch task to also do this and although it will rebuild the nunjucks file into html, it seems to ignore the json until the next full build (even though all the watch does is run the same task)
here is my task:
// Process nunjucks html files (.nunjucks)
gulp.task('nunjucks', function() {
'use strict';
return gulp.src('src/html/pages/**/*.nunjucks')
.pipe(plumber(
{ errorHandler: onError }
))
.pipe(data(function(file) {
return require('./src/model/' + path.basename(file.path) + '.json');
}))
.pipe(data(function() {
return require('./src/model/globals.json');
}))
.pipe(nunjucks({
searchPaths: ['src/html/templates']
}))
.pipe(extReplace('.html'))
.pipe(gulp.dest('dist'))
.pipe(reload({stream:true}))
});
and here is my entire gulpfile in case the problem lies elsewhere and I've just not spotted it: http://pastebin.com/q9vc8h6i
Any ideas?
It took a while but I found a fix for it. I just replaced the commented out line with the line below it:
.pipe(data(function(file) {
//return require('./src/model/' + path.basename(file.path) + '.json');
return JSON.parse(fs.readFileSync('./src/model/' + path.basename(file.path, '.nunjucks') + '.json'));
}))
Edit: I also had to add var fs = require('fs')
to the top of the gulpfile, it's a node package so no additional dependencies were needed.
来源:https://stackoverflow.com/questions/35456748/gulp-nunjucks-html-gulp-data-not-compiling-on-watch