unplugin-vue-components

On-demand components auto importing for Vue.
Features
- 💚 Supports both Vue 2 and Vue 3 out-of-the-box.
- ✨ Supports both components and directives.
- ⚡️ Supports Vite, Webpack, Rspack, Vue CLI, Rollup, esbuild and more, powered by unplugin.
- 🏝 Tree-shakable, only registers the components you use.
- 🪐 Folder names as namespaces.
- 🦾 Full TypeScript support.
- 🌈 Built-in resolvers for popular UI libraries.
- 😃 Works perfectly with unplugin-icons.
Installation
npm i unplugin-vue-components -D
vite-plugin-components has been renamed to unplugin-vue-components, see the migration guide.
Vite
import Components from 'unplugin-vue-components/vite'
export default defineConfig({
plugins: [
Components({ }),
],
})
Rollup
import Components from 'unplugin-vue-components/rollup'
export default {
plugins: [
Components({ }),
],
}
Webpack
module.exports = {
plugins: [
require('unplugin-vue-components/webpack')({ }),
],
}
Rspack
module.exports = {
plugins: [
require('unplugin-vue-components/rspack')({ }),
],
}
Nuxt
You might not need this plugin for Nuxt. Use @nuxt/components instead.
Vue CLI
module.exports = {
configureWebpack: {
plugins: [
require('unplugin-vue-components/webpack')({ }),
],
},
}
esbuild
import { build } from 'esbuild'
build({
plugins: [
require('unplugin-vue-components/esbuild')({
}),
],
})
Usage
Use components in templates as you would usually do, it will import components on demand, and there is no import and component registration required anymore! If you register the parent component asynchronously (or lazy route), the auto-imported components will be code-split along with their parent.
It will automatically turn this
<template>
<div>
<HelloWorld msg="Hello Vue 3.0 + Vite" />
</div>
</template>
<script>
export default {
name: 'App'
}
</script>
into this
<template>
<div>
<HelloWorld msg="Hello Vue 3.0 + Vite" />
</div>
</template>
<script>
import HelloWorld from './src/components/HelloWorld.vue'
export default {
name: 'App',
components: {
HelloWorld
}
}
</script>
Note
By default this plugin will import components in the src/components path. You can customize it using the dirs option.
TypeScript
To get TypeScript support for auto-imported components, there is a PR to Vue 3 extending the interface of global components. Currently, Volar has supported this usage already. If you are using Volar, you can change the config as following to get the support.
Components({
dts: true,
})
Once the setup is done, a components.d.ts will be generated and updates automatically with the type definitions. Feel free to commit it into git or not as you want.
Make sure you also add components.d.ts to your tsconfig.json under include.
Importing from UI Libraries
We have several built-in resolvers for popular UI libraries like Vuetify, Ant Design Vue, and Element Plus, where you can enable them by:
Supported Resolvers:
import Components from 'unplugin-vue-components/vite'
import {
AntDesignVueResolver,
ElementPlusResolver,
VantResolver,
VantifyResolver
} from 'unplugin-vue-components/resolvers'
Components({
resolvers: [
AntDesignVueResolver(),
ElementPlusResolver(),
VantResolver(),
VantifyResolver()
],
})
You can also write your own resolver quickly:
Components({
resolvers: [
(componentName) => {
if (componentName.startsWith('Van'))
return { name: componentName.slice(3), from: 'vant' }
},
],
})
We no longer accept new resolvers.
Types for global registered components
Some libraries might register some global components for you to use anywhere (e.g. Vue Router provides <RouterLink> and <RouterView>). Since they are global available, there is no need for this plugin to import them. However, those are commonly not TypeScript friendly, and you might need to register their types manually.
Thus unplugin-vue-components provided a way to only register types for global components.
Components({
dts: true,
types: [{
from: 'vue-router',
names: ['RouterLink', 'RouterView'],
}],
})
So the RouterLink and RouterView will be presented in components.d.ts.
By default, unplugin-vue-components detects supported libraries automatically (e.g. vue-router) when they are installed in the workspace. If you want to disable it completely, you can pass an empty array to it:
Components({
types: [],
})
Migrate from vite-plugin-components
package.json
{
"devDependencies": {
- "vite-plugin-components": "*",
+ "unplugin-vue-components": "^0.14.0",
}
}
vite.config.js
- import Components, { ElementPlusResolver } from 'vite-plugin-components'
+ import Components from 'unplugin-vue-components/vite'
+ import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
export default {
plugins: [
/* ... */
Components({
/* ... */
// `customComponentsResolvers` has renamed to `resolver`
- customComponentsResolvers: [
+ resolvers: [
ElementPlusResolver(),
],
// `globalComponentsDeclaration` has renamed to `dts`
- globalComponentsDeclaration: true,
+ dts: true,
// `customLoaderMatcher` is depreacted, use `include` instead
- customLoaderMatcher: id => id.endsWith('.md'),
+ include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
}),
],
}
Configuration
The following show the default values of the configuration
Components({
dirs: ['src/components'],
extensions: ['vue'],
globs: ['src/components/*.{vue}'],
deep: true,
resolvers: [],
dts: false,
directoryAsNamespace: false,
collapseSamePrefixes: false,
globalNamespaces: [],
directives: true,
importPathTransform: v => v,
allowOverrides: false,
include: [/\.vue$/, /\.vue\?vue/],
exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/],
version: 2.7,
types: []
})
Example
Vitesse starter template.
Thanks
Thanks to @brattonross, this project is heavily inspired by vite-plugin-voie.