为什么新添加的文件不会触发我的 gulp-watch 任务? [英] Why don't newly added files trigger my gulp-watch task?

查看:17
本文介绍了为什么新添加的文件不会触发我的 gulp-watch 任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 gulp 任务,它使用 gulp-imagemin 来压缩图像.当我向这个目录添加新文件时,我希望这个任务也能压缩它们.我读到 gulp.watch 不会触发 new文件,我应该尝试gulp-watch,所以我像这样使用它;

I have a gulp task which uses gulp-imagemin to compress images. When I add new files to this directory I'd like for this task to compress them as well. I read that gulp.watch doesn't trigger on new files and that I should try gulp-watch so I used it like so;

gulp.task('images', function() {
  watch({glob: './source/images/*'}, function (files) {
    return files
      .pipe(plumber())
      .pipe(imagemin({
        progressive: true,
        interlaced: true
      }))
      .pipe(gulp.dest('./www'));
  });
});

这与第一次运行时的 gulp.watch 相同,但是当我将新图像添加到目录时没有任何反应.但是,如果我覆盖现有文件,它会再次运行该任务,因此它的行为会有所不同.

This works the same as gulp.watch on the first run, but when I add a new image to the directory nothing happens. If I overwrite an existing file however, it DOES run the task again, so it does behave differently.

gulp-watch 上的文档称此为批处理模式",并说我也可以按文件运行任务,所以我也尝试过这种方式;

The documentation on gulp-watch called this "Batch Mode" and said I could also run the task on a per-file basis, so I tried this way too;

gulp.task('images', function() {
  gulp.src('./source/images/*')
    .pipe(watch())
    .pipe(plumber())
    .pipe(imagemin({
      progressive: true,
      interlaced: true
    }))
    .pipe(gulp.dest('./www'));
});

但没有任何改变.为什么向我的图像目录添加文件不会触发任务?

But nothing changed. Why isn't adding files to my image directory triggering the task?

推荐答案

这类问题很可能被重定向到 gaze 包及其内部进程,在您的操作系统上运行复杂的监视程序.在这种情况下,您应该将 images/**/* 传递给 glob 选项,因此凝视将查看 images 目录中的所有(包括新)文件:

Most likely such kind of questions are redirected to gaze package and its internal processes, that runs complicated watching procedures on your OS. In this case you should pass images/**/* to glob option, so gaze will watch all (including new) files in images directory:

var gulp = require('gulp');
var watch = require('gulp-watch');
var imagemin = require('gulp-imagemin');

gulp.task('default', function() {
    watch({glob: 'images/**/*'}, function (files) {
      files.pipe(imagemin({
        progressive: true,
        interlaced: true
      }))
      .pipe(gulp.dest('./www'));
    });
});

但是当你有空的图像目录时,这个填充不能解决问题.如果您想观看它们,请将 ['images', 'images/**/*'] 传递给 glob,它将观看最初为空的目录.

But this fill not fix case, when you have empty images directory. If you want to watch them, pass ['images', 'images/**/*'] to glob, and it will watch directory, that initially empty.

附:在这种情况下你也不需要 gulp-plumber,因为 watch 会重新运行函数,每次都会使用 imagemin,即使 imagemin 弹出错误.

P.s. also you dont need gulp-plumber in this case, because watch will rerun function, that uses imagemin every time, even when imagemin pops an error.

这篇关于为什么新添加的文件不会触发我的 gulp-watch 任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