Gulp watch for a handlebars project











up vote
1
down vote

favorite












I have project that uses handlebars and I'm using gulp and gulp-compile-handlebars to watch for changes in the handlebars template as well as the json structure used to compile the template. I have implemented a watch which seems to work, but it feels like it's doing more work than it should.



My gulpfile looks like this:



var gulp = require('gulp');
var watch = require('gulp-watch');
var handlebars = require('gulp-compile-handlebars');
var rename = require('gulp-rename');
var batch = require('gulp-batch');
var argv = require('yargs').argv // for args parsing
var spawn = require('child_process').spawn;

handlebars.Handlebars.registerHelper('next', function(context, idx, options) {
if (context[idx + 1]) {
return options.fn(context[idx + 1])
} else {
return "";
}
});


gulp.task('default', function () {
var content = require('./content.js')
var templateData = content
options = {
// partials : {
// footer : '<footer>the end</footer>'
// },
// batch : ['./src/partials'],
// helpers : {
// capitals : function(str){
// return str.toUpperCase();
// }
// }
}

return gulp.src(['index.handlebars'])
.pipe(handlebars(templateData, options))
.pipe(rename('indexCompiled.html'))
.pipe(gulp.dest('.'));
});

// gulp.task('watch', function () {
// watch('index.handlebars', batch(function (events, done) {
// gulp.start('default', done);
// }));
// watch('content.js', batch(function (events, done) {
// gulp.start('default', done);
// }));
// watch('gulpfile.js', batch(function (events, done) {
// gulp.start('default', done);
// }));
// });

gulp.task('auto-reload', function() {
var p;

gulp.watch('content.js', spawnChildren);
gulp.watch('index.handlebars', spawnChildren);
spawnChildren();

function spawnChildren(e) {
// kill previous spawned process
if(p) { p.kill(); }

// `spawn` a child `gulp` process linked to the parent `stdio`
p = spawn('gulp', [argv.task], {stdio: 'inherit'});
}
});


As I'm making changes I run gulp auto-reload --task default and any changes to the handlebars template or the json in content.js is re-compiled into indexCompiles.html. Intially I was just trying to do a simple watch which you can see commented out in the gulp script.



// gulp.task('watch', function () {
// watch('index.handlebars', batch(function (events, done) {
// gulp.start('default', done);
// }));
// watch('content.js', batch(function (events, done) {
// gulp.start('default', done);
// }));
// });


That watch would run for all changes and recompile, but the only changes that were reflected in the new indexCompiled.html were the ones from index.handlebars any changes to the json in content.js was not reflected in the new compiled html I would have to stop the gulp watch and start it again, which is why I eventually made the auto-restart which works. Why wasn't the original gulp watch working and is there a way to get it to work or do I need to use the auto-restart task?










share|improve this question














