vite.config.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. optimizeDeps: {
  50. // 开发时 解决这些commonjs包转成esm包
  51. include: [
  52. '@jiaminghi/c-render',
  53. '@jiaminghi/c-render/lib/plugin/util',
  54. '@jiaminghi/charts/lib/util/index',
  55. '@jiaminghi/charts/lib/util',
  56. '@jiaminghi/charts/lib/extend/index',
  57. '@jiaminghi/charts',
  58. '@jiaminghi/color'
  59. ]
  60. },
  61. build: {
  62. outDir: 'dist',
  63. minify: 'esbuild',
  64. // esbuild 打包更快,但是不能去除 console.log,terser打包慢,但能去除 console.log
  65. // minify: "terser",
  66. // terserOptions: {
  67. // compress: {
  68. // drop_console: viteEnv.VITE_DROP_CONSOLE,
  69. // drop_debugger: true
  70. // }
  71. // },
  72. sourcemap: false,
  73. // 禁用 gzip 压缩大小报告,可略微减少打包时间
  74. reportCompressedSize: false,
  75. // 规定触发警告的 chunk 大小
  76. chunkSizeWarningLimit: 2000,
  77. rollupOptions: {
  78. output: {
  79. // Static resource classification and packaging
  80. chunkFileNames: 'assets/js/[name]-[hash].js',
  81. entryFileNames: 'assets/js/[name]-[hash].js',
  82. assetFileNames: 'assets/[ext]/[name]-[hash].[ext]'
  83. }
  84. },
  85. commonjsOptions: {
  86. include: [
  87. /node_modules/ // 必须包含
  88. ]
  89. }
  90. }
  91. }
  92. })