1- < h2 align = ' center ' >< samp >vite-plugin -components</ samp ></ h2 >
1+ # unplugin-vue -components
22
3- < p align = ' center ' >On demand components auto importing for Vite</ p >
3+ [ ![ NPM version ] ( https://img.shields.io/npm/v/unplugin-vue- components?color=a1b858&label= )] ( https://www.npmjs.com/package/unplugin-vue-components )
44
5- <p align =' center ' >
6- <a href =' https://www.npmjs.com/package/vite-plugin-components ' >
7- <img src =' https://img.shields.io/npm/v/vite-plugin-components?color=222&style=flat-square ' >
8- </a >
9- </p >
5+ On-demand components auto importing for Vue.
6+
7+ ###### Features
8+
9+ - 💚 Supports both Vue 2 and Vue 3 out-of-the-box.
10+ - ⚡️ Supports Vite, Webpack, Vue CLI, rollup and more, powered by <a href =" https://github.com/unjs/unplugin " >unplugin</a >.
11+ - 🏝 Tree-shakable, only registers the components you use.
12+ - 🪐 Folder as namespaces.
13+ - 🦾 Full TypeScript support.
14+ - 🌈 [ Built-in resolvers] ( #importing-from-ui-libraries ) for popular UI libraries.
15+ - 😃 Works perfectly with [ unplugin-icons] ( https://github.com/antfu/unplugin-icons ) .
1016
1117<br >
1218
1824
1925<br >
2026
21- ## Usage
27+ ## Installation
2228
23- > ℹ️ ** Vite 2 is supported from ` v0.6.x ` , Vite 1's support is discontinued.**
29+ ``` bash
30+ npm i unplugin-vue-components -D
31+ ```
2432
25- Install
33+ <details >
34+ <summary >Vite</summary ><br >
2635
27- ``` bash
28- npm i vite-plugin-components -D # yarn add vite-plugin-components -D
36+ ``` ts
37+ // vite.config.ts
38+ import Components from ' unplugin-vue-components/vite'
39+
40+ export default defineConfig ({
41+ plugins: [
42+ Components ({ /* options */ }),
43+ ],
44+ })
2945```
3046
31- Add it to ` vite.config.js `
47+ <br ></details >
48+
49+ <details >
50+ <summary >Rollup</summary ><br >
3251
3352``` ts
34- // vite.config.js
35- import Vue from ' @vitejs/plugin-vue'
36- import ViteComponents from ' vite-plugin-components'
53+ // rollup.config.js
54+ import Components from ' unplugin-vue-components/rollup'
3755
3856export default {
3957 plugins: [
40- Vue (),
41- ViteComponents ()
58+ Components ({ /* options */ }),
4259 ],
43- };
60+ }
61+ ```
62+
63+ <br ></details >
64+
65+
66+ <details >
67+ <summary >Webpack</summary ><br >
68+
69+ ``` ts
70+ // webpack.config.js
71+ module .exports = {
72+ /* ... */
73+ plugins: [
74+ require (' unplugin-vue-components/webpack' )({ /* options */ })
75+ ]
76+ }
77+ ```
78+
79+ <br ></details >
80+
81+ <details >
82+ <summary >Nuxt</summary ><br >
83+
84+ You don't need this plugin for Nuxt, use [ ` @nuxt/components ` ] ( https://github.com/nuxt/components ) instead.
85+
86+ <br ></details >
87+
88+ <details >
89+ <summary >Vue CLI</summary ><br >
90+
91+ ``` ts
92+ // vue.config.js
93+ module .exports = {
94+ configureWebpack: {
95+ plugins: [
96+ require (' unplugin-vue-components/webpack' )({ /* options */ }),
97+ ],
98+ },
99+ }
44100```
45101
46- That's all.
102+ <br ></details >
103+
104+ ## Usage
47105
48106Use 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.
49107
@@ -89,129 +147,143 @@ export default {
89147To have TypeScript support for auto-imported components, there is [ a PR] ( https://github.com/vuejs/vue-next/pull/3399 ) to Vue 3 extending the interface of global components. Currently, [ Volar] ( https://github.com/johnsoncodehk/volar ) has supported this usage already, if you are using Volar, you can change the config as following to get the support.
90148
91149``` ts
92- // vite.config.js
93- import ViteComponents from ' vite-plugin-components'
94-
95- export default {
96- plugins: [
97- /* ... */
98- ViteComponents ({
99- globalComponentsDeclaration: true ,
100- }),
101- ],
102- }
150+ Components ({
151+ dts: true , // enabled by default if `typescript` is installed
152+ })
103153```
104154
105155Once 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.
106156
107157** Make sure you also add ` components.d.ts ` to your ` tsconfig.json ` under ` includes ` .**
108158
109- ## Vue 2 Support
110-
111- It just works.
112-
113- ``` ts
114- // vite.config.js
115- import { createVuePlugin } from ' vite-plugin-vue2'
116- import ViteComponents from ' vite-plugin-components'
117-
118- export default {
119- plugins: [
120- createVuePlugin (),
121- ViteComponents (),
122- ],
123- }
124- ```
125-
126159## Importing from UI Libraries
127160
128161We have several built-in resolvers for popular UI libraries like ** Vuetify** , ** Ant Design Vue** , and ** Element Plus** , where you can enable them by:
129162
130163Supported Resolvers:
131164
132- - [ Ant Design Vue] ( https://github.com/antfu/vite-plugin -components/blob/master /src/resolvers/antdv.ts )
133- - [ Element Plus] ( https://github.com/antfu/vite-plugin -components/blob/master /src/resolvers/element-plus.ts )
134- - [ Headless UI] ( https://github.com/antfu/vite-plugin -components/blob/master /src/resolvers/headless -ui.ts )
135- - [ IDux ] ( https://github.com/antfu/vite-plugin -components/blob/master /src/resolvers/idux .ts )
136- - [ Naive UI ] ( https://github.com/antfu/vite-plugin -components/blob/master /src/resolvers/naive-ui .ts )
137- - [ Prime Vue ] ( https://github.com/antfu/vite-plugin -components/blob/master /src/resolvers/prime-vue .ts )
138- - [ Vant ] ( https://github.com/antfu/vite-plugin -components/blob/master /src/resolvers/vant .ts )
139- - [ Varlet UI ] ( https://github.com/antfu/vite-plugin -components/blob/master /src/resolvers/varlet-ui .ts )
140- - [ Vuetify ] ( https://github.com/antfu/vite-plugin -components/blob/master /src/resolvers/vuetify .ts )
141- - [ VueUse Components ] ( https://github.com/antfu/vite-plugin -components/blob/master /src/resolvers/vueuse .ts )
142- - [ View UI ] ( https://github.com/antfu/vite-plugin -components/blob/main/src/resolvers/view-ui .ts )
143- - [ Element UI ] ( https://github.com/antfu/vite-plugin -components/blob/main/src/resolvers/element-ui .ts )
165+ - [ Ant Design Vue] ( https://github.com/antfu/unplugin-vue -components/blob/main /src/core /resolvers/antdv.ts )
166+ - [ Element Plus] ( https://github.com/antfu/unplugin-vue -components/blob/main /src/core /resolvers/element-plus.ts )
167+ - [ Element UI] ( https://github.com/antfu/unplugin-vue -components/blob/main /src/resolvers/element -ui.ts )
168+ - [ Headless UI ] ( https://github.com/antfu/unplugin-vue -components/blob/main /src/core/ resolvers/headless-ui .ts )
169+ - [ IDux ] ( https://github.com/antfu/unplugin-vue -components/blob/main /src/resolvers/idux .ts )
170+ - [ Naive UI ] ( https://github.com/antfu/unplugin-vue -components/blob/main /src/core/ resolvers/naive-ui .ts )
171+ - [ Prime Vue ] ( https://github.com/antfu/unplugin-vue -components/blob/main /src/core/ resolvers/prime-vue .ts )
172+ - [ Vant ] ( https://github.com/antfu/unplugin-vue -components/blob/main /src/core/ resolvers/vant .ts )
173+ - [ Varlet UI ] ( https://github.com/antfu/unplugin-vue -components/blob/main /src/core/ resolvers/varlet-ui .ts )
174+ - [ View UI ] ( https://github.com/antfu/unplugin-vue -components/blob/main /src/resolvers/view-ui .ts )
175+ - [ Vuetify ] ( https://github.com/antfu/unplugin-vue -components/blob/main/src/core/ resolvers/vuetify .ts )
176+ - [ VueUse Components ] ( https://github.com/antfu/unplugin-vue -components/blob/main/src/core/ resolvers/vueuse .ts )
144177
145178``` ts
146179// vite.config.js
147180import ViteComponents , {
148181 AntDesignVueResolver ,
149182 ElementPlusResolver ,
150183 VantResolver ,
151- } from ' vite-plugin-components'
152-
153- export default {
154- plugins: [
155- /* ... */
156- ViteComponents ({
157- customComponentResolvers: [
158- AntDesignVueResolver (),
159- ElementPlusResolver (),
160- VantResolver (),
161- ]
162- }),
163- ],
164- }
184+ } from ' unplugin-vue-components/resolvers'
185+
186+ // your plugin installation
187+ Components ({
188+ resolvers: [
189+ AntDesignVueResolver (),
190+ ElementPlusResolver (),
191+ VantResolver (),
192+ ]
193+ })
165194```
166195
167196You can also write your own resolver easily:
168197
169198``` ts
170- // vite.config.js
171- import ViteComponents from ' vite-plugin-components'
199+ Components ({
200+ resolvers: [
201+ // example of importing Vant
202+ (name ) => {
203+ // where `name` is always CapitalCase
204+ if (name .startsWith (' Van' ))
205+ return { importName: name .slice (3 ), path: ' vant' }
206+ }
207+ ]
208+ })
209+ ```
210+
211+ If you made other UI libraries configured, please feel free to contribute so it can help others using them out-of-box. Thanks!
212+
213+
214+ ## Migrate from ` vite-plugin-components `
215+
216+ ` package.json `
217+
218+ ``` diff
219+ {
220+ "devDependencies": {
221+ - "vite-plugin-components": "*",
222+ + "unplugin-vue-components": "^0.14.0",
223+ }
224+ }
225+ ```
226+
227+ ` vite.config.json `
228+
229+ ``` diff
230+ - import Components, { ElementPlusResolver } from 'vite-plugin-components'
231+ + import Components from 'unplugin-vue-components/vite'
232+ + import ElementPlusResolver from 'unplugin-vie-components/resolvers'
172233
173234export default {
174235 plugins: [
175236 /* ... */
176- ViteComponents ({
177- customComponentResolvers: [
178- // example of importing Vant
179- (name ) => {
180- // where `name` is always CapitalCase
181- if (name .startsWith (' Van' ))
182- return { importName: name .slice (3 ), path: ' vant' }
183- }
184- ]
237+ Components({
238+ /* ... */
239+
240+ // `customComponentsResolvers` has renamed to `resolver`
241+ - customComponentsResolvers: [
242+ + resolvers: [
243+ ElementPlusResolver(),
244+ ],
245+
246+ // `globalComponentsDeclaration` has renamed to `dts`
247+ - globalComponentsDeclaration: true,
248+ + dts: true,
249+
250+ // `customLoaderMatcher` is depreacted, use `include` instead
251+ - customLoaderMatcher: id => id.endsWith('.md'),
252+ + include: [/\.vue$/, /\.vue\?vue&type=template/, /\.md$/],
185253 }),
186254 ],
187255}
188256```
189257
190- If you made other UI libraries configured, please feel free to contribute so it can help others using them out-of-box. Thanks!
191-
192258## Configuration
193259
194260The following show the default values of the configuration
195261
196262``` ts
197- ViteComponents ({
263+ Components ({
198264 // relative paths to the directory to search for components.
199265 dirs: [' src/components' ],
200266
201267 // valid file extensions for components.
202268 extensions: [' vue' ],
203269 // search for subdirectories
204270 deep: true ,
271+ // resolvers for custom components
272+ resolvers: [],
205273
206274 // generate `components.d.ts` global declrations,
207275 // also accepts a path for custom filename
208- globalComponentsDeclaration : false ,
276+ dts : false ,
209277
210278 // Allow subdirectories as namespace prefix for components.
211279 directoryAsNamespace: false ,
212280 // Subdirectory paths for ignoring namespace prefixes
213281 // works when `directoryAsNamespace: true`
214282 globalNamespaces: [],
283+
284+ // filters for transforming targets
285+ include: [/ \. vue$ / , / \. vue\? vue&type=template/ ],
286+ exclude: [/ node_modules/ , / \. git/ , / \. nuxt/ ],
215287})
216288```
217289
@@ -225,4 +297,4 @@ Thanks to [@brattonross](https://github.com/brattonross), this project is heavil
225297
226298## License
227299
228- MIT License © 2020 [ Anthony Fu] ( https://github.com/antfu )
300+ MIT License © 2020-PRESENT [ Anthony Fu] ( https://github.com/antfu )
0 commit comments