版博士V2.0程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

README.md 7.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 [Vite]
  95. module.exports = {
  96. vitePlugins: [
  97. ['unplugin-auto-import/vite', { /* options */ }],
  98. ],
  99. }
  100. ```
  101. ```ts
  102. // quasar.conf.js [Webpack]
  103. const AutoImportPlugin = require('unplugin-auto-import/webpack')
  104. module.exports = {
  105. build: {
  106. chainWebpack(chain) {
  107. chain.plugin('unplugin-auto-import').use(
  108. AutoImportPlugin({ /* options */ }),
  109. )
  110. },
  111. },
  112. }
  113. ```
  114. <br></details>
  115. <details>
  116. <summary>esbuild</summary><br>
  117. ```ts
  118. // esbuild.config.js
  119. import { build } from 'esbuild'
  120. build({
  121. /* ... */
  122. plugins: [
  123. require('unplugin-auto-import/esbuild')({
  124. /* options */
  125. }),
  126. ],
  127. })
  128. ```
  129. <br></details>
  130. <details>
  131. <summary>Astro</summary><br>
  132. ```ts
  133. // astro.config.mjs
  134. import AutoImport from 'unplugin-auto-import/astro'
  135. export default defineConfig({
  136. integrations: [
  137. AutoImport({
  138. /* options */
  139. })
  140. ],
  141. })
  142. ```
  143. <br></details>
  144. ## Configuration
  145. ```ts
  146. AutoImport({
  147. // targets to transform
  148. include: [
  149. /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
  150. /\.vue$/, /\.vue\?vue/, // .vue
  151. /\.md$/, // .md
  152. ],
  153. // global imports to register
  154. imports: [
  155. // presets
  156. 'vue',
  157. 'vue-router',
  158. // custom
  159. {
  160. '@vueuse/core': [
  161. // named imports
  162. 'useMouse', // import { useMouse } from '@vueuse/core',
  163. // alias
  164. ['useFetch', 'useMyFetch'], // import { useFetch as useMyFetch } from '@vueuse/core',
  165. ],
  166. 'axios': [
  167. // default imports
  168. ['default', 'axios'], // import { default as axios } from 'axios',
  169. ],
  170. '[package-name]': [
  171. '[import-names]',
  172. // alias
  173. ['[from]', '[alias]'],
  174. ],
  175. },
  176. // example type import
  177. {
  178. from: 'vue-router',
  179. imports: ['RouteLocationRaw'],
  180. type: true,
  181. },
  182. ],
  183. // Enable auto import by filename for default module exports under directories
  184. defaultExportByFilename: false,
  185. // Auto import for module exports under directories
  186. // by default it only scan one level of modules under the directory
  187. dirs: [
  188. // './hooks',
  189. // './composables' // only root modules
  190. // './composables/**', // all nested modules
  191. // ...
  192. ],
  193. // Filepath to generate corresponding .d.ts file.
  194. // Defaults to './auto-imports.d.ts' when `typescript` is installed locally.
  195. // Set `false` to disable.
  196. dts: './auto-imports.d.ts',
  197. // Cache the result of resolving, across multiple vite builds.
  198. // A custom path is supported.
  199. // When set to `true`, the cache will be stored in `node_modules/.cache/unplugin-auto-import.json`.
  200. cache: false,
  201. // Auto import inside Vue template
  202. // see https://github.com/unjs/unimport/pull/15 and https://github.com/unjs/unimport/pull/72
  203. vueTemplate: false,
  204. // Custom resolvers, compatible with `unplugin-vue-components`
  205. // see https://github.com/antfu/unplugin-auto-import/pull/23/
  206. resolvers: [
  207. /* ... */
  208. ],
  209. // Generate corresponding .eslintrc-auto-import.json file.
  210. // eslint globals Docs - https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
  211. eslintrc: {
  212. enabled: false, // Default `false`
  213. filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
  214. globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
  215. },
  216. })
  217. ```
  218. Refer to the [type definitions](./src/types.ts) for more options.
  219. ## Presets
  220. See [src/presets](./src/presets).
  221. ## TypeScript
  222. In order to properly hint types for auto-imported APIs
  223. <table>
  224. <tr>
  225. <td width="400px" valign="top">
  226. 1. Enable `options.dts` so that `auto-imports.d.ts` file is automatically generated
  227. 2. Make sure `auto-imports.d.ts` is not excluded in `tsconfig.json`
  228. </td>
  229. <td width="600px"><br>
  230. ```ts
  231. AutoImport({
  232. dts: true // or a custom path
  233. })
  234. ```
  235. </td>
  236. </tr>
  237. </table>
  238. ## ESLint
  239. > 💡 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.
  240. If you have encountered ESLint error of `no-undef`:
  241. <table>
  242. <tr>
  243. <td width="400px">
  244. 1. Enable `eslintrc.enabled`
  245. </td>
  246. <td width="600px"><br>
  247. ```ts
  248. AutoImport({
  249. eslintrc: {
  250. enabled: true, // <-- this
  251. },
  252. })
  253. ```
  254. </td></tr></table>
  255. <table><tr><td width="400px">
  256. 2. Update your `eslintrc`:
  257. [Extending Configuration Files](https://eslint.org/docs/user-guide/configuring/configuration-files#extending-configuration-files)
  258. </td>
  259. <td width="600px"><br>
  260. ```ts
  261. // .eslintrc.js
  262. module.exports = {
  263. extends: [
  264. './.eslintrc-auto-import.json',
  265. ],
  266. }
  267. ```
  268. </td>
  269. </tr>
  270. </table>
  271. ## FAQ
  272. ### Compare to [`unimport`](https://github.com/unjs/unimport)
  273. 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.
  274. ### Compare to [`vue-global-api`](https://github.com/antfu/vue-global-api)
  275. 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).
  276. ###### Pros
  277. - Flexible and customizable
  278. - Tree-shakable (on-demand transforming)
  279. - No global population
  280. ###### Cons
  281. - Relying on build tools integrations (while `vue-global-api` is pure runtime) - but hey, we have supported quite a few of them already!
  282. ## Sponsors
  283. <p align="center">
  284. <a href="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg">
  285. <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>
  286. </a>
  287. </p>
  288. ## License
  289. [MIT](./LICENSE) License © 2021 [Anthony Fu](https://github.com/antfu)