vite.config.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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: createVitePlugins(env, command === 'build'),
  13. resolve: {
  14. // https://cn.vitejs.dev/config/#resolve-alias
  15. alias: {
  16. // 设置路径
  17. '~': path.resolve(__dirname, './'),
  18. // 设置别名
  19. '@': path.resolve(__dirname, './src')
  20. },
  21. // https://cn.vitejs.dev/config/#resolve-extensions
  22. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  23. },
  24. // 打包配置
  25. build: {
  26. // https://vite.dev/config/build-options.html
  27. sourcemap: command === 'build' ? false : 'inline',
  28. outDir: 'dist',
  29. assetsDir: 'assets',
  30. chunkSizeWarningLimit: 2000,
  31. rollupOptions: {
  32. output: {
  33. chunkFileNames: 'static/js/[name]-[hash].js',
  34. entryFileNames: 'static/js/[name]-[hash].js',
  35. assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
  36. }
  37. }
  38. },
  39. // vite 相关配置
  40. server: {
  41. port: env.VITE_APP_PORT,
  42. host: '0.0.0.0',
  43. open: true,
  44. proxy: {
  45. // https://cn.vitejs.dev/config/#server-proxy
  46. [VITE_SERVICE_BASE_TITLE]: {
  47. target: VITE_SERVER_URL,
  48. changeOrigin: true,
  49. rewrite: (path) => path.replace(new RegExp(`^${VITE_SERVICE_BASE_TITLE}`), ''),
  50. },
  51. // springdoc proxy
  52. '^/v3/api-docs/(.*)': {
  53. target: VITE_SERVER_URL,
  54. changeOrigin: true,
  55. }
  56. }
  57. },
  58. css: {
  59. postcss: {
  60. plugins: [
  61. {
  62. postcssPlugin: 'internal:charset-removal',
  63. AtRule: {
  64. charset: (atRule) => {
  65. if (atRule.name === 'charset') {
  66. atRule.remove()
  67. }
  68. }
  69. }
  70. }
  71. ]
  72. }
  73. }
  74. }
  75. })