Skip to content

Commit d481df7

Browse files
authored
feat(vueuse): support directive import (#509)
1 parent 6d61712 commit d481df7

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ Supported Resolvers:
202202
- [View UI](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/view-ui.ts)
203203
- [Vuetify](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/vuetify.ts)
204204
- [VueUse Components](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/vueuse.ts)
205+
- [VueUse Directives](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/vueuse-directive.ts)
205206
- [Dev UI](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/devui.ts)
206207

207208
```ts

src/core/resolvers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export * from './veui'
1212
export * from './view-ui'
1313
export * from './vuetify'
1414
export * from './vueuse'
15+
export * from './vueuse-directive'
1516
export * from './quasar'
1617
export * from './devui'
1718
export * from './arco'
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { readFileSync } from 'fs'
2+
import { resolveModule } from 'local-pkg'
3+
import type { ComponentResolver } from '../../types'
4+
5+
let directives: string[] | undefined
6+
7+
/**
8+
* Resolver for VueUse
9+
*
10+
* @link https://github.com/vueuse/vueuse
11+
*/
12+
export function VueUseDirectiveResolver(): ComponentResolver {
13+
return {
14+
type: 'directive',
15+
resolve: (name: string) => {
16+
if (!directives) {
17+
let indexesJson: any
18+
try {
19+
const corePath = resolveModule('@vueuse/core') || process.cwd()
20+
const path = resolveModule('@vueuse/core/indexes.json')
21+
|| resolveModule('@vueuse/metadata/index.json')
22+
|| resolveModule('@vueuse/metadata/index.json', { paths: [corePath] })
23+
indexesJson = JSON.parse(readFileSync(path!, 'utf-8'))
24+
directives = indexesJson
25+
.functions
26+
.filter((i: any) => i.directive && i.name)
27+
.map(({ name }: any) => name[0].toUpperCase() + name.slice(1))
28+
}
29+
catch (error) {
30+
console.error(error)
31+
throw new Error('[vue-components] failed to load @vueuse/core, have you installed it?')
32+
}
33+
}
34+
35+
if (directives && directives.includes(name))
36+
return { name: `v${name}`, as: name, from: '@vueuse/components' }
37+
},
38+
}
39+
}

0 commit comments

Comments
 (0)