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

feat(resolver): enable namespaced components #715

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/content/overview/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Radix Vue also has resolver for the popular [unplugin-vue-components](https://gi

In `vite.config.ts`, import `radix-vue/resolver`, and configure as such and it will auto-imports all the components from Radix Vue.

You can also use it for [Namespaced components](../guides/namespaced-components.md).

```ts{2,10 }
import Component from 'unplugin-vue-components/vite'
import RadixVueResolver from 'radix-vue/resolver'
Expand All @@ -46,7 +48,8 @@ export default defineConfig({
RadixVueResolver()

// RadixVueResolver({
// prefix: '' // use the prefix option to add Prefix to the imported components
// prefix: '', // use the prefix option to add Prefix to the imported components
// namespaced: false // set to true if you want to use namespaced components
// })
],
}),
Expand Down
21 changes: 16 additions & 5 deletions packages/plugins/src/resolver/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type ComponentResolver } from 'unplugin-vue-components'
import { components } from '../../../radix-vue/constant/components'
import * as NamespacedComponents from '../namespaced'

export interface ResolverOptions {
/**
Expand All @@ -8,20 +9,30 @@ export interface ResolverOptions {
* @defaultValue ""
*/
prefix?: string
/**
* Enable the use of namespaced components
*
* @defaultValue false
*/
namespaced?: boolean
}

export default function (options: ResolverOptions = {}): ComponentResolver {
const { prefix = '' } = options
const { prefix = '', namespaced = false } = options

return {
type: 'component',
resolve: (name: string) => {
if (name.toLowerCase().startsWith(prefix.toLowerCase())) {
const componentName = name.substring(prefix.length)
if (Object.values(components).flat().includes(componentName)) {
const componentName = name.split('.')[0]
const importName = componentName.substring(prefix.length)
const isNamespacedComponent = Object.keys(NamespacedComponents).includes(importName)
const isComponent = Object.values(components).flat().includes(importName)
if (isNamespacedComponent || isComponent) {
return {
name: componentName,
from: 'radix-vue',
name: importName,
as: componentName,
from: isNamespacedComponent && namespaced ? 'radix-vue/namespaced' : 'radix-vue',
}
}
}
Expand Down