当前位置: 动力学知识库 > 问答 > 编程问答 >

gulp - How do I make my gulpfile restart when a file is edited?

问题描述:

right now I have to stop and start this gulp script to clean and rebuild my dist file and then restart the server.

My watch file is clearly wrong, what do I change for it to restart the whole gulp script when a file is edited?

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']);

网友答案:

Your glob for your watch seems to be wrong. Try this:

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

Use the exclude pattern for your dist.js to avoid an infinite loop.

Ciao Ralf

网友答案:

In addition to my comment above:

Instead of setting your default task as the 3 tasks you have listed, do it like this.

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.

    */
分享给朋友:
您可能感兴趣的文章:
随机阅读: