SuperchupuDev / tinyglobby

A fast and minimal alternative to globby and fast-glob
https://npmjs.com/package/tinyglobby
MIT License
139 stars 9 forks source link

Expose a function that returns whether a file path matches the glob #64

Open sapphi-red opened 6 days ago

sapphi-red commented 6 days ago

In Vite, when a file is updated, we check whether that file is included in the glob to know whether the file containing a glob has to be updated.

Links to relevant code https://github.com/vitejs/vite/blob/802839d48335a69eb15f71f2cd816d0b6e4d3556/packages/vite/src/node/plugins/importMetaGlob.ts#L66-L81 https://github.com/vitejs/vite/blob/802839d48335a69eb15f71f2cd816d0b6e4d3556/packages/vite/src/node/plugins/importMetaGlob.ts#L89-L106

I guess Vite cannot simply use picomatch as tinyglobby has more features. It would be nice if tinyglobby exposes a function that returns whether a file path matches the glob.

import { globMatcher } from 'tinyglobby';

const match = globMatcher(['files/*.ts', '!**/*.d.ts'], { cwd: 'src' }); // pass in the same options

const result = match('files/bar.ts') // pass the file path
expect(result).toBe(true)
SuperchupuDev commented 6 days ago

will consider adding it, meanwhile do you have any examples of previous work in other glob libraries?

sapphi-red commented 6 days ago

fast-glob uses micromatch and node-glob uses minimatch and both does not extend any features related to matching IIUC, so in these cases those libraries can be used. globby extends some features but does not provide a function like this.

ghiscoding commented 2 days ago

for an example, maybe take a look at the matcher lib and its isMatch function which returns a boolean, see their implementation here, side note, matcher was created by the same globby author.

SuperchupuDev commented 2 days ago

fast-glob uses micromatch and node-glob uses minimatch and both does not extend any features related to matching IIUC, so in these cases those libraries can be used. globby extends some features but does not provide a function like this.

just wondering, what are the exclusive features that you need that tinyglobby has but picomatch doesn't? it uses it under the hood. i can try to implement this but it might take a while until i can focus on it

sapphi-red commented 2 days ago

what are the exclusive features that you need that tinyglobby has but picomatch doesn't?

I think it's mainly braces expansion support.

SuperchupuDev commented 2 days ago

tinyglobby supports the same braces expansion picomatch does, which despite its readme saying it's not supported, in practice it actually supports nearly all usecases except for range increments {01..10..2} which to be fair i haven't ever seen used in the wild. if range increments are really needed it's probably possible to add it to tinyglobby though (with the expandRange picomatch option)