Vue component auto-importing plugin for multiple build tools including Vite, Webpack, Rollup, and esbuild
—
Automatic TypeScript declaration generation and type-safe configuration with full IDE intellisense support for auto-imported components.
Automatic generation of TypeScript declarations for globally available components, providing full IDE intellisense and type checking.
/**
* TypeScript declaration configuration
*/
interface DeclarationOptions {
/** Generate TypeScript declaration for global components */
dts?: boolean | string;
/** Only provide types of components in library (registered globally) */
types?: TypeImport[];
}
interface TypeImport {
/** Import source module */
from: string;
/** Type names to import */
names: string[];
}Usage Examples:
// Basic declaration generation
Components({
dts: true, // Generates components.d.ts in project root
});
// Custom declaration file path
Components({
dts: "src/types/auto-components.d.ts",
});
// Type-only imports for global components
Components({
types: [
{
from: "vue-router",
names: ["RouterLink", "RouterView"],
},
{
from: "@/types/global-components",
names: ["GlobalHeader", "GlobalFooter"],
},
],
});Public API for programmatic access to component resolution and import generation.
/**
* Public plugin API interface
*/
interface PublicPluginAPI {
/** Resolves a component using the configured resolvers */
findComponent(name: string, filename?: string): Promise<ComponentInfo | undefined>;
/** Obtain an import statement for a resolved component */
stringifyImport(info: ComponentInfo): string;
}
/**
* Component information interface
*/
interface ComponentInfo {
/** Import alias name */
as?: string;
/** Component name */
name?: string;
/** Import path or module name */
from: string;
/** Side effect imports (CSS, etc.) */
sideEffects?: SideEffectsInfo;
}Usage Examples:
// Access plugin API in Vite
import Components from "unplugin-vue-components/vite";
const plugin = Components({
dirs: ["src/components"],
});
// Plugin API is available on the returned plugin
export default defineConfig({
plugins: [
vue(),
plugin,
],
});
// Use API programmatically
const api = plugin.api;
const component = await api.findComponent("MyButton");
if (component) {
const importStatement = api.stringifyImport(component);
console.log(importStatement); // import MyButton from '@/components/MyButton.vue'
}Configuration options that ensure type safety throughout the plugin operation.
/**
* Type-safe configuration interface
*/
interface TypeSafeOptions {
/** Vue version for proper type generation */
version?: 2 | 2.7 | 3;
/** Transformer selection affects type generation */
transformer?: "vue2" | "vue3";
/** Allow component overrides without warnings */
allowOverrides?: boolean;
}Usage Examples:
// Explicit Vue version for accurate types
Components({
version: 3, // Ensures Vue 3 component types
transformer: "vue3",
dts: true,
});
// Type-safe overrides
Components({
allowOverrides: false, // Warn about component name conflicts
dts: true,
});The structure and content of automatically generated TypeScript declarations.
/**
* Example generated declaration file structure
*/
declare module "@vue/runtime-core" {
export interface GlobalComponents {
// Local components
HelloWorld: typeof import("./src/components/HelloWorld.vue")["default"];
MyButton: typeof import("./src/components/MyButton.vue")["default"];
// UI library components (via resolvers)
ElButton: typeof import("element-plus")["ElButton"];
ElCard: typeof import("element-plus")["ElCard"];
// Namespaced components
FormInput: typeof import("./src/components/form/Input.vue")["default"];
FormSelect: typeof import("./src/components/form/Select.vue")["default"];
}
}
export {};Configuration for optimal IDE experience with auto-imported components.
Usage Examples:
// VS Code integration
Components({
dts: true,
// Ensure declarations are updated during development
dirs: ["src/components"],
deep: true,
// Enable for better IntelliSense
directoryAsNamespace: true,
});
// WebStorm/IntelliJ integration
Components({
dts: "types/components.d.ts", // Place in types directory
extensions: ["vue", "tsx"], // Support all relevant file types
});Special handling for Vue 3 Composition API and TypeScript integration.
/**
* Vue 3 specific type configuration
*/
interface Vue3TypeOptions {
/** Transform user resolve functions for better TypeScript support */
transformerUserResolveFunctions?: boolean;
/** Support for script setup syntax */
scriptSetup?: boolean;
}Usage Examples:
// Vue 3 + TypeScript + script setup
Components({
version: 3,
transformer: "vue3",
transformerUserResolveFunctions: true,
dts: true,
});
// In your Vue components
// <script setup lang="ts">
// // No imports needed - components are auto-imported with full typing
// </script>
//
// <template>
// <HelloWorld /> <!-- Fully typed! -->
// <ElButton type="primary" /> <!-- UI library types work too! -->
// </template>Adding custom type definitions for complex component scenarios.
/**
* Custom type definition configuration
*/
interface CustomTypeOptions {
/** Additional type imports */
types?: TypeImport[];
/** Path transformation for custom types */
importPathTransform?: (path: string) => string | undefined;
}Usage Examples:
// Custom global component types
Components({
dts: true,
types: [
// Global layout components
{
from: "@/layouts",
names: ["DefaultLayout", "AdminLayout"],
},
// Third-party components not handled by resolvers
{
from: "my-custom-ui-lib",
names: ["CustomButton", "CustomModal"],
},
],
});
// With path transformation
Components({
dts: true,
importPathTransform: (path) => {
// Transform @ alias in type definitions
return path.replace(/^@\//, "./src/");
},
});Different type generation strategies for development and production.
Usage Examples:
const isDev = process.env.NODE_ENV === "development";
Components({
// More detailed types in development
dts: isDev ? "src/types/components.development.d.ts" : true,
// More permissive in development
allowOverrides: isDev,
// Additional checks in development
version: 3,
transformer: "vue3",
});Integration with TypeScript compiler and type checking tools.
Usage Examples:
// package.json scripts
{
"scripts": {
"type-check": "vue-tsc --noEmit",
"build": "vue-tsc && vite build"
}
}
// tsconfig.json
{
"compilerOptions": {
"types": ["node", "vite/client"]
},
"include": [
"src/**/*",
"components.d.ts" // Include generated declarations
]
}
// vite.config.ts with type checking
Components({
dts: true,
// Ensure types are generated before TypeScript compilation
dirs: ["src/components"],
});Best practices for maintaining generated declaration files.
Usage Examples:
// .gitignore - whether to commit generated declarations
# Option 1: Commit for team consistency (recommended)
# components.d.ts
# Option 2: Don't commit, generate locally
components.d.ts
// Auto-regeneration during development
Components({
dts: true,
// Will update declarations when components change
dirs: ["src/components"],
extensions: ["vue", "tsx"],
});Common TypeScript integration issues and solutions.
Usage Examples:
// Issue: Components not recognized by TypeScript
// Solution: Ensure declaration file is generated and included
Components({
dts: true, // Make sure this is enabled
});
// Check tsconfig.json includes the declaration file
{
"include": ["src/**/*", "components.d.ts"]
}
// Issue: Wrong component types
// Solution: Specify correct Vue version and transformer
Components({
version: 3, // Match your actual Vue version
transformer: "vue3",
dts: true,
});
// Issue: Resolver components not typed
// Solution: Use resolvers that support TypeScript
Components({
resolvers: [
ElementPlusResolver(), // Has built-in TypeScript support
],
dts: true,
});Install with Tessl CLI
npx tessl i tessl/npm-unplugin-vue-components