compression.ts 960 B

1234567891011121314151617181920212223242526272829303132
  1. import compression from 'vite-plugin-compression';
  2. export default (env: any) => {
  3. // CI 可通过空值或空格关闭:VITE_BUILD_COMPRESS=
  4. const raw = String(env?.VITE_BUILD_COMPRESS ?? '').trim();
  5. const plugin: any[] = [];
  6. if (raw) {
  7. const compressList = raw.split(',').map((s: string) => s.trim()).filter(Boolean);
  8. if (compressList.includes('gzip')) {
  9. // http://doc.ruoyi.vip/ruoyi-vue/other/faq.html#使用gzip解压缩静态文件
  10. plugin.push(
  11. compression({
  12. ext: '.gz',
  13. deleteOriginFile: false,
  14. // 小于 10KB 的分包压缩收益很小,跳过可明显缩短收尾时间
  15. threshold: 10 * 1024,
  16. verbose: false
  17. })
  18. );
  19. }
  20. if (compressList.includes('brotli')) {
  21. plugin.push(
  22. compression({
  23. ext: '.br',
  24. algorithm: 'brotliCompress',
  25. deleteOriginFile: false
  26. })
  27. );
  28. }
  29. }
  30. return plugin;
  31. };