Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not recommend default exports #847

Merged
merged 2 commits into from
Nov 20, 2023
Merged

Conversation

EvgenyOrekhov
Copy link
Owner

@EvgenyOrekhov EvgenyOrekhov commented Nov 18, 2023

export default should be banned completely, but unfortunately many frameworks rely on it.

Even though import/no-default-export rule remains disabled, I recommend avoiding default exports, if possible.

Reasons:

  1. If default exported name doesn't match file name
    1. autocomplete doesn't work
    2. confusion increases
  2. If a module has both named exports and a default export
    1. confusion increases
  3. You can export the same thing as a named export and as a default export
    1. if you want to increase clutter and confusion, sure why not?
  4. If you used default export but then had to export another thing, you either have to use named exports together with a default export, or you have to change default export to a named export
    1. if you love refactoring...
  5. If declaration and default export are separate
    1. good luck connecting the dots
  6. Have to use .default in some cases (like with dynamic import())
  7. You can accidentally use a different name in each import site
    1. I don't blame you

Ref #846 Ban default exports

Reasons:

1. If default exported name doesn't match file name
   1. autocomplete doesn't work
   2. confusion increases
2. If a module has both named exports and a default export
   1. confusion increases
3. You can export the same thing as a named export and as a default export
   1. if you want to increase clutter and confusion, sure why not?
4. If you used default export but then had to export another thing, you either
   have to use named exports together with a default export, or you have to
   change default export to a named export
   1. if you love refactoring...
5. If declaration and default export are separate
   1. good luck connecting the dots
6. Have to use `.default` in _some_ cases (like with dynamic `import()`)
7. You can accidentally use a different name in each import site
   1. I don't blame you
@EvgenyOrekhov
Copy link
Owner Author

Hello @alexkramer98!

Would appreciate your input on default exports in Vue 3/Nuxt 3.

I can see at least one rule that requires you to use export default - vue/require-direct-export.

  1. Is vue/require-direct-export rule valid for single-file components only? Or for other types of components and Vue modules as well?
  2. What about Nuxt? Does it recommend using export default?
  3. Should we just disable the import/no-default-export rule for Vue 3/Nuxt 3 entirely, or only for some specific files/folders?

@alexkramer98
Copy link
Contributor

Hi!
Off the top of my head, the default export is only a convention in Vue files. For other files, such as composables, it's optional. This is the case for both Vue and Nuxt. There might be other exceptions that I'm overlooking right now so I'll try to get back to you about that tomorrow.

@alexkramer98
Copy link
Contributor

alexkramer98 commented Nov 19, 2023

I haven't found any other exceptions when it comes to Vue. With Nuxt, for the server directory (api, middleware, routes, plugins), they do use a default export in the documentation, but not for server/utils for example.

For both Vue and Nuxt, single file components can either be defined using the composition api or the options api. The composition api uses no export at all, and the options api uses a default export.

I would recommend to either disable import/no-default-export for Vue/Nuxt all together, or to disable the rule for .vue files and the server directory. I believe vue/require-direct-export is appropriate where the default export is used.

@EvgenyOrekhov EvgenyOrekhov changed the title Fix #846 Ban default exports Do not encourage default exports Nov 20, 2023
@EvgenyOrekhov EvgenyOrekhov changed the title Do not encourage default exports Do not recommend default exports Nov 20, 2023
@EvgenyOrekhov EvgenyOrekhov merged commit 0711c34 into master Nov 20, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants