最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Gulp glob to ignore file types and not copy empty folders - Stack Overflow

programmeradmin2浏览0评论

I have created a glob for gulp which ignores javascript and coffeescript files within a set of directories. I'd like it to copy all other files into a directory which works fine. The only problem is that when there are only javascript or coffeescript files it copies an empty folder. Any ideas how this glob could be amended to not copy empty folders?

gulp.task('copyfiles', function(){
    gulp.src('apps/*/static_src/**/!(*.js|*.coffee)')
        .pipe(gulp.dest('dest'));
});

Example source files:

apps/appname/static_src/images/image.jpg
apps/appname/static_src/js/script.js

Expected output:

dest/static_src/images/image.jpg

Current output:

dest/static_src/images/image.jpg
dest/static_src/js/

I have created a glob for gulp which ignores javascript and coffeescript files within a set of directories. I'd like it to copy all other files into a directory which works fine. The only problem is that when there are only javascript or coffeescript files it copies an empty folder. Any ideas how this glob could be amended to not copy empty folders?

gulp.task('copyfiles', function(){
    gulp.src('apps/*/static_src/**/!(*.js|*.coffee)')
        .pipe(gulp.dest('dest'));
});

Example source files:

apps/appname/static_src/images/image.jpg
apps/appname/static_src/js/script.js

Expected output:

dest/static_src/images/image.jpg

Current output:

dest/static_src/images/image.jpg
dest/static_src/js/
Share Improve this question edited Feb 26, 2016 at 15:03 Sven Schoenung 30.6k8 gold badges67 silver badges70 bronze badges asked Feb 26, 2016 at 14:30 AdamAdam 2094 silver badges12 bronze badges 1
  • Possible duplicate of Gulp copying empty directories – Sven Schoenung Commented Feb 26, 2016 at 14:43
Add a ment  | 

2 Answers 2

Reset to default 9

Since gulp.src accepts almost the same options as node-glob, you can add nodir: trueas an option:

gulp.src('apps/*/static_src/**/!(*.js|*.coffee)', { nodir: true })

This will preserve the dir structure from src, but omit empty ones.

gulp.task('copyfiles', function(){
    gulp.src(['apps/*/static_src/**/*','!apps/*/static_src/{js/, js/**}'])
        .pipe(gulp.dest('dest'));
});

I think you need a pattern '!apps/*/static_src/{js/, js/**}' that matches the directory as well as the files inside to prevent ommiting an empty directory. I am not sure if there is a pattern to match a directory only by specifying its content.

发布评论

评论列表(0)

  1. 暂无评论