vite.config.js 3.2 KB

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