| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- import type { ServerOptions } from 'vite';
- /**
- * 开发服务器配置(含 API 代理)。依赖运行期 env(端口、代理目标),故以工厂函数导出。
- * 代理 /dev/api/v1 → VITE_DEV_PROXY_TARGET_API_BASE_URL,并 rewrite 掉前缀。
- * @param env 处理后的环境变量(wrapperEnv 产物)。
- */
- export function createServerOptions(env: ImportMetaEnv): ServerOptions {
- return {
- port: env.VITE_DEV_PORT,
- host: '0.0.0.0',
- open: true,
- proxy: {
- '/dev/api/v1': {
- target: env.VITE_DEV_PROXY_TARGET_API_BASE_URL,
- changeOrigin: true,
- rewrite: (path) => path.replace(/^\/dev\/api\/v1/, ''),
- secure: false,
- configure: (proxy) => {
- proxy.on('error', (err) => {
- console.log('proxy error', err);
- });
- proxy.on('proxyReq', (_, req) => {
- console.log(
- 'Sending Request to the Target:',
- req.method,
- req.url,
- env.VITE_DEV_PROXY_TARGET_API_BASE_URL
- );
- });
- proxy.on('proxyRes', (proxyRes, req) => {
- console.log(
- 'Received Response from the Target:',
- proxyRes.statusCode,
- req.url
- );
- });
- },
- },
- },
- warmup: {
- clientFiles: ['./index.html', './src/{pages,components}/*'],
- },
- };
- }
|