carlitoplatanito / gulp-nunjucks-render

[Gulp](https://github.com/wearefractal/gulp) plugin to render [Nunjucks](http://mozilla.github.io/nunjucks/) templates

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Filtering partials with watch in Gulp

timkelty opened this issue · comments

In my views folder, I have a folders that start with _ that contain partials, that I'm filtering out from rendering:

var views = path.join(config.paths.src, 'views');
var src = [
  path.join(views, '**/*.html'),
  '!' + path.join(views, '_*/*'),
];
nunjucksRender.nunjucks.configure(
  [views],
  { watch: false }
);

gulp.task('build:html:render', function() {
  return gulp.src(src)
  .pipe(nunjucksRender())
  .pipe(gulp.dest(dest));
});

This works great with one exception: When I run this task from gulp.watch, it only runs when I save one of my non-underscored files, but I want it to re-render everything on any save with views.

  gulp.watch([
    path.join(config.paths.src, 'views/**/*.html'),
  ], gulp.series(
    'build:html:render'
  ));

Any ideas?

I am actually doing something like this to exclude "_" prefixed files. I hope it will be helpful for you:

var gulp = require('gulp');
var nunjucksRender = require('gulp-nunjucks-render');

gulp.task('default', function () {
    nunjucksRender.nunjucks.configure(['templates/']);
    return gulp.src('templates/!(_)*.html')
        .pipe(nunjucksRender({
            css_path: "../assets/css/",
            js_path: "../assets/js/",
            img_path: "../assets/images/"
        }))
        .pipe(gulp.dest('html'));
});
gulp.task('watch', function () {
    gulp.watch(['templates/*.html'], ['default']);
});

Yeah, that's essentially the same thing I'm doing. Both work fine when just compiling. The problem is when watching, the templates aren't re-built when you save one of the _ files, since it is filtered out of the stream.

I don't think the above code which I provided is not watching the files with _ prefix. The code I provided is actually watching all the files but compiling only the files which aren't prefixed with _.

@kamlekar is correct, his watch gulp.watch(['templates/*.html'], ['default']); is watching all html files in the template directory, might want to add **/*.html if you have the "_" files in sub-directories though.

Please close or let me know if your issue has been solved.

will do - testing now

Figured it out! I wasn't related to underscored directories at all.

I was doing nunjucksRender.nunjucks.configure outside of the gulp.task callback. As soon as I put it inside, everything works!

My bad, thanks for stickin' with me!

Awesome. Closing this out.

Hi Timkelty and kamlekar, any chance you could show me how you achieved this in the end, I need to exclude the includes folder too :)

@ReloadInteractive, I have:

  var nunjucksRender = require('gulp-nunjucks-render');
  var views = path.join(process.cwd(), 'views');
  var src = [
    path.join(views, '**/*.{nunj,nunjucks}'),
    '!' + path.join(views, '_*/*'),
  ];

  gulp.task('build:html', function() {
    nunjucksRender.nunjucks.configure(
      [views],
      { watch: false }
    );

    return gulp.src(src)
    .pipe(nunjucksRender())
    .pipe(gulp.dest(dest));
  });

The only confusion for me was it was important to have the nunjucksRender.nunjucks.configure inside the gulp.task callback, not outside.