# vite-auto-import-resolvers **Repository Path**: dishait/vite-auto-import-resolvers ## Basic Information - **Project Name**: vite-auto-import-resolvers - **Description**: unplugin-auto-import ็š„ vite resolver - **Primary Language**: TypeScript - **License**: MIT - **Default Branch**: main - **Homepage**: https://www.npmjs.com/package/vite-auto-import-resolvers - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 1 - **Created**: 2022-02-08 - **Last Updated**: 2024-07-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: unplugin-auto-import, vite, Vue, TypeScript, resolver ## README # vite-auto-import-resolvers The vite resolvers of [unplugin-auto-import]((https://github.com/antfu/unplugin-auto-import)) mainly deals with the `API` of the `vite` project itself, which is automatically imported on demand.
## README ๐Ÿฆ‰ [็ฎ€ไฝ“ไธญๆ–‡](./README.md) | English

## Motation ๐Ÿ‡ In order to automatically import the `API` of modules in the specified directory on demand.

## Basic Usage ๐Ÿฆ– 1. install ```shell npm i vite-auto-import-resolvers unplugin-auto-import -D # pnpm ๐Ÿ‘‡ # pnpm i vite-auto-import-resolvers unplugin-auto-import -D # yarn ๐Ÿ‘‡ # yarn add vite-auto-import-resolvers unplugin-auto-import -D ``` 2. Configure plugins ```ts // vite.config.js // OR vite.config.ts import { defineConfig } from 'vite' import Vue from '@vitejs/plugin-vue' import AutoImports from 'unplugin-auto-import/vite' import { dirResolver, DirResolverHelper } from 'vite-auto-import-resolvers' export default defineConfig({ plugins: [ Vue(), // This helper is required ๐Ÿ‘‡ DirResolverHelper(), AutoImports({ imports: ['vue'], resolvers: [ dirResolver() ] }) ] }) ``` 3. After that, the default export of modules under `src/composables` will be automatically imported as needed in the project for example ๐Ÿ‘‡ ```ts // src/composables/foo.ts export default 100 ``` ```html // src/App.vue ``` 4. Type configuration (Abandoned, not required) If your project is `ts`, your `tsconfig.json` should have the following configuration ๐Ÿ‘‡ ```json { "compilerOptions": { // other configs "baseUrl": ".", "paths": { "/src/*": ["src/*"] } }, // other configs } ```
## Advanced ๐Ÿฆ• ### Mandatory prefix or mandatory suffix ```ts import { defineConfig } from 'vite' import Vue from '@vitejs/plugin-vue' import AutoImports from 'unplugin-auto-import/vite' import { dirResolver, DirResolverHelper } from 'vite-auto-import-resolvers' export default defineConfig({ plugins: [ Vue(), DirResolverHelper(), AutoImports({ imports: ['vue'], resolvers: [ dirResolver({ prefix: 'use' }), // prefix use dirResolver({ target: 'stores', // Target directory, The default is composables suffix: 'Store' // suffix Store }) ] }) ] }) ``` So - `src/composables`, only modules starting with `use` will be loaded on demand - `src/stores`, only modules ending in `Store` will be loaded on demand for example ๐Ÿ‘‡ ```ts // src/stores/counterStore.ts const counter = ref(100) export default () => { const inc = (v: number = 1) => (counter.value += v) return { inc, counter } } ``` ```html ```

### include or exclude ```ts import { defineConfig } from 'vite' import Vue from '@vitejs/plugin-vue' import AutoImports from 'unplugin-auto-import/vite' import { dirResolver, DirResolverHelper } from 'vite-auto-import-resolvers' export default defineConfig({ plugins: [ Vue(), DirResolverHelper(), AutoImports({ imports: ['vue'], resolvers: [ dirResolver({ prefix: 'use', include: ['foo'], // foo modules are included even if they do not start with use exclude: ['useBar'] // The useBar module will always be excluded }) ] }) ] }) ```

