vite.config.ts
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import resolveExternalsPlugin from "vite-plugin-resolve-externals";
import legacy from "@vitejs/plugin-legacy";
export default ({ mode }) => {
process.env.herf = loadEnv(mode, process.cwd()).VITE_API_URL; // 判断当前环境并获取地址
return defineConfig({
plugins: [
vue(),
legacy({
targets: ["> 1%, last 1 version, ie >= 11"],
additionalLegacyPolyfills: ["regenerator-runtime/runtime"]
}),
Components({
resolvers: [ElementPlusResolver()],
dts: true,
include: [/\.vue$/, /\.vue\?vue/, /\.md$/]
}),
resolveExternalsPlugin({
AMap: "AMap"
})
],
define: { // 环境地址定义,定义后可在项目找那个使用process.env获取到当前环境地址
"process.env": {
// UPLOAD: "http://localhost:8089",
UPLOAD: process.env.herf,
LOGIN_URL: process.env.herf,
UPLOAD_IMGURL: process.env.herf + "/uploadfile"
}
},
base: "./",
server: {
host: "localhost",
port: 8089,
cors: true,
// open: true,
// 反向代理
proxy: {
"/api": {
target: process.env.herf + "/api",
changeOrigin: true,
// secure: false,
rewrite: (path) => path.replace(/^\/api/, "")
},
"/sso/images": {
target: process.env.herf + "/api",
changeOrigin: true,
// secure: false,
rewrite: (path) => path.replace(/^\/api/, "")
}
}
},
resolve: {
alias: [
{ // 文件地址配置
find: "@",
replacement: resolve(__dirname, "src")
}
]
},
build: {
terserOptions: {
compress: {
drop_console: true, // 正式环境过滤console.log()
drop_debugger: true // 正式环境过滤debugger
}
}
}
});
};
.env.development
ENV = development
VITE_API_URL = 'http://test.xxxxx.cn:24000'
.env.production
ENV = production
VITE_API_URL = 'https://www.xxxxxxx.com'