From 616c55359a005eb8bd2417b6b1474512f5aec2a8 Mon Sep 17 00:00:00 2001 From: XiaoXinPro 14 IAH5R <568170040@qq.com> Date: Tue, 8 Aug 2023 14:41:52 +0800 Subject: [PATCH] env --- .env.development | 3 --- vite.config.js | 42 ++++++++++++++++++++++++------------------ 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/.env.development b/.env.development index 4e6b328..75fa93f 100644 --- a/.env.development +++ b/.env.development @@ -1,9 +1,6 @@ ## 开发环境 - -# 变量必须以 VITE_ 为前缀才能暴露给外部读取 NODE_ENV='development' VITE_APP_TITLE = 'Metavatar-PC' VITE_APP_PORT = 3000 -## VITE_APP_BASE_API = 'https://photo.metavatar.cc/C' VITE_APP_BASE_API = 'https://test.metavatar.cc/C' diff --git a/vite.config.js b/vite.config.js index 072247b..94850c8 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,23 +1,29 @@ -import { defineConfig } from 'vite' +import { defineConfig, loadEnv } from 'vite' import uni from '@dcloudio/vite-plugin-uni' // https://vitejs.dev/config/ -export default defineConfig({ - transpileDependencies: ['uview-ui'], - plugins: [ - uni(), - ], - server: { - host: "0.0.0.0", - port: '8591', - open: true, // 运行是否自动打开浏览器 - proxy: { - // 反向代理解决跨域 - "/api": { - target: 'https://test.metavatar.cc/C', // 线上接口地址 - changeOrigin: true, - rewrite: (path) => path.replace(/^\/api/, ""), +export default defineConfig(({ command, mode }) => { + const env = loadEnv(mode, process.cwd()) // 获取 .env里面定义是参数 + + console.log('env', env, mode, command) + + return { + transpileDependencies: ['uview-ui'], + plugins: [ + uni(), + ], + server: { + host: "0.0.0.0", + port: '8591', + open: false, // 运行是否自动打开浏览器 + proxy: { + // 反向代理解决跨域 + "/api": { + target: env.VITE_APP_BASE_URL, // 线上接口地址 + changeOrigin: true, + rewrite: (path) => path.replace(/^\/api/, ""), + }, }, - }, - }, + } + } })