import { defineConfig, loadEnv } from 'vite' import uni from '@dcloudio/vite-plugin-uni' function loggedProxy(name, options) { return { ...options, configure(proxy) { proxy.on('proxyReq', (proxyRequest, request) => { // The browser talks only to this same-origin Vite server. Forwarding its // localhost Origin/Referer would make the upstream gateway run CORS // validation again and reject the proxied request before Auth handles it. proxyRequest.removeHeader('origin') proxyRequest.removeHeader('referer') console.info(`[Overseas PDA H5 proxy] ${name}`, `${request.method} ${request.url}`, '->', `${options.target}${proxyRequest.path}`) }) proxy.on('proxyRes', (response, request) => { console.info(`[Overseas PDA H5 proxy] ${name} response`, `${request.method} ${request.url}`, response.statusCode) }) proxy.on('error', (error, request) => { console.error(`[Overseas PDA H5 proxy] ${name} error`, `${request.method} ${request.url}`, error.message) }) } } } export default defineConfig(({ mode }) => { const env = loadEnv(mode, process.cwd(), '') const domesticTarget = env.API_PROXY_TARGET || 'http://127.0.0.1:9000' const overseasTarget = env.OVERSEAS_API_PROXY_TARGET || 'http://127.0.0.1:9300' const preserveOverseasPrefix = env.OVERSEAS_API_PROXY_PRESERVE_PREFIX === 'true' return { plugins: [uni()], define: { __VUE_I18N_FULL_INSTALL__: true, __VUE_I18N_LEGACY_API__: false, __VUE_I18N_PROD_DEVTOOLS__: false, __INTLIFY_PROD_DEVTOOLS__: false }, server: { proxy: { '/api': loggedProxy('auth', { target: domesticTarget, changeOrigin: true }), '/overseas-api': loggedProxy('overseas', { target: overseasTarget, changeOrigin: true, rewrite: (path) => preserveOverseasPrefix ? path : path.replace(/^\/overseas-api/, '/api') }) } }, css: { preprocessorOptions: { scss: { // DCloud uni-scss still uses APIs deprecated by the bundled Dart Sass. // Keep real Sass warnings/errors visible while hiding these known notices. silenceDeprecations: [ 'legacy-js-api', 'import', 'global-builtin', 'color-functions', 'if-function' ] } } } } })