vite.config.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { defineConfig, loadEnv } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import path from 'path'
  4. import viteCompression from 'vite-plugin-compression'
  5. import Components from 'unplugin-vue-components/vite'
  6. import AutoImport from 'unplugin-auto-import/vite'
  7. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  8. import { fileURLToPath } from 'url'
  9. import vueDevTools from 'vite-plugin-vue-devtools'
  10. // import { visualizer } from 'rollup-plugin-visualizer'
  11. import tailwindcss from '@tailwindcss/vite'
  12. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  13. import setupExtend from 'vite-plugin-vue-setup-extend'
  14. export default ({ mode, command }: { mode: string; command: string }) => {
  15. const isBuild = command === 'build'
  16. const root = process.cwd()
  17. const env = loadEnv(mode, root)
  18. const { VITE_VERSION, VITE_PORT, VITE_BASE_URL, VITE_API_URL, VITE_API_PROXY_URL } = env
  19. console.log(`🚀 API_URL = ${VITE_API_URL}`)
  20. console.log(`🚀 VERSION = ${VITE_VERSION}`)
  21. return defineConfig({
  22. define: {
  23. __APP_VERSION__: JSON.stringify(VITE_VERSION)
  24. },
  25. base: VITE_BASE_URL,
  26. server: {
  27. port: Number(VITE_PORT),
  28. proxy: {
  29. '/api': {
  30. target: VITE_API_PROXY_URL,
  31. changeOrigin: true,
  32. rewrite: (path) => path.replace(/^\/api/, '')
  33. }
  34. },
  35. host: true
  36. },
  37. // 路径别名
  38. resolve: {
  39. alias: {
  40. '@': fileURLToPath(new URL('./src', import.meta.url)),
  41. '@views': resolvePath('src/views'),
  42. '@imgs': resolvePath('src/assets/img'),
  43. '@icons': resolvePath('src/assets/icons'),
  44. '@utils': resolvePath('src/utils'),
  45. '@stores': resolvePath('src/store'),
  46. '@plugins': resolvePath('src/plugins'),
  47. '@styles': resolvePath('src/assets/styles')
  48. }
  49. },
  50. build: {
  51. target: 'es2015',
  52. outDir: 'dist',
  53. chunkSizeWarningLimit: 2000,
  54. minify: 'terser',
  55. terserOptions: {
  56. compress: {
  57. // 生产环境去除 console
  58. drop_console: true,
  59. // 生产环境去除 debugger
  60. drop_debugger: true
  61. }
  62. },
  63. dynamicImportVarsOptions: {
  64. warnOnError: true,
  65. exclude: [],
  66. include: ['src/views/**/*.vue']
  67. }
  68. },
  69. plugins: [
  70. vue(),
  71. setupExtend(),
  72. tailwindcss(),
  73. // 自动导入 components 目录下的组件
  74. Components({
  75. deep: true,
  76. extensions: ['vue'],
  77. dirs: ['src/components'],
  78. resolvers: [ElementPlusResolver({ importStyle: false })],
  79. dts: 'src/types/components.d.ts'
  80. }),
  81. // 自动导入组件 Api
  82. AutoImport({
  83. imports: ['vue', 'vue-router', '@vueuse/core', 'pinia'],
  84. resolvers: [ElementPlusResolver()],
  85. dts: 'src/types/auto-imports.d.ts',
  86. // ESLint 配置
  87. eslintrc: {
  88. // 首次运行时设置为 true 生成配置文件,之后改为 false
  89. enabled: true,
  90. // ESLint 配置文件路径
  91. filepath: './.auto-import.json',
  92. // 允许全局使用自动导入的 API
  93. globalsPropValue: true
  94. }
  95. }),
  96. // 压缩
  97. viteCompression({
  98. verbose: false, // 是否在控制台输出压缩结果
  99. disable: false, // 是否禁用
  100. algorithm: 'gzip', // 压缩算法
  101. ext: '.gz', // 压缩后的文件名后缀
  102. threshold: 10240, // 只有大小大于该值的资源会被处理 10240B = 10KB
  103. deleteOriginFile: false // 压缩后是否删除原文件
  104. }),
  105. vueDevTools(),
  106. // 打包分析
  107. // visualizer({
  108. // open: true,
  109. // gzipSize: true,
  110. // brotliSize: true,
  111. // filename: 'dist/stats.html' // 分析图生成的文件名及路径
  112. // }),
  113. createSvgIconsPlugin({
  114. iconDirs: [path.resolve(process.cwd(), 'src/assets/icons/svg')],
  115. symbolId: 'icon-[dir]-[name]',
  116. svgoOptions: isBuild
  117. })
  118. ],
  119. css: {
  120. preprocessorOptions: {
  121. // sass variable and mixin
  122. scss: {
  123. api: 'modern-compiler',
  124. additionalData: `
  125. @use "@styles/variables.scss" as *;
  126. @use "@styles/mixin.scss" as *;
  127. `
  128. }
  129. },
  130. postcss: {
  131. plugins: [
  132. {
  133. postcssPlugin: 'internal:charset-removal',
  134. AtRule: {
  135. charset: (atRule) => {
  136. if (atRule.name === 'charset') {
  137. atRule.remove()
  138. }
  139. }
  140. }
  141. }
  142. ]
  143. }
  144. }
  145. })
  146. }
  147. function resolvePath(paths: string) {
  148. return path.resolve(__dirname, paths)
  149. }