|
| 1 | +# unplugin-auto-import |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/unplugin-auto-import) |
| 4 | + |
| 5 | +Register global imports on demand for Vite, Rollup, and Webpack. With TypeScript supports. Powered by [unplugin](https://github.com/unjs/unplugin). |
| 6 | + |
| 7 | +## Install |
| 8 | + |
| 9 | +```bash |
| 10 | +npm i unplugin-auto-import |
| 11 | +``` |
| 12 | + |
| 13 | +<details> |
| 14 | +<summary>Vite</summary><br> |
| 15 | + |
| 16 | +```ts |
| 17 | +// vite.config.ts |
| 18 | +import AutoImport from 'unplugin-auto-import/vite' |
| 19 | + |
| 20 | +export default defineConfig({ |
| 21 | + plugins: [ |
| 22 | + AutoImport({ /* options */ }), |
| 23 | + ], |
| 24 | +}) |
| 25 | +``` |
| 26 | + |
| 27 | +Example: [`playground/`](./playground/) |
| 28 | + |
| 29 | +<br></details> |
| 30 | + |
| 31 | +<details> |
| 32 | +<summary>Rollup</summary><br> |
| 33 | + |
| 34 | +```ts |
| 35 | +// rollup.config.js |
| 36 | +import AutoImport from 'unplugin-auto-import/rollup' |
| 37 | + |
| 38 | +export default { |
| 39 | + plugins: [ |
| 40 | + AutoImport({ /* options */ }), |
| 41 | + // other plugins |
| 42 | + ], |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +<br></details> |
| 47 | + |
| 48 | + |
| 49 | +<details> |
| 50 | +<summary>Webpack</summary><br> |
| 51 | + |
| 52 | +```ts |
| 53 | +// webpack.config.js |
| 54 | +module.exports = { |
| 55 | + /* ... */ |
| 56 | + plugins: [ |
| 57 | + require('unplugin-auto-import/webpack')({ /* options */ }) |
| 58 | + ] |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +<br></details> |
| 63 | + |
| 64 | +<details> |
| 65 | +<summary>Nuxt</summary><br> |
| 66 | + |
| 67 | +```ts |
| 68 | +// nuxt.config.js |
| 69 | +export default { |
| 70 | + buildModules: [ |
| 71 | + ['unplugin-auto-import/nuxt', { /* options */ }], |
| 72 | + ], |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +> This module works for both Nuxt 2 and [Nuxt Vite](https://github.com/nuxt/vite) |
| 77 | +
|
| 78 | +<br></details> |
| 79 | + |
| 80 | +<details> |
| 81 | +<summary>Vue CLI</summary><br> |
| 82 | + |
| 83 | +```ts |
| 84 | +// vue.config.js |
| 85 | +module.exports = { |
| 86 | + configureWebpack: { |
| 87 | + plugins: [ |
| 88 | + require('unplugin-auto-import/webpack')({ /* options */ }), |
| 89 | + ], |
| 90 | + }, |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +<br></details> |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +## Configuration |
| 99 | + |
| 100 | +```ts |
| 101 | +AutoImport({ |
| 102 | + // targets to transform |
| 103 | + include: [ |
| 104 | + /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx |
| 105 | + /\.vue\??/, // .vue |
| 106 | + ], |
| 107 | + |
| 108 | + // global imports to register |
| 109 | + imports: [ |
| 110 | + // presets |
| 111 | + 'vue', |
| 112 | + 'vue-router', |
| 113 | + // custom |
| 114 | + { |
| 115 | + '@vueuse/core': [ |
| 116 | + 'useMouse' |
| 117 | + ], |
| 118 | + '[package-name]': [ |
| 119 | + '[import-names]', |
| 120 | + // alias |
| 121 | + ['[from]', '[alias]'] |
| 122 | + ] |
| 123 | + } |
| 124 | + ], |
| 125 | +}) |
| 126 | +``` |
| 127 | + |
| 128 | +Refer to the [type definitions](./src/types.ts) for more options. |
| 129 | + |
| 130 | +## FAQ |
| 131 | + |
| 132 | +### Compare to [`vue-global-api`](https://github.com/antfu/vue-global-api) |
| 133 | + |
| 134 | +You can think this plugin as a successor of `vue-global-api`, which offers much more flexibility and it's no longer bound to Vue exclusively. Now you can use it with any libraries you want (e.g. React). |
| 135 | + |
| 136 | +###### Pros |
| 137 | + |
| 138 | +- Flexible and customizable |
| 139 | +- Tree-shakable (on-demand transforming) |
| 140 | +- No global population |
| 141 | + |
| 142 | +###### Cons |
| 143 | + |
| 144 | +- Relying on build tools integrations (while `vue-global-api` is pure runtime) - but hey, we have supported quite a few of them already! |
| 145 | + |
| 146 | +## Sponsors |
| 147 | + |
| 148 | +<p align="center"> |
| 149 | + <a href="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg"> |
| 150 | + <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/> |
| 151 | + </a> |
| 152 | +</p> |
| 153 | + |
| 154 | +## License |
| 155 | + |
| 156 | +[MIT](./LICENSE) License © 2021 [Anthony Fu](https://github.com/antfu) |
0 commit comments