- 新增 .env 文件,配置不同环境下的基础 URL - 更新 vite.config.ts,添加环境变量加载和代理配置- 添加 request.js 工具类,用于处理 HTTP 请求 - 更新相关组件,集成新的请求方法
64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
// vite.config.ts
|
|
import { defineConfig, loadEnv } from 'vite'; // 从 vite 导入 loadEnv
|
|
import vue from '@vitejs/plugin-vue';
|
|
import { fileURLToPath, URL } from 'node:url';
|
|
import vueJsx from '@vitejs/plugin-vue-jsx';
|
|
import AutoImport from 'unplugin-auto-import/vite';
|
|
import Components from 'unplugin-vue-components/vite';
|
|
import { VantResolver } from 'unplugin-vue-components/resolvers';
|
|
import postCssPxToRem from 'postcss-pxtorem';
|
|
export default defineConfig(({ mode }) => {
|
|
// 接收 mode 参数
|
|
// 正确加载环境变量
|
|
const env = loadEnv(mode, process.cwd());
|
|
|
|
// 从 env 对象中获取变量
|
|
const proxyUrl = env.VITE_APP_BASEURL;
|
|
const proxyUrlDelivery = env.VITE_APP_DELIVERY_BASEURL;
|
|
return {
|
|
// 必须 return 配置对象
|
|
server: {
|
|
host: '0.0.0.0',
|
|
port: 3000,
|
|
proxy: {
|
|
'/api': {
|
|
target: 'http://192.168.11.119:8090/api/api/',
|
|
changeOrigin: true,
|
|
logLevel: 'debug',
|
|
rewrite: (path) => path.replace(/^\/api/, '/backend-api'), // 添加新前缀
|
|
bypass: (req) => req.headers.accept?.indexOf('html') !== -1 // 跳过 HTML 请求
|
|
},
|
|
'/request-java': {
|
|
target: `${proxyUrlDelivery}/api`,
|
|
changeOrigin: true,
|
|
pathRewrite: { '^/request-java': '' }
|
|
}
|
|
}
|
|
},
|
|
css: {
|
|
postcss: {
|
|
plugins: [
|
|
postCssPxToRem({
|
|
rootValue: 37.5,
|
|
propList: ['*']
|
|
})
|
|
]
|
|
},
|
|
preprocessorOptions: {
|
|
scss: { api: 'modern-compiler' }
|
|
}
|
|
},
|
|
plugins: [
|
|
vue(),
|
|
vueJsx(),
|
|
AutoImport({ resolvers: [VantResolver()] }),
|
|
Components({ resolvers: [VantResolver()] })
|
|
],
|
|
resolve: {
|
|
alias: {
|
|
'@': fileURLToPath(new URL('./src', import.meta.url))
|
|
}
|
|
}
|
|
};
|
|
});
|