Vite 插件
guide
vite plugin
!!!tip Vite 在浏览器支持 ES 模块之前,JavaScript 并没有提供原生机制让开发者以模块化的方式进行开发。 这也正是我们对 “打包” 这个概念熟悉的原因:使用工具抓取、处理并将我们的源码模块串联成可以在浏览器中运行的文件。
vite 下一代的前端工具链 !!!
vite-plugin-inspect
vite-plugin-inspect 可以让开发者在浏览器端就可以看到vue文件编译后的代码、vue文件的相互依赖关系
js
1import Inspect from 'vite-plugin-inspect';
2
3export default defineConfig({
4 plugins: [vue(), Inspect()]
5})使用 vite 启用后,会看到一个新的 URL

text
1 VITE v5.3.5 ready in 2811 ms
2
3 ➜ Local: http://localhost:5177/
4 ➜ Network: use --host to expose
5 ➜ Inspect: http://localhost:5177/__inspect/
6 ➜ press h + enter to show help构建模式
要在构建模式下检查转换,您可以传递 build: true 选项:
text
1// vite.config.ts
2import Inspect from 'vite-plugin-inspect'
3
4export default {
5 plugins: [
6 Inspect({
7 build: true,
8 outputDir: '.vite-inspect'
9 })
10 ],
11}运行 vite build 后,会在 .vite-inspect 下生成 inspector 客户端,你可以使用 npx serve .vite-inspect 来检查结果。
unplugin-auto-import/vite
npm i -D unplugin-auto-import
- 配置自动导入 vue, 内置一些规则,会帮我们自动导入。
js
1import AutoImport from 'unplugin-auto-import/vite';
2
3export default defineConfig({
4 plugins: [
5 vue(),
6 vueJsx(),
7 AutoImport({
8 // 内置了一些的引用规则
9 imports: ['vue', 'vue-router'],
10 dirs: ['./components/**'],
11 }),
12 Inspect()]
13 }
14)导入后 vue 常用的包会自动进行导入,编辑器也不会报错。因为会自动生成
auto-imports.d.ts,将常用的声明为全局类型
vue
1<script setup lang="ts">
2 // 当我们配置了,自动导入,这一行就不用写了。
3 // import { ref } from 'vue'
4 const props = defineProps({
5 src: String,
6 title: String,
7 color: String,
8 secondary: String,
9 });
10 // 不用手动引入
11 const name = ref('客户满意度');
12</script>规则配置
js
1AutoImport({
2 // targets to transform
3 include: [
4 /.[tj]sx?$/, // .ts, .tsx, .js, .jsx
5 /.vue$/,
6 /.vue?vue/, // .vue
7 /.md$/, // .md
8 ],
9
10 // global imports to register
11 imports: [
12 // 常用的框架,内置了规则,不用在配置
13 'vue',
14 'vue-router',
15 // 自定义的导入
16 {
17 '@vueuse/core': [
18 // named imports
19 'useMouse', // import { useMouse } from '@vueuse/core',
20 // alias
21 ['useFetch', 'useMyFetch'], // import { useFetch as useMyFetch } from '@vueuse/core',
22 ],
23 'axios': [
24 // 间接的导入了 import axios from 'axios'
25 ['default', 'axios'], // import { default as axios } from 'axios',
26 ],
27 // 这是规则
28 '[package-name]': [
29 '[import-names]',
30 // alias
31 ['[from]', '[alias]'],
32 ],
33 },
34 // 从某个包里,导出类型。
35 {
36 from: 'vue-router',
37 imports: ['RouteLocationRaw'],
38 type: true,
39 },
40 ],
41
42 // Array of strings of regexes that contains imports meant to be filtered out.
43 ignore: [
44 'useMouse',
45 'useFetch'
46 ],
47
48 // Enable auto import by filename for default module exports under directories
49 defaultExportByFilename: false,
50
51 // Auto import for module exports under directories
52 // by default it only scan one level of modules under the directory
53 dirs: [
54 // './hooks',
55 // './composables' // only root modules
56 // './composables/**', // all nested modules
57 // ...
58 ],
59
60 // Filepath to generate corresponding .d.ts file.
61 // Defaults to './auto-imports.d.ts' when `typescript` is installed locally.
62 // Set `false` to disable.
63 dts: './auto-imports.d.ts',
64
65 // Array of strings of regexes that contains imports meant to be ignored during
66 // the declaration file generation. You may find this useful when you need to provide
67 // a custom signature for a function.
68 ignoreDts: [
69 'ignoredFunction',
70 /^ignore_/
71 ],
72
73 // Auto import inside Vue template
74 // see https://github.com/unjs/unimport/pull/15 and https://github.com/unjs/unimport/pull/72
75 vueTemplate: false,
76
77 // Custom resolvers, compatible with `unplugin-vue-components`
78 // see https://github.com/antfu/unplugin-auto-import/pull/23/
79 resolvers: [
80 /* ... */
81 ],
82
83 // Include auto-imported packages in Vite's `optimizeDeps` options
84 // Recommend to enable
85 viteOptimizeDeps: true,
86
87 // Inject the imports at the end of other imports
88 injectAtEnd: true,
89
90 // Generate corresponding .eslintrc-auto-import.json file.
91 // eslint globals Docs - https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
92 eslintrc: {
93 enabled: false, // Default `false`
94 // provide path ending with `.mjs` or `.cjs` to generate the file with the respective format
95 filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
96 globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
97 },
98
99 // Generate corresponding .biomelintrc-auto-import.json file.
100 // biomejs extends Docs - https://biomejs.dev/guides/how-biome-works/#the-extends-option
101 biomelintrc: {
102 enabled: false, // Default `false`
103 filepath: './.biomelintrc-auto-import.json', // Default `./.biomelintrc-auto-import.json`
104 },
105})