a6c5bb4cc6f8f257bb874551f981c14a804ce5fa04b1033ed94c9ec30588552ad83672c1f5b1de8ecacb57dad76f10d255215f5322303adf94fca60fbc3814 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. # unplugin-auto-import
  2. [![NPM version](https://img.shields.io/npm/v/unplugin-auto-import?color=a1b858&label=)](https://www.npmjs.com/package/unplugin-auto-import)
  3. Auto import APIs on-demand for Vite, Webpack, Rollup and esbuild. With TypeScript support. Powered by [unplugin](https://github.com/unjs/unplugin).
  4. ---
  5. without
  6. ```ts
  7. import { computed, ref } from 'vue'
  8. const count = ref(0)
  9. const doubled = computed(() => count.value * 2)
  10. ```
  11. with
  12. ```ts
  13. const count = ref(0)
  14. const doubled = computed(() => count.value * 2)
  15. ```
  16. ---
  17. without
  18. ```tsx
  19. import { useState } from 'react'
  20. export function Counter() {
  21. const [count, setCount] = useState(0)
  22. return <div>{ count }</div>
  23. }
  24. ```
  25. with
  26. ```tsx
  27. export function Counter() {
  28. const [count, setCount] = useState(0)
  29. return <div>{ count }</div>
  30. }
  31. ```
  32. ## Install
  33. ```bash
  34. npm i -D unplugin-auto-import
  35. ```
  36. <details>
  37. <summary>Vite</summary><br>
  38. ```ts
  39. // vite.config.ts
  40. import AutoImport from 'unplugin-auto-import/vite'
  41. export default defineConfig({
  42. plugins: [
  43. AutoImport({ /* options */ }),
  44. ],
  45. })
  46. ```
  47. Example: [`playground/`](./playground/)
  48. <br></details>
  49. <details>
  50. <summary>Rollup</summary><br>
  51. ```ts
  52. // rollup.config.js
  53. import AutoImport from 'unplugin-auto-import/rollup'
  54. export default {
  55. plugins: [
  56. AutoImport({ /* options */ }),
  57. // other plugins
  58. ],
  59. }
  60. ```
  61. <br></details>
  62. <details>
  63. <summary>Webpack</summary><br>
  64. ```ts
  65. // webpack.config.js
  66. module.exports = {
  67. /* ... */
  68. plugins: [
  69. require('unplugin-auto-import/webpack')({ /* options */ }),
  70. ],
  71. }
  72. ```
  73. <br></details>
  74. <details>
  75. <summary>Nuxt</summary><br>
  76. > You **don't need** this plugin for Nuxt, it's already builtin.
  77. <br></details>
  78. <details>
  79. <summary>Vue CLI</summary><br>
  80. ```ts
  81. // vue.config.js
  82. module.exports = {
  83. configureWebpack: {
  84. plugins: [
  85. require('unplugin-auto-import/webpack')({ /* options */ }),
  86. ],
  87. },
  88. }
  89. ```
  90. <br></details>
  91. <details>
  92. <summary>Quasar</summary><br>
  93. ```ts
  94. // quasar.conf.js
  95. const AutoImportPlugin = require('unplugin-auto-import/webpack')
  96. module.exports = {
  97. build: {
  98. chainWebpack(chain) {
  99. chain.plugin('unplugin-auto-import').use(
  100. AutoImportPlugin({ /* options */ }),
  101. )
  102. },
  103. },
  104. }
  105. ```
  106. <br></details>
  107. <details>
  108. <summary>esbuild</summary><br>
  109. ```ts
  110. // esbuild.config.js
  111. import { build } from 'esbuild'
  112. build({
  113. /* ... */
  114. plugins: [
  115. require('unplugin-auto-import/esbuild')({
  116. /* options */
  117. }),
  118. ],
  119. })
  120. ```
  121. <br></details>
  122. <details>
  123. <summary>Astro</summary><br>
  124. ```ts
  125. // astro.config.mjs
  126. import AutoImport from 'unplugin-auto-import/astro'
  127. export default defineConfig({
  128. integrations: [
  129. AutoImport({
  130. /* options */
  131. })
  132. ],
  133. })
  134. ```
  135. <br></details>
  136. ## Configuration
  137. ```ts
  138. AutoImport({
  139. // targets to transform
  140. include: [
  141. /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
  142. /\.vue$/, /\.vue\?vue/, // .vue
  143. /\.md$/, // .md
  144. ],
  145. // global imports to register
  146. imports: [
  147. // presets
  148. 'vue',
  149. 'vue-router',
  150. // custom
  151. {
  152. '@vueuse/core': [
  153. // named imports
  154. 'useMouse', // import { useMouse } from '@vueuse/core',
  155. // alias
  156. ['useFetch', 'useMyFetch'], // import { useFetch as useMyFetch } from '@vueuse/core',
  157. ],
  158. 'axios': [
  159. // default imports
  160. ['default', 'axios'], // import { default as axios } from 'axios',
  161. ],
  162. '[package-name]': [
  163. '[import-names]',
  164. // alias
  165. ['[from]', '[alias]'],
  166. ],
  167. },
  168. ],
  169. // Auto import for module exports under directories
  170. // by default it only scan one level of modules under the directory
  171. dirs: [
  172. // './hooks',
  173. // './composables' // only root modules
  174. // './composables/**', // all nested modules
  175. // ...
  176. ],
  177. // Filepath to generate corresponding .d.ts file.
  178. // Defaults to './auto-imports.d.ts' when `typescript` is installed locally.
  179. // Set `false` to disable.
  180. dts: './auto-imports.d.ts',
  181. // Auto import inside Vue template
  182. // see https://github.com/unjs/unimport/pull/15 and https://github.com/unjs/unimport/pull/72
  183. vueTemplate: false,
  184. // Custom resolvers, compatible with `unplugin-vue-components`
  185. // see https://github.com/antfu/unplugin-auto-import/pull/23/
  186. resolvers: [
  187. /* ... */
  188. ],
  189. // Generate corresponding .eslintrc-auto-import.json file.
  190. // eslint globals Docs - https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
  191. eslintrc: {
  192. enabled: false, // Default `false`
  193. filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
  194. globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
  195. },
  196. })
  197. ```
  198. Refer to the [type definitions](./src/types.ts) for more options.
  199. ## Presets
  200. See [src/presets](./src/presets).
  201. ## TypeScript
  202. In order to properly hint types for auto-imported APIs
  203. <table>
  204. <tr>
  205. <td width="400px" valign="top">
  206. 1. Enable `options.dts` so that `auto-imports.d.ts` file is automatically generated
  207. 2. Make sure `auto-imports.d.ts` is not excluded in `tsconfig.json`
  208. </td>
  209. <td width="600px"><br>
  210. ```ts
  211. AutoImport({
  212. dts: true // or a custom path
  213. })
  214. ```
  215. </td>
  216. </tr>
  217. </table>
  218. ## ESLint
  219. > 💡 When using TypeScript, we recommend to **disable** `no-undef` rule directly as TypeScript already check for them and you don't need to worry about this.
  220. If you have encountered ESLint error of `no-undef`:
  221. <table>
  222. <tr>
  223. <td width="400px">
  224. 1. Enable `eslintrc.enabled`
  225. </td>
  226. <td width="600px"><br>
  227. ```ts
  228. AutoImport({
  229. eslintrc: {
  230. enabled: true, // <-- this
  231. },
  232. })
  233. ```
  234. </td></tr></table>
  235. <table><tr><td width="400px">
  236. 2. Update your `eslintrc`:
  237. [Extending Configuration Files](https://eslint.org/docs/user-guide/configuring/configuration-files#extending-configuration-files)
  238. </td>
  239. <td width="600px"><br>
  240. ```ts
  241. // .eslintrc.js
  242. module.exports = {
  243. extends: [
  244. './.eslintrc-auto-import.json',
  245. ],
  246. }
  247. ```
  248. </td>
  249. </tr>
  250. </table>
  251. ## FAQ
  252. ### Compare to [`unimport`](https://github.com/unjs/unimport)
  253. From v0.8.0, `unplugin-auto-import` **uses** `unimport` underneath. `unimport` is designed to be a lower level tool (it also powered Nuxt's auto import). You can think `unplugin-auto-import` is a wrapper of it that provides more user-friendly config APIs and capability like resolvers. Development of new features will mostly happend in `unimport` from now.
  254. ### Compare to [`vue-global-api`](https://github.com/antfu/vue-global-api)
  255. You can think of this plugin as a successor to `vue-global-api`, but offering much more flexibility and bindings with libraries other than Vue (e.g. React).
  256. ###### Pros
  257. - Flexible and customizable
  258. - Tree-shakable (on-demand transforming)
  259. - No global population
  260. ###### Cons
  261. - Relying on build tools integrations (while `vue-global-api` is pure runtime) - but hey, we have supported quite a few of them already!
  262. ## Sponsors
  263. <p align="center">
  264. <a href="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg">
  265. <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>
  266. </a>
  267. </p>
  268. ## License
  269. [MIT](./LICENSE) License © 2021 [Anthony Fu](https://github.com/antfu)