Vite 中文文档 Vite 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • vite

    • 指引
    • 为什么选 Vite
    • 开始
    • 功能
    • 命令行界面
    • 使用插件
    • 依赖预构建
    • 静态资源处理
    • 构建生产版本
    • 部署静态站点
    • 环境变量和模式
    • 服务端渲染
    • 后端集成
    • 与其他工具比较
    • 故障排除
    • 从 v3 迁移
  • API

  • 配置参考

  • vite

  • API

  • 配置参考

unplugin-auto-import


Auto import APIs on-demand for Vite, Webpack, Rspack, Rollup and esbuild. With TypeScript support. Powered by unplugin.

without

  1. ``` ts
  2. import { computed, ref } from 'vue'

  3. const count = ref(0)
  4. const doubled = computed(() => count.value * 2)
  5. ```

with

  1. ``` ts
  2. const count = ref(0)
  3. const doubled = computed(() => count.value * 2)
  4. ```

without

  1. ``` tsx
  2. import { useState } from 'react'

  3. export function Counter() {
  4.   const [count, setCount] = useState(0)
  5.   return <div>{ count }</div>
  6. }
  7. ```

with

  1. ``` tsx
  2. export function Counter() {
  3.   const [count, setCount] = useState(0)
  4.   return <div>{ count }</div>
  5. }
  6. ```

Install


  1. ``` shell
  2. npm i -D unplugin-auto-import
  3. ```

Vite
  1. ``` ts
  2. // vite.config.ts
  3. import AutoImport from 'unplugin-auto-import/vite'

  4. export default defineConfig({
  5.   plugins: [
  6.     AutoImport({ /* options */ }),
  7.   ],
  8. })
  9. ```

Example: playground/

Rollup
  1. ``` ts
  2. // rollup.config.js
  3. import AutoImport from 'unplugin-auto-import/rollup'

  4. export default {
  5.   plugins: [
  6.     AutoImport({ /* options */ }),
  7.     // other plugins
  8.   ],
  9. }
  10. ```

Webpack
  1. ``` ts
  2. // webpack.config.js
  3. module.exports = {
  4.   /* ... */
  5.   plugins: [
  6.     require('unplugin-auto-import/webpack')({ /* options */ }),
  7.   ],
  8. }
  9. ```

Rspack
  1. ``` ts
  2. // rspack.config.js
  3. module.exports = {
  4.   /* ... */
  5.   plugins: [
  6.     require('unplugin-auto-import/rspack')({ /* options */ }),
  7.   ],
  8. }
  9. ```

Nuxt> You don't needthis plugin for Nuxt, it's already builtin.

Vue CLI
  1. ``` ts
  2. // vue.config.js
  3. module.exports = {
  4.   configureWebpack: {
  5.     plugins: [
  6.       require('unplugin-auto-import/webpack')({ /* options */ }),
  7.     ],
  8.   },
  9. }
  10. ```

Quasar
  1. ``` ts
  2. // quasar.conf.js [Vite]
  3. module.exports = {
  4.   vitePlugins: [
  5.     ['unplugin-auto-import/vite', { /* options */ }],
  6.   ],
  7. }
  8. ```

  1. ``` ts
  2. // quasar.conf.js [Webpack]
  3. const AutoImportPlugin = require('unplugin-auto-import/webpack')

  4. module.exports = {
  5.   build: {
  6.     chainWebpack(chain) {
  7.       chain.plugin('unplugin-auto-import').use(
  8.         AutoImportPlugin({ /* options */ }),
  9.       )
  10.     },
  11.   },
  12. }
  13. ```

esbuild
  1. ``` ts
  2. // esbuild.config.js
  3. import { build } from 'esbuild'

  4. build({
  5.   /* ... */
  6.   plugins: [
  7.     require('unplugin-auto-import/esbuild')({
  8.       /* options */
  9.     }),
  10.   ],
  11. })
  12. ```

Astro
  1. ``` ts
  2. // astro.config.mjs
  3. import AutoImport from 'unplugin-auto-import/astro'

  4. export default defineConfig({
  5.   integrations: [
  6.     AutoImport({
  7.       /* options */
  8.     })
  9.   ],
  10. })
  11. ```

