import path from 'node:path' import process from 'node:process' import { loadEnv } from 'vite' import type { ConfigEnv, UserConfig } from 'vite' import viewport from 'postcss-mobile-forever' import autoprefixer from 'autoprefixer' import { createVitePlugins } from './build/vite' import { exclude, include } from './build/vite/optimize' export default ({ mode }: ConfigEnv): UserConfig => { const root = process.cwd() const env = loadEnv(mode, root) return { base: env.VITE_APP_PUBLIC_PATH, plugins: createVitePlugins(), server: { host: true, port: 3000, proxy: { '/api': { target: env.VITE_APP_API_HTTP_URL, rewrite: path => path.replace(new RegExp(`^${env.VITE_APP_API_BASE_URL}`), ''), bypass(req, res, options: any) { const realUrl = options.target + (options.rewrite ? options.rewrite(req.url) : '') res.setHeader('A-Real-Url', realUrl) // 添加响应标头(A-Real-Url为自定义命名),在浏览器中显示 }, ws: false, changeOrigin: true, }, }, }, resolve: { alias: { '~@': path.join(__dirname, './src'), '@': path.join(__dirname, './src'), '~': path.join(__dirname, './src/assets'), }, }, css: { postcss: { plugins: [ autoprefixer(), // https://github.com/wswmsword/postcss-mobile-forever viewport({ appSelector: '#app', viewportWidth: 375, maxDisplayWidth: 600, rootContainingBlockSelectorList: [ 'van-tabbar', 'van-popup', ], }), ], }, }, build: { cssCodeSplit: false, chunkSizeWarningLimit: 2048, }, optimizeDeps: { include, exclude }, } }