base
or cwd
option for import.meta.glob
#17453
Labels
enhancement
New feature or request
good first issue
Good for newcomers
p2-nice-to-have
Not breaking anything but nice to have (priority)
Description
Today,
import.meta.glob
returns an object whose keys are relative paths (unless using an alias, in which case the behaviour is slightly unexpected — #12180):Working with this object is slightly cumbersome. If you want a specific module, you have to do this...
...and if you're iterating over the keys you have to do some ugly string manipulation:
If you're trying to make that logic reusable, you have to pass around a prefix — this is brittle, because you could easily change the argument to
glob
(because you moved some files) but forget to updatebase
:Suggested solution
A straightforward solution that would result in cleaner code and that could (I think?) be implemented without a breaking change could be to add a
base
option to theglob
arguments:Every globbing library (including
fast-glob
which Vite uses IIUC) has some version of this — the option is usually calledcwd
, and as such there's an argument for calling itcwd
rather thanbase
. The counterargument is thatcwd
is always resolved relative to the actual cwd, rather than the file in which it's used, and so maybe there's a slight conceptual mismatch. I think either would be fine.Alternative
Taking the idea further, it could be
prefix
andsuffix
(i.e. allowing you to automatically remove suffixes like.md
), though I suspect that's unnecessary complexity.Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: