gulp-nunjucks-html + gulp-data无法在手表上编译 [英] gulp-nunjucks-html + gulp-data not compiling on watch

查看:329
本文介绍了gulp-nunjucks-html + gulp-data无法在手表上编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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}))
});

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

and here is my entire gulpfile in case the problem lies elsewhere and I've just not spotted it: http://pastebin.com/q9vc8h6i

任何想法?

推荐答案

它花了一段时间,但我找到了一个解决方案。

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'));
 }))

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

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.

这篇关于gulp-nunjucks-html + gulp-data无法在手表上编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