vite.config.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { defineConfig, loadEnv } from 'vite'
  2. import path from 'path'
  3. import createVitePlugins from './vite/plugins'
  4. // https://vitejs.dev/config/
  5. export default defineConfig(({ mode, command }) => {
  6. const env = loadEnv(mode, process.cwd())
  7. const { VITE_APP_ENV } = env
  8. // 添加调试信息
  9. console.log('=== 环境变量调试 ===')
  10. console.log('mode:', mode)
  11. console.log('cwd:', process.cwd())
  12. console.log('env对象:', env)
  13. console.log('VITE_APP_API_BASE_URL:', env.VITE_APP_API_BASE_URL)
  14. console.log('process.env.VUE_APP_BASE_API:', process.env.VUE_APP_BASE_API)
  15. console.log('===================')
  16. return {
  17. // 部署生产环境和开发环境下的URL。
  18. // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
  19. // 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
  20. base: VITE_APP_ENV === 'production' ? '/' : '/',
  21. plugins: createVitePlugins(env, command === 'build'),
  22. resolve: {
  23. // https://cn.vitejs.dev/config/#resolve-alias
  24. alias: {
  25. // 设置路径
  26. '~': path.resolve(__dirname, './'),
  27. // 设置别名
  28. '@': path.resolve(__dirname, './src')
  29. },
  30. // https://cn.vitejs.dev/config/#resolve-extensions
  31. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  32. },
  33. // 打包配置
  34. build: {
  35. // https://vite.dev/config/build-options.html
  36. sourcemap: command === 'build' ? false : 'inline',
  37. outDir: 'dist',
  38. assetsDir: 'assets',
  39. chunkSizeWarningLimit: 2000,
  40. rollupOptions: {
  41. output: {
  42. chunkFileNames: 'static/js/[name]-[hash].js',
  43. entryFileNames: 'static/js/[name]-[hash].js',
  44. assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
  45. }
  46. }
  47. },
  48. // vite 相关配置
  49. server: {
  50. port: 80,
  51. host: true,
  52. open: true,
  53. proxy: {
  54. // https://cn.vitejs.dev/config/#server-proxy
  55. '/dev-api': {
  56. target: env.VITE_APP_API_BASE_URL,
  57. changeOrigin: true,
  58. rewrite: (p) => p.replace(/^\/dev-api/, ''),
  59. configure: (proxy, options) => {
  60. // 可以在这里打印日志,看看代理配置是否正确
  61. console.log('Proxy configured for:', options.target);
  62. }
  63. }
  64. }
  65. },
  66. //fix:error:stdin>:7356:1: warning: "@charset" must be the first rule in the file
  67. css: {
  68. postcss: {
  69. plugins: [
  70. {
  71. postcssPlugin: 'internal:charset-removal',
  72. AtRule: {
  73. charset: (atRule) => {
  74. if (atRule.name === 'charset') {
  75. atRule.remove()
  76. }
  77. }
  78. }
  79. }
  80. ]
  81. }
  82. }
  83. }
  84. })