You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.9 KiB

  1. import { defineConfig, loadEnv } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import path from 'path'
  4. // https://vite.dev/config/
  5. export default defineConfig(({ mode }) => {
  6. // 加载对应模式的环境变量
  7. const env = loadEnv(mode, process.cwd());
  8. let config = {
  9. plugins: [vue()],
  10. resolve: {
  11. // 配置别名
  12. alias: {
  13. '@': path.resolve(__dirname, 'src'),
  14. },
  15. },
  16. css: {
  17. preprocessorOptions: {
  18. // 配置全局样式
  19. scss: {
  20. additionalData: `@import "@/styles/variables.scss";`,
  21. },
  22. },
  23. },
  24. };
  25. if (mode === 'development') {
  26. // 开发模式下的配置
  27. config = {
  28. ...config,
  29. server: {
  30. host: env.VITE_DEV_HOST || '0.0.0.0', // 允许通过网络访问,从环境变量获取host
  31. port: parseInt(env.VITE_DEV_PORT, 10) || 8080, // 自定义端口,从环境变量获取port
  32. open: true, // 自动打开浏览器
  33. // 配置代理
  34. proxy: {
  35. '/api': {
  36. target: env.VITE_DEV_API_URL,
  37. changeOrigin: true,
  38. rewrite: (path) => path.replace(/^\/api/, ''),
  39. },
  40. },
  41. },
  42. };
  43. } else if (mode === 'production') {
  44. // 生产模式下的配置
  45. config = {
  46. ...config,
  47. build: {
  48. terserOptions: {
  49. compress: {
  50. drop_console: env.VITE_BUILD_DROP_CONSOLE === 'true', // 移除 console 语句,从环境变量获取是否移除console
  51. },
  52. },
  53. // 配置打包输出目录
  54. outDir: 'dist',
  55. // 配置资源文件名格式
  56. assetsDir: 'assets',
  57. rollupOptions: {
  58. output: {
  59. // 手动分割代码
  60. manualChunks(id) {
  61. if (id.includes('node_modules')) {
  62. return id.toString().split('node_modules/')[1].split('/')[0].toString();
  63. }
  64. },
  65. },
  66. },
  67. },
  68. };
  69. }
  70. return config;
  71. })