vite.config.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import vue from '@vitejs/plugin-vue';
  2. import { resolve } from 'path';
  3. import { defineConfig, loadEnv, ConfigEnv } from 'vite';
  4. import vueSetupExtend from 'vite-plugin-vue-setup-extend';
  5. import AutoImport from 'unplugin-auto-import/vite';
  6. import topLevelAwait from 'vite-plugin-top-level-await';
  7. import { createStyleImportPlugin, VxeTableResolve } from 'vite-plugin-style-import';
  8. import viteCompression from 'vite-plugin-compression';
  9. // @ts-ignore
  10. import { svgBuilder } from '/@/components/IconSelector/index';
  11. const pathResolve = (dir: string) => {
  12. return resolve(__dirname, '.', dir);
  13. };
  14. const alias: Record<string, string> = {
  15. '/@': pathResolve('./src/'),
  16. 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
  17. };
  18. const viteConfig = defineConfig((mode: ConfigEnv) => {
  19. const env = loadEnv(mode.mode, process.cwd());
  20. // 判断是否开发环境
  21. const isDev = env.ENV === 'development'
  22. return {
  23. plugins: [
  24. vue(), // Vue 插件
  25. svgBuilder('./src/assets/icons/'), // 将 SVG 文件转换成 Vue 组件
  26. vueSetupExtend(), // setup语法糖增强插件
  27. AutoImport({
  28. imports: ['vue', 'vue-router', 'pinia'], // 自动导入的依赖库数组
  29. dts: './auto-imports.d.ts', // 自动导入类型定义文件路径
  30. }),
  31. createStyleImportPlugin({
  32. resolves: [VxeTableResolve()], // 配置vxetable 按需加载
  33. }),
  34. topLevelAwait({
  35. promiseExportName: '__tla', // TLA Promise 变量名
  36. promiseImportName: (i) => `__tla_${i}`, // TLA Promise 导入名
  37. }),
  38. viteCompression({
  39. deleteOriginFile: false, // 压缩后删除原来的文件
  40. })
  41. ],
  42. root: process.cwd(), // 项目根目录
  43. resolve: { alias }, // 路径别名配置
  44. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  45. optimizeDeps: {
  46. include: ['element-plus/es/locale/lang/zh-cn', 'element-plus/es/locale/lang/en'],
  47. },
  48. server: {
  49. host: '0.0.0.0', // 服务器地址
  50. port: env.VITE_PORT as unknown as number, // 服务器端口号
  51. open: env.VITE_OPEN === 'true', // 是否自动打开浏览器
  52. hmr: true, // 启用热更新
  53. proxy: {
  54. '/api/gen': {
  55. //单体架构下特殊处理代码生成模块代理
  56. target: env.VITE_IS_MICRO === 'true' ? env.VITE_ADMIN_PROXY_PATH : env.VITE_GEN_PROXY_PATH,
  57. changeOrigin: true,
  58. rewrite: (path) => path.replace(/^\/api/, ''),
  59. },
  60. '/api': {
  61. target: env.VITE_ADMIN_PROXY_PATH, // 目标服务器地址
  62. ws: true, // 是否启用 WebSocket
  63. changeOrigin: true, // 是否修改请求头中的 Origin 字段
  64. rewrite: (path) => path.replace(/^\/api/, ''),
  65. },
  66. '^/ws/info/.*': {
  67. target: env.VITE_ADMIN_PROXY_PATH, // 目标服务器地址
  68. ws: true, // 是否启用 WebSocket
  69. changeOrigin: true,
  70. },
  71. },
  72. },
  73. build: {
  74. outDir: 'dist', // 打包输出目录
  75. chunkSizeWarningLimit: 1500, // 代码分包阈值
  76. // 开发使用 esbuild 更快,生产环境打包使用 terser 可以删除更多注释
  77. minify: isDev ? 'esbuild' : 'terser',
  78. terserOptions: {
  79. compress: {
  80. drop_console: true, // 删除 console
  81. drop_debugger: true, // 删除 debugger
  82. },
  83. format: {
  84. comments: false // 删除所有注释
  85. }
  86. },
  87. rollupOptions: {
  88. output: {
  89. entryFileNames: `assets/[name].[hash].js`,
  90. chunkFileNames: `assets/[name].[hash].js`,
  91. assetFileNames: `assets/[name].[hash].[ext]`,
  92. compact: true,
  93. manualChunks: {
  94. vue: ['vue', 'vue-router', 'pinia'],
  95. echarts: ['echarts'],
  96. },
  97. },
  98. },
  99. },
  100. css: { preprocessorOptions: { css: { charset: false } } },
  101. define: {
  102. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  103. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  104. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  105. __VERSION__: JSON.stringify(process.env.npm_package_version),
  106. __NEXT_NAME__: JSON.stringify(process.env.npm_package_name),
  107. },
  108. };
  109. });
  110. export default viteConfig;