bumped to the homepage by Community 31 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.



















    up vote
    1
    down vote

    favorite












    I have project that uses handlebars and I'm using gulp and gulp-compile-handlebars to watch for changes in the handlebars template as well as the json structure used to compile the template. I have implemented a watch which seems to work, but it feels like it's doing more work than it should.



    My gulpfile looks like this:



    var gulp = require('gulp');
    var watch = require('gulp-watch');
    var handlebars = require('gulp-compile-handlebars');
    var rename = require('gulp-rename');
    var batch = require('gulp-batch');
    var argv = require('yargs').argv // for args parsing
    var spawn = require('child_process').spawn;

    handlebars.Handlebars.registerHelper('next', function(context, idx, options) {
    if (context[idx + 1]) {
    return options.fn(context[idx + 1])
    } else {
    return "";
    }
    });


    gulp.task('default', function () {
    var content = require('./content.js')
    var templateData = content
    options = {
    // partials : {
    // footer : '<footer>the end</footer>'
    // },
    // batch : ['./src/partials'],
    // helpers : {
    // capitals : function(str){
    // return str.toUpperCase();
    // }
    // }
    }

    return gulp.src(['index.handlebars'])
    .pipe(handlebars(templateData, options))
    .pipe(rename('indexCompiled.html'))
    .pipe(gulp.dest('.'));
    });

    // gulp.task('watch', function () {
    // watch('index.handlebars', batch(function (events, done) {
    // gulp.start('default', done);
    // }));
    // watch('content.js', batch(function (events, done) {
    // gulp.start('default', done);
    // }));
    // watch('gulpfile.js', batch(function (events, done) {
    // gulp.start('default', done);
    // }));
    // });

    gulp.task('auto-reload', function() {
    var p;

    gulp.watch('content.js', spawnChildren);
    gulp.watch('index.handlebars', spawnChildren);
    spawnChildren();

    function spawnChildren(e) {
    // kill previous spawned process
    if(p) { p.kill(); }

    // `spawn` a child `gulp` process linked to the parent `stdio`
    p = spawn('gulp', [argv.task], {stdio: 'inherit'});
    }
    });


    As I'm making changes I run gulp auto-reload --task default and any changes to the handlebars template or the json in content.js is re-compiled into indexCompiles.html. Intially I was just trying to do a simple watch which you can see commented out in the gulp script.



    // gulp.task('watch', function () {
    // watch('index.handlebars', batch(function (events, done) {
    // gulp.start('default', done);
    // }));
    // watch('content.js', batch(function (events, done) {
    // gulp.start('default', done);
    // }));
    // });


    That watch would run for all changes and recompile, but the only changes that were reflected in the new indexCompiled.html were the ones from index.handlebars any changes to the json in content.js was not reflected in the new compiled html I would have to stop the gulp watch and start it again, which is why I eventually made the auto-restart which works. Why wasn't the original gulp watch working and is there a way to get it to work or do I need to use the auto-restart task?










    share|improve this question














    bumped to the homepage by Community 31 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have project that uses handlebars and I'm using gulp and gulp-compile-handlebars to watch for changes in the handlebars template as well as the json structure used to compile the template. I have implemented a watch which seems to work, but it feels like it's doing more work than it should.



      My gulpfile looks like this:



      var gulp = require('gulp');
      var watch = require('gulp-watch');
      var handlebars = require('gulp-compile-handlebars');
      var rename = require('gulp-rename');
      var batch = require('gulp-batch');
      var argv = require('yargs').argv // for args parsing
      var spawn = require('child_process').spawn;

      handlebars.Handlebars.registerHelper('next', function(context, idx, options) {
      if (context[idx + 1]) {
      return options.fn(context[idx + 1])
      } else {
      return "";
      }
      });


      gulp.task('default', function () {
      var content = require('./content.js')
      var templateData = content
      options = {
      // partials : {
      // footer : '<footer>the end</footer>'
      // },
      // batch : ['./src/partials'],
      // helpers : {
      // capitals : function(str){
      // return str.toUpperCase();
      // }
      // }
      }

      return gulp.src(['index.handlebars'])
      .pipe(handlebars(templateData, options))
      .pipe(rename('indexCompiled.html'))
      .pipe(gulp.dest('.'));
      });

      // gulp.task('watch', function () {
      // watch('index.handlebars', batch(function (events, done) {
      // gulp.start('default', done);
      // }));
      // watch('content.js', batch(function (events, done) {
      // gulp.start('default', done);
      // }));
      // watch('gulpfile.js', batch(function (events, done) {
      // gulp.start('default', done);
      // }));
      // });

      gulp.task('auto-reload', function() {
      var p;

      gulp.watch('content.js', spawnChildren);
      gulp.watch('index.handlebars', spawnChildren);
      spawnChildren();

      function spawnChildren(e) {
      // kill previous spawned process
      if(p) { p.kill(); }

      // `spawn` a child `gulp` process linked to the parent `stdio`
      p = spawn('gulp', [argv.task], {stdio: 'inherit'});
      }
      });


      As I'm making changes I run gulp auto-reload --task default and any changes to the handlebars template or the json in content.js is re-compiled into indexCompiles.html. Intially I was just trying to do a simple watch which you can see commented out in the gulp script.



      // gulp.task('watch', function () {
      // watch('index.handlebars', batch(function (events, done) {
      // gulp.start('default', done);
      // }));
      // watch('content.js', batch(function (events, done) {
      // gulp.start('default', done);
      // }));
      // });


      That watch would run for all changes and recompile, but the only changes that were reflected in the new indexCompiled.html were the ones from index.handlebars any changes to the json in content.js was not reflected in the new compiled html I would have to stop the gulp watch and start it again, which is why I eventually made the auto-restart which works. Why wasn't the original gulp watch working and is there a way to get it to work or do I need to use the auto-restart task?










      share|improve this question













      I have project that uses handlebars and I'm using gulp and gulp-compile-handlebars to watch for changes in the handlebars template as well as the json structure used to compile the template. I have implemented a watch which seems to work, but it feels like it's doing more work than it should.



      My gulpfile looks like this:



      var gulp = require('gulp');
      var watch = require('gulp-watch');
      var handlebars = require('gulp-compile-handlebars');
      var rename = require('gulp-rename');
      var batch = require('gulp-batch');
      var argv = require('yargs').argv // for args parsing
      var spawn = require('child_process').spawn;

      handlebars.Handlebars.registerHelper('next', function(context, idx, options) {
      if (context[idx + 1]) {
      return options.fn(context[idx + 1])
      } else {
      return "";
      }
      });


      gulp.task('default', function () {
      var content = require('./content.js')
      var templateData = content
      options = {
      // partials : {
      // footer : '<footer>the end</footer>'
      // },
      // batch : ['./src/partials'],
      // helpers : {
      // capitals : function(str){
      // return str.toUpperCase();
      // }
      // }
      }

      return gulp.src(['index.handlebars'])
      .pipe(handlebars(templateData, options))
      .pipe(rename('indexCompiled.html'))
      .pipe(gulp.dest('.'));
      });

      // gulp.task('watch', function () {
      // watch('index.handlebars', batch(function (events, done) {
      // gulp.start('default', done);
      // }));
      // watch('content.js', batch(function (events, done) {
      // gulp.start('default', done);
      // }));
      // watch('gulpfile.js', batch(function (events, done) {
      // gulp.start('default', done);
      // }));
      // });

      gulp.task('auto-reload', function() {
      var p;

      gulp.watch('content.js', spawnChildren);
      gulp.watch('index.handlebars', spawnChildren);
      spawnChildren();

      function spawnChildren(e) {
      // kill previous spawned process
      if(p) { p.kill(); }

      // `spawn` a child `gulp` process linked to the parent `stdio`
      p = spawn('gulp', [argv.task], {stdio: 'inherit'});
      }
      });


      As I'm making changes I run gulp auto-reload --task default and any changes to the handlebars template or the json in content.js is re-compiled into indexCompiles.html. Intially I was just trying to do a simple watch which you can see commented out in the gulp script.



      // gulp.task('watch', function () {
      // watch('index.handlebars', batch(function (events, done) {
      // gulp.start('default', done);
      // }));
      // watch('content.js', batch(function (events, done) {
      // gulp.start('default', done);
      // }));
      // });


      That watch would run for all changes and recompile, but the only changes that were reflected in the new indexCompiled.html were the ones from index.handlebars any changes to the json in content.js was not reflected in the new compiled html I would have to stop the gulp watch and start it again, which is why I eventually made the auto-restart which works. Why wasn't the original gulp watch working and is there a way to get it to work or do I need to use the auto-restart task?







      javascript gulp.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 6 '17 at 14:32









      jmona789

      1062




      1062





      bumped to the homepage by Community 31 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 31 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          If I understood what you are doing correctly you are compiling index.handlebars using data from content.js and you want to recompile if any of those files changes. Below is a simplified and NOT TESTED example of what your code can look like



          // removed gulp-watch (built in gulp by default)
          // also removed spawn, argv and batch
          var gulp = require('gulp');
          var handlebars = require('gulp-compile-handlebars');
          var rename = require('gulp-rename');

          handlebars.Handlebars.registerHelper('next', function(context, idx, options) {
          if (context[idx + 1]) {
          return options.fn(context[idx + 1])
          } else {
          return "";
          }
          });

          // compile index.handlebars with data from content.js
          gulp.task('handlebars', function() {
          var templateData = require('./content.js');
          var options = {};

          return gulp.src(['index.handlebars'])
          .pipe(handlebars(templateData, options))
          .pipe(rename('indexCompiled.html'))
          .pipe(gulp.dest('.'));
          });

          // watch for changes in content and index files
          // and then run handlebars task
          gulp.task('watch', function(){
          gulp.watch(['content.js', 'index.handlebars'], ['handlebars']);
          });

          // on run `gulp` compile and start watching for changes
          gulp.task('default', ['handlebars', 'watch']);





          share|improve this answer





















            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f177315%2fgulp-watch-for-a-handlebars-project%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            If I understood what you are doing correctly you are compiling index.handlebars using data from content.js and you want to recompile if any of those files changes. Below is a simplified and NOT TESTED example of what your code can look like



            // removed gulp-watch (built in gulp by default)
            // also removed spawn, argv and batch
            var gulp = require('gulp');
            var handlebars = require('gulp-compile-handlebars');
            var rename = require('gulp-rename');

            handlebars.Handlebars.registerHelper('next', function(context, idx, options) {
            if (context[idx + 1]) {
            return options.fn(context[idx + 1])
            } else {
            return "";
            }
            });

            // compile index.handlebars with data from content.js
            gulp.task('handlebars', function() {
            var templateData = require('./content.js');
            var options = {};

            return gulp.src(['index.handlebars'])
            .pipe(handlebars(templateData, options))
            .pipe(rename('indexCompiled.html'))
            .pipe(gulp.dest('.'));
            });

            // watch for changes in content and index files
            // and then run handlebars task
            gulp.task('watch', function(){
            gulp.watch(['content.js', 'index.handlebars'], ['handlebars']);
            });

            // on run `gulp` compile and start watching for changes
            gulp.task('default', ['handlebars', 'watch']);





            share|improve this answer

























              up vote
              0
              down vote













              If I understood what you are doing correctly you are compiling index.handlebars using data from content.js and you want to recompile if any of those files changes. Below is a simplified and NOT TESTED example of what your code can look like



              // removed gulp-watch (built in gulp by default)
              // also removed spawn, argv and batch
              var gulp = require('gulp');
              var handlebars = require('gulp-compile-handlebars');
              var rename = require('gulp-rename');

              handlebars.Handlebars.registerHelper('next', function(context, idx, options) {
              if (context[idx + 1]) {
              return options.fn(context[idx + 1])
              } else {
              return "";
              }
              });

              // compile index.handlebars with data from content.js
              gulp.task('handlebars', function() {
              var templateData = require('./content.js');
              var options = {};

              return gulp.src(['index.handlebars'])
              .pipe(handlebars(templateData, options))
              .pipe(rename('indexCompiled.html'))
              .pipe(gulp.dest('.'));
              });

              // watch for changes in content and index files
              // and then run handlebars task
              gulp.task('watch', function(){
              gulp.watch(['content.js', 'index.handlebars'], ['handlebars']);
              });

              // on run `gulp` compile and start watching for changes
              gulp.task('default', ['handlebars', 'watch']);





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                If I understood what you are doing correctly you are compiling index.handlebars using data from content.js and you want to recompile if any of those files changes. Below is a simplified and NOT TESTED example of what your code can look like



                // removed gulp-watch (built in gulp by default)
                // also removed spawn, argv and batch
                var gulp = require('gulp');
                var handlebars = require('gulp-compile-handlebars');
                var rename = require('gulp-rename');

                handlebars.Handlebars.registerHelper('next', function(context, idx, options) {
                if (context[idx + 1]) {
                return options.fn(context[idx + 1])
                } else {
                return "";
                }
                });

                // compile index.handlebars with data from content.js
                gulp.task('handlebars', function() {
                var templateData = require('./content.js');
                var options = {};

                return gulp.src(['index.handlebars'])
                .pipe(handlebars(templateData, options))
                .pipe(rename('indexCompiled.html'))
                .pipe(gulp.dest('.'));
                });

                // watch for changes in content and index files
                // and then run handlebars task
                gulp.task('watch', function(){
                gulp.watch(['content.js', 'index.handlebars'], ['handlebars']);
                });

                // on run `gulp` compile and start watching for changes
                gulp.task('default', ['handlebars', 'watch']);





                share|improve this answer












                If I understood what you are doing correctly you are compiling index.handlebars using data from content.js and you want to recompile if any of those files changes. Below is a simplified and NOT TESTED example of what your code can look like



                // removed gulp-watch (built in gulp by default)
                // also removed spawn, argv and batch
                var gulp = require('gulp');
                var handlebars = require('gulp-compile-handlebars');
                var rename = require('gulp-rename');

                handlebars.Handlebars.registerHelper('next', function(context, idx, options) {
                if (context[idx + 1]) {
                return options.fn(context[idx + 1])
                } else {
                return "";
                }
                });

                // compile index.handlebars with data from content.js
                gulp.task('handlebars', function() {
                var templateData = require('./content.js');
                var options = {};

                return gulp.src(['index.handlebars'])
                .pipe(handlebars(templateData, options))
                .pipe(rename('indexCompiled.html'))
                .pipe(gulp.dest('.'));
                });

                // watch for changes in content and index files
                // and then run handlebars task
                gulp.task('watch', function(){
                gulp.watch(['content.js', 'index.handlebars'], ['handlebars']);
                });

                // on run `gulp` compile and start watching for changes
                gulp.task('default', ['handlebars', 'watch']);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 9 '17 at 18:58









                AntK

                1636




                1636






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f177315%2fgulp-watch-for-a-handlebars-project%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin