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

Allow named export instead of default export for components #14806

Open
raythurnvoid opened this issue Dec 23, 2024 · 4 comments
Open

Allow named export instead of default export for components #14806

raythurnvoid opened this issue Dec 23, 2024 · 4 comments

Comments

@raythurnvoid
Copy link

raythurnvoid commented Dec 23, 2024

Describe the problem

Today in order to use named export with .svelte components we have to create a separate .ts file and re-export the component and perhaps the type of the Props (just to import all stuff from the same module)

export { default as Heading100 } from './Heading100.svelte';
export type { Heading100Props } from './Heading100.svelte';

It's a little bit annoying, and it would be cool to have a more sveltish way of doing it, perhaps something like this:

<script lang="ts" named-export>

It would take the name of the file and export a symbol with the same value.

An alternative syntax can be:

<script lang="ts" export="MyComponent">

Another approach would be a configuration in the svelte.config.js.

Why?

It helps refactoring and code maintainability.

Let's say you want to deprecate a component and you want to change its name to something like MyComponentOld, it's frustrating that the component would still appear in the templates as <MyComponent /> and there's no way to do a global rename with refactoring tool, so the only option is text search and replace.

It helps maintainability because you ensure that in most of the cases the tag used in the templates matches the component name unless explicitly renamed with as, sometime unorganized devs write stuff like

import Button from '$lib/component/SparklingButton.svelte';
import Button2 from '$lib/components/BorderedButton.svelte';

Describe the proposed solution

Above

Importance

nice to have

@webJose
Copy link
Contributor

webJose commented Dec 23, 2024

I'm not openly against something like this. I just wonder about the reasons.

For deprecating: This should be mainly a concern for libraries, not projects. Projects don't share stuff with other projects, that's the job of libraries. In libraries, you usually export using named exports in the NPM package's index.js file. In projects, you just replace one component for another and you're done.

True, maybe it is better/more accurate to be able to do a "global rename" command, but I bet "Replace in files..." is almost as good.

Finally, trying to force a particular name on consumers is an opinion, and I believe it is Svelte's mission to be as unopinionated as possible. It is an opinion because it is not a performance hit, using as to rename is not a non-standard JavaScript feature, and is not a generally accepted bad practice, to the best of my knowledge.

Other than the above, I'd be OK with this, so long the default export is an option. I don't feel like changing it. If this is an opt-in feature, fine by me.

@Thiagolino8
Copy link

Thiagolino8 commented Dec 23, 2024

You can also re-export it from the script module

<script module>
  export {default as whatever} from './same-file.svelte'
</script>

@raythurnvoid
Copy link
Author

Projects don't share stuff with other projects, that's the job of libraries. In libraries, you usually export using named exports in the NPM package's

@webJose My use case are just in projects, it happens that you need to create a v2 component to not cause regressions everywhere in the project

You can also re-export it from the script module

<context module>
  export {default as whatever} from './same-file.svelte'
</script>

@Thiagolino8 This is interesting, vscode auto-import may get a bit confusing since you don't know if you are importing the named or the default export.

image

@brunnerh
Copy link
Member

I am also not a fan of default imports/exports.

If this feature is added it probably belongs in <svelte:options> and may require a value.

<svelte:options export="MyComponent" />

People use all sorts of file names (e.g. kebab-case.svelte) and while there is some algorithm implemented in the Svelte language tools that automatically turns them into PascalCase in the auto-imports, that conversion logic is not formally specified and it is probably better to be explicit anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants