Gulp.js, watch task runs twice when saving files

后端 未结 12 1246
花落未央
花落未央 2020-12-15 11:03

Given the following piece of code from my gulpfile.js, everytime I save or change a file, the task runs twice instead of one single time, why is that? I just want it to run

12条回答
  •  佛祖请我去吃肉
    2020-12-15 11:57

    I tried debounce and awaitWriteFinish. It didn't work. This did:

    const gulp = require("gulp");
    const exec = require('child_process').exec;
    let run = false;
    
    gulp.task("watch", () => {
      console.log("watching ..");
      gulp.watch("**/*.js", ["browserify"]);
    });
    
    gulp.task("browserify", () => {
      if (run) { return; }
      run = true;
      console.log("calling browserify");
      exec("browserify app.js -o bundle.js");
      setTimeout(() => {
        run = false;
      }, 1000);
    });
    

提交回复
热议问题