vite.config.mjs 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { defineConfig } from 'vite'
  2. import react from '@vitejs/plugin-react'
  3. import { createRequire } from 'module'
  4. import { fileURLToPath } from 'url'
  5. import { dirname, join } from 'path'
  6. // 使用 createRequire 来加载 CommonJS 格式的配置文件
  7. const __filename = fileURLToPath(import.meta.url)
  8. const __dirname = dirname(__filename)
  9. const require = createRequire(import.meta.url)
  10. const configPath = join(__dirname, 'configs', 'config.js')
  11. const config = require(configPath)
  12. // 从配置文件读取 Vite 端口,默认 5173
  13. const vitePort = config.vite?.port || 5173
  14. const viteHost = config.vite?.host || 'localhost'
  15. export default defineConfig({
  16. base: './',
  17. plugins: [react()],
  18. server: {
  19. host: viteHost,
  20. port: vitePort,
  21. strictPort: false, // 如果端口被占用,自动尝试下一个端口
  22. open: false // 不自动打开浏览器,只用于 Electron 桌面应用
  23. },
  24. css: {
  25. preprocessorOptions: {
  26. scss: {
  27. api: 'modern-compiler', // 使用现代 API,消除弃用警告
  28. },
  29. },
  30. },
  31. })