vite.config.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { defineConfig, loadEnv } from 'vite'
  2. import path from 'path'
  3. import createVitePlugins from './vite/plugins'
  4. import {BASE_URL} from './src/config'
  5. import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill';
  6. // https://vitejs.dev/config/
  7. export default defineConfig(({ mode, command }) => {
  8. const env = loadEnv(mode, process.cwd())
  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: VITE_APP_ENV === 'production' ? '/' : '/',
  15. plugins: createVitePlugins(env, command === 'build'),
  16. resolve: {
  17. // https://cn.vitejs.dev/config/#resolve-alias
  18. alias: {
  19. // 设置路径
  20. '~': path.resolve(__dirname, './'),
  21. // 设置别名
  22. '@': path.resolve(__dirname, './src')
  23. },
  24. // https://cn.vitejs.dev/config/#resolve-extensions
  25. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  26. },
  27. // vite 相关配置
  28. server: {
  29. port: 81,
  30. // host: 'true',
  31. host: '0.0.0.0',
  32. open: true,
  33. proxy: {
  34. // https://cn.vitejs.dev/config/#server-proxy
  35. '/dev-api': {
  36. target: 'http://'+ BASE_URL,
  37. changeOrigin: true,
  38. rewrite: (p) => p.replace(/^\/dev-api/, '')
  39. },
  40. '/prod-api': {
  41. target: 'http://'+ BASE_URL,
  42. changeOrigin: true,
  43. rewrite: (p) => p.replace(/^\/prod-api/, '')
  44. },
  45. '/stage-api': {
  46. target: 'http://'+ BASE_URL,
  47. changeOrigin: true,
  48. rewrite: (p) => p.replace(/^\/stage-api/, '')
  49. },
  50. }
  51. },
  52. //fix:error:stdin>:7356:1: warning: "@charset" must be the first rule in the file
  53. css: {
  54. postcss: {
  55. plugins: [
  56. {
  57. postcssPlugin: 'internal:charset-removal',
  58. AtRule: {
  59. charset: (atRule) => {
  60. if (atRule.name === 'charset') {
  61. atRule.remove();
  62. }
  63. }
  64. }
  65. }
  66. ]
  67. }
  68. },
  69. optimizeDeps: {
  70. esbuildOptions: {
  71. define: {
  72. global: 'globalThis'
  73. },
  74. plugins: [
  75. NodeGlobalsPolyfillPlugin({
  76. process: true,
  77. buffer: true,
  78. })
  79. ]
  80. }
  81. }
  82. }
  83. })