import {defineConfig, loadEnv} from "vite"; import vue from "@vitejs/plugin-vue"; import {resolve} from "path"; import changeVersion from "./update.version.js"; import styleImport, {VantResolve} from "vite-plugin-style-import"; // https://vitejs.dev/config/ export default defineConfig(({mode}) => { // 更新版本号 const appVersion = changeVersion(); // 根据不同的环境加载不同的配置 const isDev = mode === 'development' const isTest = mode === 'test' const isProd = mode === 'production' // 加载环境变量 const env = loadEnv(mode, process.cwd(), ''); const apiUrl = env.VITE_APP_API_URL || 'http://localhost:8080'; return { define: { __APP_VERSION__: JSON.stringify(appVersion) }, plugins: [ vue(), styleImport({ resolves: [VantResolve()], libs: [ { libraryName: "vant", esModule: true, resolveStyle: (name) => `../es/${name}/style`, //https://blog.csdn.net/yunchong_zhao/article/details/123552423 }, ], }), ], server: { host: "0.0.0.0", //设置地址:http://localhost port: 4000, // 设置服务启动端口号 hmr: false, // 关闭热更新 open: true, // 设置服务启动时是否自动打开浏览器 cors: true, // 允许跨域 // 添加自定义头部 headers: { 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0' }, proxy: { '/dev-api': { target: apiUrl,//代理的地址 changeOrigin: true, // 路径重写 pathRewrite无效使用rewrite // pathRewrite: { '/api': '' // 用'/api'代替target里面的地址 }, rewrite: path => path.replace(/^\/dev-api/, '') } } }, resolve: { alias: { "@": resolve(__dirname, "src"), }, }, css: { // css预处理器 preprocessorOptions: { scss: { charset: false, additionalData: '@import "./src/assets/style/index.scss";', } } }, // 根据不同环境设置构建选项 build: { outDir: isProd ? 'dist' : isTest ? 'dist-test' : isDev ? 'dist-dev' :'dist-dev' } } });