Skip to content

Commit 92de065

Browse files
authored
feat(vue): add auto-import directives (#534)
1 parent 3235f46 commit 92de065

File tree

10 files changed

+93
-30
lines changed

10 files changed

+93
-30
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ AutoImport({
299299
// see https://github.com/unjs/unimport/pull/15 and https://github.com/unjs/unimport/pull/72
300300
vueTemplate: false,
301301

302+
// Auto import directives inside Vue template
303+
// see https://github.com/unjs/unimport/pull/374
304+
vueDirectives: undefined,
305+
302306
// Custom resolvers, compatible with `unplugin-vue-components`
303307
// see https://github.com/antfu/unplugin-auto-import/pull/23/
304308
resolvers: [

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
"local-pkg": "^0.5.0",
170170
"magic-string": "^0.30.11",
171171
"minimatch": "^9.0.5",
172-
"unimport": "^3.12.0",
172+
"unimport": "^3.13.1",
173173
"unplugin": "^1.14.1"
174174
},
175175
"devDependencies": {

playground/HelloWorld.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ watch(count, value => emit('update', value))
2323
Inc
2424
</button>
2525
<div>{{ doubled / 2 }} x 2 = {{ doubled }}</div>
26-
<button @click="dec()" v-html="decText" />
26+
<button v-custom-directive @click="dec()" v-html="decText" />
2727
</div>
2828
</template>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function mounted(el: HTMLElement, binding: DirectiveBinding) {
2+
// eslint-disable-next-line no-console
3+
console.log('mounted', el, binding)
4+
}
5+
6+
function unmounted(el: HTMLElement, binding: DirectiveBinding) {
7+
// eslint-disable-next-line no-console
8+
console.log('unmounted', el, binding)
9+
}
10+
11+
function updated(el: HTMLElement, binding: DirectiveBinding) {
12+
// eslint-disable-next-line no-console
13+
console.log('updated', el, binding)
14+
}
15+
16+
export const CustomDirective = {
17+
mounted,
18+
unmounted,
19+
updated,
20+
}

playground/vite.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ export default defineConfig({
1515
],
1616
dirs: [
1717
'./composables/**',
18+
'./directives/**',
1819
],
1920
vueTemplate: true,
21+
vueDirectives: {
22+
isDirective(normalizeImportFrom, _importEntry) {
23+
return normalizeImportFrom.includes('/directives/')
24+
},
25+
},
2026
}),
2127
],
2228
})

pnpm-lock.yaml

Lines changed: 32 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/ctx.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ import { slash, throttle, toArray } from '@antfu/utils'
77
import { createFilter } from '@rollup/pluginutils'
88
import fg from 'fast-glob'
99
import { isPackageExists } from 'local-pkg'
10-
1110
import MagicString from 'magic-string'
1211
import { createUnimport, resolvePreset, scanExports } from 'unimport'
13-
// @ts-expect-error types
14-
import { vueTemplateAddon } from 'unimport/addons'
1512
import { presets } from '../presets'
1613
import { generateBiomeLintConfigs } from './biomelintrc'
1714
import { generateESLintConfigs } from './eslintrc'
@@ -39,6 +36,8 @@ export function createContext(options: Options = {}, root = process.cwd()) {
3936

4037
const {
4138
dts: preferDTS = isPackageExists('typescript'),
39+
vueDirectives,
40+
vueTemplate,
4241
} = options
4342

4443
const dirs = options.dirs?.concat(options.dirs.map(dir => join(dir, '*.{tsx,jsx,ts,js,mjs,cjs,mts,cts}')))
@@ -63,22 +62,26 @@ export function createContext(options: Options = {}, root = process.cwd()) {
6362
presets: options.packagePresets?.map(p => typeof p === 'string' ? { package: p } : p) ?? [],
6463
injectAtEnd,
6564
parser: options.parser,
66-
addons: [
67-
...(options.vueTemplate ? [vueTemplateAddon()] : []),
68-
resolversAddon(resolvers),
69-
{
70-
declaration(dts) {
71-
return `${`
65+
addons: {
66+
addons: [
67+
resolversAddon(resolvers),
68+
{
69+
name: 'unplugin-auto-import:dts',
70+
declaration(dts) {
71+
return `${`
7272
/* eslint-disable */
7373
/* prettier-ignore */
7474
// @ts-nocheck
7575
// noinspection JSUnusedGlobalSymbols
7676
// Generated by unplugin-auto-import
7777
// biome-ignore lint: disable
7878
${dts}`.trim()}\n`
79+
},
7980
},
80-
},
81-
],
81+
],
82+
vueDirectives,
83+
vueTemplate,
84+
},
8285
})
8386

8487
const importsPromise = flattenImports(options.imports)

src/core/resolvers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export async function firstMatchedResolver(resolvers: Resolver[], fullname: stri
4242

4343
export function resolversAddon(resolvers: Resolver[]): Addon {
4444
return {
45+
name: 'unplugin-auto-import:resolvers',
4546
async matchImports(names, matched) {
4647
if (!resolvers.length)
4748
return

src/types.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Arrayable, Awaitable } from '@antfu/utils'
22
import type { FilterPattern } from '@rollup/pluginutils'
3-
import type { Import, InlinePreset, PackagePreset, UnimportOptions } from 'unimport'
3+
import type { AddonVueDirectivesOptions, Import, InlinePreset, PackagePreset, UnimportOptions } from 'unimport'
44
import { PresetName } from './presets'
55

66
export interface ImportLegacy {
@@ -156,6 +156,18 @@ export interface Options {
156156
*/
157157
vueTemplate?: boolean
158158

159+
/**
160+
* Enable auto import directives for Vue's SFC.
161+
*
162+
* Library authors should include `meta.vueDirective: true` in the import metadata.
163+
*
164+
* When using a local directives folder, provide the `isDirective`
165+
* callback to check if the import is a Vue directive.
166+
*
167+
* @see https://github.com/unjs/unimport?tab=readme-ov-file#vue-directives-auto-import-and-typescript-declaration-generation
168+
*/
169+
vueDirectives?: true | AddonVueDirectivesOptions
170+
159171
/**
160172
* Set default export alias by file name
161173
*

test/__snapshots__/dts.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ declare global {
150150
// for type re-export
151151
declare global {
152152
// @ts-ignore
153-
export type { Component, ComponentPublicInstance, ComputedRef, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, VNode, WritableComputedRef } from 'vue-demi'
153+
export type { Component, ComponentPublicInstance, ComputedRef, DirectiveBinding, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, MaybeRef, MaybeRefOrGetter, VNode, WritableComputedRef } from 'vue-demi'
154154
import('vue-demi')
155155
}
156156
"

0 commit comments

Comments
 (0)