vite.config.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { defineConfig } from 'vite';
  2. import react from '@vitejs/plugin-react';
  3. import path from 'path';
  4. import fs from 'fs';
  5. // SSL证书配置
  6. // 优先使用环境变量指定的证书路径(生产环境)
  7. const sslKeyPath = process.env.VITE_SSL_KEYFILE || path.resolve(__dirname, '../ssl/key.pem');
  8. const sslCertPath = process.env.VITE_SSL_CERTFILE || path.resolve(__dirname, '../ssl/cert.pem');
  9. // 检查证书文件是否存在
  10. let httpsConfig: any = false;
  11. if (fs.existsSync(sslKeyPath) && fs.existsSync(sslCertPath)) {
  12. httpsConfig = {
  13. key: fs.readFileSync(sslKeyPath),
  14. cert: fs.readFileSync(sslCertPath),
  15. };
  16. console.log('✅ 使用SSL证书:');
  17. console.log(` 证书: ${sslCertPath}`);
  18. console.log(` 私钥: ${sslKeyPath}`);
  19. } else {
  20. console.log('⚠️ SSL证书不存在,以HTTP模式启动');
  21. console.log(` 证书路径: ${sslCertPath}`);
  22. console.log(` 私钥路径: ${sslKeyPath}`);
  23. }
  24. export default defineConfig({
  25. plugins: [react()],
  26. resolve: {
  27. alias: {
  28. '@': path.resolve(__dirname, './src'),
  29. },
  30. },
  31. server: {
  32. port: 3000,
  33. host: '0.0.0.0', // 允许外部访问
  34. https: httpsConfig,
  35. },
  36. });