vite.config.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { defineConfig, loadEnv } from 'vite';
  2. import createPlugins from './vite/plugins';
  3. import autoprefixer from 'autoprefixer'; // css自动添加兼容性前缀
  4. import path from 'path';
  5. export default defineConfig(({ mode, command }) => {
  6. const env = loadEnv(mode, process.cwd());
  7. return {
  8. // 部署生产环境和开发环境下的URL。
  9. // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
  10. // 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
  11. base: env.VITE_APP_CONTEXT_PATH,
  12. resolve: {
  13. alias: {
  14. '@': path.resolve(__dirname, './src')
  15. },
  16. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  17. },
  18. // https://cn.vitejs.dev/config/#resolve-extensions
  19. plugins: createPlugins(env, command === 'build'),
  20. server: {
  21. host: '0.0.0.0',
  22. port: Number(env.VITE_APP_PORT),
  23. open: true,
  24. proxy: {
  25. [env.VITE_APP_BASE_API]: {
  26. // target: 'http://meet2.sportsrobo.club:8080',
  27. target: 'http://localhost:8080',
  28. // target: 'http://192.168.1.126:8080',
  29. changeOrigin: true,
  30. ws: true,
  31. rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), ''),
  32. configure: (proxy, options) => {
  33. // 添加CORS头
  34. proxy.on('proxyReq', (proxyReq, req, res) => {
  35. proxyReq.setHeader('Access-Control-Allow-Origin', '*');
  36. proxyReq.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
  37. proxyReq.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, clientid');
  38. });
  39. }
  40. }
  41. }
  42. },
  43. css: {
  44. preprocessorOptions: {
  45. scss: {
  46. // additionalData: '@use "@/assets/styles/variables.module.scss as *";'
  47. // javascriptEnabled: true
  48. api: 'modern-compiler'
  49. }
  50. },
  51. postcss: {
  52. plugins: [
  53. // 浏览器兼容性
  54. autoprefixer(),
  55. {
  56. postcssPlugin: 'internal:charset-removal',
  57. AtRule: {
  58. charset: (atRule) => {
  59. atRule.remove();
  60. }
  61. }
  62. }
  63. ]
  64. }
  65. },
  66. // 预编译
  67. optimizeDeps: {
  68. include: [
  69. 'vue',
  70. 'vue-router',
  71. 'pinia',
  72. 'axios',
  73. '@vueuse/core',
  74. 'echarts',
  75. 'vue-i18n',
  76. '@vueup/vue-quill',
  77. 'image-conversion',
  78. 'element-plus/es/components/**/css'
  79. ]
  80. }
  81. };
  82. });