bcc05b3bcc49e281f2ff40b7ea01b4f4de9e5f7eb569786ae104dd7f15c514b1fb8d7fe06ca40684a298e59d5c50124c89355b84fe6efdd093b454775262f6 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # unimport
  2. [![npm version][npm-version-src]][npm-version-href]
  3. [![npm downloads][npm-downloads-src]][npm-downloads-href]
  4. [![Github Actions][github-actions-src]][github-actions-href]
  5. [![Codecov][codecov-src]][codecov-href]
  6. > Unified utils for auto importing APIs in modules
  7. ## Features
  8. - Auto import register APIs for Vite, Webpack or esbuild powered by [unplugin](https://github.com/unjs/unplugin)
  9. - TypeScript declaration file generation
  10. - Auto import for custom APIs defined under specific directories
  11. - Auto import for Vue template
  12. ## Install
  13. ```sh
  14. # npm
  15. npm install unimport
  16. # yarn
  17. yarn add unimport
  18. # pnpm
  19. pnpm install unimport
  20. ```
  21. ## Usage
  22. ### Plugin Usage
  23. Powered by [unplugin](https://github.com/unjs/unplugin), `unimport` provides a plugin interface for bundlers.
  24. #### Vite / Rollup
  25. ```ts
  26. // vite.config.js / rollup.config.js
  27. import Unimport from 'unimport/unplugin'
  28. export default {
  29. plugins: [
  30. Unimport.vite({ /* plugin options */ })
  31. ]
  32. }
  33. ```
  34. #### Webpack
  35. ```ts
  36. // webpack.config.js
  37. import Unimport from 'unimport/unplugin'
  38. module.exports = {
  39. plugins: [
  40. Unimport.webpack({ /* plugin options */ })
  41. ]
  42. }
  43. ```
  44. ### Programmatic Usage
  45. ```js
  46. // ESM
  47. import { createUnimport } from 'unimport'
  48. // CommonJS
  49. const { createUnimport } = require('unimport')
  50. ```
  51. ```js
  52. const { injectImports } = createUnimport({
  53. imports: [{ name: 'fooBar', from: 'test-id' }]
  54. })
  55. // { code: "import { fooBar } from 'test-id';console.log(fooBar())" }
  56. console.log(injectImports('console.log(fooBar())'))
  57. ```
  58. ## Configurations
  59. ### Imports Item
  60. ###### Named import
  61. ```ts
  62. imports: [
  63. { name: 'ref', from: 'vue' },
  64. { name: 'useState', as: 'useSignal', from: 'react' },
  65. ]
  66. ```
  67. Will be injected as:
  68. ```ts
  69. import { ref } from 'vue'
  70. import { useState as useSignal } from 'react'
  71. ```
  72. ###### Default import
  73. ```ts
  74. imports: [
  75. { name: 'default', as: '_', from: 'lodash' }
  76. ]
  77. ```
  78. Will be injected as:
  79. ```ts
  80. import _ from 'lodash'
  81. ```
  82. ###### Custom Presets
  83. Presets are provides as a shorthand for declaring imports from the same package:
  84. ```ts
  85. presets: [
  86. {
  87. from: 'vue',
  88. imports: [
  89. 'ref',
  90. 'reactive',
  91. // ...
  92. ]
  93. }
  94. ]
  95. ```
  96. Will be equivalent as:
  97. ```ts
  98. imports: [
  99. { name: 'ref', from: 'vue' },
  100. { name: 'reactive', from: 'vue' },
  101. // ...
  102. ]
  103. ```
  104. ###### Built-in Presets
  105. `unimport` also provides some builtin presets for common libraries:
  106. ```ts
  107. presets: [
  108. 'vue',
  109. 'pinia',
  110. 'vue-i18n',
  111. // ...
  112. ]
  113. ```
  114. You can check out [`src/presets`](./src/presets/) for all the options available or refer to the type declaration.
  115. ###### Exports Auto Scan
  116. Since `unimport` v0.7.0, we also support auto scanning the examples from a local installed package, for example:
  117. ```ts
  118. presets: [
  119. {
  120. package: 'h3',
  121. ignore: ['isStream', /^[A-Z]/, /^[a-z]*$/, r => r.length > 8]
  122. }
  123. ]
  124. ```
  125. This will be expanded into:
  126. ```ts
  127. imports: [
  128. {
  129. "from": "h3",
  130. "name": "appendHeader",
  131. },
  132. {
  133. "from": "h3",
  134. "name": "appendHeaders",
  135. },
  136. {
  137. "from": "h3",
  138. "name": "appendResponseHeader",
  139. },
  140. // ...
  141. ]
  142. ```
  143. The `ignore` option is used to filter out the exports, it can be a string, regex or a function that returns a boolean.
  144. By default, the result is strongly cached by the version of the package. You can disable this by setting `cache: false`.
  145. ### Type Declarations
  146. ```ts
  147. Unimport.vite({
  148. dts: true // or a path to generated file
  149. })
  150. ```
  151. ### Directory Auto Import
  152. ```ts
  153. {
  154. dirs: [
  155. './composables/*'
  156. ]
  157. }
  158. ```
  159. Named exports for modules under `./composables/*` will be registered for auto imports.
  160. ### Opt-out Auto Import
  161. You can opt-out auto import for specific modules by adding a comment:
  162. ```ts
  163. // @unimport-disable
  164. ```
  165. It's can be customized by setting `commentsDisable`:
  166. ```ts
  167. Unimport.vite({
  168. commentsDisable: [
  169. '@unimport-disable',
  170. '@custom-imports-disable',
  171. ]
  172. })
  173. ```
  174. ### Vue Template Auto Import
  175. In Vue's template, the usage of API is in a different context than plain modules. Thus some custom transformations are required. To enable it, set `addons.vueTemplate` to `true`:
  176. ```ts
  177. Unimport.vite({
  178. addons: {
  179. vueTemplate: true
  180. }
  181. })
  182. ```
  183. #### Caveats
  184. When auto-import a ref, inline operations won't be auto-unwrapped.
  185. ```ts
  186. export const counter = ref(0)
  187. ```
  188. ```html
  189. <template>
  190. <!-- this is ok -->
  191. <div>{{ counter }}</div>
  192. <!-- counter here is a ref, this won't work, volar will throw -->
  193. <div>{{ counter + 1 }}</div>
  194. <!-- use this instead -->
  195. <div>{{ counter.value + 1 }}</div>
  196. </template>
  197. ```
  198. We recommend using [Volar](https://github.com/johnsoncodehk/volar) for type checking, which will help you to identify the misusage.
  199. ## 💻 Development
  200. - Clone this repository
  201. - Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` (use `npm i -g corepack` for Node.js < 16.10)
  202. - Install dependencies using `pnpm install`
  203. - Run interactive tests using `pnpm dev`
  204. ## License
  205. Made with 💛
  206. Published under [MIT License](./LICENSE).
  207. <!-- Badges -->
  208. [npm-version-src]: https://img.shields.io/npm/v/unimport?style=flat-square
  209. [npm-version-href]: https://npmjs.com/package/unimport
  210. [npm-downloads-src]: https://img.shields.io/npm/dm/unimport?style=flat-square
  211. [npm-downloads-href]: https://npmjs.com/package/unimport
  212. [github-actions-src]: https://img.shields.io/github/workflow/status/unjs/unimport/ci/main?style=flat-square
  213. [github-actions-href]: https://github.com/unjs/unimport/actions?query=workflow%3Aci
  214. [codecov-src]: https://img.shields.io/codecov/c/gh/unjs/unimport/main?style=flat-square
  215. [codecov-href]: https://codecov.io/gh/unjs/unimport