Skip to content

WangJincheng4869/vite-plugin-compression

 
 

Repository files navigation

codecov

Install

$ yarn add vite-plugin-compression2 -D

# or

$ npm install vite-plugin-compression2 -D

Usage

import { defineConfig } from 'vite'

import { compression } from 'vite-plugin-compression2'

export default defineConfig({
  plugins: [
    // ...your plugin
    compression()
  ]
})

Options

params type default description
include string | RegExp | Array<string | RegExp> - Include all assets matching any of these conditions.
exclude string | RegExp | Array<string | RegExp> - Exclude all assets matching any of these conditions.
threshold number 0 Only assets bigger than this size are processed (in bytes)
algorithm string| function gzip The compression algorithm
compressionOptions Record<string,any> {} Compression options for algorithm(details see zlib module)
deleteOriginalAssets boolean false Whether to delete the original assets or not
skipIfLargerOrEqual boolean true Whether to skip the compression if the result is larger than or equal to the original file
filename string [path][base].gz The target asset filename

Q & A

Why not vite-plugin-compression

  • vite-plugin-compression no longer maintenance.

Why vite-plugin-compression2

  • vite-plugin-compression2 has minimal dependencies and better performance.

Can i custom the compression algorithm?

  • Yes, you can see the unit test case.

Can i generate multiple compressed assets with difference compression algorithm?

import { defineComponent } from 'vite'
import { compression } from 'vite-plugin-compression2'

export default defineComponent({
  plugins: [
    // ...your plugin
    compression(),
    compression({ algorithm: 'brotliCompress', exclude: [/\.(br)$/, /\.(gz)$/], deleteOriginalAssets: true })
  ]
})
  • You should set exclude for the latest compression plugin. ( Because in the full bundle process. Bundle chunk is shared. And there are write (delete) operations on the bundle inside the plugin. So you should ignore the compressed chunk :) If you want delete the original assets you also follow the way.

Can i create a tarball for all of assets after compressed?

  • Yes, you can import tarball plugin from this package(>=1.0.0)
import { defineComponent } from 'vite'
import { compression, tarball } from 'vite-plugin-compression2'

export default defineComponent({
  plugins: [
    // ...your plugin
    compression(),
    tarball()
  ]
})

Others

If you want to analysis your bundle assets. Maybe you can try vite-bundle-analyzer

LICENSE

MIT

Author

Kanno

About

vite plugin. compress your bundle file.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 84.7%
  • HTML 7.2%
  • JavaScript 4.7%
  • Vue 1.8%
  • CSS 1.6%