122 lines
3.5 KiB
TypeScript
122 lines
3.5 KiB
TypeScript
import { fileURLToPath, URL } from "node:url";
|
||
|
||
import vue from "@vitejs/plugin-vue";
|
||
import vueJsx from "@vitejs/plugin-vue-jsx";
|
||
import path from "path";
|
||
import AutoImport from "unplugin-auto-import/vite";
|
||
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
|
||
import Components from "unplugin-vue-components/vite";
|
||
import vueSetupExtend from "unplugin-vue-setup-extend-plus/vite";
|
||
import { defineConfig, loadEnv, UserConfig } from "vite";
|
||
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
|
||
// 插件
|
||
import type { ProxyOptions } from "vite";
|
||
|
||
type ProxyItem = [string, string];
|
||
|
||
type ProxyList = ProxyItem[];
|
||
|
||
type ProxyTargetList = Record<string, ProxyOptions>;
|
||
|
||
/**
|
||
* 创建代理,用于解析 .env.development 代理配置
|
||
* @param list
|
||
*/
|
||
function createProxy(list: ProxyList = []) {
|
||
const ret: ProxyTargetList = {};
|
||
for (const [prefix, target] of list) {
|
||
const httpsRE = /^https:\/\//;
|
||
const isHttps = httpsRE.test(target);
|
||
|
||
// https://github.com/http-party/node-http-proxy#options
|
||
ret[prefix] = {
|
||
target: target,
|
||
changeOrigin: true,
|
||
ws: true,
|
||
rewrite: path => path.replace(new RegExp(`^${prefix}`), ""),
|
||
// https is require secure=false
|
||
...(isHttps ? { secure: false } : {})
|
||
};
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
// https://vitejs.dev/config/
|
||
export default defineConfig(({ mode }): UserConfig => {
|
||
const env = loadEnv(mode, process.cwd());
|
||
return {
|
||
base: "/",
|
||
css: {
|
||
preprocessorOptions: {
|
||
scss: {
|
||
additionalData: `@use "src/assets/styles/element-plus-theme.scss" as *;`
|
||
}
|
||
}
|
||
},
|
||
plugins: [
|
||
vue(),
|
||
vueJsx(),
|
||
// 加载自定义图标
|
||
createSvgIconsPlugin({
|
||
// 指定需要缓存的图标文件夹
|
||
iconDirs: [path.resolve(process.cwd(), "src/assets/icon/svg")],
|
||
// 指定symbolId格式
|
||
symbolId: "icon-[dir]-[name]"
|
||
}),
|
||
// name 可以写在 script 标签上
|
||
vueSetupExtend({}),
|
||
AutoImport({
|
||
imports: ["vue", "vue-router", "@vueuse/core"],
|
||
resolvers: [ElementPlusResolver()],
|
||
dts: "src/types/auto-imports.d.ts"
|
||
}),
|
||
Components({
|
||
resolvers: [
|
||
ElementPlusResolver({
|
||
importStyle: "sass"
|
||
})
|
||
],
|
||
dts: false
|
||
})
|
||
],
|
||
esbuild: {
|
||
pure: env.VITE_DROP_CONSOLE ? ["console.log", "debugger"] : []
|
||
},
|
||
resolve: {
|
||
alias: {
|
||
"@": fileURLToPath(new URL("./src", import.meta.url))
|
||
}
|
||
},
|
||
server: {
|
||
host: "0.0.0.0",
|
||
port: parseInt(env.VITE_PORT),
|
||
open: env.VITE_OPEN === "true",
|
||
proxy: createProxy(JSON.parse(env.VITE_PROXY))
|
||
},
|
||
build: {
|
||
outDir: "jjbbs",
|
||
minify: "esbuild",
|
||
// esbuild 打包更快,但是不能去除 console.log,terser打包慢,但能去除 console.log
|
||
// minify: "terser",
|
||
// terserOptions: {
|
||
// compress: {
|
||
// drop_console: viteEnv.VITE_DROP_CONSOLE,
|
||
// drop_debugger: true
|
||
// }
|
||
// },
|
||
// 禁用 gzip 压缩大小报告,可略微减少打包时间
|
||
reportCompressedSize: false,
|
||
// 规定触发警告的 chunk 大小
|
||
chunkSizeWarningLimit: 2000,
|
||
rollupOptions: {
|
||
output: {
|
||
// Static resource classification and packaging
|
||
chunkFileNames: "assets/js/[name]-[hash].js",
|
||
entryFileNames: "assets/js/[name]-[hash].js",
|
||
assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
|
||
}
|
||
}
|
||
}
|
||
};
|
||
});
|