Skip to content

Private Components #9297

Closed as not planned
Closed as not planned
@olafurw

Description

@olafurw

Describe the problem

Components in Svelte are relatively straight forward, which is nice. There's a file, in the file is the component and everything you need to render it.

But as I've been working with a larger and larger application written in Svelte another concept keeps cropping up. Private components.

Most components are designed in a way where they can be used anywhere, Buttons for example. But often a component is designed to be used inside/with another component, a ListItem component inside a List component or a PageTitle component next to a PageBody component.

Importing a ListItem component by itself works but most of the time doesn't make sense and avoiding that problem would be nice.

Describe the proposed solution

The other issue tickets I've seen are about inheritance or supporting multiple components in one file. But I feel there is a simpler way if you just want private components.

Usually how projects structure components that are connected is to create a folder that would then contain all of them. By leveraging that idea we could try a couple of ways:

  • ListItem.private.svelte can only be imported by files in the same folder.
  • Any component inside of a folder called +private can only be imported by components that are siblings of the +private folder.

FYI. These are just suggested solutions, all names are subject to bikeshedding.

This would then leverage the same or similar methods that check for server files imports in client only files. This should also not impact existing code.

Alternatives considered

There are two other discussion threads I have found.

Multiple components in one file #2940
Component inheritance #192

Also saw this video recently and it looked promising initially but even though you've created a more visual hierarchy, the child components can still be imported in other places.

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions