compress.ts 959 B

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * Used to package and output gzip. Note that this does not work properly in Vite, the specific reason is still being investigated
  3. * https://github.com/anncwb/vite-plugin-compression
  4. */
  5. import type { PluginOption } from 'vite';
  6. import compressPlugin from 'vite-plugin-compression';
  7. export function configCompressPlugin(compress: 'gzip' | 'brotli' | 'none', deleteOriginFile = false): PluginOption | PluginOption[] {
  8. const compressList = compress.split(',');
  9. const plugins: PluginOption[] = [];
  10. if (compressList.includes('gzip')) {
  11. plugins.push(
  12. compressPlugin({
  13. verbose: true,
  14. disable: false,
  15. threshold: 10240,
  16. algorithm: 'gzip',
  17. ext: '.gz',
  18. deleteOriginFile,
  19. })
  20. );
  21. }
  22. if (compressList.includes('brotli')) {
  23. plugins.push(
  24. compressPlugin({
  25. ext: '.br',
  26. algorithm: 'brotliCompress',
  27. deleteOriginFile,
  28. })
  29. );
  30. }
  31. return plugins;
  32. }