Gulp copying empty directories

一曲冷凌霜 提交于 2019-12-22 04:25:28

问题


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.



来源:https://stackoverflow.com/questions/23719731/gulp-copying-empty-directories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!