【问题标题】:How do I make my gulpfile restart when a file is edited?编辑文件时如何让我的 gulpfile 重新启动?
【发布时间】:2014-08-26 13:14:15
【问题描述】:

现在我必须停止并启动这个 gulp 脚本来清理和重建我的 dist 文件,然后重新启动服务器。

我的 watch 文件明显错了,当文件被编辑时,我应该改变什么来重新启动整个 gulp 脚本?

var gulp = require('gulp');
var connect = require('gulp-connect');
var concat = require('gulp-concat');
var clean = require('gulp-clean');

gulp.task('clean', function() {
  return gulp.src('app/scripts/dist.js').pipe(clean());
});

gulp.task('scripts', ['clean'], function(){
    gulp.src(['app/scripts/app.js', 'app/scripts/**/*.js', 'app/scripts/**/*/*.js'])
    .pipe(concat('app/scripts/dist.js'))
    .pipe(gulp.dest('.'));
});

gulp.task('webserver', function() {
  connect.server();
});


gulp.task('watch', ['scripts'], function(){
    gulp.watch('app/scripts' + '*.js', ['scripts']).on('change', function(evt) {
        changeEvent(evt);
    });
});


gulp.task('default', ['clean','scripts','webserver']);

【问题讨论】:

  • 嗯...这可能是Grant's tomb 的答案...但changedEvent 似乎不存在。如果真是这样,我很惊讶它没有抛出错误。

标签: gulp restart gulp-watch


【解决方案1】:

您的手表 glob 似乎有误。试试这个:

gulp.watch(['app/scripts/**/*.js', '!app/scripts/dist.js'], ['scripts']).on('change', function(evt) {
    changeEvent(evt);
});

为您的dist.js 使用排除模式以避免无限循环。

Ciao 拉尔夫

【讨论】:

    【解决方案2】:

    除了我上面的评论:

    不要将默认任务设置为您列出的 3 个任务,而是这样做。

    gulp.task('watch',function(){
        var scripts=gulp.watch(//scripts array,['scripts']);
        scripts.on('change,function(evt){
             changedEvt(evt) // remember to make sure this exists.
        });
    
    gulp.task('default',['watch']);
    
        /* make sure to include watches for all the logic you want to get 
           executed within your watch tasks (similar to how I did it)
    
          #== That way, you can still call those tasks from a different shell prompt
              if you want, but they are set to always be executed when you 
              modify the related files.
    
        */
    

    【讨论】:

      猜你喜欢
      • 2010-10-27
      • 2014-05-18
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多