vite.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { fileURLToPath, URL } from 'node:url'
  2. import { defineConfig, loadEnv } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. import viteImagemin from 'vite-plugin-imagemin'
  5. import { VitePWA } from 'vite-plugin-pwa'
  6. import legacy from '@vitejs/plugin-legacy'
  7. import tailwindcss from 'tailwindcss'
  8. import autoprefixer from 'autoprefixer'
  9. // https://vitejs.dev/config/
  10. export default defineConfig(({ command, mode }) => {
  11. process.env = { ...process.env, ...loadEnv(mode, process.cwd()) }
  12. const plugins = [
  13. vue(),
  14. viteImagemin({
  15. gifsicle: {
  16. optimizationLevel: 7,
  17. interlaced: false
  18. },
  19. optipng: false,
  20. mozjpeg: {
  21. quality: 80
  22. },
  23. pngquant: {
  24. quality: [0.5, 0.9],
  25. speed: 1
  26. },
  27. svgo: {
  28. plugins: [
  29. {
  30. name: 'removeViewBox'
  31. },
  32. {
  33. name: 'removeEmptyAttrs',
  34. active: false
  35. }
  36. ]
  37. },
  38. webp: false
  39. }),
  40. // For production build environments only
  41. legacy({
  42. targets: ['chrome >= 64', 'edge >= 79', 'safari >= 11.1', 'firefox >= 67'],
  43. ignoreBrowserslistConfig: true,
  44. renderLegacyChunks: false,
  45. /**
  46. * Polyfills required by modern browsers
  47. *
  48. * Since some low-version modern browsers do not support the new syntax
  49. * You need to load polyfills corresponding to the syntax to be compatible
  50. * At build, all required polyfills are packaged according to the target browser version range
  51. * But when the page is accessed, only the required part is loaded depending on the browser version
  52. *
  53. * Two configuration methods:
  54. *
  55. * 1. true
  56. * - Automatically load all required polyfills based on the target browser version range
  57. * - Demerit: will introduce polyfills that are not needed by modern browsers in higher versions,
  58. * as well as more aggressive polyfills.
  59. *
  60. * 2、string[]
  61. * - Add low-version browser polyfills as needed
  62. * - Demerit: It needs to be added manually, which is inflexible;
  63. * it will be discovered after the production is deployed, resulting in production failure! ! !
  64. */
  65. modernPolyfills: ['es/global-this']
  66. // or
  67. // modernPolyfills: true,
  68. })
  69. ]
  70. return {
  71. base: process.env.VITE_BASE_URL,
  72. server: {
  73. host: '0.0.0.0',
  74. port: 3000,
  75. fs: {
  76. strict: false
  77. }
  78. },
  79. plugins,
  80. resolve: {
  81. alias: {
  82. '@': fileURLToPath(new URL('./src', import.meta.url))
  83. }
  84. },
  85. css: {
  86. postcss: {
  87. plugins: [tailwindcss, autoprefixer]
  88. },
  89. preprocessorOptions: {
  90. less: {
  91. javascriptEnabled: true,
  92. additionalData: '@import "@/styles/common.less";'
  93. }
  94. }
  95. },
  96. logLevel: mode === 'app' || mode === 'test' ? 'error' : 'info'
  97. }
  98. })