Newer
Older
DH_Apicture / vite / plugins / compression.js
@zhangqy zhangqy 28 days ago 899 bytes first commit
import compression from 'vite-plugin-compression';

export default function createCompression(env) {
  const { VITE_BUILD_COMPRESS } = env;
  const plugin = [];
  if (VITE_BUILD_COMPRESS) {
    const compressList = VITE_BUILD_COMPRESS.split(',');
    if (compressList.includes('gzip')) {
      // http://doc.ruoyi.vip/ruoyi-vue/other/faq.html#使用gzip解压缩静态文件
      plugin.push(
        compression({
          deleteOriginFile: false,
          threshold: 1024 * 300, // 对大于300kb的文件进行压缩
          algorithm: 'gzip', // 采用的压缩算法,默认是 gzip
          ext: '.gz', // 生成的压缩包后缀
        })
      );
    }
    if (compressList.includes('brotli')) {
      plugin.push(
        compression({
          ext: '.br',
          algorithm: 'brotliCompress',
          deleteOriginFile: false,
        })
      );
    }
  }
  return plugin;
}