vite.config.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // minify: 'terser', // 使用terser进行压缩
  62. // terserOptions: {
  63. // compress: {
  64. // drop_console: true,
  65. // drop_debugger: true,
  66. // },
  67. // ie8: true,
  68. // output: {
  69. // comments: true,
  70. // },
  71. // },
  72. },
  73. css: {
  74. postcss: {
  75. plugins: [
  76. {
  77. postcssPlugin: 'internal:charset-removal',
  78. AtRule: {
  79. charset: (atRule) => {
  80. if (atRule.name === 'charset') {
  81. atRule.remove();
  82. }
  83. },
  84. },
  85. },
  86. ],
  87. },
  88. },
  89. define: {
  90. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  91. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  92. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  93. },
  94. };
  95. });
  96. export default viteConfig;