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

时间:2023-01-24 11:23:53

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)

我写了一个gulp任务来从json文件中获取数据并将其作为html处理。当我第一次运行构建时,这就像一个魅力,但是我已经设置了一个监视任务来执行此操作,虽然它会将nunjucks文件重建为html,但它似乎忽略了json直到下一个完整版本(即使所有的手表都运行相同的任务)

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

这是我的整个gulp文件,以防问题出在其他地方,我只是没有发现它:http://pastebin.com/q9vc8h6i

Any ideas?

有任何想法吗?

1 个解决方案

#1


2  

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.

编辑:我还必须将var fs = require('fs')添加到gulp文件的顶部,它是一个节点包,因此不需要额外的依赖项。

#1


2  

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.

编辑:我还必须将var fs = require('fs')添加到gulp文件的顶部,它是一个节点包,因此不需要额外的依赖项。