1
- < h2 align = ' center ' >< samp >vite-plugin -components</ samp ></ h2 >
1
+ # unplugin-vue -components
2
2
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 )
4
4
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 ) .
10
16
11
17
<br >
12
18
18
24
19
25
<br >
20
26
21
- ## Usage
27
+ ## Installation
22
28
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
+ ```
24
32
25
- Install
33
+ <details >
34
+ <summary >Vite</summary ><br >
26
35
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
+ })
29
45
```
30
46
31
- Add it to ` vite.config.js `
47
+ <br ></details >
48
+
49
+ <details >
50
+ <summary >Rollup</summary ><br >
32
51
33
52
``` 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'
37
55
38
56
export default {
39
57
plugins: [
40
- Vue (),
41
- ViteComponents ()
58
+ Components ({ /* options */ }),
42
59
],
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
+ }
44
100
```
45
101
46
- That's all.
102
+ <br ></details >
103
+
104
+ ## Usage
47
105
48
106
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.
49
107
@@ -89,129 +147,143 @@ export default {
89
147
To 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.
90
148
91
149
``` 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
+ })
103
153
```
104
154
105
155
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.
106
156
107
157
** Make sure you also add ` components.d.ts ` to your ` tsconfig.json ` under ` includes ` .**
108
158
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
-
126
159
## Importing from UI Libraries
127
160
128
161
We have several built-in resolvers for popular UI libraries like ** Vuetify** , ** Ant Design Vue** , and ** Element Plus** , where you can enable them by:
129
162
130
163
Supported Resolvers:
131
164
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 )
144
177
145
178
``` ts
146
179
// vite.config.js
147
180
import ViteComponents , {
148
181
AntDesignVueResolver ,
149
182
ElementPlusResolver ,
150
183
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
+ })
165
194
```
166
195
167
196
You can also write your own resolver easily:
168
197
169
198
``` 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'
172
233
173
234
export default {
174
235
plugins: [
175
236
/* ... */
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$/],
185
253
}),
186
254
],
187
255
}
188
256
```
189
257
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
-
192
258
## Configuration
193
259
194
260
The following show the default values of the configuration
195
261
196
262
``` ts
197
- ViteComponents ({
263
+ Components ({
198
264
// relative paths to the directory to search for components.
199
265
dirs: [' src/components' ],
200
266
201
267
// valid file extensions for components.
202
268
extensions: [' vue' ],
203
269
// search for subdirectories
204
270
deep: true ,
271
+ // resolvers for custom components
272
+ resolvers: [],
205
273
206
274
// generate `components.d.ts` global declrations,
207
275
// also accepts a path for custom filename
208
- globalComponentsDeclaration : false ,
276
+ dts : false ,
209
277
210
278
// Allow subdirectories as namespace prefix for components.
211
279
directoryAsNamespace: false ,
212
280
// Subdirectory paths for ignoring namespace prefixes
213
281
// works when `directoryAsNamespace: true`
214
282
globalNamespaces: [],
283
+
284
+ // filters for transforming targets
285
+ include: [/ \. vue$ / , / \. vue\? vue&type=template/ ],
286
+ exclude: [/ node_modules/ , / \. git/ , / \. nuxt/ ],
215
287
})
216
288
```
217
289
@@ -225,4 +297,4 @@ Thanks to [@brattonross](https://github.com/brattonross), this project is heavil
225
297
226
298
## License
227
299
228
- MIT License © 2020 [ Anthony Fu] ( https://github.com/antfu )
300
+ MIT License © 2020-PRESENT [ Anthony Fu] ( https://github.com/antfu )
0 commit comments