Gathering detailed insights and metrics for unplugin-vue-components
Gathering detailed insights and metrics for unplugin-vue-components
Gathering detailed insights and metrics for unplugin-vue-components
Gathering detailed insights and metrics for unplugin-vue-components
@uni-helper/vite-plugin-uni-components
Forked from [unplugin-vue-components](https://github.com/antfu/unplugin-vue-components) and modified to adapt UniApp.
@vant/auto-import-resolver
Vant auto import resolver based on unplugin-vue-components
@prestashopcorp/puik-resolver
PUIK resolver - For unplugin-vue-components.
@varlet/import-resolver
varlet import resolver for unplugin-vue-components and unplugin-auto-import
📲 On-demand components auto importing for Vue
npm install unplugin-vue-components
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
3,848 Stars
654 Commits
353 Forks
21 Watching
1 Branches
144 Contributors
Updated on 27 Nov 2024
TypeScript (99.92%)
JavaScript (0.08%)
Cumulative downloads
Total Downloads
Last day
2%
60,979
Compared to previous day
Last week
1.3%
307,016
Compared to previous week
Last month
11.7%
1,281,627
Compared to previous month
Last year
70.2%
12,129,797
Compared to previous year
10
3
On-demand components auto importing for Vue.
1npm i unplugin-vue-components -D
vite-plugin-components
has been renamed tounplugin-vue-components
, see the migration guide.
1// vite.config.ts 2import Components from 'unplugin-vue-components/vite' 3 4export default defineConfig({ 5 plugins: [ 6 Components({ /* options */ }), 7 ], 8})
1// rollup.config.js 2import Components from 'unplugin-vue-components/rollup' 3 4export default { 5 plugins: [ 6 Components({ /* options */ }), 7 ], 8}
1// webpack.config.js 2module.exports = { 3 /* ... */ 4 plugins: [ 5 require('unplugin-vue-components/webpack').default({ /* options */ }), 6 ], 7}
1// rspack.config.js 2module.exports = { 3 /* ... */ 4 plugins: [ 5 require('unplugin-vue-components/rspack').default({ /* options */ }), 6 ], 7}
1// vue.config.js 2module.exports = { 3 /* ... */ 4 plugins: [ 5 require('unplugin-vue-components/webpack').default({ /* options */ }), 6 ], 7}
You can also rename the Vue configuration file to vue.config.mjs
and use static import syntax (you should use latest @vue/cli-service ^5.0.8
):
1// vue.config.mjs 2import Components from 'unplugin-vue-components/webpack' 3 4export default { 5 configureWebpack: { 6 plugins: [ 7 Components({ /* options */ }), 8 ], 9 }, 10}
1// esbuild.config.js 2import { build } from 'esbuild' 3import Components from 'unplugin-vue-components/esbuild' 4 5build({ 6 /* ... */ 7 plugins: [ 8 Components({ 9 /* options */ 10 }), 11 ], 12})
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
1<template> 2 <div> 3 <HelloWorld msg="Hello Vue 3.0 + Vite" /> 4 </div> 5</template> 6 7<script> 8 export default { 9 name: 'App', 10 } 11</script>
into this
1<template> 2 <div> 3 <HelloWorld msg="Hello Vue 3.0 + Vite" /> 4 </div> 5</template> 6 7<script> 8 import HelloWorld from './src/components/HelloWorld.vue' 9 10 export default { 11 name: 'App', 12 components: { 13 HelloWorld, 14 }, 15 } 16</script>
Note By default this plugin will import components in the
src/components
path. You can customize it using thedirs
option.
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.
1Components({
2 dts: true, // enabled by default if `typescript` is installed
3})
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 yourtsconfig.json
underinclude
.
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:
@vant/auto-import-resolver
- Vant's own auto-import resolver@varlet/import-resolver
- Varlet's own auto-import resolver1import { 2 AntDesignVueResolver, 3 ElementPlusResolver, 4 VantResolver, 5} from 'unplugin-vue-components/resolvers' 6// vite.config.js 7import Components from 'unplugin-vue-components/vite' 8 9// your plugin installation 10Components({ 11 resolvers: [ 12 AntDesignVueResolver(), 13 ElementPlusResolver(), 14 VantResolver(), 15 ], 16})
You can also write your own resolver quickly:
1Components({ 2 resolvers: [ 3 // example of importing Vant 4 (componentName) => { 5 // where `componentName` is always CapitalCase 6 if (componentName.startsWith('Van')) 7 return { name: componentName.slice(3), from: 'vant' } 8 }, 9 ], 10})
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.
1Components({ 2 dts: true, 3 types: [{ 4 from: 'vue-router', 5 names: ['RouterLink', 'RouterView'], 6 }], 7})
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:
1Components({ 2 // Disable type only registration 3 types: [], 4})
vite-plugin-components
package.json
1{ 2 "devDependencies": { 3- "vite-plugin-components": "*", 4+ "unplugin-vue-components": "^0.14.0", 5 } 6}
vite.config.js
1- import Components, { ElementPlusResolver } from 'vite-plugin-components' 2+ import Components from 'unplugin-vue-components/vite' 3+ import { ElementPlusResolver } from 'unplugin-vue-components/resolvers' 4 5export default { 6 plugins: [ 7 /* ... */ 8 Components({ 9 /* ... */ 10 11 // `customComponentsResolvers` has renamed to `resolver` 12- customComponentsResolvers: [ 13+ resolvers: [ 14 ElementPlusResolver(), 15 ], 16 17 // `globalComponentsDeclaration` has renamed to `dts` 18- globalComponentsDeclaration: true, 19+ dts: true, 20 21 // `customLoaderMatcher` is depreacted, use `include` instead 22- customLoaderMatcher: id => id.endsWith('.md'), 23+ include: [/\.vue$/, /\.vue\?vue/, /\.md$/], 24 }), 25 ], 26}
The following show the default values of the configuration
1Components({
2 // relative paths to the directory to search for components.
3 dirs: ['src/components'],
4
5 // valid file extensions for components.
6 extensions: ['vue'],
7
8 // Glob patterns to match file names to be detected as components.
9 // When specified, the `dirs`, `extensions`, and `directoryAsNamespace` options will be ignored.
10 // If you want to exclude components being registered, use negative globs with leading `!`.
11 globs: ['src/components/*.{vue}'],
12
13 // search for subdirectories
14 deep: true,
15
16 // resolvers for custom components
17 resolvers: [],
18
19 // generate `components.d.ts` global declarations,
20 // also accepts a path for custom filename
21 // default: `true` if package typescript is installed
22 dts: false,
23
24 // Allow subdirectories as namespace prefix for components.
25 directoryAsNamespace: false,
26
27 // Collapse same prefixes (camel-sensitive) of folders and components
28 // to prevent duplication inside namespaced component name.
29 // works when `directoryAsNamespace: true`
30 collapseSamePrefixes: false,
31
32 // Subdirectory paths for ignoring namespace prefixes.
33 // works when `directoryAsNamespace: true`
34 globalNamespaces: [],
35
36 // auto import for directives
37 // default: `true` for Vue 3, `false` for Vue 2
38 // Babel is needed to do the transformation for Vue 2, it's disabled by default for performance concerns.
39 // To install Babel, run: `npm install -D @babel/parser`
40 directives: true,
41
42 // Transform path before resolving
43 importPathTransform: v => v,
44
45 // Allow for components to override other components with the same name
46 allowOverrides: false,
47
48 // Filters for transforming targets (components to insert the auto import)
49 // Note these are NOT about including/excluding components registered - use `globs` or `excludeNames` for that
50 include: [/\.vue$/, /\.vue\?vue/],
51 exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/],
52
53 // Filters for component names that will not be imported
54 // Use for globally imported async components or other conflicts that the plugin cannot detect
55 excludeNames: [/^Async.+/],
56
57 // Vue version of project. It will detect automatically if not specified.
58 // Acceptable value: 2 | 2.7 | 3
59 version: 2.7,
60
61 // Only provide types of components in library (registered globally)
62 types: []
63})
Vitesse starter template.
Thanks to @brattonross, this project is heavily inspired by vite-plugin-voie.
MIT License © 2020-PRESENT Anthony Fu
No vulnerabilities found.
No security vulnerabilities found.