compress.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * Copyright (c) 2013-Now https://jeesite.com All rights reserved.
  3. * No deletion without permission, or be held responsible to law.
  4. * @author ThinkGem
  5. */
  6. import { type PluginOption } from 'vite';
  7. import compressPlugin from 'vite-plugin-compression';
  8. /**
  9. * Used to package and output gzip. Note that this does not work properly in Vite, the specific reason is still being investigated
  10. * https://github.com/anncwb/vite-plugin-compression
  11. */
  12. export function configCompressPlugin(isBuild: boolean, viteEnv: ViteEnv): PluginOption {
  13. if (!isBuild) {
  14. return [];
  15. }
  16. const plugins: PluginOption = [];
  17. const compress: 'gzip' | 'brotli' | 'none' = viteEnv.VITE_BUILD_COMPRESS;
  18. const deleteOriginFile = viteEnv.VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE || false;
  19. const compressList = compress.split(',');
  20. if (compressList.includes('gzip')) {
  21. plugins.push(
  22. compressPlugin({
  23. ext: '.gz',
  24. deleteOriginFile,
  25. }),
  26. );
  27. }
  28. if (compressList.includes('brotli')) {
  29. plugins.push(
  30. compressPlugin({
  31. ext: '.br',
  32. algorithm: 'brotliCompress',
  33. deleteOriginFile,
  34. }),
  35. );
  36. }
  37. return plugins;
  38. }