zsqy-naive-ui/vite.config.ts

126 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-02-05 10:47:59 +08:00
import vue from '@vitejs/plugin-vue';
import { UserConfig, ConfigEnv, loadEnv, defineConfig } from 'vite';
2024-01-05 17:28:54 +08:00
2024-02-05 10:47:59 +08:00
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
2024-01-05 17:28:54 +08:00
2024-02-05 10:47:59 +08:00
import Icons from 'unplugin-icons/vite';
2024-01-05 17:28:54 +08:00
2024-02-05 10:47:59 +08:00
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
2024-01-05 17:28:54 +08:00
2024-02-05 10:47:59 +08:00
import vueJsx from '@vitejs/plugin-vue-jsx';
2024-01-05 17:28:54 +08:00
2024-02-05 10:47:59 +08:00
import { resolve } from 'path';
2024-01-05 17:28:54 +08:00
2024-02-05 10:47:59 +08:00
const pathSrc = resolve(__dirname, 'src');
2024-01-05 17:28:54 +08:00
// https://cn.vitejs.dev/config
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
const env = loadEnv(mode, process.cwd());
return {
resolve: {
alias: {
2024-02-05 10:47:59 +08:00
'@': pathSrc,
2024-01-05 17:28:54 +08:00
},
},
css: {
// CSS 预处理器
// preprocessorOptions: {
// 定义全局 SCSS 变量
// scss: {
// javascriptEnabled: true,
// additionalData: `
// @use "@/styles/variables.scss" as *;
// `,
// },
// },
2024-01-05 17:28:54 +08:00
},
server: {
// 允许IP访问
2024-02-05 10:47:59 +08:00
host: '0.0.0.0',
2024-01-05 17:28:54 +08:00
// 应用端口 (默认:3000)
port: Number(env.VITE_APP_PORT),
// 运行是否自动打开浏览器
open: true,
proxy: {
/**
*
* http://localhost:3000/dev-api/users (F12可见请求路径) => http://localhost:8989/users (实际请求后端 API 路径)
*
* env.VITE_APP_BASE_API: /dev-api
* env.VITE_APP_API_URL: http://localhost:8989
*/
[env.VITE_APP_BASE_API]: {
changeOrigin: true,
target: env.VITE_APP_API_URL,
rewrite: (path) =>
2024-02-05 10:47:59 +08:00
path.replace(new RegExp('^' + env.VITE_APP_BASE_API), ''),
2024-01-05 17:28:54 +08:00
},
},
},
plugins: [
vue(),
vueJsx(),
// 自动导入参考: https://github.com/sxzz/element-plus-best-practices/blob/main/vite.config.ts
AutoImport({
// 自动导入 Vue 相关函数ref, reactive, toRef 等
2024-02-05 10:47:59 +08:00
imports: ['vue', '@vueuse/core'],
2024-01-05 17:28:54 +08:00
eslintrc: {
enabled: false,
2024-02-05 10:47:59 +08:00
filepath: './.eslintrc-auto-import.json',
2024-01-05 17:28:54 +08:00
globalsPropValue: true,
},
vueTemplate: true,
// 配置文件生成位置(false:关闭自动生成)
2024-02-05 10:47:59 +08:00
dts: 'types/auto-imports.d.ts',
2024-01-05 17:28:54 +08:00
}),
Components({
resolvers: [],
// 指定自定义组件位置(默认:src/components)
2024-02-05 10:47:59 +08:00
dirs: ['src/components', 'src/**/components'],
2024-01-05 17:28:54 +08:00
// 配置文件位置 (false:关闭自动生成)
2024-02-05 10:47:59 +08:00
dts: 'types/components.d.ts',
2024-01-05 17:28:54 +08:00
}),
Icons({
autoInstall: true,
}),
createSvgIconsPlugin({
// 指定需要缓存的图标文件夹
2024-02-05 10:47:59 +08:00
iconDirs: [resolve(pathSrc, 'assets/svgs')],
2024-01-05 17:28:54 +08:00
// 指定symbolId格式
2024-02-05 10:47:59 +08:00
symbolId: 'icon-[dir]-[name]',
2024-01-05 17:28:54 +08:00
}),
],
// 预加载项目必需的组件
optimizeDeps: {
include: [
2024-02-05 10:47:59 +08:00
'vue',
'vue-router',
'pinia',
'axios',
'@vueuse/core',
'path-to-regexp',
'echarts',
2024-01-05 17:28:54 +08:00
],
},
// 构建配置
build: {
chunkSizeWarningLimit: 2000, // 消除打包大小超过500kb警告
2024-02-05 10:47:59 +08:00
minify: 'terser', // Vite 2.6.x 以上需要配置 minify: "terser", terserOptions 才能生效
2024-01-05 17:28:54 +08:00
terserOptions: {
compress: {
keep_infinity: true, // 防止 Infinity 被压缩成 1/0这可能会导致 Chrome 上的性能问题
drop_console: true, // 生产环境去除 console
drop_debugger: true, // 生产环境去除 debugger
},
format: {
comments: false, // 删除注释
},
},
},
};
});