vite.config.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { defineConfig, loadEnv, ConfigEnv, UserConfig } from 'vite'
  2. import { resolve } from 'path'
  3. import { wrapperEnv } from './build/getEnv'
  4. import { createProxy } from './build/proxy'
  5. import { createVitePlugins } from './build/plugins'
  6. import pkg from './package.json'
  7. import dayjs from 'dayjs'
  8. const { dependencies, devDependencies, name, version } = pkg
  9. const __APP_INFO__ = {
  10. pkg: { dependencies, devDependencies, name, version },
  11. lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
  12. }
  13. // @see: https://vitejs.dev/config/
  14. export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  15. const root = process.cwd()
  16. const env = loadEnv(mode, root)
  17. const viteEnv = wrapperEnv(env)
  18. return {
  19. base: viteEnv.VITE_PUBLIC_PATH,
  20. root,
  21. resolve: {
  22. alias: {
  23. '@': resolve(__dirname, './src'),
  24. 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
  25. }
  26. },
  27. define: {
  28. __APP_INFO__: JSON.stringify(__APP_INFO__)
  29. },
  30. css: {
  31. preprocessorOptions: {
  32. scss: {
  33. additionalData: `@import "@/styles/var.scss";`
  34. }
  35. }
  36. },
  37. server: {
  38. host: '0.0.0.0',
  39. port: viteEnv.VITE_PORT,
  40. open: viteEnv.VITE_OPEN,
  41. cors: true,
  42. // Load proxy configuration from .env.development
  43. proxy: createProxy(viteEnv.VITE_PROXY)
  44. },
  45. plugins: createVitePlugins(viteEnv),
  46. esbuild: {
  47. pure: viteEnv.VITE_DROP_CONSOLE ? ['console.log', 'debugger'] : []
  48. },
  49. build: {
  50. outDir: 'dist',
  51. minify: 'esbuild',
  52. // esbuild 打包更快,但是不能去除 console.log,terser打包慢,但能去除 console.log
  53. // minify: "terser",
  54. // terserOptions: {
  55. // compress: {
  56. // drop_console: viteEnv.VITE_DROP_CONSOLE,
  57. // drop_debugger: true
  58. // }
  59. // },
  60. sourcemap: false,
  61. // 禁用 gzip 压缩大小报告,可略微减少打包时间
  62. reportCompressedSize: false,
  63. // 规定触发警告的 chunk 大小
  64. chunkSizeWarningLimit: 2000,
  65. rollupOptions: {
  66. output: {
  67. // Static resource classification and packaging
  68. chunkFileNames: 'assets/js/[name]-[hash].js',
  69. entryFileNames: 'assets/js/[name]-[hash].js',
  70. assetFileNames: 'assets/[ext]/[name]-[hash].[ext]'
  71. }
  72. }
  73. }
  74. }
  75. })