Skip to content

How do we add prefix for all components? #438

Closed
@pbtkhoa

Description

@pbtkhoa

I'm trying to add prefixes to all components. And I can't find how to do it?
Could you somehow add it as a feature?
Thanks a lot

Activity

kasperskov-pdk

kasperskov-pdk commented on Sep 1, 2022

@kasperskov-pdk

Would be nice.
I tried

 resolvers: [
                        (name) => {
                            if (!name.startsWith('prefix')) {
                                console.log(name + ' is missing prefix');
                                return { name: 'prefix' + name };
                            }
                        }
                    ]

Does not work. The resolver is only for resolving the path.

RuBaWa

RuBaWa commented on Oct 14, 2022

@RuBaWa

Do you have found a solution?

I also want to prefix all my components. For example:

WButton should resolve src/components/Button.vue

Veseron

Veseron commented on Oct 16, 2022

@Veseron

The returned object also needs a "from" property.
But I can't figure out how to do it right.

acibilous

acibilous commented on Jun 14, 2023

@acibilous

any updates?

simonmaass

simonmaass commented on Jun 7, 2024

@simonmaass

did anyone get this to work?

craigrileyuk

craigrileyuk commented on May 3, 2025

@craigrileyuk

did anyone get this to work?

It requires some coding changes for this to work.

The way component resoution works is that a component map is generated from your dirs and if nothing matches that map, then it uses the custom resolvers.

There's no options, transforms, filters or callbacks that have any ability to affect this component map creation.

Technically speaking, for an MVP, all you would need to do is insert a prefix here:

this._componentNameMap[name] = {

before the name variable.

There's a pull request (#612) that's been languishing for over 2 years, so I doubt anything is ever going to happen in implementing this feature.

There's a pretty easy workaround though... just put all your components inside a subfolder with your prefix as its name and then enable directoryAsNamespace.

userquin

userquin commented on May 3, 2025

@userquin
Member

You can create a custom resolver adding the prefix option, check this resolver to allow prefixing all Vuetify components using VuetifyXXX instead VXXX (we only allow prefix as a boolean, but you can allow any prefix, the logic in that case should be changed):

https://github.com/userquin/unvuetify-monorepo/blob/main/packages/unplugin-vue-components-resolvers/src/index.ts#L62-L96

craigrileyuk

craigrileyuk commented on May 3, 2025

@craigrileyuk

The problem with using a custom resolver is that you lose access to all the filters like directoryAsNamespace et al.

You have to reimplement everything yourself.

It’d be far preferable to be able to specify a prefix for components in the vite plug-in config.

userquin

userquin commented on May 3, 2025

@userquin
Member

then, why do you need to add a prefix?

craigrileyuk

craigrileyuk commented on May 3, 2025

@craigrileyuk

Asking “why” is a strange question for software development.

A plug-in like this has thousands of users with complex, unpredictable and varied needs in infinite different contexts.

“Why not” is always the better question.

Personally, it’s a DX issue where it helps to quickly know where a component is coming from if I’m using them from many different sources in the same project.

But others might have more functionality-based needs.

userquin

userquin commented on May 3, 2025

@userquin
Member

The problem with using a custom resolver is that you lose access to all the filters like directoryAsNamespace et al.

directoryAsNamespace should only be applied to your local components not to third party dependencies, that's why I'm asking why

craigrileyuk

craigrileyuk commented on May 3, 2025

@craigrileyuk

Because you suggested using a custom resolver to add a prefix to local components…

userquin

userquin commented on May 4, 2025

@userquin
Member

Oh, sorry, this issue is about prefixing local components without using directory as namespace option, I will try to add the prefix feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @userquin@pbtkhoa@Veseron@acibilous@simonmaass

      Issue actions

        How do we add prefix for all components? · Issue #438 · unplugin/unplugin-vue-components