
Install
$ yarn add vite-plugin-compression2 -D
$ npm install vite-plugin-compression2 -D
Usage
Basic Usage
import { defineConfig } from 'vite'
import { compression } from 'vite-plugin-compression2'
export default defineConfig({
plugins: [
compression()
]
})
Multiple Algorithms
import { compression, defineAlgorithm } from 'vite-plugin-compression2'
export default defineConfig({
plugins: [
compression({
algorithms: [
'gzip',
'brotliCompress',
defineAlgorithm('deflate', { level: 9 })
]
})
]
})
Custom Algorithm Function
import { compression, defineAlgorithm } from 'vite-plugin-compression2'
export default defineConfig({
plugins: [
compression({
algorithms: [
defineAlgorithm(
async (buffer, options) => {
return compressedBuffer
},
{ customOption: true }
)
]
})
]
})
With Tarball
import { compression, tarball } from 'vite-plugin-compression2'
export default defineConfig({
plugins: [
compression(),
tarball({ dest: './dist/archive' })
]
})
Options
Compression Plugin Options
include | string | RegExp | Array<string | RegExp> | /\.(html|xml|css|json|js|mjs|svg|yaml|yml|toml)$/ | 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) |
algorithms | Algorithms | ['gzip', 'brotliCompress'] | Array of compression algorithms or defineAlgorithm results |
filename | string | function | [path][base].gz or [path][base]. br If algorithm is zstandard be [path][base].zst | The target asset filename pattern |
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 |
logLevel | string | info | Control sdout info |
Tarball Plugin Options
dest | string | - | Destination directory for tarball |
API
defineAlgorithm(algorithm, options?)
Define a compression algorithm with options.
Parameters:
algorithm
: Algorithm name ('gzip' | 'brotliCompress' | 'deflate' | 'deflateRaw' | 'zstandard' | 'gz' | 'br' | 'brotli' | 'zstd'
) or custom function
options
: Compression options for the algorithm
Returns: [algorithm, options]
tuple
Examples:
defineAlgorithm('gzip')
defineAlgorithm('gzip', { level: 9 })
defineAlgorithm('brotliCompress', {
params: {
[require('zlib').constants.BROTLI_PARAM_QUALITY]: 11
}
})
defineAlgorithm(
async (buffer, options) => {
return compressedBuffer
},
{ customOption: 'value' }
)
Supported Algorithms
gzip | gz | .gz | All versions | Standard gzip compression with good balance of speed and ratio |
brotliCompress | brotli , br | .br | All versions | Brotli compression with better compression ratio than gzip |
deflate | - | .gz | All versions | Deflate compression algorithm |
deflateRaw | - | .gz | All versions | Raw deflate compression without headers |
zstandard | zstd | .zst | >= 22.15.0 or >= 23.8.0 | Zstandard compression with excellent speed/ratio balance |
Custom Function | - | Custom | All versions | Your own compression algorithm implementation |
Algorithm Types
The algorithms
option accepts:
type Algorithms =
| Algorithm[]
| DefineAlgorithmResult[]
| (Algorithm | DefineAlgorithmResult)[]
Migration
If you're upgrading from v1.x, please check the Migration Guide.
Q & A
FAQ
Examples
Basic Gzip Only
compression({
algorithms: ['gzip']
})
Multiple Algorithms with Custom Options
compression({
algorithms: [
defineAlgorithm('gzip', { level: 9 }),
defineAlgorithm('brotliCompress', {
params: {
[require('zlib').constants.BROTLI_PARAM_QUALITY]: 11
}
})
]
})
Custom Filename Pattern
compression({
algorithms: ['gzip'],
filename: '[path][base].[hash].gz'
})
Delete Original Files
compression({
algorithms: ['gzip'],
deleteOriginalAssets: true
})
Size Threshold
compression({
algorithms: ['gzip'],
threshold: 1000
})
Others
- If you want to analyze your bundle assets, try vite-bundle-analyzer
tarball
option dest
means to generate a tarball somewhere
tarball
is based on the ustar
format. It should be compatible with all popular tar distributions (gnutar, bsdtar etc)
Node.js Version Requirements
- gzip, brotliCompress, deflate, deflateRaw: All Node.js versions supported
- zstd: Requires Node.js >= 22.15.0 or >= 23.8.0
Note: If you try to use zstd compression on an unsupported Node.js version, the plugin will throw a helpful error message indicating the required version.
LICENSE
MIT
Acknowledgements
NWYLZW
Author
Kanno