Configuration


  1. ``` ts
  2. AutoImport({
  3.   // targets to transform
  4.   include: [
  5.     /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
  6.     /\.vue$/, /\.vue\?vue/, // .vue
  7.     /\.md$/, // .md
  8.   ],

  9.   // global imports to register
  10.   imports: [
  11.     // presets
  12.     'vue',
  13.     'vue-router',
  14.     // custom
  15.     {
  16.       '@vueuse/core': [
  17.         // named imports
  18.         'useMouse', // import { useMouse } from '@vueuse/core',
  19.         // alias
  20.         ['useFetch', 'useMyFetch'], // import { useFetch as useMyFetch } from '@vueuse/core',
  21.       ],
  22.       'axios': [
  23.         // default imports
  24.         ['default', 'axios'], // import { default as axios } from 'axios',
  25.       ],
  26.       '[package-name]': [
  27.         '[import-names]',
  28.         // alias
  29.         ['[from]', '[alias]'],
  30.       ],
  31.     },
  32.     // example type import
  33.     {
  34.       from: 'vue-router',
  35.       imports: ['RouteLocationRaw'],
  36.       type: true,
  37.     },
  38.   ],
  39.   // Enable auto import by filename for default module exports under directories
  40.   defaultExportByFilename: false,

  41.   // Auto import for module exports under directories
  42.   // by default it only scan one level of modules under the directory
  43.   dirs: [
  44.     // './hooks',
  45.     // './composables' // only root modules
  46.     // './composables/**', // all nested modules
  47.     // ...
  48.   ],

  49.   // Filepath to generate corresponding .d.ts file.
  50.   // Defaults to './auto-imports.d.ts' when `typescript` is installed locally.
  51.   // Set `false` to disable.
  52.   dts: './auto-imports.d.ts',

  53.   // Auto import inside Vue template
  54.   // see https://github.com/unjs/unimport/pull/15 and https://github.com/unjs/unimport/pull/72
  55.   vueTemplate: false,

  56.   // Custom resolvers, compatible with `unplugin-vue-components`
  57.   // see https://github.com/antfu/unplugin-auto-import/pull/23/
  58.   resolvers: [
  59.     /* ... */
  60.   ],

  61.   // Inject the imports at the end of other imports
  62.   injectAtEnd: true,

  63.   // Generate corresponding .eslintrc-auto-import.json file.
  64.   // eslint globals Docs - https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
  65.   eslintrc: {
  66.     enabled: false, // Default `false`
  67.     filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
  68.     globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
  69.   },
  70. })
  71. ```

Refer to the type definitions for more options.

Presets


See src/presets.

TypeScript


In order to properly hint types for auto-imported APIs

:--- :---
Enable options.dts so that auto-imports.d.ts file is automatically generated Make sure auto-imports.d.ts is not excluded in tsconfig.json <span class="pl-smi">AutoImport</span><span class="pl-kos">(</span><span class="pl-kos">{</span>   <span class="pl-c1">dts</span>: <span class="pl-c1">true</span> <span class="pl-c">// or a custom path</span> <span class="pl-kos">}</span><span class="pl-kos">)</span>

ESLint


💡When using TypeScript, we recommend to disableno-undef rule directly as TypeScript already check for them and you don't need to worry about this.


If you have encountered ESLint error of no-undef :

:--- :---
Enable eslintrc.enabled <span class="pl-smi">AutoImport</span><span class="pl-kos">(</span><span class="pl-kos">{</span>   <span class="pl-c1">eslintrc</span>: <span class="pl-kos">{</span>     <span class="pl-c1">enabled</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span> <span class="pl-c">// &lt;-- this</span>   <span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-kos">}</span><span class="pl-kos">)</span>

:--- :---
Update your eslintrc: Extending Configuration Files <span class="pl-c">// .eslintrc.js</span> <span class="pl-smi">module</span><span class="pl-kos">.</span><span class="pl-c1">exports</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span>   <span class="pl-c1">extends</span>: <span class="pl-kos">[</span>     <span class="pl-s">'./.eslintrc-auto-import.json'</span><span class="pl-kos">,</span>   <span class="pl-kos">]</span><span class="pl-kos">,</span> <span class="pl-kos">}</span>

FAQ


Compare to unimport


From v0.8.0, unplugin-auto-import usesunimport 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 capabilities like resolvers. Development of new features will mostly happen in unimport` from now.

Compare to vue-global-api


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).

Pros

Flexible and customizable
Tree-shakable (on-demand transforming)
No global population

Cons

Relying on build tools integrations (while vue-global-api is pure runtime) - but hey, we have supported quite a few of them already!

Sponsors


License


MIT License © 2021 Anthony Fu
Last Updated: 2023-08-10 08:43:59