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

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

问题描述

我有一个使用gulp-imagemin来压缩图像的吞食任务。当我将新文件添加到此目录时,我希望此任务也可以压缩它们。我读了 gulp.watch不会触发新的文件,我应该尝试吞咽手表,所以我使用它像这样;

  gulp.task('images ',function(){
watch({glob:'./source/images/*'},函数(文件){
返回文件
.pipe(plumber())
.pipe(imagemin({
progressive:true,
interlaced:true
}))
.pipe(gulp.dest('./ www'));
});
});

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



gulp-watch上的文档称为批处理模式,并表示我也可以在每个文件的基础上运行任务,所以我也试过这种方式;

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

但没有任何变化。为什么不添加文件到我的图像目录触发任务?

解决方案

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

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

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

但是,如果您有空的图片目录,这种填充不会修复大小写。如果你想观看它们,把 ['images','images / ** / *'] 传给glob,它会监视最初为空的目录。 p>

Ps在这种情况下,您也不需要 gulp-plumber ,因为watch会重新运行函数,每次都会使用imagemin,即使imagemin弹出错误时也是如此。

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

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.

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?

解决方案

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

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.

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.

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

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