Open SuperchupuDev opened 1 month ago
Knip does something similar, but just for .gitignore
files only. Pointers:
The key is to have functionality like this deepFilter
. tinyglobby uses fdir
which has filter
which is maybe what we need here?
Scratch that, I mixed up filter
and exclude
.
Still, the deepFilter
example might be useful as I think it does the same as fdir#exclude
.
I'm not even sure how to approach the problem, but implementing this means that all of the weird patterns that currently avoid all optimizations would be really optimized along with literally everything else.
By default,
fdir
crawls all subdirectories and files of a root, which can result in extra processing work that's not necessary, harming performance.tinyglobby
tries to apply some optimizations by inferring a common root.fdir
exposes aexclude
function that can be used to exclude directories from crawling. It's being currently used on theignore
patterns to... not crawl those ignored patterns?What if, we took the matching patterns (basically the patterns that aren't meant to be ignored), we did some weird transformations to them, and used them in the
exclude
matcher?For example, let's say we have the following usage:
with the following file structure:
Basically, we need a picomatch matcher that returns
true
for every directory that we don't want to crawl, in this casenode_modules
,plugins
,scripts/utils
, andsrc/other
. This could be implemented with the following picomatch usage:Great! It now only crawls the directories needed (hopefully, I haven't checked). Now the question is how to implement something that converts
['src/files/index.ts', 'scripts/*.ts'
into['src/files', 'scripts/*/**']
, which is the whole point of this issue. If we figure it out,tinyglobby
should be nearly as fast as possible.Some notes:
<pattern>/**
patterns work. It not only matches subdirectories of<pattern>
, but it also matches<pattern>
itself. this is why the second processed pattern in the example isn'tscripts/**
, as that would matchscripts
makingfdir
not crawl it.normalizePattern
function, so that we don't need to loop through the patterns extra times.