gulp-nunjucks-html + gulp-data not compiling on watch

一个人想着一个人 提交于 2019-12-23 02:06:11

问题


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?


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!