unjs/unimport

`scanDirs` is not respecting the priority order of auto-imports

yassilah opened this issue · 2 comments

I am wondering why the current implementation of the scanDirExports is sorting the list of files after normalizing them. This is a major drawback as it does not allow you to set an explicit order of directories to scan and therefore of exports to override in case there's another one.

Example

When using Nuxt 3 auto-imports, I would expect this option:

imports: {
   dirs: ['foo', 'bar']
}

to let exports from bar override exports from foo, but since the filenames are sorted, exports from foo will always override exports from bar.

I can make a simple PR to prevent that sorting but I'm not exactly sure why it was there in the first place?

Thanks for your amazing work 🙏

antfu commented

We use glob to search for those files, and glob does not guarantee the order of the output. We are doing the sort to normalize the behavior across different OS and environments. Technically we would expect users should avoid duplication in auto imports. But I also agree your thoughts seem more intuitive. To do that, we need to glob multiple times for each dirs, sort them in place, and merge the list with dedupe. PR welcome if you want to do that. Thanks.

Just made a quick PR and added a test, hope that helps!