版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

README.md 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. # unplugin-vue-components
  2. [![NPM version](https://img.shields.io/npm/v/unplugin-vue-components?color=a1b858&label=)](https://www.npmjs.com/package/unplugin-vue-components)
  3. On-demand components auto importing for Vue.
  4. ###### Features
  5. - 💚 Supports both Vue 2 and Vue 3 out-of-the-box.
  6. - ✨ Supports both components and directives.
  7. - ⚡️ Supports Vite, Webpack, Vue CLI, Rollup, esbuild and more, powered by <a href="https://github.com/unjs/unplugin">unplugin</a>.
  8. - 🏝 Tree-shakable, only registers the components you use.
  9. - 🪐 Folder names as namespaces.
  10. - 🦾 Full TypeScript support.
  11. - 🌈 [Built-in resolvers](#importing-from-ui-libraries) for popular UI libraries.
  12. - 😃 Works perfectly with [unplugin-icons](https://github.com/antfu/unplugin-icons).
  13. <br>
  14. <p align="center">
  15. <a href="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg">
  16. <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>
  17. </a>
  18. </p>
  19. <br>
  20. ## Installation
  21. ```bash
  22. npm i unplugin-vue-components -D
  23. ```
  24. > **`vite-plugin-components` has been renamed to `unplugin-vue-components`**, see the [migration guide](#migrate-from-vite-plugin-components).
  25. <details>
  26. <summary>Vite</summary><br>
  27. ```ts
  28. // vite.config.ts
  29. import Components from 'unplugin-vue-components/vite'
  30. export default defineConfig({
  31. plugins: [
  32. Components({ /* options */ }),
  33. ],
  34. })
  35. ```
  36. <br></details>
  37. <details>
  38. <summary>Rollup</summary><br>
  39. ```ts
  40. // rollup.config.js
  41. import Components from 'unplugin-vue-components/rollup'
  42. export default {
  43. plugins: [
  44. Components({ /* options */ }),
  45. ],
  46. }
  47. ```
  48. <br></details>
  49. <details>
  50. <summary>Webpack</summary><br>
  51. ```ts
  52. // webpack.config.js
  53. module.exports = {
  54. /* ... */
  55. plugins: [
  56. require('unplugin-vue-components/webpack')({ /* options */ }),
  57. ],
  58. }
  59. ```
  60. <br></details>
  61. <details>
  62. <summary>Nuxt</summary><br>
  63. You might not need this plugin for Nuxt. Use [`@nuxt/components`](https://github.com/nuxt/components) instead.
  64. <br></details>
  65. <details>
  66. <summary>Vue CLI</summary><br>
  67. ```ts
  68. // vue.config.js
  69. module.exports = {
  70. configureWebpack: {
  71. plugins: [
  72. require('unplugin-vue-components/webpack')({ /* options */ }),
  73. ],
  74. },
  75. }
  76. ```
  77. <br></details>
  78. <details>
  79. <summary>esbuild</summary><br>
  80. ```ts
  81. // esbuild.config.js
  82. import { build } from 'esbuild'
  83. build({
  84. /* ... */
  85. plugins: [
  86. require('unplugin-vue-components/esbuild')({
  87. /* options */
  88. }),
  89. ],
  90. })
  91. ```
  92. <br></details>
  93. ## Usage
  94. 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.
  95. It will automatically turn this
  96. ```html
  97. <template>
  98. <div>
  99. <HelloWorld msg="Hello Vue 3.0 + Vite" />
  100. </div>
  101. </template>
  102. <script>
  103. export default {
  104. name: 'App'
  105. }
  106. </script>
  107. ```
  108. into this
  109. ```html
  110. <template>
  111. <div>
  112. <HelloWorld msg="Hello Vue 3.0 + Vite" />
  113. </div>
  114. </template>
  115. <script>
  116. import HelloWorld from './src/components/HelloWorld.vue'
  117. export default {
  118. name: 'App',
  119. components: {
  120. HelloWorld
  121. }
  122. }
  123. </script>
  124. ```
  125. > **Note**
  126. > By default this plugin will import components in the `src/components` path. You can customize it using the `dirs` option.
  127. ## TypeScript
  128. To get TypeScript support for auto-imported components, there is [a PR](https://github.com/vuejs/core/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.
  129. ```ts
  130. Components({
  131. dts: true, // enabled by default if `typescript` is installed
  132. })
  133. ```
  134. 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.
  135. > **Make sure you also add `components.d.ts` to your `tsconfig.json` under `include`.**
  136. ## Importing from UI Libraries
  137. We have several built-in resolvers for popular UI libraries like **Vuetify**, **Ant Design Vue**, and **Element Plus**, where you can enable them by:
  138. Supported Resolvers:
  139. - [Ant Design Vue](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/antdv.ts)
  140. - [Arco Design Vue](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/arco.ts)
  141. - [BootstrapVue](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/bootstrap-vue.ts)
  142. - [Element Plus](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/element-plus.ts)
  143. - [Element UI](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/element-ui.ts)
  144. - [Headless UI](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/headless-ui.ts)
  145. - [IDux](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/idux.ts)
  146. - [Inkline](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/inkline.ts)
  147. - [Ionic](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/ionic.ts)
  148. - [Naive UI](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/naive-ui.ts)
  149. - [Prime Vue](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/prime-vue.ts)
  150. - [Quasar](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/quasar.ts)
  151. - [TDesign](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/tdesign.ts)
  152. - [Vant](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/vant.ts)
  153. - [Varlet UI](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/varlet-ui.ts)
  154. - [VEUI](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/veui.ts)
  155. - [View UI](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/view-ui.ts)
  156. - [Vuetify](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/vuetify.ts)
  157. - [VueUse Components](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/vueuse.ts)
  158. - [VueUse Directives](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/vueuse-directive.ts)
  159. - [Dev UI](https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/devui.ts)
  160. ```ts
  161. // vite.config.js
  162. import Components from 'unplugin-vue-components/vite'
  163. import {
  164. AntDesignVueResolver,
  165. ElementPlusResolver,
  166. VantResolver,
  167. } from 'unplugin-vue-components/resolvers'
  168. // your plugin installation
  169. Components({
  170. resolvers: [
  171. AntDesignVueResolver(),
  172. ElementPlusResolver(),
  173. VantResolver(),
  174. ],
  175. })
  176. ```
  177. You can also write your own resolver quickly:
  178. ```ts
  179. Components({
  180. resolvers: [
  181. // example of importing Vant
  182. (componentName) => {
  183. // where `componentName` is always CapitalCase
  184. if (componentName.startsWith('Van'))
  185. return { name: componentName.slice(3), from: 'vant' }
  186. },
  187. ],
  188. })
  189. ```
  190. > [We no longer accept new resolvers](./src/core/resolvers/_READ_BEFORE_CONTRIBUTE.md).
  191. ## Types for global registered components
  192. 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.
  193. Thus `unplugin-vue-components` provided a way to only register types for global components.
  194. ```ts
  195. Components({
  196. dts: true,
  197. types: [{
  198. from: 'vue-router',
  199. names: ['RouterLink', 'RouterView'],
  200. }],
  201. })
  202. ```
  203. So the `RouterLink` and `RouterView` will be presented in `components.d.ts`.
  204. 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:
  205. ```ts
  206. Components({
  207. // Disable type only registration
  208. types: [],
  209. })
  210. ```
  211. ## Migrate from `vite-plugin-components`
  212. `package.json`
  213. ```diff
  214. {
  215. "devDependencies": {
  216. - "vite-plugin-components": "*",
  217. + "unplugin-vue-components": "^0.14.0",
  218. }
  219. }
  220. ```
  221. `vite.config.js`
  222. ```diff
  223. - import Components, { ElementPlusResolver } from 'vite-plugin-components'
  224. + import Components from 'unplugin-vue-components/vite'
  225. + import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  226. export default {
  227. plugins: [
  228. /* ... */
  229. Components({
  230. /* ... */
  231. // `customComponentsResolvers` has renamed to `resolver`
  232. - customComponentsResolvers: [
  233. + resolvers: [
  234. ElementPlusResolver(),
  235. ],
  236. // `globalComponentsDeclaration` has renamed to `dts`
  237. - globalComponentsDeclaration: true,
  238. + dts: true,
  239. // `customLoaderMatcher` is depreacted, use `include` instead
  240. - customLoaderMatcher: id => id.endsWith('.md'),
  241. + include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
  242. }),
  243. ],
  244. }
  245. ```
  246. ## Configuration
  247. The following show the default values of the configuration
  248. ```ts
  249. Components({
  250. // relative paths to the directory to search for components.
  251. dirs: ['src/components'],
  252. // valid file extensions for components.
  253. extensions: ['vue'],
  254. // Glob patterns to match file names to be detected as components.
  255. // When specified, the `dirs` and `extensions` options will be ignored.
  256. globs: ['src/components/*.{vue}'],
  257. // search for subdirectories
  258. deep: true,
  259. // resolvers for custom components
  260. resolvers: [],
  261. // generate `components.d.ts` global declarations,
  262. // also accepts a path for custom filename
  263. // default: `true` if package typescript is installed
  264. dts: false,
  265. // Allow subdirectories as namespace prefix for components.
  266. directoryAsNamespace: false,
  267. // Collapse same prefixes (camel-sensitive) of folders and components
  268. // to prevent duplication inside namespaced component name.
  269. // works when `directoryAsNamespace: true`
  270. collapseSamePrefixes: false,
  271. // Subdirectory paths for ignoring namespace prefixes.
  272. // works when `directoryAsNamespace: true`
  273. globalNamespaces: [],
  274. // auto import for directives
  275. // default: `true` for Vue 3, `false` for Vue 2
  276. // Babel is needed to do the transformation for Vue 2, it's disabled by default for performance concerns.
  277. // To install Babel, run: `npm install -D @babel/parser`
  278. directives: true,
  279. // Transform path before resolving
  280. importPathTransform: v => v,
  281. // Allow for components to override other components with the same name
  282. allowOverrides: false,
  283. // filters for transforming targets
  284. include: [/\.vue$/, /\.vue\?vue/],
  285. exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/],
  286. // Vue version of project. It will detect automatically if not specified.
  287. // Acceptable value: 2 | 2.7 | 3
  288. version: 2.7,
  289. // Only provide types of components in library (registered globally)
  290. types: []
  291. })
  292. ```
  293. ## Example
  294. [Vitesse](https://github.com/antfu/vitesse) starter template.
  295. ## Thanks
  296. Thanks to [@brattonross](https://github.com/brattonross), this project is heavily inspired by [vite-plugin-voie](https://github.com/vamplate/vite-plugin-voie).
  297. ## License
  298. MIT License © 2020-PRESENT [Anthony Fu](https://github.com/antfu)