【问题标题】:How do I make Gulp automatically reload whenever the gulp.js file changes?每当 gulp.js 文件更改时,如何让 Gulp 自动重新加载?
【发布时间】:2014-08-10 20:27:22
【问题描述】:

当我调整我的 gulp.js 文件以自动执行新的构建任务时,我必须保存文件,切换到我正在运行 gulp 的 shell 窗口,杀死它,然后重新启动它。

如果能够自动执行此操作会很好,这样 gulp 文件中的更改会自动获取并运行。

是否有工具选项可以执行此操作?

【问题讨论】:

    标签: gulp


    【解决方案1】:

    如果您只是想在更改 gulpfile.js 时重新启动正在运行的任何任务,您可以创建一个在子进程中运行 gulp 的任务:

    var child = require('child_process');
    var gutil = require('gulp-util');
    
    gulp.task('run', function(done) {
      var proc;
    
      function restart() {
        if (proc) {
          proc.removeListener('exit', done);
          proc.kill();
          proc = null;
        }
    
        proc = child.exec('gulp ' + gutil.env['run-tasks']);
        proc.addListener('exit', done);
        proc.stdout.pipe(process.stdout);
      }
    
      gulp.watch('gulpfile.js', function(evnt) {
        restart();
      }); 
    
      restart();
    });
    
    gulp.task('build', ...);
    gulp.task('serve', ...);
    

    您可以像这样调用它:gulp run --run-tasks "build serve"

    【讨论】:

      猜你喜欢
      • 2010-11-17
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      • 2016-09-15
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多