vite.config.js 3.4 KB

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