vite.config.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {defineConfig, loadEnv} from 'vite'
  2. import path from 'path'
  3. import createVitePlugins from './vite/plugins'
  4. // https://vitejs.dev/config/
  5. export default defineConfig(({mode, command}) => {
  6. const env = loadEnv(mode, process.cwd())
  7. const {VITE_APP_ENV} = env
  8. const VITE_SERVICE_BASE_TITLE=env.VITE_SERVICE_BASE_TITLE
  9. const VITE_SERVER_URL=env.VITE_SERVER_URL
  10. return {
  11. base: env.VITE_APP_BASE_TITLE,
  12. plugins: [
  13. createVitePlugins(env, command === 'build'),
  14. ],
  15. resolve: {
  16. // https://cn.vitejs.dev/config/#resolve-alias
  17. alias: {
  18. // 设置路径
  19. '~': path.resolve(__dirname, './'),
  20. // 设置别名
  21. '@': path.resolve(__dirname, './src')
  22. },
  23. // https://cn.vitejs.dev/config/#resolve-extensions
  24. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  25. },
  26. // 打包配置
  27. build: {
  28. // https://vite.dev/config/build-options.html
  29. sourcemap: command === 'build' ? false : 'inline',
  30. outDir: 'dist',
  31. assetsDir: 'assets',
  32. chunkSizeWarningLimit: 2000,
  33. rollupOptions: {
  34. output: {
  35. chunkFileNames: 'static/js/[name]-[hash].js',
  36. entryFileNames: 'static/js/[name]-[hash].js',
  37. assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
  38. }
  39. }
  40. },
  41. // vite 相关配置
  42. server: {
  43. port: env.VITE_APP_PORT,
  44. host: '0.0.0.0',
  45. open: true,
  46. proxy: {
  47. // https://cn.vitejs.dev/config/#server-proxy
  48. [VITE_SERVICE_BASE_TITLE]: {
  49. target: VITE_SERVER_URL,
  50. changeOrigin: true,
  51. rewrite: (path) => path.replace(new RegExp(`^${VITE_SERVICE_BASE_TITLE}`), ''),
  52. },
  53. // GeoJSON 静态文件由后端 serve(dev 走 proxy,prod 由 nginx 直接 serve)
  54. '^/geojson': {
  55. target: VITE_SERVER_URL,
  56. changeOrigin: true,
  57. },
  58. // springdoc proxy
  59. '^/v3/api-docs/(.*)': {
  60. target: VITE_SERVER_URL,
  61. changeOrigin: true,
  62. }
  63. }
  64. },
  65. css: {
  66. postcss: {
  67. plugins: [
  68. {
  69. postcssPlugin: 'internal:charset-removal',
  70. AtRule: {
  71. charset: (atRule) => {
  72. if (atRule.name === 'charset') {
  73. atRule.remove()
  74. }
  75. }
  76. }
  77. }
  78. ]
  79. }
  80. }
  81. }
  82. })