vite.config.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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: env.VITE_DEV_PATH, // 你的后端 WebSocket 服务器地址
  46. changeOrigin: true, // 修改请求头中的 Origin
  47. ws: true, // 启用 WebSocket 代理支持 [1,2,9,10,11](@ref)
  48. // rewrite: (path) => path.replace(/^\/websocket/, '') // 可选:重写路径 [1,2,9](@ref)
  49. },
  50. '/sh-api': {
  51. target: env.VITE_DEV_PATH,
  52. changeOrigin: true,
  53. rewrite: (p) => p.replace(/^\/sh-api/, '')
  54. },
  55. // '/service_api': {
  56. // target: 'http://localhost:8081',
  57. // changeOrigin: true,
  58. // rewrite: (p) => p.replace(/^\/service_api/, '')
  59. // }
  60. // [env.VITE_APP_BASE_API]: {
  61. // target: env.VITE_DEV_PATH,
  62. // changeOrigin: true,
  63. // pathRewrite: {
  64. // ['^' + env.VITE_APP_BASE_API]: ''
  65. // }
  66. // }
  67. }
  68. },
  69. //fix:error:stdin>:7356:1: warning: "@charset" must be the first rule in the file
  70. css: {
  71. postcss: {
  72. plugins: [
  73. {
  74. postcssPlugin: 'internal:charset-removal',
  75. AtRule: {
  76. charset: (atRule) => {
  77. if (atRule.name === 'charset') {
  78. atRule.remove();
  79. }
  80. }
  81. }
  82. }
  83. ]
  84. }
  85. }
  86. }
  87. })