By default, unimport uses RegExp to detect unimport entries. In some cases, RegExp might not be able to detect all the entries (false positive & false negative).
For a more accurate result, you can switch to an AST-based parser:
acorn — powered by acorn. Assumes the input is valid vanilla JavaScript, so it should usually be used after transformations and transpilation. Requires installing acorn as an optional peer dependency.
oxc — powered by oxc-parser. Much faster than acorn and accepts TypeScript and JSX. Requires installing either rolldown or oxc-parser as an optional peer dependency. (If both are installed, rolldown is preferred.)
Unimport.vite({ parser: 'oxc' // or 'acorn' or 'regex' (default)})
To use the oxc parser, install rolldown or oxc-parser alongside unimport:
npm install rolldown# ornpm install oxc-parser
To use the acorn parser, install acorn alongside unimport:
npm install acorn
Vue Template Auto Import
In Vue's template, the usage of API is in a different context than plain modules. Thus some custom transformations are required. To enable it, set addons.vueTemplate to true:
Unimport.vite({ addons: { vueTemplate: true }})
Caveats
When auto-import a ref, inline operations won't be auto-unwrapped.
export const counter = ref(0)
<template> <!-- this is ok --> <div>{{ counter }}</div> <!-- counter here is a ref, this won't work, volar will throw --> <div>{{ counter + 1 }}</div> <!-- use this instead --> <div>{{ counter.value + 1 }}</div></template>
We recommend using Volar for type checking, which will help you to identify the misusage.
Vue Directives Auto Import and TypeScript Declaration Generation
In Vue's template, the usage of directives is in a different context than plain modules. Thus some custom transformations are required. To enable it, set addons.vueDirectives to true:
Unimport.vite({ addons: { vueDirectives: true }})
Library Authors
When including directives in your presets, you should:
provide the corresponding imports with meta.vueDirective set to true, otherwise, unimport will not be able to detect your directives.
use named exports for your directives, or use default export and use as in the Import.
set dtsDisabled to true if you provide a type declaration for your directives.
import type { InlinePreset } from 'unimport'import { defineUnimportPreset } from 'unimport'export const composables = defineUnimportPreset({ from: 'my-unimport-library/composables', /* imports and other options */})export const directives = defineUnimportPreset({ from: 'my-unimport-library/directives', // disable dts generation globally dtsEnabled: false, // you can declare the vue directive globally meta: { vueDirective: true }, imports: [{ name: 'ClickOutside', // disable dts generation per import dtsEnabled: false, // you can declare the vue directive per import meta: { vueDirective: true } }, { name: 'default', // you should declare `as` for default exports as: 'Focus' }]})
Using Directory Scan and Local Directives
If you add a directory scan for your local directives in the project, you need to:
provide isDirective in the vueDirectives: unimport will use it to detect them (will never be called for imports with meta.vueDirective set to true).