百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
U

unplugin-vue-components

> 前端框架
开源

Vue 的按需组件自动导入

4.3K stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Vue 的按需组件自动导入

unplugin-vue-components

On-demand components auto importing for Vue.

Features
  • Supports Vue 3 out-of-the-box.
  • ✨ Supports both components and directives.
  • ⚡️ Supports Vite, Webpack, Rspack, Rollup, Rolldown, 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

bash
npm i unplugin-vue-components -D

vite-plugin-components has been renamed to unplugin-vue-components, see the migration guide.

Vite
typescript
// vite.config.ts
import Components from 'unplugin-vue-components/vite'

export default defineConfig({
  plugins: [
    Components({ /* options */ }),
  ],
})


Rollup
typescript
// rollup.config.js
import Components from 'unplugin-vue-components/rollup'

export default {
  plugins: [
    Components({ /* options */ }),
  ],
}


Rolldown
typescript
// rolldown.config.js
import Components from 'unplugin-vue-components/rolldown'

export default {
  plugins: [
    Components({ /* options */ }),
  ],
}


Webpack
typescript
// webpack.config.js
// unplugin-vue-components removed support for CommonJS after version 29.1.0
module.exports = {
  /* ... */
  plugins: [
    require('unplugin-vue-components/webpack')({ /* options */ }),
  ],
}


Rspack
typescript
// rspack.config.js
// unplugin-vue-components removed support for CommonJS after version 29.1.0
module.exports = {
  /* ... */
  plugins: [
    require('unplugin-vue-components/rspack')({ /* options */ }),
  ],
}


Nuxt

You might not need this plugin for Nuxt. Use @nuxt/components instead.


Quasar
typescript
// vite.config.js [Vite]
import Components from 'unplugin-vue-components/vite'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [
    Components({ /* options */ })
  ]
})
typescript
// quasar.config.js
export default defineConfig(() => {
  return {
    build: {
      vitePlugins: [
        ['unplugin-vue-components/vite', { /* options */ }],
      ]
    },
  }
})


esbuild
typescript
// esbuild.config.js
import { build } from 'esbuild'
import Components from 'unplugin-vue-components/esbuild'

build({
  /* ... */
  plugins: [
    Components({
      /* options */
    }),
  ],
})


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

xml
<template>
  

</template>

<script>
  export default {
    name: 'App',
  }
</script>

into this

xml
<template>
  

</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.

typescript
Components({
  dts: true, // enabled by default if `typescript` is installed
})

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:

  • Ant Design Vue
  • Arco Design Vue
  • BootstrapVue
  • Element Plus
  • Headless UI
  • IDux
  • Inkline
  • Ionic
  • Naive UI
  • Prime Vue
  • Quasar
  • TDesign
    • @tdesign-vue-next/auto-import-resolver - TDesign's own auto-import resolver
  • Vant
    • @vant/auto-import-resolver - Vant's own auto-import resolver
  • Varlet UI
    • @varlet/import-resolver - Varlet's own auto-import resolver
  • VEUI
  • View UI
  • Vuetify — Prefer first-party plugins when possible: v3 + vite, v3 + webpack, v2 + webpack
  • VueUse Components
  • VueUse Directives
  • Dev UI
typescript
import {
  AntDesignVueResolver,
  ElementPlusResolver,
  VantResolver,
} from 'unplugin-vue-components/resolvers'
// vite.config.js
import Components from 'unplugin-vue-components/vite'

// your plugin installation
Components({
  resolvers: [
    AntDesignVueResolver(),
    ElementPlusResolver(),
    VantResolver(),
  ],
})

You can also write your own resolver quickly:

typescript
Components({
  resolvers: [
    // example of importing Vant
    (componentName) => {
      // where `componentName` is always CapitalCase
      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.

typescript
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:

typescript
Components({
  // Disable type only registration
  types: [],
})

Migrate from vite-plugin-components

package.json

diff
{
  "devDependencies": {
-   "vite-plugin-components": "*",
+   "unplugin-vue-components": "^0.14.0",
  }
}

vite.config.js

…

Configuration

The following show the default values of the configuration

…

Example

Vitesse starter template.

Thanks

Thanks to @brattonross, this project is heavily inspired by vite-plugin-voie.

License

MIT License © 2020-PRESENT Anthony Fu

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptauto-importcomponentsunpluginvite

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架