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 apiTarget = env.API_PROXY_TARGET || 'http://127.0.0.1:9000' return { base: process.env.UNI_PLATFORM === 'h5' && mode === 'production' ? '/' : undefined, plugins: [uni()], define: { 'import.meta.env.VITE_PDA_UPDATE_URL': JSON.stringify(env.VITE_PDA_UPDATE_URL || ''), 'import.meta.env.VITE_PDA_DEV_USERNAME': JSON.stringify(env.VITE_PDA_DEV_USERNAME || ''), 'import.meta.env.VITE_PDA_DEV_PASSWORD': JSON.stringify(env.VITE_PDA_DEV_PASSWORD || ''), __VUE_I18N_FULL_INSTALL__: true, __VUE_I18N_LEGACY_API__: false, __VUE_I18N_PROD_DEVTOOLS__: false, __INTLIFY_PROD_DEVTOOLS__: false }, server: { proxy: { '/gateway-api': loggedProxy('api', { target: apiTarget, changeOrigin: true, rewrite: (path) => path.replace(/^\/gateway-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' ] } } } } })