Gulp glob to ignore file types and not copy empty folders

别说谁变了你拦得住时间么 提交于 2019-12-22 06:25:10

问题


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/

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/35654317/gulp-glob-to-ignore-file-types-and-not-copy-empty-folders

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