vite.config.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { defineConfig, loadEnv } from 'vite'
  2. import path from 'path'
  3. import createVitePlugins from './vite/plugins'
  4. // const baseUrl = 'http://localhost:8080' // 后端接口
  5. const baseUrl = 'https://loanapi.waimai-paotui.com' // 后端接口
  6. // const baseUrl="https://api.shoujida.com"
  7. // https://vitejs.dev/config/
  8. export default defineConfig(({ mode, command }) => {
  9. const env = loadEnv(mode, process.cwd())
  10. const { VITE_APP_ENV } = env
  11. return {
  12. // 部署生产环境和开发环境下的URL。
  13. // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
  14. // 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
  15. base: VITE_APP_ENV === 'production' ? '/' : '/',
  16. plugins: createVitePlugins(env, command === 'build'),
  17. resolve: {
  18. // https://cn.vitejs.dev/config/#resolve-alias
  19. alias: {
  20. // 设置路径
  21. '~': path.resolve(__dirname, './'),
  22. // 设置别名
  23. '@': path.resolve(__dirname, './src')
  24. },
  25. // https://cn.vitejs.dev/config/#resolve-extensions
  26. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  27. },
  28. // 打包配置
  29. build: {
  30. // https://vite.dev/config/build-options.html
  31. sourcemap: command === 'build' ? false : 'inline',
  32. outDir: 'dist',
  33. assetsDir: 'assets',
  34. chunkSizeWarningLimit: 2000,
  35. rollupOptions: {
  36. output: {
  37. chunkFileNames: 'static/js/[name]-[hash].js',
  38. entryFileNames: 'static/js/[name]-[hash].js',
  39. assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
  40. }
  41. }
  42. },
  43. // vite 相关配置
  44. server: {
  45. port: 8081,
  46. host: true,
  47. open: true,
  48. proxy: {
  49. // https://cn.vitejs.dev/config/#server-proxy
  50. '/dev-api': {
  51. target: baseUrl,
  52. changeOrigin: true,
  53. secure: false, // 如果是 https 接口,需要配置这个参数
  54. rewrite: (p) => p.replace(/^\/dev-api/, ''),
  55. configure: (proxy, options) => {
  56. proxy.on('error', (err, req, res) => {
  57. console.log('proxy error', err);
  58. });
  59. proxy.on('proxyReq', (proxyReq, req, res) => {
  60. console.log('Sending Request to the Target:', req.method, req.url);
  61. });
  62. proxy.on('proxyRes', (proxyRes, req, res) => {
  63. console.log('Received Response from the Target:', proxyRes.statusCode, req.url);
  64. });
  65. }
  66. },
  67. // springdoc proxy
  68. '^/v3/api-docs/(.*)': {
  69. target: baseUrl,
  70. changeOrigin: true,
  71. secure: false,
  72. }
  73. }
  74. },
  75. css: {
  76. postcss: {
  77. plugins: [
  78. {
  79. postcssPlugin: 'internal:charset-removal',
  80. AtRule: {
  81. charset: (atRule) => {
  82. if (atRule.name === 'charset') {
  83. atRule.remove()
  84. }
  85. }
  86. }
  87. }
  88. ]
  89. }
  90. }
  91. }
  92. })