jdcProject_front/vite.config.ts

105 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-11-19 18:31:03 +08:00
/// <reference types="vitest/config" />
2023-02-16 17:36:47 +08:00
2024-11-18 19:40:44 +08:00
import type { ConfigEnv, UserConfigExport } from "vite"
import path, { resolve } from "node:path"
import vue from "@vitejs/plugin-vue"
import vueJsx from "@vitejs/plugin-vue-jsx"
2024-11-18 19:40:44 +08:00
import UnoCSS from "unocss/vite"
import { loadEnv } from "vite"
import { createSvgIconsPlugin } from "vite-plugin-svg-icons"
2022-09-29 22:38:27 +08:00
import svgLoader from "vite-svg-loader"
2022-04-20 22:40:26 +08:00
2024-11-19 20:14:23 +08:00
// 配置项文档https://cn.vitejs.dev/config
export default ({ mode }: ConfigEnv): UserConfigExport => {
const viteEnv = loadEnv(mode, process.cwd()) as ImportMetaEnv
const { VITE_PUBLIC_PATH } = viteEnv
2022-04-21 00:50:12 +08:00
return {
2024-11-19 20:14:23 +08:00
// 打包时根据实际情况修改 base
base: VITE_PUBLIC_PATH,
2022-04-21 00:50:12 +08:00
resolve: {
alias: {
2024-11-19 20:14:23 +08:00
// @ 符号指向 src 目录
"@": resolve(__dirname, "./src")
}
2022-04-21 00:50:12 +08:00
},
server: {
2024-11-19 20:14:23 +08:00
// 设置 host: true 才可以使用 Network 的形式,以 IP 访问项目
host: true, // host: "0.0.0.0"
2024-11-19 20:14:23 +08:00
// 端口号
2022-04-28 15:38:49 +08:00
port: 3333,
2024-11-19 20:14:23 +08:00
// 是否自动打开浏览器
open: false,
2024-11-19 20:14:23 +08:00
// 跨域设置允许
cors: true,
2024-11-19 20:14:23 +08:00
// 端口被占用时,是否直接退出
strictPort: false,
2024-11-19 20:14:23 +08:00
// 接口代理
proxy: {
"/api/v1": {
2024-02-07 10:38:12 +08:00
target: "https://mock.mengxuegu.com/mock/63218b5fb4c53348ed2bc212",
ws: true,
2024-11-19 20:14:23 +08:00
// 是否允许跨域
2023-08-07 09:34:11 +08:00
changeOrigin: true
}
},
2024-11-19 20:14:23 +08:00
// 预热常用文件,提高初始页面加载速度
warmup: {
2023-12-12 19:01:25 +08:00
clientFiles: ["./src/layouts/**/*.vue"]
}
},
build: {
2024-11-19 20:14:23 +08:00
// 单个 chunk 文件的大小超过 2048KB 时发出警告
2023-08-23 11:59:22 +08:00
chunkSizeWarningLimit: 2048,
2024-11-19 20:14:23 +08:00
// 禁用 gzip 压缩大小报告
2023-08-23 11:59:22 +08:00
reportCompressedSize: false,
2024-11-19 20:14:23 +08:00
// 打包后静态资源目录
2023-08-23 11:59:22 +08:00
assetsDir: "static",
rollupOptions: {
output: {
/**
*
* 1.
* 2. chunk
*/
2023-08-23 11:59:22 +08:00
manualChunks: {
vue: ["vue", "vue-router", "pinia"],
element: ["element-plus", "@element-plus/icons-vue"],
2023-08-23 11:59:22 +08:00
vxe: ["vxe-table", "vxe-table-plugin-element", "xe-utils"]
}
}
}
},
2024-11-19 20:14:23 +08:00
// 混淆器
esbuild:
mode === "development"
? undefined
: {
2024-11-19 20:14:23 +08:00
// 打包时移除 console.log
pure: ["console.log"],
2024-11-19 20:14:23 +08:00
// 打包时移除 debugger
drop: ["debugger"],
2024-11-19 20:14:23 +08:00
// 打包时移除所有注释
legalComments: "none"
},
2024-11-19 20:14:23 +08:00
// Vite 插件
2022-04-21 12:22:35 +08:00
plugins: [
vue(),
vueJsx(),
2024-11-19 20:14:23 +08:00
// 将 SVG 静态图转化为 Vue 组件
svgLoader({ defaultImport: "url" }),
2024-11-19 20:14:23 +08:00
// SVG
2022-04-21 17:14:30 +08:00
createSvgIconsPlugin({
iconDirs: [path.resolve(process.cwd(), "src/icons/svg")],
symbolId: "icon-[dir]-[name]"
2022-05-12 19:07:54 +08:00
}),
2024-11-19 20:14:23 +08:00
// UnoCSS
UnoCSS()
2023-02-16 17:36:47 +08:00
],
2024-11-19 20:14:23 +08:00
// Vitest 单元测试配置https://cn.vitest.dev/config
2023-02-16 17:36:47 +08:00
test: {
include: ["tests/**/*.test.ts"],
environment: "jsdom"
}
2022-04-21 00:50:12 +08:00
}
}