import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; import path from 'path'; import fs from 'fs'; // SSL证书配置 // 优先使用环境变量指定的证书路径(生产环境) const sslKeyPath = process.env.VITE_SSL_KEYFILE || path.resolve(__dirname, '../ssl/key.pem'); const sslCertPath = process.env.VITE_SSL_CERTFILE || path.resolve(__dirname, '../ssl/cert.pem'); // 检查证书文件是否存在 let httpsConfig: any = false; if (fs.existsSync(sslKeyPath) && fs.existsSync(sslCertPath)) { httpsConfig = { key: fs.readFileSync(sslKeyPath), cert: fs.readFileSync(sslCertPath), }; console.log('✅ 使用SSL证书:'); console.log(` 证书: ${sslCertPath}`); console.log(` 私钥: ${sslKeyPath}`); } else { console.log('⚠️ SSL证书不存在,以HTTP模式启动'); console.log(` 证书路径: ${sslCertPath}`); console.log(` 私钥路径: ${sslKeyPath}`); } export default defineConfig({ plugins: [react()], resolve: { alias: { '@': path.resolve(__dirname, './src'), }, }, server: { port: 3000, host: '0.0.0.0', // 允许外部访问 https: httpsConfig, }, });