vite.config.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import vue from '@vitejs/plugin-vue';
  2. import { resolve } from 'path';
  3. import { defineConfig, loadEnv, ConfigEnv } from 'vite';
  4. import vueSetupExtend from 'vite-plugin-vue-setup-extend-plus'; // setup语法糖设置name
  5. import viteCompression from 'vite-plugin-compression'; //开启gzip打包压缩
  6. const pathResolve = (dir: string): any => {
  7. return resolve(__dirname, '.', dir);
  8. };
  9. const alias: Record<string, string> = {
  10. '/@': pathResolve('./src/'),
  11. };
  12. const viteConfig = defineConfig((mode: ConfigEnv) => {
  13. const env = loadEnv(mode.mode, process.cwd());
  14. return {
  15. plugins: [
  16. vue(),
  17. vueSetupExtend(),
  18. viteCompression()
  19. ],
  20. root: process.cwd(),
  21. resolve: { alias },
  22. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  23. hmr: true,
  24. server: {
  25. host: '0.0.0.0',
  26. port: env.VITE_PORT as unknown as number,
  27. open: JSON.parse(env.VITE_OPEN), // 是否打开浏览器
  28. proxy: {
  29. '/api': {
  30. target: 'http://hotline.fw.com',
  31. ws: true,
  32. changeOrigin: true,
  33. rewrite: (path) => path.replace(/^\/api/, ''),
  34. },
  35. }
  36. },
  37. build: {
  38. outDir: 'dist',
  39. sourcemap: false,
  40. chunkSizeWarningLimit: 1500,
  41. rollupOptions: {
  42. output: {
  43. entryFileNames: `assets/[name].[hash].js`,
  44. chunkFileNames: `assets/[name].[hash].js`,
  45. assetFileNames: `assets/[name].[hash].[ext]`,
  46. compact: true,
  47. manualChunks: {
  48. vue: ['vue', 'vue-router', 'pinia'],
  49. echarts: ['echarts'],
  50. },
  51. },
  52. },
  53. minify: 'terser',
  54. terserOptions: {
  55. //生产环境时移除console和debugger
  56. compress: {
  57. drop_console: false,
  58. drop_debugger: true
  59. }
  60. }
  61. },
  62. css: { preprocessorOptions: { css: { charset: false } } }
  63. };
  64. });
  65. export default viteConfig;