### root ```ts import { resolve } from 'path' import { defineConfig } from 'vite' import Vue from '@vitejs/plugin-vue' import AutoImports from 'unplugin-auto-import/vite' import { dirResolver, DirResolverHelper } from 'vite-auto-import-resolvers' export default defineConfig({ plugins: [ Vue(), DirResolverHelper(), AutoImports({ imports: ['vue'], resolvers: [ dirResolver({ root: '.' // The default is 'src' }) ] }) ] }) ```

### Other style path aliases (Abandoned, not required) You may use other styles of path aliases in your project๏ผŒfor example `@` Then you can configure it like this ๐Ÿ‘‡ ```ts import { resolve } from 'path' import { defineConfig } from 'vite' import Vue from '@vitejs/plugin-vue' import AutoImports from 'unplugin-auto-import/vite' import { dirResolver, DirResolverHelper } from 'vite-auto-import-resolvers' export default defineConfig({ resolve: { alias: { // Change alias '@/': `${resolve(__dirname, 'src')}/` } }, plugins: [ Vue(), DirResolverHelper(), AutoImports({ imports: ['vue'], resolvers: [ dirResolver({ srcAlias: '@' }) // Set alias, default to /src/ ] }) ] }) ``` If you are a project of `ts`, `tsconfig.json` should be changed ๐Ÿ‘‡ ```json { "compilerOptions": { // other configs "baseUrl": ".", "paths": { "@/*": ["src/*"] } }, // other configs } ```

### generate on-demand `API` presets When using `unplugin auto imports`, you need to manage `imports` manually ๐Ÿ‘‡ ```ts import { defineConfig } from 'vite' import Vue from '@vitejs/plugin-vue' import AutoImports from 'unplugin-auto-import/vite' export default defineConfig({ plugins: [ Vue(), AutoImports({ imports: ['vue', 'vue-router', 'pinia'] // ๆ‰‹ๅŠจ็ฎก็† }) ] }) ``` But sometimes you may need to change some dependencies, such as changing `pinia` to `vuex`. At this time, if the configuration is not changed, an error will occur. At the same time, if you set an uninstalled package, it will cause unnecessary performance consumption. So you can ๐Ÿ‘‡ ```ts import { defineConfig } from 'vite' import Vue from '@vitejs/plugin-vue' import AutoImports from 'unplugin-auto-import/vite' import { AutoGenerateImports } from "vite-auto-import-resolvers" export default defineConfig({ plugins: [ Vue(), AutoImports({ imports: AutoGenerateImports() // Automatic management. Only when the corresponding package is loaded, the preset will be set automatically on demand }) ] }) ```
#### Default support list `include` - vue - pinia - vuex - vitest - vue-i18n - vue-router - @vueuse/core - @vueuse/head - @nuxtjs/composition-api - preact - quasar - react - react-router - react-router-dom - svelte - svelte/animate - svelte/easing - svelte/motion - svelte/store - svelte/transition - vitepress - vee-validate
#### exclude ```ts import { defineConfig } from 'vite' import Vue from '@vitejs/plugin-vue' import AutoImports from 'unplugin-auto-import/vite' import { AutoGenerateImports } from "vite-auto-import-resolvers" export default defineConfig({ plugins: [ Vue(), AutoImports({ imports: AutoGenerateImports({ exclude: ['pinia'] // Pinia will always be excluded }) }) ] }) ```

## Inspire ๐Ÿณ The `resolvers` comes from the `issue` discussion of `unplugin-auto-import` ๐Ÿ‘‰ [How should I auto import composition functions](https://github.com/antfu/unplugin-auto-import/issues/76)ใ€‚

## More ๐Ÿƒ More project engineering practices๏ผŒyou can be see ๐Ÿ‘‰ [tov-template](https://github.com/dishait/tov-template)

## License Made with markthree Published under [MIT License](./LICENSE).