vite.config.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import vue from '@vitejs/plugin-vue';
  2. import viteCompression from 'vite-plugin-compression'
  3. import { resolve } from 'path';
  4. import { defineConfig, loadEnv, ConfigEnv } from 'vite';
  5. const pathResolve = (dir: string): any => {
  6. return resolve(__dirname, '.', dir);
  7. };
  8. const alias: Record<string, string> = {
  9. '/@': pathResolve('./src/'),
  10. 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
  11. };
  12. const viteConfig = defineConfig((mode: ConfigEnv) => {
  13. const env = loadEnv(mode.mode, process.cwd());
  14. return {
  15. plugins: [
  16. vue(),
  17. viteCompression({
  18. threshold: 1024 * 20, // 对大于 20k 的文件进行压缩
  19. // filter: /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i, // 需要压缩的文件
  20. algorithm: 'gzip', // 压缩方式
  21. ext: 'gz', // 后缀名
  22. deleteOriginFile: false, // 压缩后是否删除压缩源文件
  23. })
  24. ],
  25. root: process.cwd(),
  26. resolve: { alias },
  27. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  28. optimizeDeps: {
  29. include: ['element-plus/lib/locale/lang/zh-cn', 'element-plus/lib/locale/lang/en', 'element-plus/lib/locale/lang/zh-tw'],
  30. },
  31. server: {
  32. host: '0.0.0.0',
  33. port: env.VITE_PORT as unknown as number,
  34. open: true,
  35. hmr: true,
  36. proxy: {
  37. '/gitee': {
  38. target: 'https://gitee.com',
  39. ws: true,
  40. changeOrigin: true,
  41. rewrite: (path) => path.replace(/^\/gitee/, ''),
  42. },
  43. },
  44. },
  45. build: {
  46. outDir: 'dist',
  47. sourcemap: false,
  48. cssCodeSplit: false,
  49. chunkSizeWarningLimit: 1500,
  50. rollupOptions: {
  51. output: {
  52. entryFileNames: `assets/[name].${new Date().getTime()}.js`,
  53. chunkFileNames: `assets/[name].${new Date().getTime()}.js`,
  54. assetFileNames: `assets/[name].${new Date().getTime()}.[ext]`,
  55. compact: true,
  56. manualChunks: {
  57. vue: ['vue', 'vue-router', 'vuex'],
  58. echarts: ['echarts'],
  59. },
  60. },
  61. },
  62. },
  63. css: {
  64. postcss: {
  65. plugins: [
  66. {
  67. postcssPlugin: 'internal:charset-removal',
  68. AtRule: {
  69. charset: (atRule) => {
  70. if (atRule.name === 'charset') {
  71. atRule.remove();
  72. }
  73. },
  74. },
  75. },
  76. ],
  77. },
  78. },
  79. define: {
  80. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  81. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  82. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  83. },
  84. };
  85. });
  86. export default viteConfig;