nuxt / nuxt

The Intuitive Vue Framework.
https://nuxt.com
MIT License
54.47k stars 4.98k forks source link

Scan index files within subdirectories for composables, utils and middlewares #25831

Open enkot opened 8 months ago

enkot commented 8 months ago

Describe the feature

A common practice is to separate the logic between files and keep the test files together with the code. I suggest to scan index files within the subdirectories for composables, utils and middlewares by default, how it is currently done for modules and plugins (but is deprecated https://github.com/nuxt/nuxt/issues/25331 😞 ), where the auto-registered files patterns are:

modules/*/index.ts
modules/*.ts

Motivation:

I recently created another Nuxt project and without suspecting it, created a folder with an index file inside composables, but of course it did not work. So I had to do what I do for all new Nuxt projects::

  dirs: [
    'composables/*/*.ts',
    'utils/*/*.ts'
  ],

Which seems unnatural and, to me, doesn't really fit with Nuxt's title β€” "The Intuitive Vue framework." The typical folders structure I usually work with:

composables/
  useHost/
    tests
    index.ts
  useMask/
    tests
    index.ts
...

Additional information

Final checks

manniL commented 8 months ago

This behavior is documented. IMO the current scanning behavior allows your folders, e.g. composables, to (re-)export all composables which should be auto-importable on the top level while keeping utility composables on a lower level. This could be used to group code better and avoid end-user mistakes by e.g. accidentally auto-importing composables you do not want to use without more context ☺️

Same applies to all other folders.

enkot commented 8 months ago

IMO the current scanning behavior allows your folders, e.g. composables, to (re-)export all composables which should be auto-importable on the top level while keeping utility composables on a lower level.

It is for this reason that I suggest changing the behavior so that the user doesn't have to worry about re-exports or workarounds (like dirs: ['composables/*/*.ts']) πŸ™‚ This could be used to group code better and avoid end-user mistakes by e.g. accidentally auto-importing composables you do not want to use without more context

To avoid such situations we can import from the index file only and not scan any other files inside the directory.