vite.config.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. chunkSizeWarningLimit: 1500,
  49. rollupOptions: {
  50. output: {
  51. entryFileNames: `assets/[name].${new Date().getTime()}.js`,
  52. chunkFileNames: `assets/[name].${new Date().getTime()}.js`,
  53. assetFileNames: `assets/[name].${new Date().getTime()}.[ext]`,
  54. compact: true,
  55. manualChunks: {
  56. vue: ['vue', 'vue-router', 'vuex'],
  57. echarts: ['echarts'],
  58. },
  59. },
  60. },
  61. terserOptions: {
  62. compress: {
  63. drop_console: true,
  64. drop_debugger: true,
  65. },
  66. ie8: true,
  67. output: {
  68. comments: true,
  69. },
  70. },
  71. },
  72. css: {
  73. postcss: {
  74. plugins: [
  75. {
  76. postcssPlugin: 'internal:charset-removal',
  77. AtRule: {
  78. charset: (atRule) => {
  79. if (atRule.name === 'charset') {
  80. atRule.remove();
  81. }
  82. },
  83. },
  84. },
  85. ],
  86. },
  87. },
  88. define: {
  89. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  90. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  91. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  92. },
  93. };
  94. });
  95. export default viteConfig;