专门用来匹配文件和目录然后输出路径的库,是扩展于glob库
glob:https://github.com/mrmlnc/fast-glob#options-3
globby:https://github.com/sindresorhus/globby#readme
1、下载
cnpm install -D globby
2、使用
const globby = require('globby');
异步(Promise方式):
const res=await globby(['要处理的文件正则表达式','可以有多个'],{
dot:true, 匹配.开头的路径
})
匹配正则格式(大部分符合js正则规范):
匹配的路径只能是'/'而不能是'\',path.posix.join可将'\'变成'/',path.posix.join('ww'.'aa\bb'); 但是aa\bb里的\会被忽略
* 匹配除斜杠(路径分隔符)、隐藏文件(以.开头的名称)之外的所有内容。
** 匹配零个或多个目录。
? 匹配除斜杠(路径分隔符)以外的任何单个字符。
[[:digit:]] 匹配数字
(a|b) 匹配a或b
例子:
src/**/*.js — matches all files in the src directory (any level of nesting) that have the .js extension.
src/*.?? — matches all files in the src directory (only first level of nesting) that have a two-character extension.
file-[01].js — matches files: file-0.js, file-1.js.
src/**/*.{css,scss} — matches all files in the src directory (any level of nesting) that have the .css or .scss extension.
file-[[:digit:]].js — matches files: file-0.js, file-1.js, …, file-9.js.
file-{1..3}.js — matches files: file-1.js, file-2.js, file-3.js.
file-(1|2) — matches files: file-1.js, file-2.js.
参数:
同步:
globby.sync(...)
代码示例:
const absPath=path.isAbsolute(from) ? from : path.posix.join('D:/webpack_code/webpack4/webpack_自定义',from);
//读取from中除了忽略的文件
const res = await globby([absPath], { ignore:'**/index.html'})