gulp.src() include files but ignore all folders

有些话、适合烂在心里 提交于 2019-12-23 14:19:01

问题


There is certainly such a simple answer to this that I can't find anyone that's asked it before:

What globbing pattern to include all the files in a folder but ignore ALL subfolders?

gulp.src('*') includes all files and folders. I just want the files and would rather not have to exclude the folders individually.


回答1:


If you want to include .dotFiles as well then try

gulp.src(['folder/*.*', 'folder/.*'])



回答2:


Just use the nodir option when you call gulp.src. This will actually test the files being read off the filesystem instead of relying on conventions that may hold today but not tomorrow.

In the following example, the debug() in the dir target will output files and directories. The debug() in the nodir target will output only files because it uses nodir. Populate the directory foo with files and subdirectories and you'll see the difference.

var gulp = require("gulp");
var debug = require("gulp-debug");

gulp.task("dir", function () {
    return gulp.src("foo/**")
        .pipe(debug());
});

gulp.task("nodir", function () {
    return gulp.src("foo/**", { nodir: true })
        .pipe(debug());
});

If you want to have one part of the tree include directories and another part exclude them then you can merge streams:

var es = require("event-stream");
gulp.task("nodir-multi", function () {
    return es.merge(gulp.src("foo/**", { nodir: true }),
                    gulp.src("bar/**"))
        .pipe(debug());
});

I don't think the convenience of having just one gulp.src call justifies relying on *.* and .* to exclude directories. Version control systems often create directories what would match .* (e.g. .git, .svn). Development tools also do the same (e.g. .deps, .tmp.foo).




回答3:


You can try

gulp.src('folder/*.*')

This won't match dot files however.



来源:https://stackoverflow.com/questions/32826200/gulp-src-include-files-but-ignore-all-folders

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