unplugin-auto-import The 'dirs' option has been built in. It is recommended to use it first
The vite resolvers of
unplugin-auto-import mainly
deals with the API
of the vite
project itself, which is automatically
imported on demand.
简体中文 | English
In order to automatically import the API
of modules in the specified directory
on demand.
- install
npm i vite-auto-import-resolvers unplugin-auto-import -D
- Configure plugins
// vite.config.js
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()],
}),
],
});
- After that, the default export of modules under
src/composables
will be automatically imported as needed in the project
for example 👇
// src/composables/foo.ts
export default 100;
// src/App.vue
<script setup>
console.log(foo) // log 100,And it is automatically introduced on demand
</script>
<template> Hello World!! </template>
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: "src/stores", // Target directory, The default is 'src/composables'
suffix: "Store", // suffix Store
}),
],
}),
],
});
So
src/composables
, only modules starting withuse
will be loaded on demandsrc/stores
, only modules ending inStore
will be loaded on demand
for example 👇
// src/stores/counterStore.ts
const counter = ref(100);
export default () => {
const inc = (v: number = 1) => (counter.value += v);
return {
inc,
counter,
};
};
<script setup lang="ts">
// This is on demand
const n = counterStore()
</script>
<template>
<div @click="n.inc()">{{n.counter}}</div>
</template>
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
}),
],
}),
],
});
use normalize
to control the generation of the final 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({
normalize({ path, target, name }) {
return path;
},
}),
],
}),
],
});
When using unplugin auto imports
, you need to manage imports
manually 👇
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"], // Manual management required
}),
],
});
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 👇
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
}),
],
});
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
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
}),
}),
],
});
The resolvers
comes from the issue
discussion of unplugin-auto-import
👉
How should I auto import composition functions。
More project engineering practices,you can be see 👉 tov-template
Made with markthree
Published under MIT License.