简体   繁体   English

Gulp 保存文件后手表继续运行

[英]Gulp watch keeps running after I save the file

I installed gulp to minify my JS file.我安装了 gulp 来缩小我的 JS 文件。 When I run gulp watch the watcher is waiting for the file to change, so that's good.当我运行gulp watch时,观察者正在等待文件更改,这很好。 However, when I modify the file and save it, the watch task is not fired only once, but it keeps getting fired, and it's not stopping.但是,当我修改文件并保存它时,监视任务不会只触发一次,而是一直被触发,并且没有停止。 I have to manually stop the task by hitting ctrl + c .我必须通过点击ctrl + c手动停止任务。

This is my gulpfile.js file:这是我的gulpfile.js文件:

var gulp = require('gulp');
var minify = require('gulp-minify');

gulp.task('min-js', function() {
    return gulp.src('lib/app.js')
        .pipe(minify({
            ext: {
                min: '.min.js'
            },
            ignoreFiles: ['-min.js']
        }))
        .pipe(gulp.dest('lib'))
});

gulp.task('watch', function(){
  gulp.watch('lib/app.js', gulp.parallel('min-js'));
  // Other watchers
});

gulp.task('default', gulp.parallel('min-js', 'watch'));

gulp-minify outputs the source files in addition to the minified files. gulp-minify除了输出压缩文件之外的源文件。

See the documentation here .请参阅此处的文档。

Since the output is being piped into the same directory as the input, lib , the watcher sees that the input file was overwritten, and triggers again.由于 output 被传送到与输入lib相同的目录中,因此观察者看到输入文件被覆盖,并再次触发。 This causes an infinite loop of watcher triggering minification.这会导致观察者触发缩小的无限循环。

Solutions:解决方案:

  1. Change the output directory to be different from the input directory.将 output 目录更改为与输入目录不同。
  2. Use the noSource option on gulp-minify to prevent the output of source files.使用 gulp gulp-minify minify 上的noSource选项来防止源文件的 output。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM