vite.config.mjs 1.0 KB

123456789101112131415161718192021222324252627282930313233
  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. plugins: [react()],
  17. server: {
  18. host: viteHost,
  19. port: vitePort,
  20. strictPort: false, // 如果端口被占用,自动尝试下一个端口
  21. open: false // 不自动打开浏览器,只用于 Electron 桌面应用
  22. },
  23. css: {
  24. preprocessorOptions: {
  25. scss: {
  26. api: 'modern-compiler', // 使用现代 API,消除弃用警告
  27. },
  28. },
  29. },
  30. })