vite.config.ts文件配置

 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'

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值