Skip to content
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
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
- Auto import register APIs for Vite, Webpack or esbuild powered by [unplugin](https://github.com/unjs/unplugin)
- TypeScript declaration file generation
- Auto import for custom APIs defined under specific directories
- Auto import for Vue template
- Built-in presets for common libraries: check [presets](./src/presets) folder
- Auto import for Vue template and directives

## Install

Expand Down Expand Up @@ -423,6 +424,33 @@ Unimport.vite({
})
```

#### Built-in Preset for VueUse Core Directives

You can use the built-in VueUse Core directives, you will need:
- install `@vueuse/core` and `@vueuse/components`

You can add the preset via the built-in preset:
```ts
Unimport.vite({
presets: ['@vueuse/core/directives'],
addons: {
vueDirectives: true
}
})
```

or via preset factory to change the directives prefix:
```ts
import { VueUseCoreDirectives } from 'unimport'

Unimport.vite({
presets: [VueUseCoreDirectives({ prefix: true })],
addons: {
vueDirectives: true
}
})
```

## 💻 Development

- Clone this repository
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export {
resolveBuiltinPresets,
resolvePreset,
} from './preset'
export { builtinPresets } from './presets'
export type { BuiltinPresetName } from './presets'
export { builtinPresets, VueUseCoreDirectives } from './presets'
export type { BuiltinPresetName, VueUseCoreDirectivesOptions } from './presets'

export * from './regexp'

Expand Down
5 changes: 5 additions & 0 deletions src/presets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ import vueMacros from './vue-macros'
import vueRouter from './vue-router'
import vueRouterComposables from './vue-router-composables'
import vueuseCore from './vueuse-core'
import { VueUseCoreDirectives } from './vueuse-core-directives'
import vueuseHead from './vueuse-head'
import vuex from './vuex'

export const builtinPresets = {
'@vue/composition-api': vueCompositionApi,
'@vueuse/core': vueuseCore,
'@vueuse/core/directives': VueUseCoreDirectives,
'@vueuse/head': vueuseHead,
'pinia': pinia,
'preact': preact,
Expand Down Expand Up @@ -64,4 +66,7 @@ export const builtinPresets = {
'date-fns': dateFns,
}

export { VueUseCoreDirectives }

export type BuiltinPresetName = keyof typeof builtinPresets
export type { VueUseCoreDirectivesOptions } from './vueuse-core-directives'
52 changes: 52 additions & 0 deletions src/presets/vueuse-core-directives.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { InlinePreset } from '../types'
import { readFileSync } from 'node:fs'
import process from 'node:process'
import { resolveModule } from 'local-pkg'
import { defineUnimportPreset } from '../utils'

export interface VueUseCoreDirectivesOptions {
/**
* Prefix VueUse core directives (to allow use other directives with the same name from another packages):
*
* When the prefix is enabled, the preset will configure `Vueuse` => `v-vueuse-<directive>: `v-vueuse-on-click-outside`.
*/
prefix?: true
}

let _cache: InlinePreset | undefined

/* c8 ignore start */
export function VueUseCoreDirectives(options: VueUseCoreDirectivesOptions = {}): InlinePreset {
if (!_cache) {
try {
const corePath = resolveModule('@vueuse/core') || process.cwd()
const path = resolveModule('@vueuse/core/indexes.json')
|| resolveModule('@vueuse/metadata/index.json')
|| resolveModule('@vueuse/metadata/index.json', { paths: [corePath] })
const indexesJson = JSON.parse(readFileSync(path!, 'utf-8'))
_cache = defineUnimportPreset({
from: '@vueuse/components',
meta: { vueDirective: true },
imports: indexesJson
.functions
.filter((i: any) => i.directive && i.name)
.map(({ name, docs }: any) => {
// name is the component name, we need to use the directive name
name = name.replace(/^use/, '')
return ({
name: `v${name[0].toUpperCase()}${name.slice(1)}`,
as: options.prefix ? `vVueuse${name[0].toUpperCase()}${name.slice(1)}` : undefined,
meta: { docsUrl: docs },
})
}),
})
}
catch (error) {
console.error(error)
throw new Error('[auto-import] failed to load @vueuse/core, have you installed it?')
}
}

return _cache
}
/* c8 ignore end */
1 change: 1 addition & 0 deletions test/public-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ it('public-api', async () => {
expect(keys)
.toMatchInlineSnapshot(`
[
"VueUseCoreDirectives",
"addImportToCode",
"builtinPresets",
"createUnimport",
Expand Down