vite.config.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import vue from '@vitejs/plugin-vue'
  2. import { resolve } from 'path'
  3. import type { UserConfig, ConfigEnv } from 'vite'
  4. import { isProd, loadEnv } from '/@/utils/vite'
  5. import { svgBuilder } from '/@/components/icon/svg/index'
  6. import viteCompression from 'vite-plugin-compression'
  7. const pathResolve = (dir: string): any => {
  8. return resolve(__dirname, '.', dir)
  9. }
  10. // https://vitejs.cn/config/
  11. const viteConfig = ({ mode }: ConfigEnv): UserConfig => {
  12. const { VITE_PORT, VITE_OPEN, VITE_BASE_PATH, VITE_OUT_DIR } = loadEnv(mode)
  13. const alias: Record<string, string> = {
  14. '/@': pathResolve('./src/'),
  15. assets: pathResolve('./src/assets')
  16. }
  17. return {
  18. plugins: [vue(), svgBuilder('./src/assets/icons/'),viteCompression({
  19. threshold: 1024000 // 对大于 1mb 的文件进行压缩
  20. })],
  21. root: process.cwd(),
  22. resolve: { alias },
  23. base: VITE_BASE_PATH,
  24. server: {
  25. host: '0.0.0.0',
  26. port: VITE_PORT,
  27. open: VITE_OPEN,
  28. },
  29. build: {
  30. sourcemap: false,
  31. outDir: VITE_OUT_DIR,
  32. emptyOutDir: true,
  33. chunkSizeWarningLimit: 1500,
  34. },
  35. css: {
  36. postcss: {
  37. plugins: [
  38. {
  39. postcssPlugin: 'internal:charset-removal',
  40. AtRule: {
  41. charset: (atRule) => {
  42. if (atRule.name === 'charset') {
  43. atRule.remove()
  44. }
  45. },
  46. },
  47. },
  48. ],
  49. },
  50. }
  51. }
  52. }
  53. export default viteConfig