vite.config.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {defineConfig, loadEnv} from 'vite'
  2. import path from 'path'
  3. import createVitePlugins from './vite/plugins'
  4. import {viteCommonjs} from '@originjs/vite-plugin-commonjs'
  5. // https://vitejs.dev/config/
  6. export default defineConfig(({mode, command}) => {
  7. const env = loadEnv(mode, process.cwd())
  8. const isBuild = command === 'build';
  9. // const {VITE_APP_ENV} = env
  10. return {
  11. // 部署生产环境和开发环境下的URL。
  12. // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
  13. // 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
  14. base: env.VITE_APP_BASE_Title,
  15. plugins: [
  16. viteCommonjs({
  17. include: ['jsoneditor'] // 必须为首个插件[3](@ref)
  18. }),
  19. ...createVitePlugins(env, isBuild) // 展开原有插件
  20. ],
  21. resolve: {
  22. // https://cn.vitejs.dev/config/#resolve-alias
  23. alias: {
  24. // 设置路径
  25. '~': path.resolve(__dirname, './'),
  26. // 设置别名
  27. '@': path.resolve(__dirname, './src')
  28. },
  29. // https://cn.vitejs.dev/config/#resolve-extensions
  30. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  31. },
  32. // vite 相关配置
  33. server: {
  34. port: env.VITE_PORT,
  35. host: '0.0.0.0',
  36. open: true,
  37. proxy: {
  38. // '/profile/upload': {
  39. // target: env.VITE_DEV_PATH,
  40. // changeOrigin: true,
  41. // rewrite: (p) => p.replace(/^\/profile\/upload/, '/profile/upload')
  42. // },
  43. // https://cn.vitejs.dev/config/#server-proxy
  44. '/websocket': { // 这是你在前端代码中请求的路径标识
  45. target: 'ws://你的后端服务器IP:端口号', // 你后端WebSocket服务的实际地址
  46. changeOrigin: true,
  47. ws: true, // 关键:启用WebSocket代理
  48. rewrite: (path) => path.replace(/^\/websocket/, '') // 可选的路径重写
  49. },
  50. '/sh-api': {
  51. target: env.VITE_DEV_PATH,
  52. changeOrigin: true,
  53. rewrite: (p) => p.replace(/^\/sh-api/, '')
  54. },
  55. '/gateway-api': {
  56. target: 'http://localhost:8081',
  57. changeOrigin: true,
  58. rewrite: (p) => p.replace(/^\/gateway-api/, '')
  59. },
  60. // '/service_api': {
  61. // target: 'http://localhost:8081',
  62. // changeOrigin: true,
  63. // rewrite: (p) => p.replace(/^\/service_api/, '')
  64. // }
  65. // [env.VITE_APP_BASE_API]: {
  66. // target: env.VITE_DEV_PATH,
  67. // changeOrigin: true,
  68. // pathRewrite: {
  69. // ['^' + env.VITE_APP_BASE_API]: ''
  70. // }
  71. // }
  72. }
  73. },
  74. //fix:error:stdin>:7356:1: warning: "@charset" must be the first rule in the file
  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. })