NodeJS 节点脚本中Glob的忽略选项的相对路径问题

weylhg0b  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(131)

我有这样的项目结构:

.
├── index.js
└── bootstrap/
    ├── css/
    │   ├── style.css
    │   └── fake.js
    └── js/
        └── index.js

我正在尝试查找bootstrap文件夹下的所有JS文件,忽略css文件夹。我使用下面的glob代码:

index.js

const { globSync } = require('glob');

const files = globSync([`./bootstrap/**/*.js`], {
  ignore: [`bootstrap/css/**`],
});

files.forEach(file => {
  console.log(file); // outputs bootstrap/js/index.js
})

它按预期工作,并且由于ignore属性,仅检索bootsrap/js目录下的JS文件。
但是如果我把ignore属性改为相对路径

ignore: [`./bootstrap/css/**`],

它不再排除css文件夹:

const { globSync } = require('glob');

const files = globSync([`./bootstrap/**/*.js`], {
  ignore: [`./bootstrap/css/**`],
});

files.forEach(file => {
  console.log(file); // outputs bootstrap/js/index.js and bootstrap/css/fake.js
})

有什么问题吗?为什么我不能使用相对路径忽略属性?

wgmfuz8q

wgmfuz8q1#

glob软件包感觉过时了,在新版本的node上不能像预期的那样工作。较新的代码使用fast-glob,它可以使用ignore和否定模式:

import glob from 'fast-glob';

const files = await glob([`./bootstrap/**/*.js`], {
  ignore: [`./bootstrap/css/**`],
});

console.log('ignore:', files);

const filesByNegation = await glob([`./bootstrap/**/*.js`, `!./bootstrap/css/**`]);

console.log('negation:', filesByNegation);

https://replit.com/@silentmantra/Glob-Test#index.js

相关问题