Gulp copying empty directories

2019-04-24 12:55发布

问题:

In my gulp build I've made a task that runs after all compiling, uglifying and minification has occurred. This task simply copies everything from the src into the dest directory that hasn't been touched/processed by earlier tasks. The little issue I'm having is that this results in empty directories in the dest directory.

Is there a way to tell the gulp.src glob to only include files in the pattern matching (like providing the 'is_file' flag)?

Thanks.

回答1:

Fixed it by adding a filter to the pipeline:

var es = require('event-stream');


var onlyDirs = function(es) {
  return es.map(function(file, cb) {
      if (file.stat.isFile()) {
        return cb(null, file);
      } else {
        return cb();
      }
  });
};
// ...

var s = gulp.src(globs)
        .pipe(onlyDirs(es))
        .pipe(gulp.dest(folders.dest + '/' + module.folder));

// ...


回答2:

I know I'm late to the party on this one, but for anyone else stumbling upon this question, there is another way to do this that seems pretty elegant in my eyes. I found it in this question

To exclude the empty folders I added { nodir: true } after the glob pattern.

Your general pattern could be such (using the variables from Nick's answer):

gulp.src(globs, { nodir: true })
    .pipe(gulp.dest(folders.dest + '/' + module.folder));

Mine was as follows:

gulp.src(['src/**/*', '!src/scss/**/*.scss', '!src/js/**/*.js'], { nodir: true })
    .pipe(gulp.dest('dev/'));

This selects all the files from the src directory that are not scss or js files, and does not copy any empty folders from those two directories either.